60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import importlib.util
|
|
import pathlib
|
|
import unittest
|
|
|
|
|
|
MODULE_PATH = pathlib.Path(__file__).with_name("tts_gateway.py")
|
|
SPEC = importlib.util.spec_from_file_location("tts_gateway", MODULE_PATH)
|
|
gateway = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(gateway)
|
|
|
|
|
|
class LanguageSegmentationTests(unittest.TestCase):
|
|
def test_german_only(self):
|
|
self.assertEqual(
|
|
gateway.segment_languages("Guten Abend, wie warm ist es heute?"),
|
|
[("de", "Guten Abend, wie warm ist es heute?")],
|
|
)
|
|
|
|
def test_english_only(self):
|
|
text = "This is a short test and it is running on the local server."
|
|
self.assertEqual(gateway.segment_languages(text), [("en", text)])
|
|
|
|
def test_mixed_compounds(self):
|
|
text = "Ich öffne das Unraid-Dashboard und prüfe die Docker-Container."
|
|
self.assertEqual(
|
|
gateway.segment_languages(text),
|
|
[
|
|
("de", "Ich öffne das "),
|
|
("en", "Unraid-Dashboard"),
|
|
("de", " und prüfe die "),
|
|
("en", "Docker-Container"),
|
|
("de", "."),
|
|
],
|
|
)
|
|
|
|
|
|
class FallbackTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.original_xtts = gateway.synthesize_xtts
|
|
self.original_piper = gateway.synthesize_piper
|
|
|
|
def tearDown(self):
|
|
gateway.synthesize_xtts = self.original_xtts
|
|
gateway.synthesize_piper = self.original_piper
|
|
|
|
def test_piper_is_used_when_xtts_fails(self):
|
|
def fail(*_args):
|
|
raise RuntimeError("synthetic XTTS failure")
|
|
|
|
gateway.synthesize_xtts = fail
|
|
gateway.synthesize_piper = lambda *_args: (b"piper", "audio/wav")
|
|
self.assertEqual(
|
|
gateway.synthesize("synthetic test", "wav", 1.0),
|
|
(b"piper", "audio/wav"),
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|