continue fixing

This commit is contained in:
2026-08-26 11:22:55 +08:00
parent a6297e2f9f
commit b1d3a3bd5a
5 changed files with 169 additions and 31 deletions

View File

@@ -1,5 +1,8 @@
import io
import json
from pathlib import Path
import subprocess
from unittest import mock
import wave
from dataclasses import replace
@@ -20,6 +23,9 @@ VIDEO_HASH = "c" * 64
class FakeTranscriber:
available = True
def __init__(self, segments=None):
self.segments = segments or []
def transcribe(self, audio_path, language=None):
return Transcript(text="The meeting starts at nine.", segments=[])
@@ -37,7 +43,7 @@ def make_wav() -> bytes:
return output.getvalue()
def make_client(tmp_path, client_api_key=""):
def make_client(tmp_path, client_api_key="", transcriber=None):
legacy_path = tmp_path / "legacy.json"
legacy_path.write_text(json.dumps({"videos": {}}), encoding="utf-8")
settings = replace(
@@ -82,7 +88,7 @@ def make_client(tmp_path, client_api_key=""):
BoundaryStore(legacy_path),
settings=settings,
repository=repository,
transcriber=FakeTranscriber(),
transcriber=transcriber or FakeTranscriber(),
)
return TestClient(app)
@@ -148,6 +154,54 @@ def test_get_dub_share_includes_video_hash(tmp_path):
assert response.json()["video_hash"] == VIDEO_HASH
def test_create_dub_share_aligns_audio_with_whisper_boundaries(tmp_path):
from sentence_api.transcription import Transcript, TranscriptionSegment
from types import SimpleNamespace
aligned = tmp_path / "aligned.wav"
aligned.write_bytes(b"aligned")
calls = []
class AlignedFakeTranscriber(FakeTranscriber):
available = True
def transcribe(self, audio_path, language=None):
calls.append(audio_path)
return Transcript(
text="The meeting starts at nine.",
segments=[
TranscriptionSegment(
start_seconds=0.4,
end_seconds=1.6,
text="The meeting starts at nine.",
)
],
)
client = make_client(tmp_path, transcriber=AlignedFakeTranscriber())
original_run = subprocess.run
def fake_run(command, **kwargs):
Path(command[-1]).write_bytes(b"aligned-audio")
return SimpleNamespace(returncode=0)
with mock.patch("sentence_api.main.subprocess.run", side_effect=fake_run), mock.patch(
"sentence_api.main._media_duration_seconds",
return_value=2.0,
):
response = client.post(
"/api/v1/dub-shares",
data={
"video_hash": VIDEO_HASH,
"segments": json.dumps([{"sentence_index": 0}]),
},
files={"files": ("dub.wav", make_wav(), "audio/wav")},
)
assert response.status_code == 201, response.text
assert calls[0].read_bytes() == b"aligned-audio"
def test_assessment_client_key_is_enforced_when_configured(tmp_path):
client = make_client(tmp_path, client_api_key="tablet-key")
endpoint = f"/api/v1/videos/{VIDEO_HASH}/sentences/0/assessments"