Files
hermes-agent/tests/hermes_cli/test_code_skew.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

164 lines
6.4 KiB
Python

"""Tests for gateway code-skew detection (stale-checkout guard).
Companion to ``tests/test_stale_utils_module_import.py``: that test proves the
crash; these prove the guard that turns it into a clear "restart the gateway"
message before a model switch can hit it. The dashboard mirror (#86207) lives
here too: ``web_server_config._dashboard_code_skew_guard`` and the
``/api/model/options`` 503 guard, which protect the Models page from the same
stale-module ImportError after ``hermes update``.
"""
import asyncio
import contextlib
import pytest
from gateway import code_skew
import hermes_cli.web_routers.models as _rt_models
import hermes_cli.web_server_config as _web_server_config
import hermes_cli.web_server_profiles as _web_server_profiles
@pytest.fixture(autouse=True)
def _reset_boot_fingerprint(monkeypatch):
"""Each test starts with no recorded boot fingerprint."""
monkeypatch.setattr(code_skew, "_boot_fingerprint", None)
class TestDetectCodeSkew:
def test_no_boot_fingerprint_means_no_skew(self, monkeypatch):
# Nothing recorded (e.g. non-git install) -> never a false positive.
monkeypatch.setattr(code_skew, "_fingerprint", lambda: "git:refs/heads/main:def456")
assert code_skew.detect_code_skew() is None
def test_drift_is_detected_with_short_revs(self, monkeypatch):
monkeypatch.setattr(code_skew, "_fingerprint", lambda: "git:refs/heads/main:abc1234567890")
code_skew.record_boot_fingerprint()
monkeypatch.setattr(code_skew, "_fingerprint", lambda: "git:refs/heads/main:def4567890123")
skew = code_skew.detect_code_skew()
assert skew == ("abc1234567", "def4567890")
class TestShort:
def test_shortens_long_sha(self):
assert code_skew._short("git:refs/heads/main:abcdef0123456789") == "abcdef0123"
def test_keeps_unresolved_marker(self):
assert code_skew._short("git:refs/heads/main:unresolved") == "unresolved"
def test_passes_short_sha_through_untruncated(self):
assert code_skew._short("git:HEAD:abc1234") == "abc1234"
class TestModelSwitchSkewGuard:
def test_guard_returns_none_without_skew(self, monkeypatch):
from gateway import slash_commands_model as slash_commands
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: None)
assert slash_commands._model_switch_skew_guard() is None
def test_guard_message_names_revs_and_restart(self, monkeypatch):
from gateway import slash_commands_model as slash_commands
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: ("abc1234567", "def4567890"))
msg = slash_commands._model_switch_skew_guard()
assert msg is not None
assert "abc1234567" in msg
assert "def4567890" in msg
assert "hermes gateway restart" in msg
class TestDashboardCodeSkewGuard:
"""Dashboard mirror of the gateway's model-switch skew guard (#86207)."""
def test_dashboard_guard_returns_none_without_skew(self, monkeypatch):
from hermes_cli import web_server
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: None)
assert _web_server_config._dashboard_code_skew_guard() is None
def test_dashboard_guard_message_names_revs_and_restart(self, monkeypatch):
from hermes_cli import web_server
monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: ("abc1234567", "def4567890"))
msg = _web_server_config._dashboard_code_skew_guard()
assert msg is not None
assert "abc1234567" in msg
assert "def4567890" in msg
assert "restart" in msg.lower()
# Browser-dashboard path: never hardcode a Linux-only unit (#97046).
assert "systemctl" not in msg
def test_serve_guard_message_points_at_desktop_backend(self, monkeypatch):
from hermes_cli import web_server
monkeypatch.setenv("HERMES_SERVE_HEADLESS", "1")
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: ("abc1234567", "def4567890"))
msg = _web_server_config._dashboard_code_skew_guard()
assert msg is not None
assert "Desktop-owned backend" in msg
assert "systemctl" not in msg
assert "hermes-dashboard" not in msg
class TestModelOptionsSkewGuard:
"""/api/model/options must refuse with a clear 503 when the dashboard is stale.
Regression for #86207: a dashboard kept alive across ``hermes update``
serves stale modules, so the picker's lazy import of names the update added
(``agent.model_metadata.is_grok_46_family``) raised ImportError and the
handler collapsed it into a generic 500. With the guard, the stale process
surfaces "Restart required" (503) and never reaches the payload build.
"""
def test_stale_dashboard_returns_503_and_skips_payload_build(self, monkeypatch):
from fastapi import HTTPException
from hermes_cli import web_server
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: ("abc1234567", "def4567890"))
payload_calls: list = []
monkeypatch.setattr(
"hermes_cli.inventory.build_model_options_payload",
lambda *a, **k: payload_calls.append(1) or {"providers": []},
)
with pytest.raises(HTTPException) as excinfo:
asyncio.run(_rt_models.get_model_options())
assert excinfo.value.status_code == 503
assert "restart" in str(excinfo.value.detail).lower()
# The stale-import crash site (the payload build) is never reached.
assert payload_calls == []
def test_fresh_dashboard_builds_payload_unchanged(self, monkeypatch):
from hermes_cli import web_server
monkeypatch.setattr(code_skew, "detect_code_skew", lambda: None)
async def _fake_run_in_threadpool(func):
return func()
monkeypatch.setattr(_rt_models, "run_in_threadpool", _fake_run_in_threadpool)
monkeypatch.setattr(
_web_server_profiles, "_profile_scope", lambda profile: contextlib.nullcontext()
)
monkeypatch.setattr("hermes_cli.inventory.load_picker_context", lambda: {})
payload_calls: list = []
expected = {"providers": [], "model": {}, "provider": None}
monkeypatch.setattr(
"hermes_cli.inventory.build_model_options_payload",
lambda *a, **k: payload_calls.append(1) or expected,
)
result = asyncio.run(_rt_models.get_model_options())
assert result == expected
assert payload_calls == [1]