135 lines
4.2 KiB
Python
135 lines
4.2 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from backend.subtitle_ai import process_subtitles_with_ai
|
|
|
|
|
|
class FakeClient:
|
|
def chat_json(self, system_prompt, user_payload):
|
|
assert "不要修改时间轴" in system_prompt
|
|
assert "start" not in user_payload["cues"][0]
|
|
assert "end" not in user_payload["cues"][0]
|
|
return {
|
|
"translations": [
|
|
{
|
|
"index": 1,
|
|
"start": "00:00:99.000",
|
|
"end": "00:00:99.999",
|
|
"zh": "我第一次做投资人是在 24 岁。",
|
|
}
|
|
]
|
|
}
|
|
|
|
|
|
def test_process_subtitles_with_ai_returns_clean_en_and_zh_vtt():
|
|
raw = """WEBVTT
|
|
|
|
00:00:00.872 --> 00:00:04.959
|
|
My first job as an investor was when I was 24 years old,
|
|
|
|
00:00:00.872 --> 00:00:04.959
|
|
My first job as an investor was when I was 24 years old,
|
|
"""
|
|
|
|
result = process_subtitles_with_ai(raw, FakeClient())
|
|
|
|
assert "My first job as an investor" in result["subtitle_en_content"]
|
|
assert "我第一次做投资人是在 24 岁。" in result["subtitle_zh_content"]
|
|
assert result["report"]["duplicate_count"] == 1
|
|
assert result["report"]["translation_count"] == 1
|
|
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_en_content"]
|
|
assert "00:00:00.872 --> 00:00:04.959" in result["subtitle_zh_content"]
|
|
assert "timestamp_calibrated" not in result["report"]
|
|
assert "timestamp_offset_seconds" not in result["report"]
|
|
|
|
|
|
class SingleBatchClient:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def chat_json(self, system_prompt, user_payload):
|
|
self.calls.append(user_payload)
|
|
return {
|
|
"translations": [
|
|
{"index": 3, "zh": "第三句"},
|
|
{"index": 1, "zh": "第一句"},
|
|
{"index": 2, "zh": "第二句"},
|
|
]
|
|
}
|
|
|
|
|
|
def test_process_subtitles_with_ai_translates_all_cues_by_default_and_matches_index():
|
|
raw = """WEBVTT
|
|
|
|
00:00:00.000 --> 00:00:01.000
|
|
One.
|
|
|
|
00:00:01.000 --> 00:00:02.000
|
|
Two.
|
|
|
|
00:00:02.000 --> 00:00:03.000
|
|
Three.
|
|
"""
|
|
|
|
client = SingleBatchClient()
|
|
result = process_subtitles_with_ai(raw, client)
|
|
|
|
assert len(client.calls) == 1
|
|
assert [cue["index"] for cue in client.calls[0]["cues"]] == [1, 2, 3]
|
|
assert "第一句" in result["subtitle_zh_content"]
|
|
assert "第二句" in result["subtitle_zh_content"]
|
|
assert "第三句" in result["subtitle_zh_content"]
|
|
assert result["subtitle_zh_content"].index("第一句") < result["subtitle_zh_content"].index("第二句")
|
|
assert result["subtitle_zh_content"].index("第二句") < result["subtitle_zh_content"].index("第三句")
|
|
assert result["report"]["translation_batches"] == 1
|
|
|
|
|
|
class MissingThenRepairClient:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def chat_json(self, system_prompt, user_payload):
|
|
self.calls.append(user_payload)
|
|
indexes = [cue["index"] for cue in user_payload["cues"]]
|
|
if indexes == [1, 2, 3]:
|
|
return {
|
|
"translations": [
|
|
{"index": 1, "zh": "第一句"},
|
|
{"index": 3, "zh": "第三句"},
|
|
]
|
|
}
|
|
if indexes == [2]:
|
|
return {"translations": [{"index": 2, "zh": "第二句"}]}
|
|
raise AssertionError(f"unexpected retry indexes: {indexes}")
|
|
|
|
|
|
def test_process_subtitles_with_ai_repairs_missing_translation_indexes():
|
|
raw = """WEBVTT
|
|
|
|
00:00:00.000 --> 00:00:01.000
|
|
One.
|
|
|
|
00:00:01.000 --> 00:00:02.000
|
|
Two.
|
|
|
|
00:00:02.000 --> 00:00:03.000
|
|
Three.
|
|
"""
|
|
|
|
client = MissingThenRepairClient()
|
|
result = process_subtitles_with_ai(raw, client)
|
|
|
|
assert [[cue["index"] for cue in call["cues"]] for call in client.calls] == [[1, 2, 3], [2]]
|
|
assert "第一句" in result["subtitle_zh_content"]
|
|
assert "第二句" in result["subtitle_zh_content"]
|
|
assert "第三句" in result["subtitle_zh_content"]
|
|
assert result["report"]["translation_count"] == 3
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_process_subtitles_with_ai_returns_clean_en_and_zh_vtt()
|
|
test_process_subtitles_with_ai_translates_all_cues_by_default_and_matches_index()
|
|
test_process_subtitles_with_ai_repairs_missing_translation_indexes()
|