#!/usr/bin/env python3 """ Simple test to verify ConfigurationLoader integration works correctly """ import os import tempfile from pathlib import Path from configuration_loader import ConfigurationLoader, ConfigurationError, EnvironmentVariableError def test_basic_environment_variable_expansion(): """Test basic environment variable expansion functionality""" # Set up test environment variables os.environ['TEST_API_KEY'] = 'test-key-123' os.environ['TEST_URL'] = 'https://test.example.com' # Create test configuration test_config = { 'api': { 'key': '${TEST_API_KEY}', 'url': '${TEST_URL}', 'timeout': '${TEST_TIMEOUT:-30}', # With default value 'retries': 3 # No environment variable }, 'nested': { 'values': ['${TEST_API_KEY}', 'static-value', '${TEST_URL}'] } } # Test expansion loader = ConfigurationLoader() expanded = loader.expand_environment_variables(test_config) # Verify results assert expanded['api']['key'] == 'test-key-123' assert expanded['api']['url'] == 'https://test.example.com' assert expanded['api']['timeout'] == '30' # Default value used assert expanded['api']['retries'] == 3 # Unchanged assert expanded['nested']['values'][0] == 'test-key-123' assert expanded['nested']['values'][1] == 'static-value' assert expanded['nested']['values'][2] == 'https://test.example.com' print("✓ Basic environment variable expansion test passed") def test_missing_required_variable(): """Test error handling for missing required environment variables""" # Ensure variable is not set if 'MISSING_VAR' in os.environ: del os.environ['MISSING_VAR'] test_config = { 'api': { 'key': '${MISSING_VAR}' } } loader = ConfigurationLoader() try: loader.expand_environment_variables(test_config) assert False, "Should have raised EnvironmentVariableError" except EnvironmentVariableError as e: assert 'MISSING_VAR' in str(e) print("✓ Missing required variable error test passed") def test_config_file_loading(): """Test loading configuration from YAML file""" # Set up test environment variable os.environ['TEST_CLAUDE_KEY'] = 'sk-ant-test-key' # Create temporary YAML config file yaml_content = """ obsidian: vault_path: "/tmp/test-vault" rest_api: url: "https://localhost:27123" api_key: "test-obsidian-key" verify_ssl: false claude: api_key: "${TEST_CLAUDE_KEY}" api_url: "${CLAUDE_URL:-https://api.anthropic.com}" model: "claude-3-5-sonnet-20241022" max_tokens: 4096 temperature: 0.7 """ with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: f.write(yaml_content) temp_file = f.name try: # Load and expand configuration expanded_config = ConfigurationLoader.load_config(temp_file) # Verify expansion worked assert expanded_config['claude']['api_key'] == 'sk-ant-test-key' assert expanded_config['claude']['api_url'] == 'https://api.anthropic.com' # Default value assert expanded_config['obsidian']['rest_api']['api_key'] == 'test-obsidian-key' print("✓ Config file loading test passed") finally: # Clean up Path(temp_file).unlink() def test_validation_methods(): """Test validation helper methods""" # Set up test environment os.environ['PRESENT_VAR'] = 'present' if 'MISSING_VAR' in os.environ: del os.environ['MISSING_VAR'] test_config = { 'present': '${PRESENT_VAR}', 'missing': '${MISSING_VAR}', 'with_default': '${MISSING_VAR:-default_value}' } loader = ConfigurationLoader() # Test validation missing_vars = loader.validate_environment_variables(test_config) assert len(missing_vars) == 1 assert 'MISSING_VAR' in missing_vars[0] # Test environment variable references env_refs = loader.get_environment_variable_references(test_config) assert 'PRESENT_VAR' in env_refs assert 'MISSING_VAR' in env_refs assert len(env_refs['MISSING_VAR']) == 2 # Used in 'missing' and 'with_default' print("✓ Validation methods test passed") if __name__ == '__main__': print("Testing ConfigurationLoader integration...") try: test_basic_environment_variable_expansion() test_missing_required_variable() test_config_file_loading() test_validation_methods() print("\n✅ All tests passed! ConfigurationLoader integration is working correctly.") except Exception as e: print(f"\n❌ Test failed: {e}") import traceback traceback.print_exc() exit(1) finally: # Clean up test environment variables for var in ['TEST_API_KEY', 'TEST_URL', 'TEST_CLAUDE_KEY', 'PRESENT_VAR']: if var in os.environ: del os.environ[var]