Files
speaking-practice/tests/subtitle_alignment_test.py
T

40 lines
1.1 KiB
Python

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from backend.subtitle_alignment import align_first_sentence_with_asr
from backend.subtitle_tools import SubtitleCue
class FakeAsrClient:
def transcribe(self, audio_path):
return {
"text": "hello world",
"segments": [
{"start": 1.5, "end": 3.0, "text": "hello world"}
],
}
def test_align_first_sentence_with_asr_uses_first_segment_start(tmp_path=None):
video_path = Path("tests") / "tmp-video.mp4"
video_path.write_bytes(b"fake video")
try:
result = align_first_sentence_with_asr(
video_path,
SubtitleCue("00:00:00.500", "00:00:02.000", "Hello world"),
FakeAsrClient(),
extract_audio=lambda source, target: target.write_bytes(b"fake wav"),
)
finally:
video_path.unlink(missing_ok=True)
assert result.applied is True
assert result.first_sentence_start == 1.5
assert result.offset_seconds == 1.0
if __name__ == "__main__":
test_align_first_sentence_with_asr_uses_first_segment_start()