This commit is contained in:
2026-10-25 10:30:22 +08:00
parent 38894ba80c
commit ae84347912
11 changed files with 287 additions and 12 deletions

View File

@@ -212,3 +212,39 @@ def test_parse_translation_payload_accepts_fenced_json_and_numbered_lines():
12: "甲",
13: "乙",
}
def test_ai_tutor_retranslates_english_existing_cache(monkeypatch):
def fake_post(endpoint, headers, json, timeout):
return _translation_response_from_prompt(json)
monkeypatch.setattr("sentence_api.ai_tutor.requests.post", fake_post)
tutor = AITutor("https://ai.test/v1", "secret", "test-model", 1)
translated = tutor.translate_sentences(
[(0, "Hello."), (1, "Thanks.")],
existing={0: "Hello.", 1: "谢谢。"},
)
assert translated[0] == "中文0"
assert translated[1] == "谢谢。"
def test_ai_tutor_retries_when_translation_stays_english(monkeypatch):
calls = {"count": 0}
def fake_post(endpoint, headers, json, timeout):
calls["count"] += 1
if calls["count"] == 1:
return FakeResponse(
body={"choices": [{"message": {"content": '{"0":"Hello."}'}}]}
)
return FakeResponse(
body={"choices": [{"message": {"content": '{"0":"你好。"}'}}]}
)
monkeypatch.setattr("sentence_api.ai_tutor.requests.post", fake_post)
tutor = AITutor("https://ai.test/v1", "secret", "test-model", 1)
translated = tutor.translate_sentences([(0, "Hello.")])
assert translated == {0: "你好。"}
assert calls["count"] >= 2