File size: 3,660 Bytes
1f47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311bc41
1f47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Tests for the Spik API endpoints."""
import os
import pytest
from fastapi.testclient import TestClient
from app.main import app

# Test client
client = TestClient(app)

# Test data
TEST_TEXT = "Hello, this is a test message."
TEST_VOICE = "Deepgram Aura2"

@pytest.fixture(scope="module")
def test_audio_file():
    """Generate a test audio file."""
    # This would be replaced with an actual test audio file in a real test
    test_file = "test_audio.wav"
    with open(test_file, "wb") as f:
        # Write a minimal WAV header (44 bytes)
        f.write(b'RIFF$\xbc\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00')
        f.write(b'\x44\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\x00\x00\x00\x00')
    yield test_file
    # Cleanup
    if os.path.exists(test_file):
        os.remove(test_file)

def test_health_check():
    """Test the health check endpoint."""
    response = client.get("/api/health")
    assert response.status_code == 200
    assert response.json()["status"] == "healthy"

@pytest.mark.skipif(
    not os.getenv("DEEPGRAM_API_KEY"),
    reason="DEEPGRAM_API_KEY not set in environment"
)
def test_tts_endpoint():
    """Test the text-to-speech endpoint."""
    response = client.post(
        "/api/tts",
        json={"text": TEST_TEXT, "voice": TEST_VOICE}
    )
    assert response.status_code == 200
    data = response.json()
    assert "audio_url" in data
    assert data["audio_url"].startswith("/output/tts_")
    assert "metadata" in data
    assert data["metadata"]["voice"] == TEST_VOICE

@pytest.mark.skipif(
    not os.getenv("DEEPGRAM_API_KEY"),
    reason="DEEPGRAM_API_KEY not set in environment"
)
def test_stt_endpoint(test_audio_file):
    """Test the speech-to-text endpoint with file upload."""
    with open(test_audio_file, "rb") as f:
        response = client.post(
            "/api/stt",
            files={"audio": ("test.wav", f, "audio/wav")},
            data={"language": "en", "model": "nova-3"}
        )
    assert response.status_code == 200
    data = response.json()
    assert "transcript" in data
    # We can't assert the exact transcript since it depends on the audio content
    assert isinstance(data["transcript"], str)

@pytest.mark.skipif(
    not os.getenv("DEEPGRAM_API_KEY"),
    reason="DEEPGRAM_API_KEY not set in environment"
)
def test_stt_with_url():
    """Test the speech-to-text endpoint with a URL."""
    # Using a short test audio URL - in a real test, this would be a URL to a test audio file
    test_audio_url = "https://dpgr.am/spacewalk.wav"
    response = client.post(
        "/api/stt",
        data={
            "audio_url": test_audio_url,
            "language": "en",
            "model": "nova-3"
        }
    )
    assert response.status_code == 200
    data = response.json()
    assert "transcript" in data
    assert isinstance(data["transcript"], str)

def test_tts_validation():
    """Test validation of the TTS endpoint."""
    # Test empty text
    response = client.post("/api/tts", json={"text": "", "voice": TEST_VOICE})
    assert response.status_code == 400
    
    # Test invalid voice
    response = client.post(
        "/api/tts",
        json={"text": TEST_TEXT, "voice": "Invalid Voice"}
    )
    assert response.status_code == 400

def test_stt_validation():
    """Test validation of the STT endpoint."""
    # Test no audio or URL provided
    response = client.post("/api/stt")
    assert response.status_code == 400
    
    # Test invalid content type
    response = client.post(
        "/api/stt",
        files={"audio": ("test.txt", b"not an audio file", "text/plain")}
    )
    assert response.status_code == 400