spik-backend / tests /test_api.py
Nam Fam
update files
311bc41
"""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