110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
"""HTTP contracts with synthetic bytes and MockTransport only."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from decimal import Decimal
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from voice_dictation.core.transcriber import WhisperTranscriber
|
|
from voice_dictation.infrastructure.errors import TranscriptionError
|
|
|
|
|
|
def _engine(payload, model="gpt-transcribe"):
|
|
requests = []
|
|
|
|
def respond(request):
|
|
requests.append(request.read())
|
|
return httpx.Response(200, json=payload)
|
|
|
|
engine = WhisperTranscriber("synthetic-key", model=model)
|
|
engine._client.close()
|
|
engine._client = httpx.Client(transport=httpx.MockTransport(respond))
|
|
return engine, requests
|
|
|
|
|
|
def test_gpt_transcribe_multipart_language_and_duration_price(tmp_path):
|
|
audio = tmp_path / "synthetic.wav"
|
|
audio.write_bytes(b"synthetic audio placeholder")
|
|
engine, requests = _engine(
|
|
{
|
|
"text": " Test ",
|
|
"languages": [{"code": "ru"}],
|
|
"usage": {"type": "duration", "seconds": 60.0},
|
|
}
|
|
)
|
|
try:
|
|
result = engine.transcribe(audio, language="ru", prompt="domain context", duration_sec=20)
|
|
finally:
|
|
engine.close()
|
|
body = requests[0]
|
|
assert b'name="languages[]"\r\n\r\nru' in body
|
|
assert b'name="language"' not in body
|
|
assert b'name="keywords[]"' not in body
|
|
assert b'name="prompt"\r\n\r\ndomain context' in body
|
|
assert b'name="response_format"\r\n\r\njson' in body
|
|
assert result.text == "Test"
|
|
assert result.language == "ru"
|
|
assert result.duration_sec == 60
|
|
assert result.cost_usd == Decimal("0.004500")
|
|
|
|
|
|
@pytest.mark.parametrize("model", ["whisper-1", "gpt-4o-transcribe", "gpt-4o-mini-transcribe"])
|
|
def test_existing_models_keep_singular_language(tmp_path, model):
|
|
audio = tmp_path / "synthetic.wav"
|
|
audio.write_bytes(b"synthetic")
|
|
engine, requests = _engine({"text": "test"}, model=model)
|
|
try:
|
|
result = engine.transcribe(audio, language="ru", duration_sec=60)
|
|
finally:
|
|
engine.close()
|
|
assert b'name="language"\r\n\r\nru' in requests[0]
|
|
assert b'name="languages[]"' not in requests[0]
|
|
assert result.language == "ru"
|
|
expected = Decimal("0.003000") if model == "gpt-4o-mini-transcribe" else Decimal("0.006000")
|
|
assert result.cost_usd == expected
|
|
|
|
|
|
@pytest.mark.parametrize("languages", [[], [{"code": "ru"}, {"code": "en"}]])
|
|
def test_language_hint_is_not_reported_as_detected(tmp_path, languages):
|
|
audio = tmp_path / "synthetic.wav"
|
|
audio.write_bytes(b"synthetic")
|
|
engine, _ = _engine({"text": "text", "languages": languages})
|
|
try:
|
|
assert engine.transcribe(audio, language="ru", duration_sec=1).language is None
|
|
finally:
|
|
engine.close()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"payload",
|
|
[
|
|
[],
|
|
{"text": None},
|
|
{"text": "x", "languages": ["ru"]},
|
|
{"text": "x", "usage": {"type": "duration", "seconds": True}},
|
|
],
|
|
)
|
|
def test_invalid_response_rejected(tmp_path, payload):
|
|
audio = tmp_path / "synthetic.wav"
|
|
audio.write_bytes(b"synthetic")
|
|
engine, _ = _engine(payload)
|
|
try:
|
|
with pytest.raises(TranscriptionError):
|
|
engine.transcribe(audio)
|
|
finally:
|
|
engine.close()
|
|
|
|
|
|
def test_auto_language_omits_hints_and_uses_audio_duration_hint(tmp_path):
|
|
audio = tmp_path / "synthetic.wav"
|
|
audio.write_bytes(b"synthetic")
|
|
engine, requests = _engine({"text": "test", "languages": []})
|
|
try:
|
|
result = engine.transcribe(audio, language=None, duration_sec=30)
|
|
finally:
|
|
engine.close()
|
|
assert b'name="languages[]"' not in requests[0]
|
|
assert result.duration_sec == 30
|
|
assert result.cost_usd == Decimal("0.002250")
|