portfolio-optimiser-claude/tests/test_ingest_adoption.py
Kjell Tore Guttormsen 5732d13369 feat(ingest): adopt llm-ingestion-okf as Door A implementation (first consumer)
Replace the local 391-line ingest implementation with a thin adapter over
the shared llm-ingestion-okf library (git-pinned dae0bd1a via Forgejo,
tool.uv.sources). The materialize() signature is preserved; error types are
now the library's typed hierarchy rooted in IngestError, re-exported from
the consumer seam.

- tests/test_ingest_adoption.py: new load-bearing seam tests (delegation,
  offline invariant — allow_network is never passed, error contract),
  detach-proven red twice.
- Golden suites (file + sql) pass UNCHANGED — byte-exact behaviour proven
  against the repo-local fixtures.
- 6 test files migrated to the library error hierarchy; escaping/typed-cell
  unit tests dropped (byte-bound by the ingest-edge.md golden, unit-owned by
  the library's own 189-test suite). Provenance stamp now asserted
  independently from the §5 rule.
- mypy override follow_untyped_imports for llm_ingestion_okf (no py.typed
  upstream yet — reported as a finding).

Suite: 386 passed; ruff + format + mypy --strict clean; shared/, examples/,
runs/s10/ and run_s10.py byte-untouched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-16 20:46:51 +02:00

101 lines
3.8 KiB
Python

"""Adoption seam: Door A ingest is the shared llm-ingestion-okf library (§11).
This repo's local ingest implementation was replaced by the shared library
(first consumer adoption). These load-bearing tests bind the adapter seam:
- Delegation — ``ingest.materialize`` IS a call into the library (RED if a
local reimplementation sneaks back in).
- Offline invariant — the adapter NEVER passes the per-run network opt-in:
an http-source manifest is refused at the library's network gate (RED if
the adapter starts granting network access).
- Error contract — the consumer-facing error types ARE the library's typed
hierarchy rooted in ``IngestError`` (RED if the seam re-wraps or forks).
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
import llm_ingestion_okf
from portfolio_optimiser_claude import ingest
INGESTED_AT = "2026-07-16T12:00:00Z"
def _http_manifest() -> dict[str, Any]:
return {
"manifest_version": 1,
"source": {"type": "http", "id": "api", "base_url": "https://example.invalid"},
"bundle_summary": "s",
"extractions": [
{"id": "e", "title": "T", "query": "rows", "okf_type": "dataset", "max_rows": 1}
],
}
class TestDelegation:
"""Seam: the consumer entry points are the library's (RED if detached)."""
def test_load_manifest_is_the_library_entry_point(self) -> None:
assert ingest.load_manifest is llm_ingestion_okf.load_manifest
def test_materialize_delegates_with_the_offline_default(
self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
calls: dict[str, Any] = {}
def fake(
manifest_path: Path, bundle_dir: Path, ingested_at: str, **kwargs: Any
) -> llm_ingestion_okf.IngestResult:
calls["args"] = (manifest_path, bundle_dir, ingested_at)
calls["kwargs"] = kwargs
return llm_ingestion_okf.IngestResult(written=(tmp_path / "ingest-e.md",))
monkeypatch.setattr(ingest, "materialize_bundle", fake)
out = ingest.materialize(tmp_path / "manifest.json", tmp_path / "bundle", INGESTED_AT)
assert out == [tmp_path / "ingest-e.md"] # IngestResult.written → list, order kept
assert calls["args"] == (tmp_path / "manifest.json", tmp_path / "bundle", INGESTED_AT)
# The offline invariant at the seam: allow_network/http_get are NEVER
# passed — the library's local-only default stays in force.
assert calls["kwargs"] == {}
class TestOfflineInvariant:
"""Seam: the adapter cannot grant network access (RED if it opts in)."""
def test_http_source_is_refused_at_the_network_gate(self, tmp_path: Path) -> None:
manifest = tmp_path / "manifest.json"
manifest.write_text(json.dumps(_http_manifest()), encoding="utf-8")
bundle = tmp_path / "bundle"
with pytest.raises(ingest.NetworkGateError):
ingest.materialize(manifest, bundle, INGESTED_AT)
assert not bundle.exists() or not any(bundle.iterdir()) # gate fires before any write
class TestErrorContract:
"""Seam: consumer-facing errors ARE the library hierarchy (RED if forked)."""
def test_error_types_are_the_library_types(self) -> None:
for name in (
"IngestError",
"ManifestError",
"MaterializationError",
"NetworkGateError",
"RenderError",
"SourceError",
):
assert getattr(ingest, name) is getattr(llm_ingestion_okf, name)
def test_every_error_roots_in_ingest_error(self) -> None:
for exc in (
ingest.ManifestError,
ingest.MaterializationError,
ingest.NetworkGateError,
ingest.RenderError,
ingest.SourceError,
):
assert issubclass(exc, ingest.IngestError)