"""中文样本模板镜像测试(步骤 0)。 断言 zh 模板与 ja 模板章节结构一致:7 个 H1 章、锚点 id(section:xxx)逐一对应, 且锚点段落文本原样保留(映射/注入器零改动)。 """ from __future__ import annotations import shutil from pathlib import Path import pytest from docx import Document from genesis.parsers.word_template_parser import WordTemplateParser from genesis.writer.template_mapper import map_template _SAMPLES = Path(__file__).resolve().parents[1] / "sample" _JA = _SAMPLES / "template_design_ja.docx" _ZH = _SAMPLES / "template_design_zh.docx" EXPECTED_ANCHORS = [ "introduction", "function_list", "screen_list", "report_list", "db_design", "if_definition", "batch_list", ] @pytest.fixture def zh_template(tmp_path): """生成 zh 模板到临时目录并返回路径(不污染 sample/)。""" from scripts.make_zh_template import build_zh_template dst = tmp_path / "template_design_zh.docx" build_zh_template(str(_JA), str(dst)) return dst def _parse(path: Path): return WordTemplateParser().parse(str(path)) def test_zh_template_has_same_seven_chapter_anchors(zh_template): ja = _parse(_JA) zh = _parse(zh_template) ja_ids = [ph for ph in ja.placeholders if ph.startswith("section:")] zh_ids = [ph for ph in zh.placeholders if ph.startswith("section:")] assert sorted(zh_ids) == sorted(ja_ids) assert zh_ids == [f"section:{a}" for a in EXPECTED_ANCHORS] def test_zh_template_anchor_paragraphs_unchanged(zh_template): """锚点段落文本在 zh 模板中必须与 ja 完全一致(映射/注入器依赖它)。""" ja_doc = Document(str(_JA)) zh_doc = Document(str(zh_template)) ja_anchors = [p.text for p in ja_doc.paragraphs if p.text.startswith("{{section:")] zh_anchors = [p.text for p in zh_doc.paragraphs if p.text.startswith("{{section:")] assert zh_anchors == ja_anchors def test_zh_template_headings_translated(zh_template): zh_doc = Document(str(zh_template)) texts = {p.text for p in zh_doc.paragraphs if p.style.name.startswith("Heading")} assert "1. 前言" in texts assert "2. 功能一览" in texts assert "5. DB设计" in texts assert "7. 批处理一览" in texts def test_zh_template_maps_to_same_seven_chapters(zh_template): """template_mapper 对 zh 模板产出 7 个章节,锚点与 ja 对应。""" pt = _parse(zh_template) specs = map_template(pt) assert len(specs) == 7 assert [s.section_placeholder for s in specs] == [ f"section:{a}" for a in EXPECTED_ANCHORS ]