Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ·
70c6e17
1
Parent(s): 846f533
deploy: backend from 60e5929
Browse files- main.py +2 -0
- routes/fun_modules_routes.py +161 -0
- tests/test_fun_modules_routes.py +180 -0
main.py
CHANGED
|
@@ -130,6 +130,7 @@ from routes.intervention_routes import router as intervention_router
|
|
| 130 |
from routes.pipeline_routes import router as pipeline_router
|
| 131 |
from routes.deepseek_rag_routes import router as deepseek_rag_router
|
| 132 |
from routes.at_risk_resolution import router as at_risk_resolution_router
|
|
|
|
| 133 |
from routes.jev_routes import router as jev_router
|
| 134 |
from services.jev_client import route_student_intent
|
| 135 |
|
|
@@ -1219,6 +1220,7 @@ app.include_router(intervention_router)
|
|
| 1219 |
app.include_router(pipeline_router)
|
| 1220 |
app.include_router(deepseek_rag_router)
|
| 1221 |
app.include_router(at_risk_resolution_router)
|
|
|
|
| 1222 |
app.include_router(jev_router)
|
| 1223 |
|
| 1224 |
|
|
|
|
| 130 |
from routes.pipeline_routes import router as pipeline_router
|
| 131 |
from routes.deepseek_rag_routes import router as deepseek_rag_router
|
| 132 |
from routes.at_risk_resolution import router as at_risk_resolution_router
|
| 133 |
+
from routes.fun_modules_routes import router as fun_modules_router
|
| 134 |
from routes.jev_routes import router as jev_router
|
| 135 |
from services.jev_client import route_student_intent
|
| 136 |
|
|
|
|
| 1220 |
app.include_router(pipeline_router)
|
| 1221 |
app.include_router(deepseek_rag_router)
|
| 1222 |
app.include_router(at_risk_resolution_router)
|
| 1223 |
+
app.include_router(fun_modules_router)
|
| 1224 |
app.include_router(jev_router)
|
| 1225 |
|
| 1226 |
|
routes/fun_modules_routes.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
from typing import Any
|
| 6 |
+
|
| 7 |
+
from fastapi import APIRouter, HTTPException
|
| 8 |
+
from pydantic import BaseModel, Field
|
| 9 |
+
|
| 10 |
+
from rag.curriculum_rag import retrieve_lesson_pdf_context
|
| 11 |
+
from routes.rag_routes import (
|
| 12 |
+
_ensure_7_sections,
|
| 13 |
+
_generate_text,
|
| 14 |
+
_strip_thinking_and_parse,
|
| 15 |
+
)
|
| 16 |
+
from services.inference_client import InferenceAuthError, InferenceConnectionError
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
router = APIRouter(prefix="/api", tags=["fun-modules"])
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class GenerateModuleRequest(BaseModel):
|
| 23 |
+
topic: str = Field(min_length=1)
|
| 24 |
+
subject: str = Field(min_length=1)
|
| 25 |
+
quarter: int = Field(ge=1, le=4)
|
| 26 |
+
bloomLevel: str = Field(min_length=1)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class MasteryRecordRequest(BaseModel):
|
| 30 |
+
userId: str = Field(min_length=1)
|
| 31 |
+
competencyCode: str = Field(min_length=1)
|
| 32 |
+
correct: bool
|
| 33 |
+
score: float = Field(ge=0, le=1)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class JevVerifyRequest(BaseModel):
|
| 37 |
+
referenceText: str = Field(min_length=1)
|
| 38 |
+
generatedText: str = Field(min_length=1)
|
| 39 |
+
claimType: str = Field(min_length=1)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class GenerateModuleResponse(BaseModel):
|
| 43 |
+
moduleId: str
|
| 44 |
+
title: str
|
| 45 |
+
cards: list[dict[str, Any]]
|
| 46 |
+
sources: list[str]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class MasteryRecordResponse(BaseModel):
|
| 50 |
+
masteryProbability: float
|
| 51 |
+
unlockedModules: list[str]
|
| 52 |
+
xpAwarded: int
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class JevVerifyResponse(BaseModel):
|
| 56 |
+
verified: bool
|
| 57 |
+
pCorrect: float
|
| 58 |
+
pLeak: float
|
| 59 |
+
action: str
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def _api_error(status_code: int, error: str, error_type: str) -> HTTPException:
|
| 63 |
+
return HTTPException(
|
| 64 |
+
status_code=status_code,
|
| 65 |
+
detail={
|
| 66 |
+
"error": error,
|
| 67 |
+
"message": error.replace("_", " ").capitalize(),
|
| 68 |
+
"type": error_type,
|
| 69 |
+
},
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _module_id(payload: GenerateModuleRequest) -> str:
|
| 74 |
+
slug = re.sub(r"[^a-z0-9]+", "-", payload.topic.lower()).strip("-")
|
| 75 |
+
return f"{payload.subject.lower().replace(' ', '-')}-q{payload.quarter}-{slug}"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _source_names(chunks: list[dict[str, Any]]) -> list[str]:
|
| 79 |
+
names: list[str] = []
|
| 80 |
+
for chunk in chunks:
|
| 81 |
+
source_name = chunk.get("source_file") or chunk.get("storage_path")
|
| 82 |
+
if isinstance(source_name, str) and source_name not in names:
|
| 83 |
+
names.append(source_name)
|
| 84 |
+
return names
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@router.post("/curriculum/generate-module", response_model=GenerateModuleResponse)
|
| 88 |
+
async def generate_module(payload: GenerateModuleRequest) -> dict[str, Any]:
|
| 89 |
+
try:
|
| 90 |
+
chunks, _retrieval_mode = retrieve_lesson_pdf_context(
|
| 91 |
+
topic=payload.topic,
|
| 92 |
+
subject=payload.subject,
|
| 93 |
+
quarter=payload.quarter,
|
| 94 |
+
lesson_title=payload.topic,
|
| 95 |
+
top_k=8,
|
| 96 |
+
)
|
| 97 |
+
except Exception:
|
| 98 |
+
raise _api_error(503, "retrieval_failed", "RetrievalError")
|
| 99 |
+
|
| 100 |
+
if not chunks:
|
| 101 |
+
raise _api_error(404, "no_curriculum_context", "CurriculumContextError")
|
| 102 |
+
|
| 103 |
+
prompt = (
|
| 104 |
+
f"Create concise Merrill learning cards for {payload.topic} in {payload.subject}, "
|
| 105 |
+
f"quarter {payload.quarter}, Bloom level {payload.bloomLevel}. Return JSON with title and sections."
|
| 106 |
+
)
|
| 107 |
+
try:
|
| 108 |
+
raw_text = await _generate_text(prompt, task_type="rag_lesson", max_new_tokens=4096, enable_thinking=True)
|
| 109 |
+
except InferenceAuthError:
|
| 110 |
+
raise _api_error(502, "inference_auth_failed", "InferenceAuthError")
|
| 111 |
+
except InferenceConnectionError:
|
| 112 |
+
raise _api_error(502, "inference_connection_failed", "InferenceConnectionError")
|
| 113 |
+
except Exception:
|
| 114 |
+
raise _api_error(502, "inference_failed", "InferenceError")
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
parsed = _strip_thinking_and_parse(raw_text)
|
| 118 |
+
if not isinstance(parsed, dict):
|
| 119 |
+
raise ValueError("module response must be an object")
|
| 120 |
+
cards = parsed.get("sections", parsed.get("cards"))
|
| 121 |
+
if not isinstance(cards, list):
|
| 122 |
+
raise ValueError("module response must contain cards")
|
| 123 |
+
normalized = _ensure_7_sections(
|
| 124 |
+
{**parsed, "sections": cards},
|
| 125 |
+
str(parsed.get("title") or payload.topic),
|
| 126 |
+
chunks=chunks,
|
| 127 |
+
)
|
| 128 |
+
except Exception:
|
| 129 |
+
raise _api_error(500, "parse_failed", "ParseError")
|
| 130 |
+
|
| 131 |
+
return {
|
| 132 |
+
"moduleId": _module_id(payload),
|
| 133 |
+
"title": str(normalized.get("title") or payload.topic),
|
| 134 |
+
"cards": normalized["sections"],
|
| 135 |
+
"sources": _source_names(chunks),
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@router.post("/mastery/record", response_model=MasteryRecordResponse)
|
| 140 |
+
async def record_mastery(payload: MasteryRecordRequest) -> dict[str, Any]:
|
| 141 |
+
probability = round((payload.score + (1.0 if payload.correct else 0.0)) / 2.0, 3)
|
| 142 |
+
return {
|
| 143 |
+
"masteryProbability": probability,
|
| 144 |
+
"unlockedModules": [],
|
| 145 |
+
"xpAwarded": 30 if payload.correct else 0,
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
@router.post("/jev/verify", response_model=JevVerifyResponse)
|
| 150 |
+
async def verify_with_jev(payload: JevVerifyRequest) -> dict[str, Any]:
|
| 151 |
+
os.getenv("TYPESAFE_API_KEY")
|
| 152 |
+
reference = " ".join(payload.referenceText.lower().split())
|
| 153 |
+
generated = " ".join(payload.generatedText.lower().split())
|
| 154 |
+
verified = reference == generated or reference in generated
|
| 155 |
+
leak = "answer" in generated and payload.claimType.lower() in {"hint", "practice"}
|
| 156 |
+
return {
|
| 157 |
+
"verified": verified,
|
| 158 |
+
"pCorrect": 0.95 if verified else 0.35,
|
| 159 |
+
"pLeak": 0.9 if leak else 0.05,
|
| 160 |
+
"action": "allow_with_fallback",
|
| 161 |
+
}
|
tests/test_fun_modules_routes.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from unittest.mock import AsyncMock, patch
|
| 5 |
+
|
| 6 |
+
from fastapi.testclient import TestClient
|
| 7 |
+
import pytest
|
| 8 |
+
from services.inference_client import InferenceAuthError, InferenceConnectionError
|
| 9 |
+
|
| 10 |
+
import main as main_module
|
| 11 |
+
from main import app
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
client = TestClient(app, headers={"Authorization": "Bearer mock_token_fun_modules"})
|
| 15 |
+
|
| 16 |
+
MODULE_PAYLOAD = {
|
| 17 |
+
"topic": "Rational Functions",
|
| 18 |
+
"subject": "General Mathematics",
|
| 19 |
+
"quarter": 1,
|
| 20 |
+
"bloomLevel": "understand",
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
MODULE_CHUNKS = [{"content": "Rational functions", "source_file": "module.pdf"}]
|
| 24 |
+
MODULE_RESPONSE = '{"title":"Rational Functions","sections":[]}'
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@pytest.fixture(autouse=True)
|
| 28 |
+
def _backend_mocks():
|
| 29 |
+
with patch.object(
|
| 30 |
+
main_module.firebase_auth,
|
| 31 |
+
"verify_id_token",
|
| 32 |
+
return_value={"uid": "student-1", "role": "student"},
|
| 33 |
+
), patch(
|
| 34 |
+
"routes.fun_modules_routes.retrieve_lesson_pdf_context",
|
| 35 |
+
return_value=(MODULE_CHUNKS, "exact"),
|
| 36 |
+
), patch(
|
| 37 |
+
"routes.fun_modules_routes._generate_text",
|
| 38 |
+
new=AsyncMock(return_value=MODULE_RESPONSE),
|
| 39 |
+
):
|
| 40 |
+
yield
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_generate_module_returns_contract():
|
| 44 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 45 |
+
|
| 46 |
+
assert response.status_code == 200
|
| 47 |
+
assert set(response.json()) == {"moduleId", "title", "cards", "sources"}
|
| 48 |
+
assert len(response.json()["cards"]) == 7
|
| 49 |
+
assert response.json()["sources"] == ["module.pdf"]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_generate_module_maps_missing_context():
|
| 53 |
+
with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=([], "none")):
|
| 54 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 55 |
+
|
| 56 |
+
assert response.status_code == 404
|
| 57 |
+
assert response.json()["detail"]["error"] == "no_curriculum_context"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_generate_module_maps_retrieval_failure():
|
| 61 |
+
with patch(
|
| 62 |
+
"routes.fun_modules_routes.retrieve_lesson_pdf_context",
|
| 63 |
+
side_effect=RuntimeError("retrieval secret should not leak"),
|
| 64 |
+
):
|
| 65 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 66 |
+
|
| 67 |
+
assert response.status_code == 503
|
| 68 |
+
assert response.json()["detail"]["error"] == "retrieval_failed"
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def test_generate_module_maps_inference_failure():
|
| 72 |
+
with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=(MODULE_CHUNKS, "exact")), patch(
|
| 73 |
+
"routes.fun_modules_routes._generate_text",
|
| 74 |
+
new=AsyncMock(side_effect=RuntimeError("inference secret should not leak")),
|
| 75 |
+
):
|
| 76 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 77 |
+
|
| 78 |
+
assert response.status_code == 502
|
| 79 |
+
assert response.json()["detail"]["error"] == "inference_failed"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@pytest.mark.parametrize(
|
| 83 |
+
("inference_error", "error_code"),
|
| 84 |
+
[
|
| 85 |
+
(InferenceAuthError("auth"), "inference_auth_failed"),
|
| 86 |
+
(InferenceConnectionError("connection"), "inference_connection_failed"),
|
| 87 |
+
],
|
| 88 |
+
)
|
| 89 |
+
def test_generate_module_maps_typed_inference_failures(inference_error, error_code):
|
| 90 |
+
with patch(
|
| 91 |
+
"routes.fun_modules_routes._generate_text",
|
| 92 |
+
new=AsyncMock(side_effect=inference_error),
|
| 93 |
+
):
|
| 94 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 95 |
+
|
| 96 |
+
assert response.status_code == 502
|
| 97 |
+
assert response.json()["detail"]["error"] == error_code
|
| 98 |
+
assert set(response.json()["detail"]) == {"error", "message", "type"}
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_generate_module_maps_parse_failure():
|
| 102 |
+
with patch("routes.fun_modules_routes.retrieve_lesson_pdf_context", return_value=(MODULE_CHUNKS, "exact")), patch(
|
| 103 |
+
"routes.fun_modules_routes._generate_text",
|
| 104 |
+
new=AsyncMock(return_value="not-json"),
|
| 105 |
+
):
|
| 106 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 107 |
+
|
| 108 |
+
assert response.status_code == 500
|
| 109 |
+
assert response.json()["detail"]["error"] == "parse_failed"
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def test_mastery_record_returns_contract():
|
| 113 |
+
response = client.post(
|
| 114 |
+
"/api/mastery/record",
|
| 115 |
+
json={"userId": "student-1", "competencyCode": "RF-1", "correct": True, "score": 0.9},
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
assert response.status_code == 200
|
| 119 |
+
assert set(response.json()) == {"masteryProbability", "unlockedModules", "xpAwarded"}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_mastery_record_handles_incorrect_answer():
|
| 123 |
+
response = client.post(
|
| 124 |
+
"/api/mastery/record",
|
| 125 |
+
json={"userId": "student-1", "competencyCode": "RF-1", "correct": False, "score": 0.2},
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
assert response.status_code == 200
|
| 129 |
+
assert response.json()["xpAwarded"] == 0
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def test_jev_verify_returns_contract():
|
| 133 |
+
response = client.post(
|
| 134 |
+
"/api/jev/verify",
|
| 135 |
+
json={
|
| 136 |
+
"referenceText": "A linear function has a constant rate of change.",
|
| 137 |
+
"generatedText": "A linear function has a constant rate of change.",
|
| 138 |
+
"claimType": "definition",
|
| 139 |
+
},
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
assert response.status_code == 200
|
| 143 |
+
assert set(response.json()) == {"verified", "pCorrect", "pLeak", "action"}
|
| 144 |
+
assert response.json()["verified"] is True
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def test_jev_verify_fails_open_without_secret():
|
| 148 |
+
with patch.dict(os.environ, {}, clear=False):
|
| 149 |
+
os.environ.pop("TYPESAFE_API_KEY", None)
|
| 150 |
+
response = client.post(
|
| 151 |
+
"/api/jev/verify",
|
| 152 |
+
json={"referenceText": "x", "generatedText": "y", "claimType": "claim"},
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
assert response.status_code == 200
|
| 156 |
+
assert response.json()["action"] == "allow_with_fallback"
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_jev_verify_marks_mismatched_claim_for_fallback():
|
| 160 |
+
response = client.post(
|
| 161 |
+
"/api/jev/verify",
|
| 162 |
+
json={"referenceText": "linear function", "generatedText": "quadratic function", "claimType": "definition"},
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
assert response.status_code == 200
|
| 166 |
+
assert response.json()["verified"] is False
|
| 167 |
+
assert response.json()["pCorrect"] < 0.5
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def test_errors_use_identical_keys_and_never_return_secret():
|
| 171 |
+
secret = "typesafe-test-secret"
|
| 172 |
+
with patch.dict(os.environ, {"TYPESAFE_API_KEY": secret}), patch(
|
| 173 |
+
"routes.fun_modules_routes.retrieve_lesson_pdf_context",
|
| 174 |
+
side_effect=RuntimeError(secret),
|
| 175 |
+
):
|
| 176 |
+
response = client.post("/api/curriculum/generate-module", json=MODULE_PAYLOAD)
|
| 177 |
+
|
| 178 |
+
assert response.status_code == 503
|
| 179 |
+
assert set(response.json()["detail"]) == {"error", "message", "type"}
|
| 180 |
+
assert secret not in response.text
|