""" 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