File size: 2,377 Bytes
44215ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for the data-collection & feedback layer (Session 4)."""

from fastapi.testclient import TestClient

from app.main import app

client = TestClient(app)

TEXT = (
    "The mitochondria is the powerhouse of the cell. "
    "The Eiffel Tower is located in Paris, France."
)


def _ask() -> dict:
    doc_id = client.post("/documents", data={"text": TEXT}).json()["document_id"]
    response = client.post(
        "/ask", json={"document_id": doc_id, "question": "Where is the Eiffel Tower?"}
    )
    assert response.status_code == 200
    return response.json()


def test_ask_persists_interaction_with_latency():
    result = _ask()
    assert "interaction_id" in result
    assert isinstance(result["latency_ms"], int)
    assert result["latency_ms"] >= 0

    stats = client.get("/stats").json()
    assert stats["total_questions"] == 1
    assert stats["median_latency_ms"] is not None


def test_feedback_updates_stats():
    result = _ask()
    response = client.post(
        "/feedback",
        json={"interaction_id": result["interaction_id"], "feedback": "up"},
    )
    assert response.status_code == 200

    stats = client.get("/stats").json()
    assert stats["thumbs_up"] == 1
    assert stats["thumbs_down"] == 0
    assert stats["thumbs_up_rate"] == 1.0


def test_thumbs_up_rate_reflects_mix():
    up = _ask()
    down = _ask()
    client.post("/feedback", json={"interaction_id": up["interaction_id"], "feedback": "up"})
    client.post("/feedback", json={"interaction_id": down["interaction_id"], "feedback": "down"})

    stats = client.get("/stats").json()
    assert stats["total_questions"] == 2
    assert stats["thumbs_up_rate"] == 0.5


def test_invalid_feedback_value_is_rejected():
    result = _ask()
    response = client.post(
        "/feedback",
        json={"interaction_id": result["interaction_id"], "feedback": "meh"},
    )
    assert response.status_code == 422


def test_feedback_unknown_interaction_returns_404():
    response = client.post(
        "/feedback", json={"interaction_id": "does-not-exist", "feedback": "up"}
    )
    assert response.status_code == 404


def test_stats_empty_by_default():
    stats = client.get("/stats").json()
    assert stats["total_questions"] == 0
    assert stats["median_latency_ms"] is None
    assert stats["thumbs_up_rate"] is None
    assert stats["questions_over_time"] == []