81 lines
3.5 KiB
Python
81 lines
3.5 KiB
Python
import asyncio
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import wave
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
try:
|
|
from tts.app import TTSSettings, create_app
|
|
except ImportError:
|
|
TTSSettings = create_app = None
|
|
|
|
|
|
def configured_app(directory, calls):
|
|
assert create_app is not None, 'The local speech HTTP provider has not been implemented'
|
|
root = Path(directory)
|
|
models = root / 'models'
|
|
(models / 'voices').mkdir(parents=True)
|
|
for name in ['config.json', 'kokoro-v1_0.pth', 'voices/zf_xiaobei.pt', 'voices/zm_yunxi.pt']:
|
|
(models / name).write_bytes(b'test-fixture')
|
|
|
|
async def synthesize(request, destination, settings):
|
|
calls.append(request.input)
|
|
with wave.open(str(destination), 'wb') as out:
|
|
out.setnchannels(1)
|
|
out.setsampwidth(2)
|
|
out.setframerate(24000)
|
|
out.writeframes(b'\x01\x00' * 240)
|
|
|
|
return create_app(TTSSettings(api_key='local-test-key', model_dir=models,
|
|
work_dir=root / 'work'), synthesize=synthesize)
|
|
|
|
|
|
def test_speech_requires_dedicated_authentication():
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
calls = []
|
|
with TestClient(configured_app(directory, calls)) as client:
|
|
response = client.post('/v1/audio/speech', json={'input': '你好'})
|
|
assert response.status_code == 401
|
|
assert calls == []
|
|
|
|
|
|
def test_invalid_voice_text_and_speed_do_not_start_model():
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
calls = []
|
|
with TestClient(configured_app(directory, calls)) as client:
|
|
for payload in [{'input': '你好', 'voice': '../../private.pt'},
|
|
{'input': ' '}, {'input': '你好', 'speed': 99},
|
|
{'input': '你好', 'model': 'untrusted-model'},
|
|
{'input': '你好', 'response_format': 'mp3'}]:
|
|
response = client.post('/v1/audio/speech', json=payload,
|
|
headers={'Authorization': 'Bearer local-test-key'})
|
|
assert response.status_code == 422
|
|
assert calls == []
|
|
|
|
|
|
def test_wav_response_contains_real_output_and_cleans_temporary_files():
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
calls = []
|
|
with TestClient(configured_app(directory, calls)) as client:
|
|
response = client.post('/v1/audio/speech', json={'input': '你好,本地语音测试。'},
|
|
headers={'Authorization': 'Bearer local-test-key'})
|
|
assert response.status_code == 200
|
|
assert response.headers['content-type'].startswith('audio/wav')
|
|
assert response.content[:4] == b'RIFF' and response.content[8:12] == b'WAVE'
|
|
assert len(response.content) == 524
|
|
assert calls == ['你好,本地语音测试。']
|
|
assert not list((Path(directory) / 'work').iterdir())
|
|
|
|
|
|
def test_missing_weights_do_not_claim_available():
|
|
assert create_app is not None, 'The local speech HTTP provider has not been implemented'
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
settings = TTSSettings(api_key='local-test-key', model_dir=Path(directory) / 'missing',
|
|
work_dir=Path(directory) / 'work')
|
|
with TestClient(create_app(settings)) as client:
|
|
response = client.get('/health', headers={'Authorization': 'Bearer local-test-key'})
|
|
assert response.status_code == 503
|