"""A declared section below markdown's sixth level keeps its own level in the plan. K3-21 D. The NISO-STS reader wrote ONE clipped level into two places: the ATX heading it emits, where markdown has six levels and the clip is the grammar's, and the `OutlineMark` the declared route builds its plan from, where nothing clips and the source's own depth is the point (`OutlineMark`'s docstring: the level is what the tree declares, "reported rather than fixed up"). On one standard 9 of 2 761 titled sections sit at depth 7, and the plan read them at 6 -- so `--shell-parent` gave its two depth-7 shells the ancestor one level too high. The heading stays clipped: `#######` matches nothing, and the extracted TEXT does not move one character. Only the mark's level does. `sts-deep.xml` is hand-written in an invented setting and carries no sentence from any source. """ from __future__ import annotations from pathlib import Path from llm_ingestion_okf import extract, propose FIXTURE = Path(__file__).parent / "fixtures" / "sts-deep.xml" #: Every flag `okf build` turns on by default. BUILD_DEFAULTS = dict( outline_run=3, table_grid=True, unit_fold=True, keep_table_heading=True, sheet_section_rows=True, drop_wrapped_outline=True, outline_gate=True, first_span_from_zero=True, close_span_gaps=True, contents_name=True, ) #: The extracted headings, as they were before this rule and after it. HEADINGS = [ "# 1 Nivaa en", "## 1.1 Nivaa to", "### 1.11 Nivaa tre", "#### 1.111 Nivaa fire", "##### 1.1111 Nivaa fem", "###### 1.11111 Nivaa seks", "###### 1.111111 Nivaa sju foerste", "###### 1.111112 Nivaa sju andre", ] def test_the_mark_carries_the_declared_depth() -> None: marks = extract.xml_outline(FIXTURE.name, FIXTURE.read_bytes()) assert [mark.level for mark in marks] == [1, 2, 3, 4, 5, 6, 7, 7] def test_the_heading_stays_markdown_and_the_text_does_not_move() -> None: data = FIXTURE.read_bytes() text = extract.extract_text(FIXTURE.name, data) lines = text.split("\n") assert [line for line in lines if line.startswith("#")] == HEADINGS for mark in extract.xml_outline(FIXTURE.name, data): assert lines[mark.line] == "#" * min(mark.level, 6) + " " + mark.title def test_a_depth_seven_shell_points_at_its_depth_six_ancestor() -> None: data = FIXTURE.read_bytes() text = extract.extract_text(FIXTURE.name, data) plan = propose.build_plan( FIXTURE, text, data, okf_type="reference", proposed_at="2026-01-01T00:00:00Z", shell_parent=True, **BUILD_DEFAULTS, # type: ignore[arg-type] ) titles = {entry["segment_id"]: entry["title"] for entry in plan["entries"]} parents = {entry["title"]: titles.get(entry.get("parent_id", "")) for entry in plan["entries"]} assert parents["Nivaa sju foerste"] == "Nivaa seks" assert parents["Nivaa sju andre"] == "Nivaa seks"