fix(provider): 测试与入口修复

This commit is contained in:
Zhengshou Lai
2026-07-16 21:13:24 +08:00
parent dd2011ae2d
commit 46a41eb82a
3 changed files with 104 additions and 115 deletions
+36 -61
View File
@@ -1,4 +1,4 @@
"""Tests for myagents.commands.backend."""
"""Tests for myagents.commands.provider (xiaohe switch provider)."""
import json
from pathlib import Path
@@ -8,7 +8,7 @@ from click.testing import CliRunner
from myagents import claude_settings as claude_settings_mod
from myagents import secrets as secrets_mod
from myagents.commands import backend as backend_mod
from myagents.commands.provider import provider_cmd
@pytest.fixture()
@@ -23,9 +23,9 @@ def fake_config_dirs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> dict[st
return {"xiaohe": xiaohe_dir, "claude": claude_dir}
class TestBackendList:
class TestProviderList:
def test_list_shows_providers(self) -> None:
result = CliRunner().invoke(backend_mod.backend_cmd, ["list"])
result = CliRunner().invoke(provider_cmd, ["list"])
assert result.exit_code == 0
assert "deepseek" in result.output
assert "kimi" in result.output
@@ -33,20 +33,25 @@ class TestBackendList:
assert "claude" in result.output
class TestBackendCurrent:
class TestProviderCurrent:
def test_current_shows_default_when_unset(
self, fake_config_dirs: dict[str, Path]
) -> None:
result = CliRunner().invoke(backend_mod.backend_cmd, ["current"])
result = CliRunner().invoke(provider_cmd, ["current"])
assert result.exit_code == 0
assert "Claude (default / not configured)" in result.output
assert "Claude (default" in result.output or "Claude (official" in result.output
class TestBackendUse:
def test_use_deepseek_stores_env(self, fake_config_dirs: dict[str, Path]) -> None:
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(
backend_mod.backend_cmd,
["use", "deepseek", "--key", "sk-test", "--yes"],
provider_cmd, ["deepseek", "--key", "sk-test", "--yes"]
)
assert result.exit_code == 0, result.output
settings_path = fake_config_dirs["claude"] / "settings.json"
@@ -54,10 +59,9 @@ class TestBackendUse:
assert settings["env"]["ANTHROPIC_AUTH_TOKEN"] == "sk-test"
assert settings["env"]["ANTHROPIC_BASE_URL"] == "https://api.deepseek.com/anthropic"
def test_use_kimi_code(self, fake_config_dirs: dict[str, Path]) -> None:
def test_switch_to_kimi_code(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd,
["use", "kimi-code", "--key", "sk-test", "--yes"],
provider_cmd, ["kimi-code", "--key", "sk-test", "--yes"]
)
assert result.exit_code == 0, result.output
settings_path = fake_config_dirs["claude"] / "settings.json"
@@ -65,46 +69,39 @@ class TestBackendUse:
assert settings["env"]["ANTHROPIC_BASE_URL"] == "https://api.kimi.com/coding"
assert settings["env"]["ANTHROPIC_MODEL"] == "kimi-for-coding"
def test_use_unknown_provider_errors(self) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd, ["use", "openai", "--yes"]
)
def test_unknown_provider_errors(self) -> None:
result = CliRunner().invoke(provider_cmd, ["openai", "--yes"])
assert result.exit_code != 0
assert "Unknown backend" in result.output
assert "Unknown provider" in result.output
def test_use_without_key_prompts(self, fake_config_dirs: dict[str, Path]) -> None:
def test_prompts_for_missing_key(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd,
["use", "deepseek", "--yes"],
input="sk-from-prompt\n",
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_use_cancelled_does_not_write(self, fake_config_dirs: dict[str, Path]) -> None:
def test_cancelled_does_not_write(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd,
["use", "deepseek", "--key", "sk-test"],
input="n\n",
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_use_model_override(self, fake_config_dirs: dict[str, Path]) -> None:
def test_model_override(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd,
["use", "deepseek", "--key", "sk-test", "--model", "custom-model", "--yes"],
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-model"
assert settings["env"]["ANTHROPIC_MODEL"] == "custom"
def test_use_claude_clears_third_party(self, fake_config_dirs: dict[str, Path]) -> None:
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(
@@ -117,33 +114,16 @@ class TestBackendUse:
}
)
)
result = CliRunner().invoke(
backend_mod.backend_cmd, ["use", "claude", "--yes"]
)
result = CliRunner().invoke(provider_cmd, ["claude", "--yes"])
assert result.exit_code == 0, result.output
settings = json.loads(settings_path.read_text())
assert "env" not in settings
class TestBackendReset:
def test_reset_alias(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_cmd,
["use", "deepseek", "--key", "sk-test", "--yes"],
)
assert result.exit_code == 0
result = CliRunner().invoke(backend_mod.backend_cmd, ["reset", "--yes"])
assert result.exit_code == 0, result.output
settings_path = fake_config_dirs["claude"] / "settings.json"
settings = json.loads(settings_path.read_text())
assert "ANTHROPIC_AUTH_TOKEN" not in settings.get("env", {})
class TestBackendKeySet:
class TestProviderKeySet:
def test_key_set_stores_in_xiaohe_config(self, fake_config_dirs: dict[str, Path]) -> None:
result = CliRunner().invoke(
backend_mod.backend_key,
["set", "deepseek", "--key", "sk-test"],
provider_cmd, ["key", "set", "deepseek", "--key", "sk-test"]
)
assert result.exit_code == 0, result.output
config_path = fake_config_dirs["xiaohe"] / "config.json"
@@ -152,22 +132,19 @@ class TestBackendKeySet:
def test_key_set_for_claude_errors(self) -> None:
result = CliRunner().invoke(
backend_mod.backend_key,
["set", "claude", "--key", "sk-test"],
provider_cmd, ["key", "set", "claude", "--key", "sk-test"]
)
assert result.exit_code != 0
assert "ANTHROPIC_API_KEY" in result.output
class TestBackendKeyRm:
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(
backend_mod.backend_key, ["rm", "deepseek", "--yes"]
)
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", {})
@@ -177,9 +154,7 @@ class TestBackendKeyRm:
fake_config_dirs["xiaohe"].mkdir(parents=True)
config_path.write_text(json.dumps({"keys": {"deepseek": "sk-test"}}))
result = CliRunner().invoke(
backend_mod.backend_key, ["rm", "deepseek"], input="y\n"
)
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", {})