File size: 7,438 Bytes
8cbe6eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Integration tests for voice control core infrastructure.

This module tests that all core components work together correctly.
"""

import pytest
from voice_control.models import ProviderConfig, ProviderType
from voice_control.config import ConfigManager, VoiceControlConfig
from voice_control.utils import generate_fingerprint, validate_audio_format, validate_language_code


class TestCoreInfrastructureIntegration:
    """Test integration of core infrastructure components."""
    
    def test_config_manager_with_provider_config(self, temp_dir):
        """Test that ConfigManager can handle ProviderConfig objects."""
        # Create a config manager with a temporary config file
        config_path = f"{temp_dir}/test_config.json"
        manager = ConfigManager(config_path)
        
        # Create a configuration with providers
        config = VoiceControlConfig()
        config.providers["test_provider"] = ProviderConfig(
            name="test_provider",
            provider_type=ProviderType.WEB_SPEECH_API,
            enabled=True,
            priority=1,
            free_tier_limits={"requests_per_day": 1000},
            rate_limits={"requests_per_minute": 10},
            supported_formats=["webm", "wav"],
            supported_languages=["en-US"],
            cost_per_minute=0.0,
            api_credentials={"api_key": "test_key"}
        )
        
        # Save and reload configuration
        manager.config = config
        manager.save_config()
        
        # Load configuration from file
        loaded_config = manager.load_config()
        
        # Verify provider configuration was preserved
        assert "test_provider" in loaded_config.providers
        provider = loaded_config.providers["test_provider"]
        assert provider.name == "test_provider"
        assert provider.provider_type == ProviderType.WEB_SPEECH_API
        assert provider.enabled is True
        assert provider.priority == 1
    
    def test_audio_fingerprint_generation(self, sample_audio_data):
        """Test audio fingerprint generation with sample data."""
        fingerprint1 = generate_fingerprint(sample_audio_data)
        fingerprint2 = generate_fingerprint(sample_audio_data)
        
        # Same data should produce same fingerprint
        assert fingerprint1 == fingerprint2
        
        # Different data should produce different fingerprint
        different_data = sample_audio_data + b"extra"
        fingerprint3 = generate_fingerprint(different_data)
        assert fingerprint1 != fingerprint3
    
    def test_format_and_language_validation(self):
        """Test audio format and language validation utilities."""
        # Valid formats
        assert validate_audio_format("webm") is True
        assert validate_audio_format("wav") is True
        assert validate_audio_format("mp3") is True
        
        # Invalid formats
        assert validate_audio_format("invalid") is False
        assert validate_audio_format("") is False
        
        # Valid language codes
        assert validate_language_code("en-US") is True
        assert validate_language_code("fr-FR") is True
        assert validate_language_code("zh-CN") is True
        
        # Invalid language codes
        assert validate_language_code("english") is False
        assert validate_language_code("en") is False
        assert validate_language_code("EN-US") is False
    
    def test_config_environment_override(self, monkeypatch, temp_dir):
        """Test that environment variables override configuration."""
        # Set environment variables
        monkeypatch.setenv("VOICE_CONTROL_ENV", "production")
        monkeypatch.setenv("VOICE_CONTROL_DEBUG", "true")
        monkeypatch.setenv("GOOGLE_SPEECH_API_KEY", "test_google_key")
        
        # Create config manager
        config_path = f"{temp_dir}/test_config.json"
        manager = ConfigManager(config_path)
        
        # Load configuration (should pick up environment variables)
        config = manager.load_config()
        
        # Verify environment overrides
        assert config.environment == "production"
        assert config.debug is True
        assert "google_speech" in config.providers
        assert config.providers["google_speech"].api_credentials["api_key"] == "test_google_key"
    
    def test_provider_config_validation_integration(self):
        """Test that provider configuration validation works end-to-end."""
        # Valid configuration should work
        config = ProviderConfig(
            name="valid_provider",
            provider_type=ProviderType.GOOGLE_SPEECH,
            enabled=True,
            priority=1,
            free_tier_limits={"requests_per_day": 1000},
            rate_limits={"requests_per_minute": 10},
            supported_formats=["wav"],
            supported_languages=["en-US"],
            cost_per_minute=0.05,
            api_credentials={"api_key": "key"}
        )
        
        # Should not raise any exceptions
        assert config.name == "valid_provider"
        assert config.cost_per_minute == 0.05
        
        # Invalid configuration should raise errors
        with pytest.raises(ValueError):
            ProviderConfig(
                name="invalid_provider",
                provider_type=ProviderType.GOOGLE_SPEECH,
                enabled=True,
                priority=-1,  # Invalid priority
                free_tier_limits={},
                rate_limits={},
                supported_formats=["wav"],
                supported_languages=["en-US"],
                cost_per_minute=0.05,
                api_credentials={}
            )
    
    def test_configuration_backup_and_restore(self, temp_dir):
        """Test configuration backup and restore functionality."""
        config_path = f"{temp_dir}/test_config.json"
        manager = ConfigManager(config_path)
        
        # Create initial configuration
        config = VoiceControlConfig()
        config.environment = "testing"
        config.debug = True
        
        manager.config = config
        manager.save_config()
        
        # Create backup
        backup_path = manager.backup_config()
        assert ".backup_" in backup_path
        
        # Modify configuration
        config.environment = "production"
        config.debug = False
        manager.save_config()
        
        # Restore from backup
        restored_config = manager.restore_config(backup_path)
        
        # Verify restoration
        assert restored_config.environment == "testing"
        assert restored_config.debug is True
    
    def test_config_history_tracking(self, temp_dir):
        """Test that configuration changes are tracked in history."""
        config_path = f"{temp_dir}/test_config.json"
        manager = ConfigManager(config_path)
        
        # Load initial config
        config = manager.load_config()
        
        # Make some updates
        manager.update_config({"environment": "staging"})
        manager.update_config({"debug": True})
        
        # Check history
        history = manager.get_config_history()
        assert len(history) >= 3  # load + 2 updates
        
        # Verify history entries have required fields
        for entry in history:
            assert "timestamp" in entry
            assert "action" in entry
            assert "data" in entry
            assert "user" in entry