Files
stack/tests/pfs/test_cpt_epub.py
kert 969d34c41c fix(pfs): CPT parser — chapter title parents its headings, numeric range containment, C9 winners verified on real editions (refs #687)
C10, two Important findings on C9 traced to one wrong structural
assumption: div.ch-title ("Surgery Guidelines", "Surgery", "Anesthesia
Guidelines", …) was being treated as a level-1 heading, so the next
real h1 popped it off the stack — every guideline-reprint heading's
path was flat (just its own title), never nested under its chapter.

1. ch-title is now the level-0 parent of every heading in its own
   chapter-group and every later chapter-group that carries no banner
   of its own — replaced only by the next ch-title, never popped by an
   hN (level >= 1 never pops level 0). Most chapter files (a chapter
   split across many numbered files for size, e.g. 2019's Surgery body
   spans Chapter07-16 with the banner only on Chapter06) carry no
   ch-title at all, so _group now takes/returns the active chapter
   title and parse_epub threads it from one chapter-group's call into
   the next's.

   Fixing this exposed a second, previously-inert bug: _chapter_groups
   returned groups in zf.namelist() order, which is book order in 2019
   but arbitrarily scrambled in 2021/2022/2024 (verified directly).
   That never mattered pre-C10 (groups were parsed independently); with
   title-threading it corrupted paths across unrelated chapters (21089
   landing under "Medicine Guidelines" in 2021, "Pathology and
   Laboratory" in 2024). Groups are now sorted by chapter number.

2. _in_range now compares numerically on min(lo, hi)..max(lo, hi) — a
   resequenced heading's own TOC span can print in display order rather
   than numeric order (lo > hi), which the old plain string comparison
   silently got wrong for.

3. C9's scoring formula is unchanged. Verified on all four real
   editions: 21089 (Surgery) now resolves under "Surgery > Head > Head
   Prosthesis > Other Procedures", its "Surgery Guidelines > Unlisted
   Service or Procedure" copy lands in cpt_code_alt (reason
   guidelines-reprint); 99100 resolves to Medicine's ranged "Qualifying
   Circumstances for Anesthesia" (the book's only TOC-declared range for
   that code), with Anesthesia's own "Qualifying Circumstances" as the
   alternate — documented in parse_epub's docstring as the accepted
   outcome, not a bug. 99490 (2024) path_key is now "Evaluation and
   Management > Care Management Services > Chronic Care Management
   Services", matching the brief's original example exactly.
   guidelines-reprint > 0 in cpt_code_alt for every edition (2019: 132,
   2021: 130, 2022: 38, 2024: 130).

4. Minors: losing entries' instructions/references are deduped by
   (code, kind, text) before resolving alternates, so an exact-repeat
   parenthetical note from a guideline reprint doesn't survive as a
   spurious extra row; removed the redundant ensure_tables call in
   cpt-ingest's write path (write_cpt_edition already calls it);
   _in_range's docstring documents the resequenced-span reversal.
2026-09-09 19:02:08 -04:00

27 KiB