teachingAssistant / test_tts_migration.py
Michael Hu
Migrate existing TTS providers to infrastructure layer
1f9c751
raw
history blame
4.38 kB
#!/usr/bin/env python3
"""Test script to verify TTS provider migration."""
import sys
import os
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
def test_provider_imports():
"""Test that all providers can be imported."""
print("Testing provider imports...")
try:
from src.infrastructure.tts import TTSProviderFactory, DummyTTSProvider
print("βœ“ Core TTS components imported successfully")
except Exception as e:
print(f"βœ— Failed to import core TTS components: {e}")
return False
try:
from src.domain.models.text_content import TextContent
from src.domain.models.voice_settings import VoiceSettings
from src.domain.models.speech_synthesis_request import SpeechSynthesisRequest
print("βœ“ Domain models imported successfully")
except Exception as e:
print(f"βœ— Failed to import domain models: {e}")
return False
return True
def test_dummy_provider():
"""Test the dummy provider functionality."""
print("\nTesting dummy provider...")
try:
from src.infrastructure.tts import DummyTTSProvider
from src.domain.models.text_content import TextContent
from src.domain.models.voice_settings import VoiceSettings
from src.domain.models.speech_synthesis_request import SpeechSynthesisRequest
# Create provider
provider = DummyTTSProvider()
print(f"βœ“ Created dummy provider: {provider.provider_name}")
# Check availability
if provider.is_available():
print("βœ“ Dummy provider is available")
else:
print("βœ— Dummy provider is not available")
return False
# Check voices
voices = provider.get_available_voices()
print(f"βœ“ Available voices: {voices}")
# Create a synthesis request
text_content = TextContent(text="Hello, world!", language="en")
voice_settings = VoiceSettings(voice_id="default", speed=1.0, language="en")
request = SpeechSynthesisRequest(
text_content=text_content,
voice_settings=voice_settings
)
print("βœ“ Created synthesis request")
# Test synthesis
audio_content = provider.synthesize(request)
print(f"βœ“ Generated audio: {len(audio_content.data)} bytes, {audio_content.duration:.2f}s")
return True
except Exception as e:
print(f"βœ— Dummy provider test failed: {e}")
import traceback
traceback.print_exc()
return False
def test_provider_factory():
"""Test the provider factory."""
print("\nTesting provider factory...")
try:
from src.infrastructure.tts import TTSProviderFactory
factory = TTSProviderFactory()
print("βœ“ Created provider factory")
available = factory.get_available_providers()
print(f"βœ“ Available providers: {available}")
if 'dummy' not in available:
print("βœ— Dummy provider should always be available")
return False
# Test creating dummy provider
provider = factory.create_provider('dummy')
print(f"βœ“ Created provider via factory: {provider.provider_name}")
# Test fallback logic
provider = factory.get_provider_with_fallback()
print(f"βœ“ Got provider with fallback: {provider.provider_name}")
return True
except Exception as e:
print(f"βœ— Provider factory test failed: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests."""
print("=== TTS Provider Migration Test ===\n")
tests = [
test_provider_imports,
test_dummy_provider,
test_provider_factory
]
passed = 0
for test in tests:
if test():
passed += 1
print()
print(f"=== Results: {passed}/{len(tests)} tests passed ===")
if passed == len(tests):
print("πŸŽ‰ All tests passed! TTS provider migration successful.")
return 0
else:
print("❌ Some tests failed. Check the output above.")
return 1
if __name__ == "__main__":
sys.exit(main())