continue fixing
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import replace
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
@@ -14,6 +16,18 @@ from sentence_api.store import BoundaryStore
|
||||
|
||||
VIDEO_HASH = "a" * 64
|
||||
|
||||
def wait_for_subtitle_translation(client, video_hash, timeout=5.0):
|
||||
deadline = time.time() + timeout
|
||||
last = None
|
||||
while time.time() < deadline:
|
||||
last = client.get(f"/api/v1/videos/{video_hash}/subtitle-translation")
|
||||
if last.status_code == 200 and last.json().get("complete"):
|
||||
return last
|
||||
time.sleep(0.05)
|
||||
raise AssertionError(f"subtitle translation did not complete: {getattr(last, 'text', last)}")
|
||||
|
||||
|
||||
|
||||
|
||||
def make_client(tmp_path, admin_api_key=""):
|
||||
index_path = tmp_path / "boundaries.json"
|
||||
@@ -172,7 +186,7 @@ def test_sentence_explanation_uses_server_side_ai_client(tmp_path, monkeypatch):
|
||||
return "这句话表示打招呼。"
|
||||
|
||||
class StubAITutor:
|
||||
pass
|
||||
available = True
|
||||
|
||||
stub = StubAITutor()
|
||||
stub.explain = explain
|
||||
@@ -244,7 +258,7 @@ def test_subtitle_translation_uses_full_document_and_is_cached(tmp_path, monkeyp
|
||||
return {index: f"中文{index}" for index, _text in sentences}
|
||||
|
||||
class StubAITutor:
|
||||
pass
|
||||
available = True
|
||||
|
||||
stub = StubAITutor()
|
||||
stub.translate_sentences = translate_sentences
|
||||
@@ -252,8 +266,12 @@ def test_subtitle_translation_uses_full_document_and_is_cached(tmp_path, monkeyp
|
||||
|
||||
response = client.post(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert response.status_code == 200, response.text
|
||||
if not response.json().get("complete"):
|
||||
response = wait_for_subtitle_translation(client, VIDEO_HASH)
|
||||
payload = response.json()
|
||||
assert payload["video_hash"] == VIDEO_HASH
|
||||
assert payload["complete"] is True
|
||||
assert payload["status"] == "ready"
|
||||
assert payload["translations"] == [
|
||||
{"index": 0, "zh_text": "中文0"},
|
||||
{"index": 1, "zh_text": "中文1"},
|
||||
@@ -263,7 +281,8 @@ def test_subtitle_translation_uses_full_document_and_is_cached(tmp_path, monkeyp
|
||||
|
||||
second = client.post(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert second.status_code == 200
|
||||
assert second.json() == payload
|
||||
assert second.json()["translations"] == payload["translations"]
|
||||
assert second.json()["complete"] is True
|
||||
assert len(requested) == 1
|
||||
assert requested[0][0] == [(0, "Hello."), (1, "Nice to meet you.")]
|
||||
|
||||
@@ -278,7 +297,7 @@ def test_subtitle_translation_is_shared_with_other_users(tmp_path, monkeypatch):
|
||||
return {index: f"中文{index}" for index, _text in sentences}
|
||||
|
||||
class StubAITutor:
|
||||
pass
|
||||
available = True
|
||||
|
||||
stub = StubAITutor()
|
||||
stub.translate_sentences = translate_sentences
|
||||
@@ -286,10 +305,13 @@ def test_subtitle_translation_is_shared_with_other_users(tmp_path, monkeypatch):
|
||||
|
||||
first = client.post(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert first.status_code == 200, first.text
|
||||
if not first.json().get("complete"):
|
||||
first = wait_for_subtitle_translation(client, VIDEO_HASH)
|
||||
|
||||
stored = client.get(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert stored.status_code == 200
|
||||
assert stored.json() == first.json()
|
||||
assert stored.json()["translations"] == first.json()["translations"]
|
||||
assert stored.json()["complete"] is True
|
||||
|
||||
other = TestClient(client.app)
|
||||
signup = other.post(
|
||||
@@ -311,7 +333,61 @@ def test_subtitle_translation_is_shared_with_other_users(tmp_path, monkeypatch):
|
||||
|
||||
reused = other.post(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert reused.status_code == 200
|
||||
assert reused.json() == first.json()
|
||||
assert reused.json()["translations"] == first.json()["translations"]
|
||||
assert reused.json()["complete"] is True
|
||||
assert len(requested) == 1
|
||||
|
||||
|
||||
|
||||
def test_subtitle_translation_does_not_block_http_on_long_jobs(tmp_path, monkeypatch):
|
||||
client = make_client(tmp_path)
|
||||
_ready_video(client.app.state.video_repository)
|
||||
started = threading.Event()
|
||||
release = threading.Event()
|
||||
requested = []
|
||||
|
||||
def translate_sentences(sentences, **kwargs):
|
||||
requested.append(sentences)
|
||||
persist = kwargs.get("persist")
|
||||
if persist is not None:
|
||||
persist({0: "中文0"})
|
||||
started.set()
|
||||
assert release.wait(timeout=5)
|
||||
if persist is not None:
|
||||
persist({1: "中文1"})
|
||||
return {0: "中文0", 1: "中文1"}
|
||||
|
||||
class StubAITutor:
|
||||
available = True
|
||||
|
||||
stub = StubAITutor()
|
||||
stub.translate_sentences = translate_sentences
|
||||
monkeypatch.setattr(client.app.state, "ai_tutor", stub)
|
||||
client.app.state.subtitle_translations.ai_tutor = stub
|
||||
client.app.state.subtitle_translations.post_wait_seconds = 0.2
|
||||
|
||||
response = client.post(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert started.wait(timeout=2)
|
||||
assert response.status_code == 200, response.text
|
||||
payload = response.json()
|
||||
assert payload["complete"] is False
|
||||
assert payload["status"] == "running"
|
||||
if {"index": 0, "zh_text": "中文0"} not in payload["translations"]:
|
||||
payload = client.get(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation").json()
|
||||
assert {"index": 0, "zh_text": "中文0"} in payload["translations"]
|
||||
|
||||
stored = client.get(f"/api/v1/videos/{VIDEO_HASH}/subtitle-translation")
|
||||
assert stored.status_code == 200
|
||||
assert stored.json()["status"] == "running"
|
||||
assert stored.json()["complete"] is False
|
||||
|
||||
release.set()
|
||||
finished = wait_for_subtitle_translation(client, VIDEO_HASH)
|
||||
assert finished.json()["complete"] is True
|
||||
assert finished.json()["translations"] == [
|
||||
{"index": 0, "zh_text": "中文0"},
|
||||
{"index": 1, "zh_text": "中文1"},
|
||||
]
|
||||
assert len(requested) == 1
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user