mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +00:00
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.
120 lines
3.9 KiB
Python
120 lines
3.9 KiB
Python
"""Tests for the pre_gateway_dispatch plugin hook.
|
|
|
|
The hook allows plugins to intercept incoming messages before auth and
|
|
agent dispatch. It runs in _handle_message and acts on returned action
|
|
dicts: {"action": "skip"|"rewrite"|"allow"}.
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import GatewayConfig, Platform, PlatformConfig
|
|
from gateway.platforms.base import MessageEvent
|
|
from gateway.session import SessionSource
|
|
|
|
|
|
def _clear_auth_env(monkeypatch) -> None:
|
|
for key in (
|
|
"TELEGRAM_ALLOWED_USERS",
|
|
"WHATSAPP_ALLOWED_USERS",
|
|
"GATEWAY_ALLOWED_USERS",
|
|
"TELEGRAM_ALLOW_ALL_USERS",
|
|
"WHATSAPP_ALLOW_ALL_USERS",
|
|
"GATEWAY_ALLOW_ALL_USERS",
|
|
):
|
|
monkeypatch.delenv(key, raising=False)
|
|
|
|
|
|
def _make_event(text: str = "hello", platform: Platform = Platform.WHATSAPP) -> MessageEvent:
|
|
return MessageEvent(
|
|
text=text,
|
|
message_id="m1",
|
|
source=SessionSource(
|
|
platform=platform,
|
|
user_id="15551234567@s.whatsapp.net",
|
|
chat_id="15551234567@s.whatsapp.net",
|
|
user_name="tester",
|
|
chat_type="dm",
|
|
),
|
|
)
|
|
|
|
|
|
def _make_runner(platform: Platform):
|
|
from gateway.run import GatewayRunner
|
|
|
|
config = GatewayConfig(
|
|
platforms={platform: PlatformConfig(enabled=True)},
|
|
)
|
|
runner = object.__new__(GatewayRunner)
|
|
runner.config = config
|
|
adapter = SimpleNamespace(send=AsyncMock())
|
|
runner.adapters = {platform: adapter}
|
|
runner.pairing_store = MagicMock()
|
|
runner.pairing_store.is_approved.return_value = False
|
|
runner.pairing_store._is_rate_limited.return_value = False
|
|
runner.session_store = MagicMock()
|
|
runner._running_agents = {}
|
|
runner._update_prompt_pending = {}
|
|
return runner, adapter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_internal_events_bypass_hook(monkeypatch):
|
|
"""Internal events (event.internal=True) skip the plugin hook entirely."""
|
|
_clear_auth_env(monkeypatch)
|
|
monkeypatch.setenv("WHATSAPP_ALLOWED_USERS", "*")
|
|
|
|
called = {"count": 0}
|
|
|
|
def _fake_hook(name, **kwargs):
|
|
called["count"] += 1
|
|
return [{"action": "skip"}]
|
|
|
|
async def _capture(event, source, _quick_key, _run_generation):
|
|
return "ok"
|
|
|
|
monkeypatch.setattr("hermes_cli.plugins.invoke_hook", _fake_hook)
|
|
|
|
runner, _adapter = _make_runner(Platform.WHATSAPP)
|
|
runner._handle_message_with_agent = _capture # noqa: SLF001
|
|
|
|
event = _make_event("hi")
|
|
event.internal = True
|
|
|
|
# Even though the hook would say skip, internal events bypass it.
|
|
await runner._handle_message(event)
|
|
assert called["count"] == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_hook_fires_without_session_store_attribute(monkeypatch):
|
|
"""A runner missing session_store still delivers the event to plugins.
|
|
|
|
Regression: the hook kwargs read ``self.session_store`` directly, so a
|
|
partially-initialized runner raised AttributeError inside the dispatch
|
|
try-block — the hook never fired, and every message logged
|
|
"pre_gateway_dispatch invocation failed: 'GatewayRunner' object has no
|
|
attribute 'session_store'". Plugins must receive the event (with
|
|
session_store=None) instead.
|
|
"""
|
|
_clear_auth_env(monkeypatch)
|
|
|
|
seen = {}
|
|
|
|
def _fake_hook(name, **kwargs):
|
|
if name == "pre_gateway_dispatch":
|
|
seen["session_store"] = kwargs.get("session_store", "MISSING")
|
|
return [{"action": "skip", "reason": "plugin-handled"}]
|
|
return []
|
|
|
|
monkeypatch.setattr("hermes_cli.plugins.invoke_hook", _fake_hook)
|
|
|
|
runner, adapter = _make_runner(Platform.WHATSAPP)
|
|
del runner.session_store
|
|
|
|
result = await runner._handle_message(_make_event("hi"))
|
|
assert result is None
|
|
# Hook actually fired (skip short-circuited before auth) with a None store.
|
|
assert seen == {"session_store": None}
|
|
adapter.send.assert_not_awaited()
|