llm-ingestion-okf/tests/test_segmented_golden.py

154 lines
5.3 KiB
Python

"""The SEGMENTED_V1 golden: bytes, pinned.
A NEW golden directory, never an edit to the four that exist. That is how
`2504011` shipped `OKF_V0_2`'s golden -- zero bytes changed in the three that
were already there -- and it is what makes "additive" a measured claim rather
than an intention. The second test below is the measurement.
The fixture is synthetic throughout. No consumer content, no real N500 or
procurement prose: this repository is public, and a golden is the most durable
place a leak could land.
Door B shaped, unlike the four Door A goldens: the inputs are an inbox, a
segmentation plan and a `bundle_id`, because those are exactly what a rebuild
replays. Re-materializing them into an empty directory must reproduce the
shipped tree file for file -- which is S7 stated as a fixture instead of as a
property test.
"""
from __future__ import annotations
import json
import subprocess
from pathlib import Path
from llm_ingestion_okf.inbox import GateDecision, process_inbox
from llm_ingestion_okf.profiles import SEGMENTED_V1
from llm_ingestion_okf.segmentation import parse_segmentation_plan
EXAMPLES = Path(__file__).resolve().parents[1] / "examples"
CASE = EXAMPLES / "ingest-golden-segmented"
EXISTING_GOLDENS = (
"ingest-golden-file",
"ingest-golden-sql",
"ingest-golden-http",
"ingest-golden-okf-v0-2",
)
def gate(text: str) -> GateDecision:
return GateDecision(sanitized_text=text, disposition="warn")
def materialize(out_dir: Path) -> None:
plan = parse_segmentation_plan(json.loads((CASE / "segmentation-plan.json").read_text("utf-8")))
process_inbox(
CASE / "fixture" / "inbox",
out_dir,
(CASE / "ingested-at.txt").read_text(encoding="utf-8").strip(),
okf_type="reference",
gate=gate,
profile=SEGMENTED_V1,
root_frontmatter_values={
"bundle_id": (CASE / "bundle-id.txt").read_text(encoding="utf-8").strip()
},
segmentation=plan,
)
def tree(root: Path) -> dict[str, bytes]:
return {
path.relative_to(root).as_posix(): path.read_bytes()
for path in sorted(root.rglob("*"))
if path.is_file()
}
def test_the_golden_rematerializes_byte_for_byte(tmp_path: Path) -> None:
out_dir = tmp_path / "bundle"
materialize(out_dir)
expected = tree(CASE / "expected-bundle")
actual = tree(out_dir)
assert sorted(actual) == sorted(expected)
for name in sorted(expected):
assert actual[name] == expected[name], f"{name} diverges from the golden bytes"
def test_the_golden_covers_the_shape_the_profile_exists_for(tmp_path: Path) -> None:
# A golden that happened to be flat would pin nothing this profile adds.
expected = tree(CASE / "expected-bundle")
concepts = [name for name in expected if not name.endswith(SEGMENTED_V1.index.name)]
assert len(concepts) >= 3
assert any("/" in name for name in concepts)
assert len([name for name in expected if name.endswith(SEGMENTED_V1.index.name)]) >= 3
assert expected["index.md"].startswith(b"---\nbundle_id: ")
def test_the_four_existing_goldens_are_untouched() -> None:
# Measured against the baseline commit, not asserted. `git status` covers
# an uncommitted edit; `git diff` against the baseline covers a committed
# one -- and this voyage makes commits, so the second is the one that bites.
baseline = (
Path(__file__).resolve().parents[1]
/ ".claude/projects/2026-08-30-door-b-concept-granularity/baseline/sha"
)
if not baseline.is_file():
# The baseline is LOCAL-ONLY and gitignored, so a fresh clone has none.
# Skipping silently would make this test vacuous where it matters most,
# so the weaker check still runs.
result = subprocess.run(
[
"git",
"status",
"--porcelain",
"--",
*(f"examples/{name}" for name in EXISTING_GOLDENS),
],
cwd=Path(__file__).resolve().parents[1],
capture_output=True,
text=True,
check=True,
)
assert result.stdout == ""
return
result = subprocess.run(
[
"git",
"diff",
"--exit-code",
baseline.read_text(encoding="utf-8").strip(),
"--",
*(f"examples/{name}" for name in EXISTING_GOLDENS),
],
cwd=Path(__file__).resolve().parents[1],
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, f"an existing golden moved:\n{result.stdout}"
def test_the_new_golden_was_absent_at_the_baseline() -> None:
# Without this, the exclusion above could be hiding churn inside an
# existing golden that had been renamed into the new directory.
baseline = (
Path(__file__).resolve().parents[1]
/ ".claude/projects/2026-08-30-door-b-concept-granularity/baseline/sha"
)
if not baseline.is_file():
return
result = subprocess.run(
[
"git",
"cat-file",
"-e",
f"{baseline.read_text(encoding='utf-8').strip()}:examples/ingest-golden-segmented",
],
cwd=Path(__file__).resolve().parents[1],
capture_output=True,
text=True,
check=False,
)
assert result.returncode != 0, "the new golden already existed at the baseline"