from genesis.writer.models import ContentBlock, ChapterContent, GenerationContext, ChapterSpec def test_content_block_defaults(): b = ContentBlock(block_id="b1", type="paragraph", text="你好") assert b.level is None assert b.source_uris == [] def test_chapter_content_holds_blocks(): b = ContentBlock(block_id="b1", type="heading", level=2, text="标题") c = ChapterContent(chapter_id="db_design", version=1, title="DB 设计", blocks=[b]) assert c.blocks[0].type == "heading" assert c.version == 1 def test_generation_context_impact_field_default_none(): ctx = GenerationContext( chapter_id="db_design", title="DB 设计", template_marker=ChapterSpec(chapter_id="db_design", title="DB 设计", section_placeholder="{{section:db_design}}"), structured_source=None, write_rules=["规则1"], design_rules=["规则2"], template_styles={"Heading 1"}, ) assert ctx.impact_report is None # to_vars 始终提供 impact 变量;无影响调查书时为空串 assert ctx.to_vars()["impact"] == "" def test_generation_context_impact_var_formats_report(): from genesis.data_models import ChangeAnalysis, ChangeElement, ChangeType, ImpactReport ca = ChangeAnalysis( project_type="enhancement", new_elements=[ChangeElement("F001", "機能", "止损风控", ChangeType.NEW)], modified_elements=[], deleted_elements=[], unchanged_elements=[], warnings=[], ) report = ImpactReport(metadata={"version": "v1"}, change_analysis=ca, summary={"new": 1}) ctx = GenerationContext( chapter_id="db_design", title="DB 设计", template_marker=ChapterSpec(chapter_id="db_design", title="DB 设计", section_placeholder="{{section:db_design}}"), structured_source=None, write_rules=["规则1"], design_rules=["规则2"], template_styles={"Heading 1"}, impact_report=report, ) impact = ctx.to_vars()["impact"] assert "止损风控" in impact assert "F001" in impact def _impact_ctx(report): return GenerationContext( chapter_id="db_design", title="DB 设计", template_marker=ChapterSpec(chapter_id="db_design", title="DB 设计", section_placeholder="{{section:db_design}}"), structured_source=None, write_rules=[], design_rules=[], template_styles=set(), impact_report=report, ) def test_impact_var_empty_when_report_without_analysis(): from genesis.data_models import ImpactReport report = ImpactReport(metadata={"version": "v1"}, change_analysis=None, summary={}) assert _impact_ctx(report).to_vars()["impact"] == "" def test_impact_var_formats_deleted_and_warnings(): from genesis.data_models import ChangeAnalysis, ChangeElement, ChangeType, ImpactReport, ImpactWarning ca = ChangeAnalysis( project_type="enhancement", new_elements=[], modified_elements=[], deleted_elements=[ChangeElement("F030", "機能", "旧功能", ChangeType.DELETED)], unchanged_elements=[], warnings=[ImpactWarning("F030", "缺少既存対応")], ) report = ImpactReport(metadata={}, change_analysis=ca, summary={"deleted": 1, "warnings": 1}) impact = _impact_ctx(report).to_vars()["impact"] assert "[削除]" in impact and "旧功能" in impact assert "[警告]" in impact and "缺少既存対応" in impact def test_chapter_spec_placeholder_optional(): s = ChapterSpec(chapter_id="x", title="X", section_placeholder=None) assert s.section_placeholder is None