from __future__ import annotations import numpy as np from fastapi.testclient import TestClient from app.api import search as search_api from app.api import suite as suite_api from app.engine.embedder import TextEmbedder from app.main import app from app.storage.project_store import LoadedRun, project_store def test_api_end_to_end_with_loaded_run( synthetic_run: LoadedRun, synthetic_embedder: TextEmbedder, monkeypatch, ): run_id = synthetic_run.manifest.run_id # Register synthetic run in global store & stub embedder lookup monkeypatch.setitem(project_store._cache, run_id, synthetic_run) monkeypatch.setattr( search_api, "_load_run_and_embedder", lambda rid: (synthetic_run, synthetic_embedder), ) monkeypatch.setattr( suite_api, "_load_run_and_embedder", lambda rid: (synthetic_run, synthetic_embedder), ) client = TestClient(app) # 1. Health & Hardware r_health = client.get("/api/health") assert r_health.status_code == 200 and r_health.json()["status"] == "ok" r_hw = client.get("/api/hardware") assert r_hw.status_code == 200 assert len(r_hw.json()["models"]) >= 4 assert len(r_hw.json()["presets"]) >= 3 # 2. Binary Float32Array Atlas endpoint r_bin = client.get(f"/api/projects/{run_id}/atlas-binary") assert r_bin.status_code == 200 arr = np.frombuffer(r_bin.content, dtype=np.float32) assert arr.size == synthetic_run.manifest.doc_count * 8 # 3. Document detail r_doc = client.get(f"/api/projects/{run_id}/docs/5") assert r_doc.status_code == 200 assert r_doc.json()["doc_id"] == 5 # 4. Hybrid search + Forensic Diff + Saliency + Stress-Test + Export s_payload = { "query": "waterproof trail running shoes", "mode": "convex", "alpha": 0.6, "normalization": "minmax", "fetch_k": 20, "top_k": 5, "pinned_doc_id": 5, } r_search = client.post(f"/api/projects/{run_id}/search", json=s_payload) assert r_search.status_code == 200 s_data = r_search.json() assert len(s_data["waterfall_rows"]) >= 5 assert s_data["query_anatomy"]["dense_token_count"] > 0 r_diff = client.post( f"/api/projects/{run_id}/forensic-diff", json={"search": s_payload, "doc_a_id": 7, "doc_b_id": 5}, ) assert r_diff.status_code == 200 assert len(r_diff.json()["verdict_bullets"]) >= 1 r_sal = client.post( f"/api/projects/{run_id}/token-saliency", json={"query": "waterproof trail running shoes", "doc_id": 5}, ) assert r_sal.status_code == 200 assert len(r_sal.json()["spans"]) > 0 r_stress = client.post( f"/api/projects/{run_id}/stress-test", json={"search": s_payload}, ) assert r_stress.status_code == 200 assert len(r_stress.json()["cases"]) == 4 r_export = client.post( f"/api/projects/{run_id}/export", json={"search": s_payload}, ) assert r_export.status_code == 200 assert "RECIPE" in r_export.json()["python_snippet"] # 5. Counterfactual Fix Finder, Query Token Ablation, What-If Doc Sandbox & Suite Auto-Tune r_fix = client.post( f"/api/projects/{run_id}/find-fixes", json={"search": s_payload, "target_doc_id": 5}, ) assert r_fix.status_code == 200 assert r_fix.json()["target_doc_id"] == 5 r_abl = client.post( f"/api/projects/{run_id}/query-ablation", json={"search": s_payload, "doc_id": 5}, ) assert r_abl.status_code == 200 assert len(r_abl.json()["tokens"]) == 4 r_whatif = client.post( f"/api/projects/{run_id}/what-if-doc", json={ "search": s_payload, "doc_id": 15, "edited_text": "waterproof trail running shoes gore-tex", "simulate_chunking": True, }, ) assert r_whatif.status_code == 200 assert r_whatif.json()["doc_id"] == 15 r_tune = client.post( f"/api/projects/{run_id}/suite/auto-tune", json={ "search": s_payload, "questions": [ { "id": "q1", "query": "waterproof trail running shoes", "expected_doc_ids": [5], }, { "id": "q2", "query": "kubernetes pvc pod error", "expected_doc_ids": [10], }, ], }, ) assert r_tune.status_code == 200 assert len(r_tune.json()["alpha_curve"]) == 21 assert len(r_tune.json()["top_recipes"]) >= 1 # 6. Live Atlas Re-clustering endpoint r_recl = client.post( f"/api/projects/{run_id}/recluster", json={"granularity": "pinpoint"}, ) assert r_recl.status_code == 200 assert r_recl.json()["health"]["cluster_count"] >= 1