gabriel / musehub public
test_overseer_provenance_f7_http.py python
132 lines 4.2 KB
Raw
sha256:f65847c6a4ae29872f3ccbc2420f91a8948949b84a06320bdaf2ac281c89ff8a docs(F7b): governance sync — BUILT awaiting Gabriel; carry … Human patch 29 days ago
1 """HTTP assertions — MUSEHUB-F7b overseer-run provenance (freeze 2026-08-11).
2
3 Four TestClient cases across the real app route table. Registration is not
4 modified. Postgres is not required: tests/unit/ no-ops the session DB fixture
5 and this module overrides only get_db (optional_token pulls it transitively).
6 """
7
8 from __future__ import annotations
9
10 from collections.abc import AsyncIterator, Iterator
11
12 import pytest
13 from fastapi.testclient import TestClient
14
15 from musehub.db.database import get_db
16 from musehub.main import app
17 from musehub.services import musehub_overseer_provenance as enrichment
18
19 FIXTURE_REF = "flow_run:fixture-overseer-001"
20 UNKNOWN_REF = "flow_run:not-seeded-0001"
21 PREFIXED_PATH = f"/api/overseer-run-provenance/{FIXTURE_REF}"
22 ENCODED_PATH = "/api/overseer-run-provenance/flow_run%3Afixture-overseer-001"
23
24 EXPECTED_RECORD: dict[str, object] = {
25 "schema": "scooling.overseer_run_provenance/v0",
26 "contractVersion": "scooling.overseer_provenance_enrichment/v0",
27 "runRef": FIXTURE_REF,
28 "reviewTrayOutcomeRef": f"outcome:{FIXTURE_REF}",
29 "outcome": "pass",
30 "constitutionVersion": "0.1.0",
31 "agentVersionRef": "agent:fixture-overseer-v0.1.0",
32 "workerModelFamily": "family:ollama",
33 "checkerModelFamily": "family:llama_cpp",
34 "externalRef": None,
35 "actorHash": "a" * 64,
36 "producedAt": "2026-07-09T12:00:00Z",
37 "untrusted": True,
38 }
39
40 RECORD_KEYS = frozenset(EXPECTED_RECORD.keys())
41 TOP_LEVEL_KEYS = frozenset({"schema", "record"})
42
43
44 @pytest.fixture
45 def client() -> Iterator[TestClient]:
46 """Plain TestClient (no lifespan) with get_db overridden only."""
47
48 async def _override_get_db() -> AsyncIterator[object]:
49 yield object()
50
51 app.dependency_overrides[get_db] = _override_get_db
52 try:
53 yield TestClient(app)
54 finally:
55 app.dependency_overrides.pop(get_db, None)
56
57
58 def _enable_posture(monkeypatch: pytest.MonkeyPatch) -> None:
59 monkeypatch.setattr(
60 enrichment.settings,
61 "musehub_overseer_provenance_enrichment_enabled",
62 "enabled",
63 )
64
65
66 def _disable_posture(monkeypatch: pytest.MonkeyPatch) -> None:
67 monkeypatch.setattr(
68 enrichment.settings,
69 "musehub_overseer_provenance_enrichment_enabled",
70 "disabled",
71 )
72
73
74 def test_a1_prefixed_path_returns_envelope(
75 client: TestClient, monkeypatch: pytest.MonkeyPatch
76 ) -> None:
77 """A1 — posture enabled; /api/…/flow_run:fixture → 200 with exact envelope."""
78 _enable_posture(monkeypatch)
79
80 response = client.get(PREFIXED_PATH)
81
82 assert response.status_code == 200
83 body = response.json()
84 assert set(body.keys()) == TOP_LEVEL_KEYS
85 assert body["schema"] == "musehub.overseer_run_provenance_envelope/v0"
86 record = body["record"]
87 assert set(record.keys()) == RECORD_KEYS
88 assert record == EXPECTED_RECORD
89
90
91 def test_a3_unknown_ref_returns_404(
92 client: TestClient, monkeypatch: pytest.MonkeyPatch
93 ) -> None:
94 """A3 — posture enabled; well-formed unknown ref → 404 (store miss)."""
95 _enable_posture(monkeypatch)
96
97 response = client.get(f"/api/overseer-run-provenance/{UNKNOWN_REF}")
98
99 assert response.status_code == 404
100
101
102 def test_a2_percent_encoded_ref_returns_decoded_record(
103 client: TestClient, monkeypatch: pytest.MonkeyPatch
104 ) -> None:
105 """A2 — percent-encoded ref → 200; runRef decoded; full record equals A1."""
106 _enable_posture(monkeypatch)
107
108 response = client.get(ENCODED_PATH)
109
110 assert response.status_code == 200
111 body = response.json()
112 assert set(body.keys()) == TOP_LEVEL_KEYS
113 assert body["schema"] == "musehub.overseer_run_provenance_envelope/v0"
114 record = body["record"]
115 assert record["runRef"] == FIXTURE_REF
116 assert record == EXPECTED_RECORD
117
118
119 def test_a4_posture_off_returns_404_fail_closed(
120 client: TestClient, monkeypatch: pytest.MonkeyPatch
121 ) -> None:
122 """A4 — posture disabled + known-good ref → 404; no record field leakage."""
123 _disable_posture(monkeypatch)
124
125 response = client.get(PREFIXED_PATH)
126
127 assert response.status_code == 404
128 body_text = response.text
129 assert "runRef" not in body_text
130 assert "outcome" not in body_text
131 assert "actorHash" not in body_text
132 assert FIXTURE_REF not in body_text
File History 1 commit
sha256:f65847c6a4ae29872f3ccbc2420f91a8948949b84a06320bdaf2ac281c89ff8a docs(F7b): governance sync — BUILT awaiting Gabriel; carry … Human patch 29 days ago