""" Unit tests for config module. """ import unittest import tempfile import os from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).parent.parent / 'src')) from config import Config class TestConfig(unittest.TestCase): """Test Config class.""" def setUp(self): """Set up test fixtures.""" self.temp_dir = tempfile.mkdtemp() self.config_path = Path(self.temp_dir) / "config.yaml" # Save and clean up environment variables self.env_backup = {} for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ: self.env_backup[key] = os.environ[key] del os.environ[key] def tearDown(self): """Restore environment variables after each test.""" # Restore original environment variables for key, value in self.env_backup.items(): os.environ[key] = value # Clean up any that weren't backed up for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ and key not in self.env_backup: del os.environ[key] def test_default_config(self): """Test default configuration.""" config = Config(config_path=str(self.config_path), load_env=False) self.assertEqual(config.get('ai.provider'), 'openai') self.assertEqual(config.get('ai.model'), 'gpt-4') self.assertTrue(config.get('chunking.enabled')) self.assertEqual(config.get('chunking.chunk_duration_seconds'), 900) def test_get_with_default(self): """Test get with default value.""" # Ensure clean environment for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ: del os.environ[key] config = Config(config_path=str(self.config_path), load_env=False) # Existing key self.assertEqual(config.get('ai.model'), 'gpt-4') # Non-existing key with default self.assertEqual(config.get('nonexistent.key', 'default'), 'default') # Non-existing key without default self.assertIsNone(config.get('nonexistent.key')) def test_set_value(self): """Test setting configuration value.""" config = Config(config_path=str(self.config_path), load_env=False) config.set('ai.model', 'gpt-3.5-turbo') self.assertEqual(config.get('ai.model'), 'gpt-3.5-turbo') config.set('custom.new_key', 'new_value') self.assertEqual(config.get('custom.new_key'), 'new_value') def test_save_and_load(self): """Test saving and loading configuration.""" # Create and save config config1 = Config(config_path=str(self.config_path), load_env=False) config1.set('ai.model', 'gpt-3.5-turbo') config1.set('custom.test', 'value') config1.save() # Load config config2 = Config(config_path=str(self.config_path), load_env=False) self.assertEqual(config2.get('ai.model'), 'gpt-3.5-turbo') self.assertEqual(config2.get('custom.test'), 'value') def test_properties(self): """Test config properties.""" # Ensure clean environment for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ: del os.environ[key] config = Config(config_path=str(self.config_path), load_env=False) self.assertIsNone(config.ai_api_key) self.assertEqual(config.ai_model, 'gpt-4') self.assertTrue(config.chunking_enabled) self.assertEqual(config.chunk_duration, 900) self.assertEqual(config.long_video_threshold, 1800) def test_vault_path(self): """Test vault path property.""" config = Config(config_path=str(self.config_path), load_env=False) path = config.vault_path self.assertTrue(isinstance(path, Path)) def test_env_override_api_key(self): """Test environment variable override for API key.""" # Clean up environment first for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ: del os.environ[key] os.environ['OPENAI_API_KEY'] = 'test_api_key_123' config = Config(config_path=str(self.config_path)) self.assertEqual(config.ai_api_key, 'test_api_key_123') # Clean up del os.environ['OPENAI_API_KEY'] def test_env_override_model(self): """Test environment variable override for model.""" # Clean up environment first for key in ['OPENAI_API_KEY', 'AI_MODEL', 'OBSIDIAN_VAULT_PATH']: if key in os.environ: del os.environ[key] os.environ['AI_MODEL'] = 'custom-model' config = Config(config_path=str(self.config_path)) self.assertEqual(config.ai_model, 'custom-model') # Clean up del os.environ['AI_MODEL'] if __name__ == '__main__': unittest.main()