Files
stack/tests/llm/test_policy_api.py
kert 6be08a3834 fix(llm): match dossier and valuation tests to current families
CCM is curated, and the book has no planned family left, so the 404
status test injects one. 99213 and 99214 expand to the office-visit
family; the concurrency test counts the asked code's own rows.
2026-11-16 13:29:01 -05:00

441 lines
15 KiB
Python

"""The policy API (lex S1 Task 3): router, per-mtime cache, ETag/304, errors."""
from __future__ import annotations
import dataclasses
import os
import duckdb
import pytest
from fastapi.testclient import TestClient
from llm import evidence, policy
from llm.api import app
from llm.config import LlmConfig
from llm.policy_slices import SECTIONS, SourceInfo
from pfs.codetables import ensure_tables
from pfs.dossier import read_dossier, write_dossier
from tests.llm.policy_fixture import fixture_dossier
CFG = LlmConfig(
ollama_hosts=("http://h1:11434",),
embed_model="e",
instruct_model="c",
embed_dim=768,
build_ann_index=False,
pg_host="x",
pg_port=5432,
pg_db="llm",
pg_user="llm",
duckdb_replica="/nonexistent/aco.ro.duckdb",
)
URL = "/api/policy/dossier/{}"
HV = {"family": "HOME-VISITS"}
def _write(path, *dossiers, sql=None):
# a republish replaces the file; drop the cached read-only handle first
if evidence._REPLICA is not None:
evidence._REPLICA[2].close()
evidence._REPLICA = None
con = duckdb.connect(str(path))
ensure_tables(con)
for d in dossiers:
write_dossier(con, d)
if sql:
con.execute(sql)
con.close()
@pytest.fixture
def replica(tmp_path, monkeypatch):
# hermetic: opening the replica must not rewrite the global registry
monkeypatch.setattr(evidence, "refresh_from", lambda con: 0)
path = tmp_path / "aco.ro.duckdb"
_write(path, fixture_dossier())
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(path)
)
evidence._REPLICA = None
policy.clear_cache()
yield path
app.dependency_overrides.clear()
evidence._REPLICA = None
policy.clear_cache()
@pytest.fixture
def client(replica):
return TestClient(app)
def _count_reads(monkeypatch):
calls = []
def counting(con, family):
calls.append(family)
return read_dossier(con, family)
monkeypatch.setattr("pfs.dossier.read_dossier", counting)
return calls
def test_section_ok_etag_and_304(client):
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
assert r.headers["cache-control"] == "no-cache"
assert r.headers["content-type"] == "application/json"
etag = r.headers["etag"]
assert etag.startswith('"') and len(etag) == 22
r2 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": etag})
assert r2.status_code == 304 and r2.content == b""
assert r2.headers["etag"] == etag
r3 = client.get(
URL.format("header"), params=HV, headers={"If-None-Match": "W/" + etag}
)
assert r3.status_code == 304
r4 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": '"x"'})
assert r4.status_code == 200
r5 = client.get(URL.format("header"), params=HV, headers={"If-None-Match": "*"})
assert r5.status_code == 304
@pytest.mark.parametrize("section", SECTIONS)
def test_every_built_section_serves(client, section):
r = client.get(URL.format(section), params=HV)
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
def _store():
from bib.store import Store
st = Store(":memory:")
con = st._con()
con.executescript(
"""
INSERT INTO items (id, key, item_type, title, url) VALUES
(1, 'PETM225D', 'regulation', 'Final rule PETM', 'https://fr.test/PETM225D'),
(2, 'ART1', 'report', 'The article', 'https://art.test/1'),
(3, 'BARE', 'report', '', '');
INSERT INTO fr_anchor_docs
(item_key, document_number, html_url, start_page, end_page, fr_volume)
VALUES ('PETM225D', '2022-1', 'https://fr.test', 1, 2, 87);
INSERT INTO attachments (item_id, key, filename) VALUES (2, 'ATT1', 'a.pdf');
"""
)
con.commit()
return st
def test_bib_info_reads_status():
st = _store()
info = policy.bib_info(st, ["PETM225D", "ART1", "BARE", "NOPE", "ART1"])
assert info["PETM225D"] == SourceInfo(
"Final rule PETM", "https://fr.test/PETM225D", True, True
)
assert info["ART1"] == SourceInfo("The article", "https://art.test/1", True, False)
assert info["BARE"] == SourceInfo("", "", False, False)
assert "NOPE" not in info
assert policy.bib_info(st, []) == {}
def test_sources_endpoint_uses_bib_once(client, monkeypatch):
calls = []
def factory():
calls.append(1)
return _store()
app.dependency_overrides[policy.bib_dep] = lambda: factory
monkeypatch.setenv("DOMAIN", "lex.test")
r1 = client.get(URL.format("sources"), params=HV)
assert r1.status_code == 200
r2 = client.get(URL.format("sources"), params=HV)
assert r2.content == r1.content and len(calls) == 1
docs = {x["key"]: x for x in r1.json()["documents"]}
assert docs["PETM225D"]["status"] == "full text on file"
assert docs["ART1"]["viewer_url"] == "https://llm.lex.test/ui/pdf/ART1"
assert docs["MZ24MX5S"]["status"] == "link only"
assert docs["BOOK2019"]["url"] == ""
def test_sources_viewer_base_defaults_to_config_domain(replica, monkeypatch):
monkeypatch.delenv("DOMAIN", raising=False)
cfg = dataclasses.replace(CFG, duckdb_replica=str(replica), domain="cfg.test")
body = policy.load_slice(cfg, "HOME-VISITS", "sources", bib=_store)
art = next(x for x in body["documents"] if x["key"] == "ART1")
assert art["viewer_url"] == "https://llm.cfg.test/ui/pdf/ART1"
def test_contents_endpoint_and_machine_counts_missing_table(replica, client):
_write(replica, fixture_dossier(), sql="DROP TABLE pfs.code_family")
policy.clear_cache()
r = client.get("/api/policy/contents")
assert r.status_code == 200
c = r.json()
assert [e["family"] for e in c["families"]] == ["HOME-VISITS"]
assert c["families"][0]["tier"] == "reviewed"
assert c["families"][0]["n_review"] == 3
assert c["machine"] is None # no pfs.code_family in the replica
assert len(c["planned"]) == 12
etag = r.headers["etag"]
r2 = client.get("/api/policy/contents", headers={"If-None-Match": etag})
assert r2.status_code == 304
def test_contents_machine_counts_and_cache(replica, client, monkeypatch):
con = duckdb.connect(str(replica))
con.execute(
"INSERT INTO pfs.code_family (key, code) VALUES "
"('A','1'),('A','2'),('B','3'),('C','4')"
)
con.close()
evidence._REPLICA = None
policy.clear_cache()
assert client.get("/api/policy/contents").json()["machine"] == {
"derived": 3,
"single_code": 2,
}
calls = _count_reads(monkeypatch)
client.get("/api/policy/contents")
client.get("/api/policy/contents")
assert calls == [] # served from the cache, no dossier re-read
def test_contents_machine_tier_and_replica_errors(tmp_path, replica, client):
other = fixture_dossier()
other = dataclasses.replace(other, family="ZZZ-MACHINE", name="Machine one")
_write(replica, fixture_dossier(), other)
policy.clear_cache()
c = client.get("/api/policy/contents").json()
tiers = {e["family"]: e["tier"] for e in c["families"]}
assert tiers == {"HOME-VISITS": "reviewed", "ZZZ-MACHINE": "machine"}
# a missing replica is a 503, an unopenable one too
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(tmp_path / "gone.duckdb")
)
assert client.get("/api/policy/contents").status_code == 503
bad = tmp_path / "bad.duckdb"
bad.write_bytes(b"not a database")
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(bad)
)
assert client.get("/api/policy/contents").status_code == 503
def test_header_tier_and_book(client):
h = client.get(URL.format("header"), params=HV).json()
assert h["tier"] == "reviewed"
assert h["phase"] is not None
def test_machine_tier_for_uncurated_family(replica, client, monkeypatch):
_write(replica, dataclasses.replace(fixture_dossier(), family="ZZZ-MACH"))
r = client.get(URL.format("header"), params={"family": "zzz-mach"})
assert r.status_code == 200
h = r.json()
assert h["tier"] == "machine" and h["phase"] is None and h["domains"] == []
def test_lowercase_family_normalized(client):
r = client.get(URL.format("header"), params={"family": "home-visits"})
assert r.status_code == 200
assert r.json()["family"] == "HOME-VISITS"
def test_bad_family_400(client):
for bad in ("a b", "../x", ""):
r = client.get(URL.format("header"), params={"family": bad})
assert r.status_code == 400, bad
assert r.json()["detail"]["message"] == "invalid family key"
assert r.json()["detail"]["family"] == bad
assert client.get(URL.format("header")).status_code == 422
def test_unknown_section_404(client):
r = client.get(URL.format("nope"), params=HV)
assert r.status_code == 404
assert r.json()["detail"]["sections"] == list(SECTIONS)
def test_no_dossier_404_with_status(client, monkeypatch):
# Every book family is curated now, so "planned" has to be injected.
from pfs.book import load_book
real_book = load_book()
monkeypatch.setattr(
"pfs.book.load_book", lambda *a, **k: {**real_book, "ZZZ-PLAN": object()}
)
r = client.get(URL.format("header"), params={"family": "ZZZ-PLAN"})
assert r.status_code == 404
d = r.json()["detail"]
assert d["status"] == "planned" and d["family"] == "ZZZ-PLAN"
assert d["message"] == "No dossier is built for ZZZ-PLAN yet."
from pfs.curated import load_curated
real = load_curated()
monkeypatch.setattr(
"pfs.curated.load_curated", lambda *a, **k: {**real, "ZZZ-CUR": 1}
)
r = client.get(URL.format("header"), params={"family": "ZZZ-CUR"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "curated"
r = client.get(URL.format("header"), params={"family": "ZZZ-NONE"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "unknown"
def test_derived_status(replica, client, monkeypatch):
from pfs.families import FAMILIES
monkeypatch.setitem(FAMILIES, "ZZZ-REG", object())
r = client.get(URL.format("header"), params={"family": "ZZZ-REG"})
assert r.json()["detail"]["status"] == "derived"
def test_slash_family_key_is_valid(client):
r = client.get(URL.format("header"), params={"family": "A/D-TX-PROGRAM-HOUR"})
assert r.status_code == 404
assert r.json()["detail"]["family"] == "A/D-TX-PROGRAM-HOUR"
def test_replica_missing_503(tmp_path, client):
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(tmp_path / "gone.duckdb")
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
assert r.json()["detail"]["message"] == "The data replica is not available."
def test_replica_unopenable_503(tmp_path, client):
bad = tmp_path / "bad.duckdb"
bad.write_bytes(b"this is not a duckdb file" * 100)
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(bad)
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
def test_dossier_read_failure_503(client, monkeypatch):
def boom(con, family):
raise RuntimeError("io")
monkeypatch.setattr("pfs.dossier.read_dossier", boom)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 503
def test_table_missing_404(tmp_path, client):
path = tmp_path / "empty.duckdb"
con = duckdb.connect(str(path))
con.execute("CREATE TABLE t (a INTEGER)")
con.close()
app.dependency_overrides[policy.config_dep] = lambda: dataclasses.replace(
CFG, duckdb_replica=str(path)
)
r = client.get(URL.format("header"), params=HV)
assert r.status_code == 404
assert r.json()["detail"]["family"] == "HOME-VISITS"
assert r.json()["detail"]["status"] == "curated"
def test_cache_reloads_after_replica_republish(replica, client):
r1 = client.get(URL.format("header"), params=HV)
assert r1.json()["built_at"] != "2026-11-14T00:00:00"
t = os.stat(replica).st_mtime
_write(
replica, dataclasses.replace(fixture_dossier(), built_at="2026-11-14T00:00:00")
)
os.utime(replica, (t + 5, t + 5))
r2 = client.get(URL.format("header"), params=HV)
assert r2.json()["built_at"] == "2026-11-14T00:00:00"
assert r2.headers["etag"] != r1.headers["etag"]
def test_cache_reads_dossier_once_per_mtime(client, monkeypatch):
calls = _count_reads(monkeypatch)
for s in ("header", "timeline", "payment", "header"):
assert client.get(URL.format(s), params=HV).status_code == 200
assert calls == ["HOME-VISITS"]
def test_cache_bounded(replica, client, monkeypatch):
_write(replica, dataclasses.replace(fixture_dossier(), family="ZZZ-TWO"))
monkeypatch.setattr(policy, "CACHE_MAX", 1)
calls = _count_reads(monkeypatch)
client.get(URL.format("header"), params=HV)
client.get(URL.format("header"), params={"family": "ZZZ-TWO"})
assert list(policy._CACHE) == ["ZZZ-TWO"]
client.get(URL.format("header"), params=HV)
assert calls == ["HOME-VISITS", "ZZZ-TWO", "HOME-VISITS"]
def test_fact_endpoint(client):
r = client.get("/api/policy/fact", params={**HV, "id": "F4"})
assert r.status_code == 200
assert r.json()["view"] == "valuation"
etag = r.headers["etag"]
r2 = client.get(
"/api/policy/fact", params={**HV, "id": "F4"}, headers={"If-None-Match": etag}
)
assert r2.status_code == 304
r = client.get("/api/policy/fact", params={**HV, "id": "F999"})
assert r.status_code == 404 and r.json()["detail"]["message"] == "unknown fact"
assert client.get("/api/policy/fact", params={**HV, "id": "x"}).status_code == 422
r = client.get("/api/policy/fact", params={"family": "CCM", "id": "F1"})
assert r.status_code == 404 and r.json()["detail"]["status"] == "curated"
def test_load_slice_matches_http(replica, client):
cfg = dataclasses.replace(CFG, duckdb_replica=str(replica))
for s in ("header", "politics", "facts"):
assert (
policy.load_slice(cfg, "home-visits", s)
== client.get(URL.format(s), params=HV).json()
)
with pytest.raises(policy.PolicyError) as ei:
policy.load_slice(cfg, "a b", "header")
assert ei.value.status == 400
assert policy.load_fact(cfg, "HOME-VISITS", "F4")["view"] == "valuation"
def test_router_mounted_on_app():
# this FastAPI nests included routers under app.routes; openapi flattens
paths = set(app.openapi()["paths"])
assert "/api/policy/dossier/{section}" in paths
assert "/api/policy/fact" in paths
def test_default_dependencies(monkeypatch):
monkeypatch.setattr("llm.config.load", lambda: "cfg")
assert policy.config_dep() == "cfg"
from conf import connect
assert policy.bib_dep() is connect.bib
def test_contents_read_failure_503(client, monkeypatch):
def boom(con, family):
raise RuntimeError("io")
monkeypatch.setattr("pfs.dossier.read_dossier", boom)
assert client.get("/api/policy/contents").status_code == 503
def test_machine_counts_reraises_other_errors():
class Cur:
def execute(self, *a):
raise RuntimeError("io")
with pytest.raises(RuntimeError):
policy._machine_counts(Cur())
def test_load_contents_matches_http(replica, client):
cfg = dataclasses.replace(CFG, duckdb_replica=str(replica))
assert policy.load_contents(cfg) == client.get("/api/policy/contents").json()