import tempfile,unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock from tts_test import TTSTests from tts_runtime import TTSRuntime,REPO,REVISION class TTSTestsValidation(unittest.TestCase): def test_validation_and_reservation_release(self): with tempfile.TemporaryDirectory() as d: t=TTSTests(d,SimpleNamespace(status=lambda:{'profiles':[]}),TTSRuntime(Path(d)/'runtime'));release=Mock();t.acquire=Mock(return_value=release) for kwargs in ({'text':''},{'text':'ok','speaker':'unknown'},{'text':'ok','language':'invalid'},{'text':'ok','speed':9}): with self.assertRaises(ValueError):t.start('p',**kwargs) t.acquire.assert_not_called() with self.assertRaises(ValueError):t.start('p','valid') release.assert_called_once() def test_audio_path_and_model_format(self): with tempfile.TemporaryDirectory() as d: r=TTSRuntime(Path(d)/'runtime');t=TTSTests(d,None,r) for ident in ('../secret','a'*32): with self.assertRaises(ValueError):t.audio(ident) self.assertTrue(t.blockers({'model':{'repo':'mlx-community/example','file':'model.safetensors'}})) self.assertTrue(t.blockers({'model':{'repo':REPO,'revision':REVISION,'file':'speech_tokenizer/model.safetensors'}})) self.assertFalse(r.status()['installed']) def test_gpu_sharing_uses_free_memory_not_process_count(self): from unittest.mock import patch with tempfile.TemporaryDirectory() as d: t=TTSTests(d,None,None) shared=dict(uuid='gpu3060',name='RTX 3060',free_mib=6000,processes=3) with patch.object(t,'_required_vram_mib',return_value=5000): with patch('tts_test.probe',return_value=[shared]):self.assertEqual(t._available_gpu(),shared) with patch('tts_test.probe',return_value=[dict(shared,free_mib=4999,processes=0)]):self.assertIsNone(t._available_gpu()) def test_memory_budget_tracks_the_actual_weights(self): with tempfile.TemporaryDirectory() as d: Path(d,'model.safetensors').write_bytes(bytes(1024*1024)) t=TTSTests(d,None,SimpleNamespace(paths=lambda:(None,Path(d)))) self.assertEqual(t._required_vram_mib(),1537) def test_library_output_cannot_corrupt_worker_protocol(self): import io,json,sys from unittest.mock import patch import tts_worker class Model: @classmethod def from_pretrained(cls,*args,**kwargs):print('Library startup notice');return cls() def generate_custom_voice(self,**kwargs):print('Library generation notice');return [[.1]*10],24000 with tempfile.TemporaryDirectory() as d: stdout=io.StringIO();stderr=io.StringIO() request=json.dumps(dict(output=d,text='Test',speaker='Ryan',language='German',speed=1))+'\n' modules={'torch':SimpleNamespace(bfloat16='bf16',cuda=SimpleNamespace(empty_cache=lambda:None)),'soundfile':SimpleNamespace(write=lambda *a,**k:None),'qwen_tts':SimpleNamespace(Qwen3TTSModel=Model)} with patch.dict(sys.modules,modules),patch.object(sys,'argv',['worker','model']),patch.object(sys,'stdin',io.StringIO(request)),patch.object(sys,'stdout',stdout),patch.object(sys,'stderr',stderr): self.assertEqual(tts_worker.main(),0) self.assertEqual([json.loads(line) for line in stdout.getvalue().splitlines()],[{'ready':True}]) self.assertIn('startup notice',stderr.getvalue());self.assertIn('generation notice',stderr.getvalue()) self.assertTrue(json.loads(Path(d,'result.json').read_text())['ok']) def test_long_text_chunks_preserve_all_words(self): from tts_worker import text_chunks text=('Dies ist ein längerer Satz mit mehreren Wörtern. '*25).strip() chunks=text_chunks(text) self.assertGreater(len(chunks),1) self.assertTrue(all(len(c)<=256 for c in chunks)) self.assertEqual(' '.join(chunks).split(),text.split()) compact='测'*1000 self.assertEqual(''.join(text_chunks(compact)),compact) self.assertTrue(all(len(c)<=256 for c in text_chunks(compact))) if __name__=='__main__':unittest.main()