| """Test voice components: TTS formatter, voice adapter, self-improvement.""" |
|
|
| import sys |
| import os |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
|
|
| from singularity_llm.voice.tts_output import TTSOutputFormatter, number_to_words |
| from singularity_llm.voice.voice_adapter import VoiceAdapter |
| from singularity_llm.voice.self_improve import SelfImprovementEngine |
|
|
|
|
| def test_tts_formatter(): |
| """Test TTS output formatting.""" |
| fmt = TTSOutputFormatter() |
|
|
| |
| result = fmt.format("**Hello** world! This is *italic* text.") |
| assert "**" not in result, f"Bold not stripped: {result}" |
| assert "*" not in result, f"Italic not stripped: {result}" |
| print(f" Markdown stripped: {result}") |
|
|
| |
| result = fmt.format("Here is code: `print('hello')` and ```python\nx=1\n```") |
| assert "```" not in result, f"Code block not stripped: {result}" |
| print(f" Code stripped: {result}") |
|
|
| |
| result = fmt.format("The API and URL are working.") |
| assert "A P I" in result, f"Abbreviation not expanded: {result}" |
| print(f" Abbreviations expanded: {result}") |
|
|
|
|
| def test_number_to_words(): |
| """Test number to words conversion.""" |
| assert number_to_words(0) == "zero" |
| assert number_to_words(1) == "one" |
| assert number_to_words(10) == "ten" |
| assert number_to_words(15) == "fifteen" |
| assert number_to_words(42) == "forty two" |
| assert number_to_words(100) == "one hundred" |
| assert "one thousand" in number_to_words(1000) |
| print(f" 42 β '{number_to_words(42)}'") |
| print(f" 100 β '{number_to_words(100)}'") |
|
|
|
|
| def test_number_normalization(): |
| """Test number normalization in TTS formatter.""" |
| fmt = TTSOutputFormatter() |
| result = fmt.format("I have 100 apples and 42 oranges.") |
| assert "one hundred" in result, f"Number not normalized: {result}" |
| assert "forty two" in result, f"Number not normalized: {result}" |
| print(f" Numbers normalized: {result}") |
|
|
|
|
| def test_voice_adapter(): |
| """Test voice adapter sentence splitting.""" |
| adapter = VoiceAdapter() |
|
|
| def mock_stream(): |
| yield "Hello " |
| yield "there. " |
| yield "How are " |
| yield "you? " |
| yield "I am fine!" |
|
|
| sentences = list(adapter.stream_sentences(mock_stream())) |
| assert len(sentences) >= 2, f"Expected 2+ sentences, got {len(sentences)}" |
| print(f" Sentences: {sentences}") |
|
|
|
|
| def test_self_improvement(): |
| """Test self-improvement engine.""" |
| engine = SelfImprovementEngine(model=None, tokenizer=None) |
|
|
| |
| engine.record_conversation("Hello", "Hi there!", confidence=0.8) |
| engine.record_conversation("What is AI?", "AI is artificial intelligence.", confidence=0.6) |
| engine.record_conversation("How to code?", "Start with Python basics.", confidence=0.7) |
|
|
| stats = engine.get_stats() |
| assert stats["conversations_learned"] == 3, f"Wrong count: {stats['conversations_learned']}" |
| assert stats["avg_confidence"] > 0.5, f"Low confidence: {stats['avg_confidence']}" |
| print(f" Conversations: {stats['conversations_learned']}") |
| print(f" Avg confidence: {stats['avg_confidence']:.2f}") |
|
|
|
|
| def test_self_talk(): |
| """Test self-talk generation.""" |
| engine = SelfImprovementEngine(model=None, tokenizer=None) |
|
|
| def mock_generate(prompt): |
| if "Ask a question" in prompt: |
| return "What is machine learning?" |
| return "Machine learning is a subset of AI that learns from data." |
|
|
| pairs = engine.self_talk(mock_generate, max_rounds=1) |
| assert len(pairs) >= 0, "Self-talk returned None" |
| print(f" Self-talk pairs: {len(pairs)}") |
|
|
|
|
| if __name__ == "__main__": |
| print("Running voice tests...") |
| test_tts_formatter() |
| print(" β test_tts_formatter") |
| test_number_to_words() |
| print(" β test_number_to_words") |
| test_number_normalization() |
| print(" β test_number_normalization") |
| test_voice_adapter() |
| print(" β test_voice_adapter") |
| test_self_improvement() |
| print(" β test_self_improvement") |
| test_self_talk() |
| print(" β test_self_talk") |
| print("\nAll voice tests passed!") |
|
|