File size: 4,176 Bytes
948a05a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""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 splitbit_llm.voice.tts_output import TTSOutputFormatter, number_to_words
from splitbit_llm.voice.voice_adapter import VoiceAdapter
from splitbit_llm.voice.self_improve import SelfImprovementEngine


def test_tts_formatter():
    """Test TTS output formatting."""
    fmt = TTSOutputFormatter()

    # Test markdown stripping
    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}")

    # Test code block stripping
    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}")

    # Test abbreviation expansion
    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)

    # Record conversations
    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!")