from pathlib import Path from uuid import uuid4 from src.phase_z2_pipeline import ( _apply_section_id_aliases_to_overrides, _build_section_id_aliases, _infer_layout_from_zone_section_keys, parse_mdx, ) def _tmp_mdx(name: str) -> Path: base = Path("data") / "_tmp_task22_tests" / uuid4().hex base.mkdir(parents=True, exist_ok=True) return base / name def test_t22_pre_intro_is_promoted_to_stable_unit(): mdx = _tmp_mdx("01.sample.mdx") mdx.write_text( """--- title: Sample --- import Something from './x.astro'; - **Intro label** - Intro body text
--- ## 1. First section - First body ## 2. Second section - Second body """, encoding="utf-8", ) title, sections, footer = parse_mdx(mdx) assert title == "Sample" assert footer is None assert [s.section_id for s in sections] == ["01-intro", "01-1", "01-2"] assert sections[0].section_num == 0 assert sections[0].title == "Intro label" assert "Intro body text" in sections[0].raw_content assert "import Something" not in sections[0].raw_content assert "---" not in sections[0].raw_content def test_t22_import_only_preamble_does_not_create_intro_unit(): mdx = _tmp_mdx("02.sample.mdx") mdx.write_text( """--- title: Sample --- import DxEffect from './dx.astro'; ## 1. Body - Body text """, encoding="utf-8", ) _, sections, _ = parse_mdx(mdx) assert [s.section_id for s in sections] == ["02-1"] def test_t22_intro_alias_rewrites_orphan_section_overrides(): _, sections, _ = parse_mdx(_write_intro_fixture("01.sample.mdx")) aliases = _build_section_id_aliases(sections) assignments, frames, trace = _apply_section_id_aliases_to_overrides( {"top-left": ["01-1"], "top-right": ["01-2"], "bottom": ["01-3"]}, {"01-3": "bim_dx_comparison_table"}, aliases, ) assert aliases["01-3"] == "01-intro" assert assignments["bottom"] == ["01-intro"] assert frames["01-intro"] == "bim_dx_comparison_table" assert trace["applied"] is True assert trace["zone_section_rewrites"] == [ {"zone_id": "bottom", "from": "01-3", "to": "01-intro"} ] def test_t22_manual_zone_keys_can_infer_matching_layout(): inferred = _infer_layout_from_zone_section_keys( {"top-left": ["01-1"], "top-right": ["01-2"], "bottom": ["01-intro"]}, unit_count=3, ) assert inferred == "top-2-bottom-1" def _write_intro_fixture(name: str) -> Path: mdx = _tmp_mdx(name) mdx.write_text( """--- title: Sample --- - **Intro label** - Intro body text ## 1. First section - First body ## 2. Second section - Second body """, encoding="utf-8", ) return mdx