diff --git a/tests/test_config.py b/tests/test_config.py index e59d740..e0a42a0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,37 +1,93 @@ -"""Tests for configuration module.""" +"""Tests for ShellGenius configuration module.""" -import tempfile import os +import tempfile +from pathlib import Path + +import yaml from shellgenius.config import Config, get_config class TestConfig: + """Test cases for configuration.""" + def test_default_config(self): """Test default configuration values.""" - with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: - f.write("") - f.flush() - - config = Config(f.name) - - assert config.get("ollama.host") == "localhost:11434" - assert config.get("ollama.model") == "llama3" - assert config.get("safety.level") == "moderate" - - os.unlink(f.name) - + with tempfile.TemporaryDirectory() as tmpdir: + config_path = Path(tmpdir) / "nonexistent.yaml" + config = Config(str(config_path)) + + assert config.ollama_host == "localhost:11434" + assert config.ollama_model == "codellama" + assert config.safety_level == "moderate" + + def test_custom_config(self): + """Test loading custom configuration.""" + custom_config = { + "ollama": { + "host": "custom:9999", + "model": "mistral", + "timeout": 60, + }, + "safety": { + "level": "strict", + }, + } + + with tempfile.TemporaryDirectory() as tmpdir: + config_path = Path(tmpdir) / "config.yaml" + with open(config_path, "w") as f: + yaml.dump(custom_config, f) + + config = Config(str(config_path)) + + assert config.ollama_host == "custom:9999" + assert config.ollama_model == "mistral" + assert config.safety_level == "strict" + + def test_env_override(self): + """Test environment variable overrides.""" + os.environ["OLLAMA_HOST"] = "env-host:1234" + os.environ["OLLAMA_MODEL"] = "env-model" + + try: + config = Config() + assert config.ollama_host == "env-host:1234" + assert config.ollama_model == "env-model" + finally: + del os.environ["OLLAMA_HOST"] + del os.environ["OLLAMA_MODEL"] + def test_get_nested_value(self): """Test getting nested configuration values.""" config = Config() - - assert config.get("ollama.host") is not None - assert config.get("nonexistent.key", "default") == "default" - - def test_ollama_properties(self): - """Test Ollama configuration properties.""" + timeout = config.get("ollama.timeout") + assert timeout == 120 or isinstance(timeout, (int, type(None))) + + def test_get_missing_key(self): + """Test getting missing key returns default.""" config = Config() - - assert "localhost" in config.ollama_host - assert config.ollama_model in ["llama3", "codellama", "llama2", "mistral"] - assert config.safety_level in ["strict", "moderate", "permissive"] + value = config.get("nonexistent.key", "default") + assert value == "default" + + def test_merge_configs(self): + """Test merging user config with defaults.""" + with tempfile.TemporaryDirectory() as tmpdir: + config_path = Path(tmpdir) / "config.yaml" + with open(config_path, "w") as f: + yaml.dump({"ollama": {"model": "llama2"}}, f) + + config = Config(str(config_path)) + + assert config.ollama_model == "llama2" + assert config.ollama_host == "localhost:11434" + + +class TestGetConfig: + """Test get_config convenience function.""" + + def test_get_config_singleton(self): + """Test get_config returns Config instance.""" + config = get_config() + assert isinstance(config, Config)