"""FastAPI endpoint tests. Use Starlette's TestClient so we never hit a real port. The tests verify the response contract the React UI relies on. """ from __future__ import annotations import pytest from fastapi.testclient import TestClient from app.api.server import app @pytest.fixture(scope="module") def client() -> TestClient: with TestClient(app) as c: yield c def test_healthz(client): r = client.get("/api/healthz") assert r.status_code == 200 body = r.json() assert body["status"] == "ok" assert "version" in body def test_status_reports_ok_with_reference_loaded(client): r = client.get("/api/status") assert r.status_code == 200 body = r.json() assert body["status"] == "ok", f"degraded: {body.get('startup_errors')}" assert body["pipeline_ready"] is True assert body["reference_counts"]["hcc_labels"] > 0 assert body["reference_counts"]["hcc_crosswalk_icd10"] > 0 assert body["gallery_size"] > 0 assert body["metrics_available"] is True def test_code_endpoint_roundtrip(client): r = client.post( "/api/code", json={"text": "A/P: Type 2 diabetes mellitus with diabetic polyneuropathy. Continue metformin."}, ) assert r.status_code == 200, r.text body = r.json() assert body["note_id"] == "adhoc" assert isinstance(body["candidates"], list) hccs = {c["hcc"] for c in body["candidates"]} assert "HCC-18" in hccs def test_gallery_returns_precomputed_cases(client): r = client.get("/api/gallery") assert r.status_code == 200 body = r.json() assert isinstance(body, list) and len(body) > 0 first = body[0] assert {"case_id", "name", "tags", "text", "result"}.issubset(first) def test_metrics_snapshot_shape(client): r = client.get("/api/metrics") assert r.status_code == 200 body = r.json() assert "overall" in body and "per_hcc" in body and "meat_category" in body assert "subgroups" in body and "note_length" in body["subgroups"] def test_reference_hcc(client): r = client.get("/api/reference/hcc") assert r.status_code == 200 rows = r.json() assert any(row["hcc_id"] == "HCC-85" for row in rows) def test_code_endpoint_rejects_empty_body(client): r = client.post("/api/code", json={"text": ""}) # Pydantic's min_length=1 returns 422 assert r.status_code == 422