llm-ingestion-okf/tests/test_table_measure.py
Kjell Tore Guttormsen b1977c27ac feat(tools): a re-measurable grid-table reach instrument for K3 [skip-docs]
`tools/okf_table_measure.py` answers how far Arm E's join reaches and what it
costs, and it imports `find_candidates`, `_GRID_RULE`, `_TABLE_ROW` and both
rule names from the shipped module rather than carrying a copy.
`tests/test_table_measure.py` pins that with `is`, not `==`: `re.compile`
returns a distinct object for an equal pattern, so equality would be satisfied
by a pasted literal and only identity catches it. `draw_sample` is imported
from `okf_outline_measure` for the same reason -- one K3 draw in the
repository, not two that can disagree.

The column this round actually needs is the `|`-row count, for EVERY file
including the ones the door refuses. It is the ceiling: a document with no
table row cannot be moved by this arm. Asserting the ceiling from entry counts
instead would assume the orphan check kept every table candidate, which nobody
measured -- and one of the K3 sample documents produces no plan at all, so its
zero would be an absence with no denominator.

Constants split the way `okf_outline_measure.py` splits them. 38 grid-rule
lines across 3 documents is a DECLARED expectation measured before the rule was
written; a different value means the shipped grammar is not the measured one
and no figure below it may be read. The Arm D entry totals are REFERENCE
values, printed beside the measured ones and gating nothing.

A file the door refuses becomes a `Row` with its reason named rather than a
missing row, because `extractable / len(rows)` is the door count this round
reports as 39/43 and a silent drop would make it unmeasurable.

Tests first: 7 red (the module did not exist), then green. 1251 -> 1258.
[skip-docs] on the same measured precedent as the flag commit: `grep -c` for
"okf_outline_measure" and "okf_cid_measure" in README.md returns 0. A
measurement instrument is documented by its own docstring and by the round's
report, and it never enters the wheel.
ruff check: exit 0. ruff format --check: exit 0. mypy --strict src/ tools/:
28 files, Success. pytest -q: exit 0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 10:56:12 +02:00

134 lines
5.3 KiB
Python

"""The grid-table reach instrument: order 20260907T075834Z-18584396-from-.claude.
Measures how far Arm E's grid-rule join reaches into a corpus, and -- the part
the round actually needs -- how many documents carry a `|` table row at all.
That count is the CEILING: Arm E cannot move a document that has no table row,
so a ceiling asserted from entry counts alone would rest on the orphan check
having kept every table candidate, which nobody measured.
The identity test below is the one that matters most, for the reason
`test_outline_measure.py` gives: an instrument that re-implements the grammar it
measures is measuring a SECOND definition, free to drift from the shipped one
without a test going red. `is`, not `==` -- `re.compile` returns a distinct
object for an equal pattern, so equality would be satisfied by a copied literal
and identity is what catches it.
The negative control is zero WITH a nonzero denominator. A document holding a
markdown pipe table has table rows and no grid rules, so it must report
`grid_rule_lines == 0` while `pipe_rows` is positive: "found nothing" and
"measured nothing" are different results and only one of them is evidence.
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools"))
import okf_outline_measure # noqa: E402
import okf_table_measure # noqa: E402
from llm_ingestion_okf import propose as okf_propose_segments # noqa: E402
GRID_DOC = """+-------+-------+
| Navn | Verdi |
+=======+=======+
| Areal | 120 |
+-------+-------+
| Hoyde | 3 |
+-------+-------+
"""
PIPE_DOC = """| Navn | Verdi |
|-------|-------|
| Areal | 120 |
Etterfoelgende avsnitt.
"""
NO_TABLE_DOC = """# Teknisk grunnlag
Innledende tekst uten tabell i det hele tatt.
"""
def test_the_instrument_and_the_tool_are_one_definition() -> None:
"""Identity, not equality: the cheapest proof there is no second grammar.
The regex identities are the load-bearing ones. `re.compile(p) == re.compile(p)`
is False in CPython, so `==` would be the wrong operator anyway -- but a
module that pasted the pattern literal would still LOOK right, and only
`is` says it came from the shipped module.
"""
assert okf_table_measure.find_candidates is okf_propose_segments.find_candidates
assert okf_table_measure._GRID_RULE is okf_propose_segments._GRID_RULE
assert okf_table_measure._TABLE_ROW is okf_propose_segments._TABLE_ROW
assert okf_table_measure.RULE_TABLE_GRID is okf_propose_segments.RULE_TABLE_GRID
assert okf_table_measure.RULE_TABLE_BLOCK is okf_propose_segments.RULE_TABLE_BLOCK
def test_the_draw_is_the_committed_one() -> None:
"""One K3 draw in the repository, not two that can disagree."""
assert okf_table_measure.draw_sample is okf_outline_measure.draw_sample
def test_a_hand_computed_grid_table_reports_its_figures() -> None:
"""Every field computed by hand from the fixture, not by calling the code.
The document is three row groups separated by rule lines: 3 `|` rows, 4
rule lines (top, header separator, and two more), 3 blocks today, 1 after
the join, 1 block joined, 3 entries before and 1 after.
"""
measurement = okf_table_measure.measure_document(GRID_DOC, "grid.md")
assert measurement.pipe_rows == 3
assert measurement.grid_rule_lines == 4
assert measurement.blocks_before == 3
assert measurement.blocks_after == 1
assert measurement.joined_blocks == 1
assert measurement.entries_before == 3
assert measurement.entries_after == 1
def test_a_document_with_pipe_tables_and_no_grid_rules_reports_zero_with_a_nonzero_denominator() -> (
None
):
"""Zero reach is evidence only when something was measured."""
measurement = okf_table_measure.measure_document(PIPE_DOC, "pipe.md")
assert measurement.grid_rule_lines == 0
assert measurement.joined_blocks == 0
assert measurement.blocks_before == measurement.blocks_after == 1
assert measurement.pipe_rows > 0
assert measurement.entries_before == measurement.entries_after
def test_a_document_with_no_table_at_all_is_zero_across_the_board() -> None:
"""The other denominator: a document Arm E cannot reach, and cannot claim."""
measurement = okf_table_measure.measure_document(NO_TABLE_DOC, "prose.md")
assert measurement.pipe_rows == 0
assert measurement.grid_rule_lines == 0
assert measurement.blocks_before == 0
assert measurement.entries_before == measurement.entries_after == 1
def test_an_empty_document_does_not_crash() -> None:
measurement = okf_table_measure.measure_document("", "empty.md")
assert measurement.pipe_rows == 0
assert measurement.entries_before == 0
def test_a_non_extractable_file_becomes_a_row_with_a_skip_reason(tmp_path: Path) -> None:
"""A file the door refuses stays in the denominator with its reason named.
Dropping it would make `extractable / len(rows)` unmeasurable, and that
ratio is the door count this round reports as 39/43.
"""
(tmp_path / "a.md").write_text(GRID_DOC, encoding="utf-8", newline="")
(tmp_path / "b.xyz").write_bytes(b"not a known type")
rows = okf_table_measure.run(tmp_path)
assert len(rows) == 2
measured = [r for r in rows if r.measurement is not None]
skipped = [r for r in rows if r.measurement is None]
assert len(measured) == 1
assert len(skipped) == 1
assert skipped[0].skip_reason