add explain module
This commit is contained in:
@@ -80,6 +80,70 @@ def test_invalid_hash_is_rejected(tmp_path):
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_sentence_explanation_requires_login(tmp_path):
|
||||
index_path = tmp_path / "boundaries.json"
|
||||
index_path.write_text("{}", encoding="utf-8")
|
||||
settings = replace(
|
||||
Settings.from_env(),
|
||||
data_dir=tmp_path / "data",
|
||||
legacy_boundaries_file=index_path,
|
||||
)
|
||||
settings.ensure_directories()
|
||||
client = TestClient(
|
||||
create_app(
|
||||
BoundaryStore(index_path),
|
||||
settings=settings,
|
||||
repository=VideoRepository(settings.database_path),
|
||||
)
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
"/api/v1/sentences/explain",
|
||||
json={"sentence_text": "Hello."},
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_sentence_explanation_uses_server_side_ai_client(tmp_path, monkeypatch):
|
||||
client = make_client(tmp_path)
|
||||
requested = []
|
||||
|
||||
def explain(text, **kwargs):
|
||||
requested.append((text, kwargs))
|
||||
return "这句话表示打招呼。"
|
||||
|
||||
class StubAITutor:
|
||||
pass
|
||||
|
||||
stub = StubAITutor()
|
||||
stub.explain = explain
|
||||
monkeypatch.setattr(client.app.state, "ai_tutor", stub)
|
||||
|
||||
response = client.post(
|
||||
"/api/v1/sentences/explain",
|
||||
json={
|
||||
"sentence_text": "Hello.",
|
||||
"course_title": "Greetings",
|
||||
"sentence_index": 0,
|
||||
"language": "en",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"explanation": "这句话表示打招呼。"}
|
||||
assert requested == [
|
||||
(
|
||||
"Hello.",
|
||||
{
|
||||
"course_title": "Greetings",
|
||||
"sentence_index": 0,
|
||||
"language": "en",
|
||||
},
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def test_oss_video_url_is_used_for_catalog_and_content(tmp_path):
|
||||
index_path = tmp_path / "boundaries.json"
|
||||
index_path.write_text("{}", encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user