File size: 2,609 Bytes
815b978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c44df3b
 
 
815b978
c44df3b
 
815b978
 
 
 
 
 
 
 
 
 
 
 
 
c44df3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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"