File size: 7,333 Bytes
70c6e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d66d64c
 
 
 
 
 
 
 
 
 
 
 
2b2e5e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c6e17
 
 
 
 
 
d66d64c
70c6e17
d66d64c
70c6e17
 
 
 
 
 
2b2e5e8
 
 
 
 
 
70c6e17
 
d66d64c
2b2e5e8
 
 
 
 
 
 
 
70c6e17
 
2b2e5e8
 
 
 
 
 
70c6e17
 
 
d66d64c
 
70c6e17
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import annotations

import os
from unittest.mock import AsyncMock, patch

from fastapi.testclient import TestClient
import pytest
from services.inference_client import InferenceAuthError, InferenceConnectionError

import main as main_module
from main import app


client = TestClient(app, headers={"Authorization": "Bearer mock_token_fun_modules"})

MODULE_PAYLOAD = {
    "topic": "Rational Functions",
    "subject": "General Mathematics",
    "quarter": 1,
    "bloomLevel": "understand",
}

MODULE_CHUNKS = [{"content": "Rational functions", "source_file": "module.pdf"}]
MODULE_RESPONSE = '{"title":"Rational Functions","sections":[]}'


@pytest.fixture(autouse=True)
def _backend_mocks():
    with patch.object(
        main_module.firebase_auth,
        "verify_id_token",
        return_value={"uid": "student-1", "role": "student"},
    ), patch(
        "routes.fun_modules_routes.retrieve_lesson_pdf_context",
        return_value=(MODULE_CHUNKS, "exact"),
    ), patch(
        "routes.fun_modules_routes._generate_text",
        new=AsyncMock(return_value=MODULE_RESPONSE),
    ):
        yield


def test_generate_module_returns_contract():
    response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 200
    assert set(response.json()) == {"moduleId", "title", "cards", "sources"}
    assert len(response.json()["cards"]) == 7
    assert response.json()["sources"] == ["module.pdf"]


def test_generate_module_maps_missing_context():
    with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=([], "none")):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 404
    assert response.json()["detail"]["error"] == "no_curriculum_context"


def test_generate_module_maps_retrieval_failure():
    with patch(
        "routes.fun_modules_routes.retrieve_lesson_pdf_context",
        side_effect=RuntimeError("retrieval secret should not leak"),
    ):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 503
    assert response.json()["detail"]["error"] == "retrieval_failed"


def test_generate_module_maps_inference_failure():
    with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=(MODULE_CHUNKS, "exact")), patch(
        "routes.fun_modules_routes._generate_text",
        new=AsyncMock(side_effect=RuntimeError("inference secret should not leak")),
    ):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 502
    assert response.json()["detail"]["error"] == "inference_failed"


@pytest.mark.parametrize(
    ("inference_error", "error_code"),
    [
        (InferenceAuthError("auth"), "inference_auth_failed"),
        (InferenceConnectionError("connection"), "inference_connection_failed"),
    ],
)
def test_generate_module_maps_typed_inference_failures(inference_error, error_code):
    with patch(
        "routes.fun_modules_routes._generate_text",
        new=AsyncMock(side_effect=inference_error),
    ):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 502
    assert response.json()["detail"]["error"] == error_code
    assert set(response.json()["detail"]) == {"error", "message", "type"}


def test_generate_module_maps_parse_failure():
    with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=(MODULE_CHUNKS, "exact")), patch(
        "routes.fun_modules_routes._generate_text",
        new=AsyncMock(return_value="not-json"),
    ):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 500
    assert response.json()["detail"]["error"] == "parse_failed"


def test_mastery_record_returns_contract():
    response = client.post(
        "/api/mastery/record",
        json={"userId": "student-1", "competencyCode": "RF-1", "correct": True, "score": 0.9},
    )

    assert response.status_code == 200
    assert set(response.json()) == {"masteryProbability", "unlockedModules", "xpAwarded"}


def test_mastery_record_handles_incorrect_answer():
    response = client.post(
        "/api/mastery/record",
        json={"userId": "student-1", "competencyCode": "RF-1", "correct": False, "score": 0.2},
    )

    assert response.status_code == 200
    assert response.json()["xpAwarded"] == 0


@pytest.fixture
def as_teacher():
    """/api/jev/verify spends a Claude call per request, so it is open to teachers and admins only."""
    with patch.object(
        main_module.firebase_auth,
        "verify_id_token",
        return_value={"uid": "teacher-1", "role": "teacher"},
    ):
        yield


def test_jev_verify_returns_canonical_contract(as_teacher):
    with patch(
        "routes.jev_routes.verify_lesson_factuality",
        new=AsyncMock(
            return_value={
                "verified": True,
                "pCorrect": 0.95,
                "pLeak": 0.05,
                "action": "verified",
            }
        ),
    ):
        response = client.post(
            "/api/jev/verify",
            json={
                "referenceText": "A linear function has a constant rate of change.",
                "generatedText": "A linear function has a constant rate of change.",
                "claimType": "definition",
            },
        )

    assert response.status_code == 200
    assert set(response.json()) == {"verified", "pCorrect", "pLeak", "action"}
    assert response.json()["verified"] is True


def test_jev_verify_fails_open_without_secret(as_teacher):
    with patch.dict(os.environ, {}, clear=False):
        os.environ.pop("ANTHROPIC_API_KEY", None)
        response = client.post(
            "/api/jev/verify",
            json={"referenceText": "x", "generatedText": "y", "claimType": "claim"},
        )

    assert response.status_code == 200
    assert response.json() == {
        "verified": True,
        "pCorrect": 1.0,
        "pLeak": 0.0,
        "action": "fallback_disabled",
    }


def test_jev_verify_is_factuality_only_for_claim_types(as_teacher):
    with patch(
        "routes.jev_routes.verify_lesson_factuality",
        new=AsyncMock(return_value={"action": "fallback_disabled", "verified": True}),
    ):
        response = client.post(
            "/api/jev/verify",
            json={"referenceText": "linear function", "generatedText": "quadratic function", "claimType": "hint"},
        )

    assert response.status_code == 200
    assert response.json() == {
        "verified": True,
        "pCorrect": 1.0,
        "pLeak": 0.0,
        "action": "fallback_disabled",
    }


def test_errors_use_identical_keys_and_never_return_secret():
    secret = "anthropic-test-secret"
    with patch.dict(os.environ, {"ANTHROPIC_API_KEY": secret}), patch(
        "routes.fun_modules_routes.retrieve_lesson_pdf_context",
        side_effect=RuntimeError(secret),
    ):
        response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)

    assert response.status_code == 503
    assert set(response.json()["detail"]) == {"error", "message", "type"}
    assert secret not in response.text