portfolio-optimiser/tests/test_ingest_golden_http.py

64 lines
2.4 KiB
Python

"""I6 golden regression — the §11 golden extraction case for `http`, byte-for-byte.
Mirrors tests/test_ingest_golden_sql.py for the third source type. The case pins the §5 http
rules AS RULES: the body is rendered verbatim inside a fenced code block (NOT a markdown table),
so a raw ``|`` and ``\`` survive un-escaped — the exact discriminator vs ``render_table``. RED when
ANY byte of the expected bundle diverges (spec §11 "Golden regression" seam).
The canned ``get`` returns the committed fixture payloads (rebuild:
``.claude/projects/2026-07-04-i6-ingest-http-maf/build_fixture.py``) — no socket, no credentials
(``credential_ref`` is null), so the golden runs offline. The base_url / query never enter the
bundle, so the golden is checkout-location independent.
"""
from __future__ import annotations
from pathlib import Path
from portfolio_optimiser.ingest import HttpGet, materialize
GOLDEN = Path(__file__).resolve().parents[1] / "examples" / "ingest-golden-http"
def _bundle_bytes(directory: Path) -> dict[str, bytes]:
return {p.name: p.read_bytes() for p in sorted(directory.iterdir()) if p.is_file()}
def _fixture_get(fixture_dir: Path) -> HttpGet:
"""Canned transport: map the joined URL's last path segment to its committed fixture file."""
def get(url: str, credential: str | None) -> str:
name = url.rsplit("/", 1)[-1]
return (fixture_dir / name).read_text(encoding="utf-8")
return get
def test_golden_http_extraction_is_bit_deterministic(tmp_path: Path) -> None:
ingested_at = (GOLDEN / "ingested-at.txt").read_text(encoding="utf-8").strip()
fake_get = _fixture_get(GOLDEN / "fixture")
out = tmp_path / "bundle"
materialize(
GOLDEN / "manifest.json",
out,
ingested_at=ingested_at,
allow_network=True,
http_get=fake_get,
)
expected = _bundle_bytes(GOLDEN / "expected-bundle")
actual = _bundle_bytes(out)
# File-SET equality first — catches extra AND missing files, not just diverging bytes.
assert actual.keys() == expected.keys()
for name, content in expected.items():
assert actual[name] == content, f"golden byte divergence in {name}"
# §10: a second run over the same output leaves every byte unchanged.
materialize(
GOLDEN / "manifest.json",
out,
ingested_at=ingested_at,
allow_network=True,
http_get=fake_get,
)
assert _bundle_bytes(out) == expected