personabot-api / tests /test_speech_endpoints.py
GitHub Actions
Deploy 2e8cff3
c44df3b
import asyncio
def test_transcribe_requires_auth(app_client):
response = app_client.post(
"/transcribe",
files={"audio": ("sample.webm", b"abc", "audio/webm")},
)
assert response.status_code == 401
def test_transcribe_success(app_client, valid_token):
async def fake_transcribe(filename, content_type, audio_bytes, language=None):
await asyncio.sleep(0)
return "hello from voice"
app_client.app.state.transcriber.transcribe = fake_transcribe
response = app_client.post(
"/transcribe",
files={"audio": ("sample.webm", b"abc", "audio/webm")},
headers={"Authorization": f"Bearer {valid_token}"},
)
assert response.status_code == 200
assert response.json()["transcript"] == "hello from voice"
def test_transcribe_rejects_oversized_audio(app_client, valid_token):
app_client.app.state.settings.TRANSCRIBE_MAX_UPLOAD_BYTES = 2
response = app_client.post(
"/transcribe",
files={"audio": ("sample.webm", b"abcdef", "audio/webm")},
headers={"Authorization": f"Bearer {valid_token}"},
)
assert response.status_code == 413
def test_tts_requires_auth(app_client):
response = app_client.post("/tts", json={"text": "Hello world"})
assert response.status_code == 401
def test_tts_success(app_client, valid_token):
captured: dict[str, str] = {}
async def fake_synthesize(text, voice="am_adam"):
await asyncio.sleep(0)
captured["text"] = text
captured["voice"] = voice
return b"RIFF....fake"
app_client.app.state.tts_client.synthesize = fake_synthesize
response = app_client.post(
"/tts",
json={"text": "Hello world"},
headers={"Authorization": f"Bearer {valid_token}"},
)
assert response.status_code == 200
assert response.headers.get("content-type", "").startswith("audio/wav")
assert response.content == b"RIFF....fake"
assert captured["text"] == "Hello world"
assert captured["voice"] == "am_adam"
def test_tts_uses_provided_voice(app_client, valid_token):
captured: dict[str, str] = {}
async def fake_synthesize(text, voice="am_adam"):
await asyncio.sleep(0)
captured["voice"] = voice
return b"RIFF....fake"
app_client.app.state.tts_client.synthesize = fake_synthesize
response = app_client.post(
"/tts",
json={"text": "Hello world", "voice": "af_heart"},
headers={"Authorization": f"Bearer {valid_token}"},
)
assert response.status_code == 200
assert captured["voice"] == "af_heart"