"""Tests for myagents.commands.provider (xiaohe switch provider).""" import json from pathlib import Path import pytest from click.testing import CliRunner from myagents import claude_settings as claude_settings_mod from myagents import secrets as secrets_mod from myagents.commands.provider import provider_cmd @pytest.fixture() def fake_config_dirs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[str, Path]: xiaohe_dir = tmp_path / ".xiaohe" / "agent" claude_dir = tmp_path / ".claude" monkeypatch.setattr(secrets_mod, "XIAOHE_CONFIG_DIR", xiaohe_dir) monkeypatch.setattr(secrets_mod, "XIAOHE_CONFIG_PATH", xiaohe_dir / "config.json") monkeypatch.setattr( claude_settings_mod, "_settings_path", lambda: claude_dir / "settings.json" ) return {"xiaohe": xiaohe_dir, "claude": claude_dir} class TestProviderList: def test_list_shows_providers(self) -> None: result = CliRunner().invoke(provider_cmd, ["list"]) assert result.exit_code == 0 assert "deepseek" in result.output assert "kimi" in result.output assert "kimi-code" in result.output assert "claude" in result.output class TestProviderCurrent: def test_current_shows_default_when_unset( self, fake_config_dirs: dict[str, Path] ) -> None: result = CliRunner().invoke(provider_cmd, ["current"]) assert result.exit_code == 0 assert "Claude (default" in result.output or "Claude (official" in result.output class TestProviderSwitch: def test_bare_invocation_lists(self) -> None: result = CliRunner().invoke(provider_cmd) assert result.exit_code == 0 assert "Available providers" in result.output assert "deepseek" in result.output def test_switch_to_deepseek(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["deepseek", "--key", "sk-test", "--yes"] ) assert result.exit_code == 0, result.output settings_path = fake_config_dirs["claude"] / "settings.json" settings = json.loads(settings_path.read_text()) assert settings["env"]["ANTHROPIC_AUTH_TOKEN"] == "sk-test" assert settings["env"]["ANTHROPIC_BASE_URL"] == "https://api.deepseek.com/anthropic" def test_switch_to_kimi_code(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["kimi-code", "--key", "sk-test", "--yes"] ) assert result.exit_code == 0, result.output settings_path = fake_config_dirs["claude"] / "settings.json" settings = json.loads(settings_path.read_text()) assert settings["env"]["ANTHROPIC_BASE_URL"] == "https://api.kimi.com/coding" assert settings["env"]["ANTHROPIC_MODEL"] == "kimi-for-coding" def test_unknown_provider_errors(self) -> None: result = CliRunner().invoke(provider_cmd, ["openai", "--yes"]) assert result.exit_code != 0 assert "Unknown provider" in result.output def test_prompts_for_missing_key(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["deepseek", "--yes"], input="sk-from-prompt\n" ) assert result.exit_code == 0, result.output settings_path = fake_config_dirs["claude"] / "settings.json" settings = json.loads(settings_path.read_text()) assert settings["env"]["ANTHROPIC_AUTH_TOKEN"] == "sk-from-prompt" def test_cancelled_does_not_write(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["deepseek", "--key", "sk-test"], input="n\n" ) assert result.exit_code == 0 assert "Cancelled" in result.output settings_path = fake_config_dirs["claude"] / "settings.json" assert not settings_path.exists() def test_model_override(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["deepseek", "--key", "sk-test", "--model", "custom", "--yes"] ) assert result.exit_code == 0, result.output settings_path = fake_config_dirs["claude"] / "settings.json" settings = json.loads(settings_path.read_text()) assert settings["env"]["ANTHROPIC_MODEL"] == "custom" def test_claude_clears_third_party(self, fake_config_dirs: dict[str, Path]) -> None: settings_path = fake_config_dirs["claude"] / "settings.json" fake_config_dirs["claude"].mkdir(parents=True) settings_path.write_text( json.dumps( { "env": { "ANTHROPIC_AUTH_TOKEN": "sk-test", "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic", } } ) ) result = CliRunner().invoke(provider_cmd, ["claude", "--yes"]) assert result.exit_code == 0, result.output settings = json.loads(settings_path.read_text()) assert "ANTHROPIC_AUTH_TOKEN" not in settings.get("env", {}) class TestProviderKeySet: def test_key_set_stores_in_xiaohe_config(self, fake_config_dirs: dict[str, Path]) -> None: result = CliRunner().invoke( provider_cmd, ["key", "set", "deepseek", "--key", "sk-test"] ) assert result.exit_code == 0, result.output config_path = fake_config_dirs["xiaohe"] / "config.json" data = json.loads(config_path.read_text()) assert data["keys"]["deepseek"] == "sk-test" def test_key_set_for_claude_errors(self) -> None: result = CliRunner().invoke( provider_cmd, ["key", "set", "claude", "--key", "sk-test"] ) assert result.exit_code != 0 assert "ANTHROPIC_API_KEY" in result.output class TestProviderKeyRm: def test_key_rm_removes_stored_key(self, fake_config_dirs: dict[str, Path]) -> None: config_path = fake_config_dirs["xiaohe"] / "config.json" fake_config_dirs["xiaohe"].mkdir(parents=True) config_path.write_text(json.dumps({"keys": {"deepseek": "sk-test"}})) result = CliRunner().invoke(provider_cmd, ["key", "rm", "deepseek", "--yes"]) assert result.exit_code == 0, result.output data = json.loads(config_path.read_text()) assert "deepseek" not in data.get("keys", {}) def test_key_rm_without_yes_confirms(self, fake_config_dirs: dict[str, Path]) -> None: config_path = fake_config_dirs["xiaohe"] / "config.json" fake_config_dirs["xiaohe"].mkdir(parents=True) config_path.write_text(json.dumps({"keys": {"deepseek": "sk-test"}})) result = CliRunner().invoke(provider_cmd, ["key", "rm", "deepseek"], input="y\n") assert result.exit_code == 0, result.output data = json.loads(config_path.read_text()) assert "deepseek" not in data.get("keys", {})