返回 Social Auto Upload
test_article_render.py
根目录 / tests / test_article_render.py
1 """验证文章渲染的内容完整性、资源路径、平台边界及 CLI 参数。"""
2 import tempfile
3 import unittest
4 from pathlib import Path
5
6 from sau_cli import build_parser
7 from conf import BASE_DIR
8 from uploader.dayu_uploader.article_content import load_dayu_article
9 from utils.article_render import ARTICLE_THEMES, load_article, render_article_file
10
11
12 class ArticleRenderTests(unittest.TestCase):
13 def setUp(self):
14 self.folder = tempfile.TemporaryDirectory()
15 self.addCleanup(self.folder.cleanup)
16 self.root = Path(self.folder.name)
17 self.source = self.root / "正文.md"
18
19 def test_all_themes_preserve_code_tables_headings_and_inline_styles(self):
20 self.source.write_text('# 小节标题\n\n正文**加粗重点**与*斜体*。\n\n'
21 '```python\nprint("你好")\n```\n\n'
22 '|项目|数量|\n|---|---|\n|甲|1|\n', encoding="utf-8-sig")
23 themed = []
24 for theme in ARTICLE_THEMES:
25 with self.subTest(theme=theme):
26 doc = load_article(self.source, theme)
27 text = "".join(doc.text)
28 for expected in ('小节标题', '加粗重点', '斜体', 'print("你好")', '项目', '甲'):
29 self.assertIn(expected, text)
30 for tag in ("h1", "strong", "em", "pre", "code", "table"):
31 self.assertEqual(doc.formats[tag], 1)
32 if theme == "plain":
33 self.assertNotIn('style=', doc.content_html)
34 else:
35 self.assertIn('style=', doc.content_html)
36 self.assertNotIn('<svg', doc.content_html)
37 themed.append(doc.content_html)
38 self.assertEqual(len(set(themed)), 4)
39
40 def test_moved_output_preserves_chinese_image_paths_order_and_styles(self):
41 image = self.root / "图片 #1.png"
42 image.write_bytes(b"local preview resource")
43 self.source.write_text('前文\n\n![示例图片](<图片 #1.png>)\n\n后文', encoding="utf-8")
44 output = self.root / "preview" / "result.html"
45 render_article_file(self.source, output, "grace")
46 original = load_article(self.source, "grace")
47 restored = load_article(output)
48 self.assertEqual(restored.images, [image.resolve()])
49 self.assertEqual(restored.manifest, original.manifest)
50 self.assertIn('alt="示例图片"', restored.content_html)
51 self.assertIn('style=', restored.content_html)
52
53 def test_html_keeps_inline_style_and_does_not_need_theme(self):
54 source = self.root / "inline.html"
55 source.write_text('<h2 style="color:red;border-left:3px solid blue">标题</h2>'
56 '<p style="line-height:1.8">正文<strong>重点</strong></p>', encoding="utf-8")
57 doc = load_article(source)
58 self.assertIn('color:red;border-left:3px solid blue', doc.content_html)
59 with self.assertRaisesRegex(ValueError, "仅适用于 Markdown"):
60 load_article(source, "grace")
61 self.assertEqual(load_dayu_article(source).content_html, doc.content_html)
62
63 def test_large_markdown_uses_file_and_platform_checks_its_own_limit(self):
64 self.source.write_text('正文段落。\n\n' * 6000, encoding="utf-8")
65 doc = load_article(self.source, "plain")
66 self.assertEqual("".join(doc.text).count("正文段落。"), 6000)
67 with self.assertRaisesRegex(ValueError, "20000"):
68 load_dayu_article(self.source)
69
70 def test_output_never_overwrites_source_or_existing_file_implicitly(self):
71 self.source.write_text('正文', encoding="utf-8")
72 target = self.root / "result.html"
73 target.write_text("existing", encoding="utf-8")
74 with self.assertRaises(FileExistsError):
75 render_article_file(self.source, target)
76 self.assertEqual(target.read_text(), "existing")
77 render_article_file(self.source, target, overwrite=True)
78 with self.assertRaisesRegex(ValueError, "输入原稿"):
79 render_article_file(target, target, overwrite=True)
80
81 def test_unsafe_or_unimplemented_formats_are_explicit_errors(self):
82 for content in ('<script>alert(1)</script>正文', '<p onclick="alert(1)">正文</p>',
83 '<p style="background:url(https://example.com/a.png)">正文</p>',
84 '<p style="color:var(--color)">正文</p>',
85 '```mermaid\ngraph TD;A-->B;\n```',
86 '```plantuml\nAlice -> Bob\n```',
87 '![外部图片](https://example.com/a.png)'):
88 with self.subTest(content=content):
89 self.source.write_text(content, encoding="utf-8")
90 with self.assertRaises((ValueError, RuntimeError)):
91 load_article(self.source, "plain")
92
93 def test_dayu_markdown_renders_selected_theme_without_plain_downgrade(self):
94 self.source.write_text('# 标题\n\n## 次标题\n\n正文**重点**\n\n> 引用\n\n- 第一项\n- 第二项\n\n---\n', encoding="utf-8")
95 doc = load_dayu_article(self.source)
96 self.assertEqual(doc.theme, "default")
97 self.assertIn('style=', doc.content_html)
98 self.assertEqual(doc.formats["h2"], 1)
99 self.assertEqual(doc.formats["li"], 2)
100 self.assertEqual(load_dayu_article(self.source, "grace").content_html,
101 load_article(self.source, "grace").content_html)
102 self.assertNotIn('style=', load_dayu_article(self.source, "plain").content_html)
103 self.source.write_text('正文*斜体*', encoding="utf-8")
104 with self.assertRaisesRegex(ValueError, "em"):
105 load_dayu_article(self.source)
106
107 def test_cli_exposes_shared_themes_and_platform_choices(self):
108 self.source.write_text('正文', encoding="utf-8")
109 parser = build_parser()
110 args = parser.parse_args(['article', 'render', '--content-file', str(self.source),
111 '--theme', 'modern', '--output', str(self.root / 'out.html')])
112 self.assertEqual(args.theme, 'modern')
113 args = parser.parse_args(['dayu', 'upload-article', '--account', 'creator', '--title', '测试文章标题',
114 '--content-file', str(self.source), '--source', 'AI生成', '--theme', 'modern'])
115 self.assertEqual(args.theme, 'modern')
116
117 def test_dayu_accepts_exported_theme_html_with_the_same_text_and_image_order(self):
118 output = self.root / 'preview' / 'modern.html'
119 render_article_file(BASE_DIR / 'examples/article.md', output, 'modern')
120 direct = load_dayu_article(BASE_DIR / 'examples/article.md', 'modern')
121 exported = load_dayu_article(output)
122 self.assertEqual(exported.content_html, direct.content_html)
123 self.assertEqual(exported.images, direct.images)
124 self.assertEqual(exported.manifest, direct.manifest)
125 self.assertIn('background: #D97757', exported.content_html)
126
127 def test_theme_lists_do_not_duplicate_markers_when_platform_removes_css(self):
128 self.source.write_text('3. 第三项\n4. 第四项\n\n- 无序甲\n- 无序乙\n', encoding='utf8')
129 plain = load_article(self.source, 'plain')
130 for theme in ('default', 'grace', 'simple', 'modern'):
131 with self.subTest(theme=theme):
132 document = load_article(self.source, theme)
133 self.assertEqual(document.manifest['text_sha256'], plain.manifest['text_sha256'])
134 self.assertEqual(document.manifest['formats'], plain.manifest['formats'])
135 self.assertIn('start="3"', document.content_html)
136 self.assertIn('display: list-item', document.content_html)
137
138
139 if __name__ == "__main__":
140 unittest.main()
141
141 lines PYTHON