hermes-agent/tests/hermes_cli/test_web_server_gateway_topology.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

161 lines
6.3 KiB
Python

"""Tests for the /api/status profile + gateway topology readout.
Covers the loopback-only ``profiles`` / ``gateway_mode`` / ``gateways`` fields
added to ``/api/status``: profile enumeration, single vs multiplex vs multiple
gateway detection, and per-platform port resolution.
"""
import pytest
from hermes_cli import web_server
from hermes_cli.web_server import (
_collect_profile_gateway_topology,
_profile_platform_ports,
)
# ---------------------------------------------------------------------------
# _profile_platform_ports
# ---------------------------------------------------------------------------
class TestProfilePlatformPorts:
def test_no_runtime_platforms_returns_empty(self, tmp_path):
assert _profile_platform_ports(tmp_path, None) == {}
assert _profile_platform_ports(tmp_path, {"platforms": {}}) == {}
def test_top_level_platforms_wins_over_gateway_block(self, tmp_path):
(tmp_path / "config.yaml").write_text(
"gateway:\n platforms:\n webhook:\n port: 1111\n"
"platforms:\n webhook:\n port: 2222\n",
encoding="utf-8",
)
runtime = {"platforms": {"webhook": {"state": "connected"}}}
assert _profile_platform_ports(tmp_path, runtime) == {"webhook": 2222}
def test_dead_platform_states_excluded(self, tmp_path):
runtime = {
"platforms": {
"webhook": {"state": "fatal"},
"api_server": {"state": "disconnected"},
"msgraph_webhook": {"state": "connected"},
}
}
assert _profile_platform_ports(tmp_path, runtime) == {"msgraph_webhook": 8646}
# ---------------------------------------------------------------------------
# _collect_profile_gateway_topology
# ---------------------------------------------------------------------------
def _patch_topology(monkeypatch, homes, running, runtimes):
"""Patch the topology collector's collaborators.
``homes``: list of (name, Path); ``running``: set of profile names with a
live gateway; ``runtimes``: {name: runtime dict}.
"""
import hermes_cli.profiles as profiles_mod
import gateway.status as status_mod
monkeypatch.setattr(profiles_mod, "profiles_to_serve", lambda multiplex: homes)
monkeypatch.setattr(
profiles_mod, "_check_gateway_running",
lambda home: next(n for n, h in homes if h == home) in running,
)
by_path = {home / "gateway_state.json": runtimes.get(name) for name, home in homes}
monkeypatch.setattr(
status_mod, "read_runtime_status", lambda path=None: by_path.get(path)
)
class TestCollectProfileGatewayTopology:
def test_no_gateways_running(self, tmp_path, monkeypatch):
homes = [("default", tmp_path / "d"), ("coder", tmp_path / "c")]
_patch_topology(monkeypatch, homes, running=set(), runtimes={})
topo = _collect_profile_gateway_topology()
assert topo["profiles"] == ["default", "coder"]
assert topo["gateway_mode"] == "none"
assert topo["gateways"] == []
def test_enumeration_failure_degrades_gracefully(self, monkeypatch):
import hermes_cli.profiles as profiles_mod
def _boom(multiplex):
raise RuntimeError("no profiles root")
monkeypatch.setattr(profiles_mod, "profiles_to_serve", _boom)
topo = _collect_profile_gateway_topology()
assert topo == {"profiles": [], "gateway_mode": "unknown", "gateways": []}
# ---------------------------------------------------------------------------
# /api/status wiring
# ---------------------------------------------------------------------------
class TestStatusEndpointTopology:
@pytest.fixture(autouse=True)
def _setup_client(self, monkeypatch, _isolate_hermes_home):
try:
from starlette.testclient import TestClient
except ImportError:
pytest.skip("fastapi/starlette not installed")
import hermes_state
from hermes_constants import get_hermes_home
from hermes_cli.web_server import app, _SESSION_HEADER_NAME, _SESSION_TOKEN
monkeypatch.setattr(
hermes_state, "DEFAULT_DB_PATH", get_hermes_home() / "state.db"
)
self.client = TestClient(app)
self.client.headers[_SESSION_HEADER_NAME] = _SESSION_TOKEN
def test_status_includes_full_topology_on_loopback(self, monkeypatch):
monkeypatch.setattr(
web_server, "_collect_profile_gateway_topology",
lambda: {
"profiles": ["default", "coder"],
"gateway_mode": "single",
"gateways": [{"profile": "default", "ports": {}}],
},
)
resp = self.client.get("/api/status")
assert resp.status_code == 200
data = resp.json()
assert data["profiles"] == ["default", "coder"]
assert data["gateway_mode"] == "single"
# The per-gateway detail (host ports) is loopback-only recon.
assert data["gateways"] == [{"profile": "default", "ports": {}}]
def test_profile_names_and_mode_public_when_auth_gated(self, monkeypatch):
# Profile NAMES + gateway_mode are low-sensitivity product surface: the
# Hermes Cloud Portal reads /api/status over the network (a gated bind)
# to render the profile list, so they must survive the auth gate.
monkeypatch.setattr(
web_server, "_collect_profile_gateway_topology",
lambda: {
"profiles": ["default", "coder"],
"gateway_mode": "multiplex",
"gateways": [{"profile": "default", "ports": {"webhook": 8644}}],
},
)
monkeypatch.setattr(web_server.app.state, "auth_required", True, raising=False)
try:
resp = self.client.get("/api/status")
assert resp.status_code == 200
data = resp.json()
assert data["profiles"] == ["default", "coder"]
assert data["gateway_mode"] == "multiplex"
# But the per-gateway detail (host ports = recon) stays gated,
# alongside hermes_home / gateway_pid.
assert "gateways" not in data
assert "hermes_home" not in data
assert "gateway_pid" not in data
finally:
monkeypatch.setattr(
web_server.app.state, "auth_required", False, raising=False
)