6b81590c55
Systematic prune per AGENTS.md test policy, one pass over every major test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli, cron, tui_gateway, honcho/openviking, root-level): - DELETE: source-reading tests (read_text/getsource on prod files), change-detector tests (exact catalog counts, model-name snapshots, config version literals), mock-echo tests (assert a mock returns what it was told), assertion-free/trivial tests, near-duplicate parametrizations (boundaries + one representative kept), async/sync twin duplicates, cosmetic within-file variations. - KEEP (mandatory): security/redaction/approval guards, message-role alternation invariants, prompt-caching/deterministic-call-id invariants, issue-number regression tests (deduped), E2E tests. - 6 test files deleted outright (script-style/no-assert or fully redundant); conftest.py, fakes/, fixtures/ untouched. - tests/acp/conftest.py added: autouse fixture stubs the live models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server tests performed on every session create — test_server.py 147s → 3.4s, and the tests are now genuinely hermetic. - Sleep-based slowness shrunk where safe (codex_ttfb_watchdog, compression_concurrent_fork, etc.); no wall-clock assertion tightened. Verification: full hermetic suite via scripts/run_tests.sh — 2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall (baseline: 583s wall, 13,564s subprocess CPU).
95 lines
3.4 KiB
Python
95 lines
3.4 KiB
Python
"""Tests for hermes_cli.auth._update_config_for_provider clearing stale fields.
|
|
|
|
When the user switches from a custom provider (e.g. MiniMax with
|
|
``api_mode: anthropic_messages``, ``api_key: mxp-...``) to a built-in
|
|
provider (e.g. OpenRouter), the stale ``api_key`` and ``api_mode`` would
|
|
otherwise override the new provider's credentials and transport choice.
|
|
|
|
Built-in providers that legitimately need a specific ``api_mode`` (copilot,
|
|
xai) compute it at request-resolution time in
|
|
``_copilot_runtime_api_mode`` / ``_detect_api_mode_for_url``, so removing
|
|
the persisted value here is safe.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import yaml
|
|
|
|
from hermes_cli.auth import _update_config_for_provider
|
|
from hermes_cli.config import clear_model_endpoint_credentials, get_config_path
|
|
|
|
|
|
def _read_model_cfg() -> dict:
|
|
path = get_config_path()
|
|
if not path.exists():
|
|
return {}
|
|
data = yaml.safe_load(path.read_text()) or {}
|
|
model = data.get("model", {})
|
|
return model if isinstance(model, dict) else {}
|
|
|
|
|
|
def _seed_custom_provider_config(api_mode: str = "anthropic_messages") -> None:
|
|
"""Write a config.yaml mimicking a user on a MiniMax-style custom provider."""
|
|
path = get_config_path()
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(
|
|
yaml.safe_dump(
|
|
{
|
|
"model": {
|
|
"provider": "custom",
|
|
"base_url": "https://api.minimax.io/anthropic",
|
|
"api_key": "mxp-stale-key",
|
|
"api_mode": api_mode,
|
|
"default": "claude-sonnet-4-6",
|
|
}
|
|
},
|
|
sort_keys=False,
|
|
)
|
|
)
|
|
|
|
|
|
class TestUpdateConfigForProviderClearsStaleCustomFields:
|
|
def test_clear_model_endpoint_credentials_removes_key_alias_and_mode(self):
|
|
model_cfg = {
|
|
"provider": "openrouter",
|
|
"default": "anthropic/claude-sonnet-4.6",
|
|
"api_key": "sk-stale",
|
|
"api": "sk-legacy-stale",
|
|
"api_mode": "anthropic_messages",
|
|
}
|
|
|
|
returned = clear_model_endpoint_credentials(model_cfg)
|
|
|
|
assert returned is model_cfg
|
|
assert "api_key" not in model_cfg
|
|
assert "api" not in model_cfg
|
|
assert "api_mode" not in model_cfg
|
|
assert model_cfg["provider"] == "openrouter"
|
|
|
|
def test_switching_to_openrouter_clears_api_key_and_api_mode(self):
|
|
_seed_custom_provider_config()
|
|
|
|
_update_config_for_provider(
|
|
"openrouter",
|
|
"https://openrouter.ai/api/v1",
|
|
default_model="anthropic/claude-sonnet-4.6",
|
|
)
|
|
|
|
model_cfg = _read_model_cfg()
|
|
assert model_cfg.get("provider") == "openrouter"
|
|
assert model_cfg.get("base_url") == "https://openrouter.ai/api/v1"
|
|
assert "api_key" not in model_cfg, (
|
|
"Stale custom api_key would leak into OpenRouter requests — must be cleared"
|
|
)
|
|
assert "api_mode" not in model_cfg, (
|
|
"Stale api_mode=anthropic_messages from MiniMax would mis-route "
|
|
"OpenRouter requests to the Anthropic SDK — must be cleared"
|
|
)
|
|
|
|
|
|
def test_switching_clears_codex_responses_api_mode(self):
|
|
"""Also covers codex_responses, not just anthropic_messages."""
|
|
_seed_custom_provider_config(api_mode="codex_responses")
|
|
_update_config_for_provider("openrouter", "https://openrouter.ai/api/v1")
|
|
assert "api_mode" not in _read_model_cfg()
|