voiceforge / backend /tests /unit /test_cloning.py
lordofgaming
Initial VoiceForge deployment (clean)
673435a
import pytest
from fastapi.testclient import TestClient
from unittest.mock import MagicMock
from app.main import app
from app.services.clone_service import CloneService, get_clone_service
class TestVoiceCloning:
@pytest.fixture
def mock_clone_service(self):
mock = MagicMock(spec=CloneService)
# Setup specific return values
mock.get_supported_languages.return_value = ["en", "es"]
mock.clone_voice.return_value = "dummy_path.wav"
return mock
@pytest.fixture
def client(self, mock_clone_service):
# Override dependency
app.dependency_overrides[get_clone_service] = lambda: mock_clone_service
with TestClient(app) as c:
yield c
# Cleanup
app.dependency_overrides.clear()
def test_synthesize_endpoint(self, client, mock_clone_service, tmp_path):
"""Test /clone/synthesize with mocked service"""
# Define side effect to create the file when called
def create_dummy_output(*args, **kwargs):
output_path = kwargs.get("output_path")
if output_path:
with open(output_path, "wb") as f:
f.write(b"fake audio data")
return output_path
mock_clone_service.clone_voice.side_effect = create_dummy_output
# Mock input file
dummy_wav = tmp_path / "ref.wav"
dummy_wav.write_bytes(b"dummy ref audio")
with open(dummy_wav, "rb") as f:
response = client.post(
"/api/v1/clone/synthesize",
data={"text": "Hello", "language": "en"},
files={"files": ("ref.wav", f, "audio/wav")}
)
assert response.status_code == 200
assert mock_clone_service.clone_voice.called
assert response.content == b"fake audio data"
def test_synthesize_no_files(self, client):
"""Test validation error when no files provided"""
response = client.post(
"/api/v1/clone/synthesize",
data={"text": "Hello"},
files=[] # Empty
)
assert response.status_code in [400, 422]
def test_languages_endpoint(self, client, mock_clone_service):
"""Test /clone/languages"""
response = client.get("/api/v1/clone/languages")
assert response.status_code == 200
assert response.json() == {"languages": ["en", "es"]}