import copy,json,tempfile,unittest from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock,patch from endpoint import APIHandler,Endpoint,APIError from tts_text_enhancer import prepare class PreparationTests(unittest.TestCase): def test_meaning_and_markup(self): self.assertEqual(prepare('- **Hinweis:** KI-Bild ≠ echtes Foto'),'Hinweis: Künstliche Intelligenz-Bild ist nicht gleich echtes Foto') def test_paths_and_urls(self): self.assertEqual(prepare('`/config/ref_imgs/`'),'Schrägstrich config Schrägstrich ref Unterstrich imgs Schrägstrich') self.assertEqual(prepare('https://example.org/a_b/'),'https://example.org/a_b/') self.assertEqual(prepare('[Dokumentation](https://example.org/a_b/)'),'Dokumentation') def test_language_and_numbers(self): self.assertEqual(prepare('403 404 12345'),'vierhundertdrei 404 12345') for language in ('English','French','Auto',''): self.assertEqual(prepare('**HTTP ≠ KI**',language),'**HTTP ≠ KI**') def test_invalid_input(self): for value in (None,{},123): with self.assertRaises(ValueError):prepare(value) class EndpointSettingTests(unittest.TestCase): def test_persistent_toggle_is_independent_of_publications_and_workers(self): with tempfile.TemporaryDirectory() as d: profiles=Mock();profiles.status.return_value={'profiles':[]} ep=Endpoint(d,profiles,Mock(),Mock(),Mock(),Mock(),8108);ep.status=Mock(return_value={}) self.assertFalse(ep.config['tts_text_enhancer']) ep.config['enabled_profiles']=['existing'];old=copy.deepcopy(ep.config) ep.configure({'tts_text_enhancer':True});saved=json.loads((Path(d)/'endpoint.json').read_text()) self.assertEqual(saved,{**old,'tts_text_enhancer':True});ep.worker.assert_not_called() reloaded=Endpoint(d,profiles,Mock(),Mock(),Mock(),Mock(),8108) self.assertTrue(reloaded.config['tts_text_enhancer']) for value in ('true',1,None): with self.assertRaises(ValueError):ep.configure({'tts_text_enhancer':value}) class BackendRoutingTests(unittest.TestCase): def test_shared_preparation_and_off_passthrough(self): from test_pocket import SpeechRoutingTests raw='**KI** ≠ Foto';prepared=prepare(raw) for family in ('pocket','qwen','omni','voxcpm'): for enabled in (False,True): for audio_format in ('wav','mp3','pcm'): if family in ('omni','voxcpm') and audio_format=='pcm':continue with self.subTest(family=family,enabled=enabled,format=audio_format): h,ep,pocket,row=SpeechRoutingTests().fixture();ep.config={'tts_text_enhancer':enabled};ep.root=Path('/synthetic') backend=pocket if family!='pocket': row['model']={'repo':'k2-fsa/OmniVoice' if family=='omni' else 'OpenBMB/VoxCPM2' if family=='voxcpm' else 'Qwen/Qwen3-TTS'} backend=ep.voxcpm=pocket if family in ('omni','voxcpm') else ep.tts backend.start.return_value={'id':'job'};backend.status.return_value={'job':{'id':'job','state':'complete'}};backend.audio.return_value=b'RIFF-test' with patch('profiles.voice_recipe',side_effect=lambda m: {'model':{}} if family in ('omni','voxcpm') else None),patch('endpoint.speech_reference',return_value={}),patch('endpoint.speech_mp3',return_value=b'MP3'): data=dict(model='tts',input=raw,voice='default',response_format=audio_format);before=copy.deepcopy(data) APIHandler.speech(h,ep,data) self.assertEqual(backend.start.call_args.args[1],prepared if enabled else raw) self.assertEqual(data,before) def test_explicit_non_german_is_not_rewritten(self): from test_pocket import SpeechRoutingTests h,ep,pocket,row=SpeechRoutingTests().fixture();ep.config={'tts_text_enhancer':True} APIHandler.speech(h,ep,dict(model='tts',input='**KI** ≠ Foto',language='English')) self.assertEqual(pocket.start.call_args.args[1],'**KI** ≠ Foto') if __name__=='__main__':unittest.main()