121 lines
3.3 KiB
Python
121 lines
3.3 KiB
Python
"""Tests for configuration management."""
|
|
|
|
import pytest
|
|
import yaml
|
|
from pathlib import Path
|
|
from src.config import Config
|
|
from src.exceptions import ConfigError
|
|
|
|
|
|
def test_config_initialization():
|
|
"""Test basic configuration initialization."""
|
|
config = Config()
|
|
assert config.data == {}
|
|
assert config.config_path is None
|
|
|
|
|
|
def test_config_get_set():
|
|
"""Test getting and setting configuration values."""
|
|
config = Config()
|
|
|
|
# Test setting and getting values
|
|
config.set("test.key", "value")
|
|
assert config.get("test.key") == "value"
|
|
|
|
# Test default value
|
|
assert config.get("nonexistent", "default") == "default"
|
|
|
|
|
|
def test_config_nested_keys():
|
|
"""Test nested configuration keys."""
|
|
config = Config()
|
|
|
|
config.set("agent1.provider", "openrouter")
|
|
config.set("agent1.model", "test-model")
|
|
|
|
assert config.get("agent1.provider") == "openrouter"
|
|
assert config.get("agent1.model") == "test-model"
|
|
|
|
|
|
def test_config_load_from_file(temp_dir, sample_config_data):
|
|
"""Test loading configuration from YAML file."""
|
|
config_file = temp_dir / "test_config.yaml"
|
|
|
|
# Write test config
|
|
with open(config_file, "w") as f:
|
|
yaml.safe_dump(sample_config_data, f)
|
|
|
|
# Load config
|
|
config = Config(str(config_file))
|
|
|
|
assert config.get("agent1.provider") == "openrouter"
|
|
assert config.get("agent2.provider") == "lmstudio"
|
|
|
|
|
|
def test_config_load_invalid_file():
|
|
"""Test loading from non-existent file."""
|
|
with pytest.raises(ConfigError):
|
|
config = Config("nonexistent.yaml")
|
|
config.load_from_file("nonexistent.yaml")
|
|
|
|
|
|
def test_config_environment_variable_priority(mock_env_vars):
|
|
"""Test that environment variables take priority for API keys."""
|
|
config = Config()
|
|
|
|
# Set API key in config
|
|
config.set("agent1.api_key", "config_api_key")
|
|
|
|
# Environment variable should take priority
|
|
api_key = config.get("agent1.api_key")
|
|
assert api_key == "test_api_key_from_env"
|
|
|
|
|
|
def test_config_validate_agent(sample_config_data):
|
|
"""Test agent configuration validation."""
|
|
config = Config()
|
|
config.data = sample_config_data
|
|
|
|
# Should pass validation
|
|
assert config.validate_agent_config("agent1") is True
|
|
|
|
# Should fail without required fields
|
|
config2 = Config()
|
|
with pytest.raises(ConfigError):
|
|
config2.validate_agent_config("agent1")
|
|
|
|
|
|
def test_config_has_sensitive_data():
|
|
"""Test detection of sensitive data in configuration."""
|
|
config = Config()
|
|
|
|
# Config without sensitive data
|
|
config.set("agent1.model", "test-model")
|
|
assert not config._has_sensitive_data()
|
|
|
|
# Config with API key
|
|
config.set("agent1.api_key", "secret_key")
|
|
assert config._has_sensitive_data()
|
|
|
|
|
|
def test_config_save_to_file(temp_dir, sample_config_data, monkeypatch):
|
|
"""Test saving configuration to file."""
|
|
config_file = temp_dir / "saved_config.yaml"
|
|
config = Config()
|
|
config.data = sample_config_data.copy()
|
|
|
|
# Mock user confirmation to skip interactive prompt
|
|
monkeypatch.setattr("rich.prompt.Confirm.ask", lambda *args, **kwargs: True)
|
|
|
|
# Save config
|
|
config.save_to_file(str(config_file))
|
|
|
|
# Verify file was created
|
|
assert config_file.exists()
|
|
|
|
# Verify content
|
|
with open(config_file) as f:
|
|
loaded_data = yaml.safe_load(f)
|
|
|
|
assert loaded_data["agent1"]["provider"] == "openrouter"
|