llm-ingestion-okf/tests/test_golden.py
Kjell Tore Guttormsen 9dd86b1b18 feat(examples): ship the §11 golden fixtures with byte-exact conformance
TDD step 8: three golden cases per the §11 format (manifest.json,
fixture/, ingested-at.txt, expected-bundle/) — ingest-golden-file and
ingest-golden-sql are the conformance MUSTs; ingest-golden-http
documents and exercises the optional extension against mock payloads.
The conformance test re-materializes each case into a fresh directory
and compares byte for byte; offline and credential-free (the sql
fixture is a committed database file, http never opens a socket).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QeqhJpYQyghASjiJo5EhGg
2026-07-16 20:09:03 +02:00

64 lines
2.4 KiB
Python

"""Golden byte-for-byte conformance (ingest-spec §11) — load-bearing.
Each golden case is re-materialized into a fresh directory and compared
against its expected-bundle file by file, byte for byte. The test MUST fail
when any byte of an expected bundle diverges. Offline, credential-free:
the sql fixture is a committed database file, the http case runs against
mock payloads served from its fixture directory.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from llm_ingestion_okf.materialize import materialize_bundle
EXAMPLES = Path(__file__).parent.parent / "examples"
INGESTED_AT_NAME = "ingested-at.txt"
def fixture_backed_get(fixture_dir: Path) -> object:
"""A mock transport serving fixture/{query path} — never a socket."""
def get(url: str, credential: str | None) -> str:
path = url.rsplit("/", 1)[-1]
return (fixture_dir / path).read_text(encoding="utf-8")
return get
def materialize_case(case_dir: Path, out_dir: Path, monkeypatch: pytest.MonkeyPatch) -> None:
ingested_at = (case_dir / INGESTED_AT_NAME).read_text(encoding="utf-8").strip()
if case_dir.name.endswith("sql"):
monkeypatch.setenv("OKF_GOLDEN_SQL_DB", str(case_dir / "fixture" / "metrics.db"))
materialize_bundle(case_dir / "manifest.json", out_dir, ingested_at)
elif case_dir.name.endswith("http"):
materialize_bundle(
case_dir / "manifest.json",
out_dir,
ingested_at,
allow_network=True,
http_get=fixture_backed_get(case_dir / "fixture"), # type: ignore[arg-type]
)
else:
materialize_bundle(case_dir / "manifest.json", out_dir, ingested_at)
@pytest.mark.parametrize("case", ["ingest-golden-file", "ingest-golden-sql", "ingest-golden-http"])
def test_golden_case_byte_for_byte(
case: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
case_dir = EXAMPLES / case
expected_dir = case_dir / "expected-bundle"
out_dir = tmp_path / "bundle"
materialize_case(case_dir, out_dir, monkeypatch)
expected_files = sorted(path.name for path in expected_dir.iterdir())
actual_files = sorted(path.name for path in out_dir.iterdir())
assert actual_files == expected_files
for name in expected_files:
assert (out_dir / name).read_bytes() == (expected_dir / name).read_bytes(), (
f"{case}/{name} diverges from the golden bytes"
)