50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
import asyncio
|
|
import pytest
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
def test_transcription_engine_is_singleton():
|
|
from transcription import engine, TranscriptionEngine
|
|
assert isinstance(engine, TranscriptionEngine)
|
|
|
|
|
|
def test_transcribe_file_calls_whisper(tmp_path):
|
|
wav = tmp_path / "test.wav"
|
|
wav.write_bytes(b"\x00" * 100)
|
|
|
|
mock_model = MagicMock()
|
|
mock_segment = MagicMock()
|
|
mock_segment.text = " Hallo Welt"
|
|
mock_model.transcribe.return_value = ([mock_segment], MagicMock())
|
|
|
|
from transcription import TranscriptionEngine
|
|
eng = TranscriptionEngine()
|
|
eng._model = mock_model
|
|
|
|
result = asyncio.run(eng.transcribe_file(str(wav), language="de"))
|
|
assert result == "Hallo Welt"
|
|
mock_model.transcribe.assert_called_once_with(str(wav), language="de")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_transcribe_uses_remote_when_base_url_set(tmp_path):
|
|
import wave, struct
|
|
wav = tmp_path / "test.wav"
|
|
with wave.open(str(wav), "wb") as wf:
|
|
wf.setnchannels(1); wf.setsampwidth(2); wf.setframerate(16000)
|
|
wf.writeframes(struct.pack("<100h", *([0] * 100)))
|
|
|
|
import respx, httpx
|
|
from transcription import TranscriptionEngine
|
|
eng = TranscriptionEngine()
|
|
|
|
with respx.mock:
|
|
respx.post("http://beastix:8000/v1/audio/transcriptions").mock(
|
|
return_value=httpx.Response(200, json={"text": "Hallo Welt"})
|
|
)
|
|
result = await eng.transcribe_file(
|
|
str(wav), language="de", model_name="large-v3",
|
|
device="auto", base_url="http://beastix:8000",
|
|
)
|
|
assert result == "Hallo Welt"
|