fix(writer): map_template consumes real ParsedTemplate sections (positional section ids)

This commit is contained in:
lhl
2026-08-13 09:38:14 +08:00
parent c1fbcff088
commit d237201407
2 changed files with 53 additions and 24 deletions
+26 -11
View File
@@ -1,17 +1,32 @@
from types import SimpleNamespace
from genesis.writer.template_mapper import map_template
from genesis.data_models import ChapterMarker, ParsedTemplate
from genesis.writer.models import ChapterSpec
from genesis.writer.template_mapper import map_template
def _fake_parsed():
ch1 = SimpleNamespace(chapter_id="intro", title="はじめに", section_placeholder="{{section:introduction}}")
ch2 = SimpleNamespace(chapter_id="db_design", title="DB 設計", section_placeholder=None)
return SimpleNamespace(chapters=[ch1, ch2])
def _parsed():
sections = [
ChapterMarker(type="heading", name="はじめに", level=1),
ChapterMarker(type="placeholder", name="section:introduction", level=0),
ChapterMarker(type="heading", name="DB 設計", level=1),
ChapterMarker(type="placeholder", name="section:db_design", level=0),
ChapterMarker(type="bookmark", name="bm1", level=0),
]
return ParsedTemplate(file_name="t.docx", sections=sections, placeholders={"section:introduction": "x"}, styles={})
def test_map_template_ordered():
specs = map_template(_fake_parsed())
assert [s.chapter_id for s in specs] == ["intro", "db_design"]
assert specs[0].section_placeholder == "{{section:introduction}}"
assert specs[1].section_placeholder is None
def test_map_template_ordered_and_assigns_ids():
specs = map_template(_parsed())
assert [s.title for s in specs] == ["はじめに", "DB 設計"]
assert specs[0].chapter_id == "introduction"
assert specs[0].section_placeholder == "section:introduction"
assert specs[1].chapter_id == "db_design"
assert specs[1].section_placeholder == "section:db_design"
assert all(isinstance(s, ChapterSpec) for s in specs)
def test_map_template_falls_back_without_placeholder():
sections = [ChapterMarker(type="heading", name="附録", level=1)]
parsed = ParsedTemplate(file_name="t.docx", sections=sections, placeholders={}, styles={})
specs = map_template(parsed)
assert specs[0].chapter_id == "chapter_1"
assert specs[0].section_placeholder is None