from __future__ import annotations import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) from cc_slim.engine import resolve_config def test_resolve_config_uses_defaults(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: monkeypatch.setenv("OPENAI_API_KEY", "env-openai-key") config = resolve_config(tmp_path, {}) assert config.provider == "openai" assert config.model == "gpt-4.1-mini" assert config.api_key == "env-openai-key" assert config.base_url is None assert config.max_turns == 12 def test_resolve_config_priority_cli_over_env_over_file(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: (tmp_path / ".cc-slim.toml").write_text( """ [cc_slim] provider = "anthropic" model = "file-model" api_key = "file-key" base_url = "https://file.example" max_turns = 4 """.strip(), encoding="utf-8", ) monkeypatch.setenv("CC_SLIM_PROVIDER", "openai") monkeypatch.setenv("CC_SLIM_MODEL", "env-model") monkeypatch.setenv("CC_SLIM_API_KEY", "env-key") monkeypatch.setenv("CC_SLIM_BASE_URL", "https://env.example") monkeypatch.setenv("CC_SLIM_MAX_TURNS", "9") config = resolve_config( tmp_path, { "provider": "anthropic", "model": "cli-model", "api_key": "cli-key", "base_url": "https://cli.example", "max_turns": 15, }, ) assert config.provider == "anthropic" assert config.model == "cli-model" assert config.api_key == "cli-key" assert config.base_url == "https://cli.example" assert config.max_turns == 15 def test_resolve_config_requires_api_key(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: monkeypatch.delenv("CC_SLIM_API_KEY", raising=False) monkeypatch.delenv("OPENAI_API_KEY", raising=False) monkeypatch.delenv("ANTHROPIC_API_KEY", raising=False) with pytest.raises(ValueError, match="缺少 API key"): resolve_config(tmp_path, {})