hermes-agent/tests/hermes_cli/test_models_dev_preferred_merge.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

156 lines
6 KiB
Python

"""Tests for the models.dev-preferred merge behavior in provider_model_ids
and list_authenticated_providers.
These guard the contract:
* For providers in ``_MODELS_DEV_PREFERRED`` (opencode-go, opencode-zen,
xiaomi, deepseek, smaller inference providers), both the CLI model
picker path (``provider_model_ids``) and the gateway ``/model`` picker
path (``list_authenticated_providers``) merge fresh models.dev entries
on top of the curated static list.
* OpenRouter and Nous Portal are NEVER merged — they keep their curated
(OpenRouter) or live-Portal (Nous) semantics.
* If models.dev is unreachable (offline / CI), the curated list is the
fallback — no crash, no empty list.
Merging is what lets new models (e.g. ``mimo-v2.5-pro`` on opencode-go)
appear in ``/model`` without a Hermes release.
"""
from unittest.mock import patch
from hermes_cli.models import (
_MODELS_DEV_PREFERRED,
_PROVIDER_MODELS,
_merge_with_models_dev,
provider_model_ids,
)
class TestMergeHelper:
def test_merge_empty_mdev_returns_curated(self):
"""When models.dev returns nothing, curated list is preserved verbatim."""
with patch("agent.models_dev.list_agentic_models", return_value=[]):
out = _merge_with_models_dev("opencode-go", ["mimo-v2-pro", "kimi-k2.6"])
assert out == ["mimo-v2-pro", "kimi-k2.6"]
def test_merge_case_insensitive_dedup(self):
"""Dedup is case-insensitive but preserves the first occurrence's casing."""
mdev = ["MiniMax-M2.7"]
curated = ["minimax-m2.7", "minimax-m2.5"]
with patch("agent.models_dev.list_agentic_models", return_value=mdev):
out = _merge_with_models_dev("minimax", curated)
# models.dev casing wins since it came first
assert out == ["MiniMax-M2.7", "minimax-m2.5"]
class TestProviderModelIdsPreferred:
def test_k3_live_discovery_is_scoped_to_kimi_coding_endpoint(self):
"""Coding keys discover K3; legacy Moonshot keys must not advertise it."""
class Response:
def __init__(self, body: bytes):
self._body = body
def read(self):
return self._body
def __enter__(self):
return self
def __exit__(self, *_args):
return False
def fake_open(req, **_kwargs):
if req.full_url == "https://api.kimi.com/coding/v1/models":
return Response(b'{"data":[{"id":"k3"}]}')
if req.full_url == "https://api.moonshot.ai/v1/models":
return Response(b'{"data":[{"id":"K3"},{"id":"kimi-k2.6"}]}')
if req.full_url == "https://example.invalid/v1/models":
return Response(b'{"data":[{"id":"k3"},{"id":"kimi-k2.6"}]}')
raise AssertionError(f"unexpected Kimi models URL: {req.full_url}")
with patch("hermes_cli.urllib_security.open_credentialed_url", side_effect=fake_open):
with patch(
"hermes_cli.auth.resolve_api_key_provider_credentials",
return_value={
"api_key": "sk-kimi-test",
"base_url": "https://api.kimi.com/coding",
},
):
coding_models = provider_model_ids("kimi-coding")
with patch(
"hermes_cli.auth.resolve_api_key_provider_credentials",
return_value={
"api_key": "legacy-test",
"base_url": "https://api.moonshot.ai/v1",
},
):
legacy_models = provider_model_ids("kimi-coding")
with patch(
"hermes_cli.auth.resolve_api_key_provider_credentials",
return_value={
"api_key": "custom-test",
"base_url": "https://example.invalid/v1",
},
):
custom_models = provider_model_ids("kimi-coding")
# The live bare wire id ``k3`` folds into the curated public slug
# ``kimi-k3`` (picker alias dedup) — one row, curated slug leads.
assert coding_models[0] == "kimi-k3"
assert all(model.lower() != "k3" for model in coding_models)
assert all(model.lower() != "k3" for model in legacy_models)
assert all(model.lower() != "k3" for model in custom_models)
# Legacy / custom endpoints never advertise the k3 family at all
# via live discovery (their curated floor may still carry kimi-k3).
def test_kimi_setup_flow_uses_same_coding_plan_catalog(self):
"""The setup wizard must not carry a stale duplicate Kimi model list."""
from hermes_cli.model_setup_flows import _model_flow_kimi
captured = {}
def fake_select(model_list, **_kwargs):
captured["models"] = model_list
return None
with (
patch("hermes_cli.main._prompt_api_key", return_value=("sk-kimi-test", False)),
patch("hermes_cli.auth._prompt_model_selection", side_effect=fake_select),
patch("hermes_cli.config.get_env_value", return_value=""),
patch("hermes_cli.config.save_env_value"),
):
_model_flow_kimi({}, current_model="")
assert captured["models"] == _PROVIDER_MODELS["kimi-coding"]
assert captured["models"][0] == "kimi-k3"
class TestOpenRouterAndNousUnchanged:
"""Per Teknium: openrouter and nous are NEVER merged with models.dev."""
def test_openrouter_does_not_call_merge(self):
"""openrouter takes its own live path — merge helper must NOT run."""
with patch(
"hermes_cli.models._merge_with_models_dev",
side_effect=AssertionError("merge should not be called for openrouter"),
):
# Even if model_ids() fails for some other reason, we just care
# that the merge path isn't invoked.
try:
provider_model_ids("openrouter")
except AssertionError:
raise
except Exception:
pass # model_ids() may fail in the hermetic test env — that's fine.