File size: 4,908 Bytes
46d70d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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