hermes-agent/tests/providers/test_plugin_discovery.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

124 lines
4.6 KiB
Python

"""Tests for the model-providers plugin discovery system.
Verifies that:
1. All bundled providers at plugins/model-providers/<name>/ are discovered
2. User plugins at $HERMES_HOME/plugins/model-providers/<name>/ override bundled
3. plugin.yaml manifests with kind=model-provider are correctly categorized
"""
from __future__ import annotations
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
def _clear_provider_caches():
"""Force providers/__init__.py to re-discover on next list_providers()."""
import providers as _pkg
_pkg._REGISTRY.clear()
_pkg._ALIASES.clear()
_pkg._discovered = False
# Evict any cached plugin modules so the next import re-executes.
for mod in list(sys.modules.keys()):
if (
mod.startswith("plugins.model_providers")
or mod.startswith("_hermes_user_provider")
):
del sys.modules[mod]
def test_bundled_plugins_discovered():
"""Every plugins/model-providers/<name>/ should contain a plugin.yaml + __init__.py."""
plugins_dir = REPO_ROOT / "plugins" / "model-providers"
assert plugins_dir.is_dir(), f"Missing {plugins_dir}"
child_dirs = [c for c in plugins_dir.iterdir() if c.is_dir()]
assert len(child_dirs) >= 28, f"Expected at least 28 provider plugins, found {len(child_dirs)}"
for child in child_dirs:
assert (child / "__init__.py").exists(), f"{child.name} missing __init__.py"
assert (child / "plugin.yaml").exists(), f"{child.name} missing plugin.yaml"
def test_all_profiles_register():
"""After discovery, the registry must contain every bundled provider directory.
This is an invariant — the number of profiles matches the number of plugin
directories, not a hardcoded count. Counts shift when providers are
added/removed; that's expected and shouldn't break CI.
"""
_clear_provider_caches()
from providers import list_providers
plugins_dir = REPO_ROOT / "plugins" / "model-providers"
plugin_dir_count = sum(1 for c in plugins_dir.iterdir() if c.is_dir())
profiles = list_providers()
names = sorted(p.name for p in profiles)
# Some plugin __init__.py files register multiple profiles, so the registry
# count is >= the directory count (never less).
assert len(names) >= plugin_dir_count, (
f"Expected at least {plugin_dir_count} profiles (one per plugin dir), got {len(names)}: {names}"
)
# Spot-check representative providers from different categories
for required in (
"openrouter", "anthropic", "custom", "bedrock", "openai-codex",
"minimax-oauth", "gmi", "xiaomi", "alibaba-coding-plan", "fireworks",
):
assert required in names, f"Missing profile: {required}"
def test_user_plugin_overrides_bundled(tmp_path, monkeypatch):
"""A user plugin with the same name must override the bundled profile."""
# Point HERMES_HOME at a fresh temp dir
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
# get_hermes_home() may be module-cached depending on codebase; ensure the
# env var is the source of truth. Most code paths re-read it each call.
# Drop a user plugin that replaces 'gmi'
user_gmi = hermes_home / "plugins" / "model-providers" / "gmi"
user_gmi.mkdir(parents=True)
(user_gmi / "__init__.py").write_text(
"from providers import register_provider\n"
"from providers.base import ProviderProfile\n"
"\n"
"custom_gmi = ProviderProfile(\n"
' name="gmi",\n'
' aliases=("gmi-user-override-test",),\n'
' env_vars=("GMI_API_KEY",),\n'
' base_url="https://user-override.example.com/v1",\n'
' auth_type="api_key",\n'
")\n"
"register_provider(custom_gmi)\n"
)
(user_gmi / "plugin.yaml").write_text(
"name: gmi-user-override\n"
"kind: model-provider\n"
"version: 0.0.1\n"
"description: Test user override\n"
)
_clear_provider_caches()
from providers import get_provider_profile
gmi = get_provider_profile("gmi")
assert gmi is not None
assert gmi.base_url == "https://user-override.example.com/v1", (
f"User override not applied; got base_url={gmi.base_url!r}"
)
assert "gmi-user-override-test" in gmi.aliases
# Clean up: reset discovery state so other tests see the bundled version
_clear_provider_caches()
# No import means the module must NOT be in the plugins list as a loaded one.
# We check that the general loader didn't crash and didn't raise from the
# broken __init__.py.