Files
Athena-Deck/test_speech_reference.py

37 lines
2.2 KiB
Python

"""Stored speaker identity survives independent TTS calls; bad names fail closed."""
import io,json,tempfile,unittest,wave
from pathlib import Path
from endpoint import speech_reference,APIError
class SpeechReferenceTests(unittest.TestCase):
def setUp(self):
self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup)
self.root=Path(self.tmp.name);self.folder=self.root/'speech-voices';self.voice=self.folder/'Anna';self.voice.mkdir(parents=True)
b=io.BytesIO()
with wave.open(b,'wb') as w:w.setnchannels(1);w.setsampwidth(2);w.setframerate(24000);w.writeframes(b'\0\0'*48000)
(self.voice/'reference.wav').write_bytes(b.getvalue());(self.voice/'reference.json').write_text(json.dumps({'text':'Synthetischer Test.'}))
(self.folder/'default.json').write_text(json.dumps({'voice':'Anna'}))
def test_same_identity_across_independent_calls(self):
first=speech_reference(self.root,'default');second=speech_reference(self.root,'Anna')
self.assertEqual(first,second);self.assertEqual(first['reference_text'],'Synthetischer Test.');self.assertEqual(first['seed'],42)
def test_unconfigured_default_preserves_previous_behavior(self):
(self.folder/'default.json').unlink();self.assertEqual(speech_reference(self.root,'default'),{})
def test_names_cannot_escape(self):
for name in ('../Anna','/tmp/Anna','',None,{},'Anna/reference.wav'):
with self.subTest(name=name),self.assertRaises(APIError):speech_reference(self.root,name)
def test_missing_voice(self):
with self.assertRaises(APIError):speech_reference(self.root,'Missing')
def test_corrupt_default(self):
(self.folder/'default.json').write_text('null')
with self.assertRaises(APIError):speech_reference(self.root,'default')
def test_corrupt_audio(self):
(self.voice/'reference.wav').write_bytes(b'not audio')
with self.assertRaises(APIError):speech_reference(self.root,'Anna')
def test_transcript_required(self):
(self.voice/'reference.json').write_text('{"text":""}')
with self.assertRaises(APIError):speech_reference(self.root,'Anna')
def test_symlink_rejected(self):
(self.voice/'reference.wav').unlink();(self.voice/'reference.wav').symlink_to('/etc/passwd')
with self.assertRaises(APIError):speech_reference(self.root,'Anna')
if __name__=='__main__':unittest.main()