mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +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.
178 lines
6.1 KiB
Python
178 lines
6.1 KiB
Python
"""Tests for the WS-upgrade ticket store (Phase 5 task 5.1).
|
|
|
|
The store is process-local and threading-safe. Tests run with xdist so
|
|
each worker has its own module instance — no cross-worker bleed — but we
|
|
call ``_reset_for_tests`` between tests to keep things deterministic.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
|
|
import pytest
|
|
|
|
from hermes_cli.dashboard_auth import ws_tickets
|
|
from hermes_cli.dashboard_auth.ws_tickets import (
|
|
TTL_SECONDS,
|
|
TicketInvalid,
|
|
_reset_for_tests,
|
|
consume_ticket,
|
|
mint_ticket,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset():
|
|
_reset_for_tests()
|
|
yield
|
|
_reset_for_tests()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Happy path
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestMintAndConsume:
|
|
def test_round_trip(self):
|
|
ticket = mint_ticket(user_id="u1", provider="nous")
|
|
info = consume_ticket(ticket)
|
|
assert info["user_id"] == "u1"
|
|
assert info["provider"] == "nous"
|
|
assert "minted_at" in info
|
|
|
|
def test_ticket_has_minimum_length(self):
|
|
# ``secrets.token_urlsafe(32)`` produces ~43 chars; enforce a floor
|
|
# so a future refactor can't accidentally shrink the entropy.
|
|
ticket = mint_ticket(user_id="u1", provider="nous")
|
|
assert len(ticket) >= 32
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Single-use
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestSingleUse:
|
|
def test_second_consume_raises(self):
|
|
ticket = mint_ticket(user_id="u1", provider="stub")
|
|
consume_ticket(ticket)
|
|
with pytest.raises(TicketInvalid, match="unknown"):
|
|
consume_ticket(ticket)
|
|
|
|
def test_unknown_ticket_rejected(self):
|
|
with pytest.raises(TicketInvalid, match="unknown"):
|
|
consume_ticket("nope-never-minted")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# TTL
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestTTL:
|
|
def test_constant_is_30_seconds(self):
|
|
# Pinned so a refactor that doubled the lifetime would surface here.
|
|
assert TTL_SECONDS == 30
|
|
|
|
def test_expired_ticket_rejected(self, monkeypatch):
|
|
# Mock time inside the ws_tickets module so mint and consume see
|
|
# different clocks. We have to patch the symbol the module actually
|
|
# binds; ``time`` is module-level there.
|
|
clock = {"now": 1_000_000}
|
|
|
|
def fake_time():
|
|
return clock["now"]
|
|
|
|
monkeypatch.setattr(ws_tickets.time, "time", fake_time)
|
|
|
|
ticket = mint_ticket(user_id="u1", provider="stub")
|
|
clock["now"] += TTL_SECONDS + 1
|
|
with pytest.raises(TicketInvalid, match="expired"):
|
|
consume_ticket(ticket)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Truncated value in error message (secret hygiene)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestErrorMessages:
|
|
def test_unknown_ticket_error_truncates_value(self):
|
|
long_value = "a" * 100
|
|
with pytest.raises(TicketInvalid) as exc_info:
|
|
consume_ticket(long_value)
|
|
# Never log more than the first 8 chars of an opaque ticket.
|
|
message = str(exc_info.value)
|
|
assert long_value not in message
|
|
assert long_value[:8] in message
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Thread safety: mint + consume from many threads doesn't deadlock or
|
|
# return duplicates.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestConcurrency:
|
|
def test_mint_and_consume_concurrent(self):
|
|
results: list[dict] = []
|
|
errors: list[Exception] = []
|
|
lock = threading.Lock()
|
|
|
|
def worker(i: int):
|
|
try:
|
|
t = mint_ticket(user_id=f"u{i}", provider="stub")
|
|
info = consume_ticket(t)
|
|
with lock:
|
|
results.append(info)
|
|
except Exception as exc: # noqa: BLE001 — collect for assert
|
|
with lock:
|
|
errors.append(exc)
|
|
|
|
threads = [threading.Thread(target=worker, args=(i,)) for i in range(20)]
|
|
for t in threads:
|
|
t.start()
|
|
for t in threads:
|
|
t.join(timeout=5.0)
|
|
assert not t.is_alive(), "thread deadlocked"
|
|
|
|
assert errors == []
|
|
assert len(results) == 20
|
|
# Every consume returns a distinct user_id (no cross-thread bleed).
|
|
assert {r["user_id"] for r in results} == {f"u{i}" for i in range(20)}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Process-lifetime internal credential (server-spawned PTY child auth).
|
|
# Direct unit coverage for internal_ws_credential / consume_internal_credential
|
|
# — _ws_auth_ok exercises these indirectly, but the mint-once, unminted, and
|
|
# empty-value branches are only reachable via direct calls.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestInternalCredential:
|
|
|
|
|
|
|
|
def test_reset_clears_and_remints(self):
|
|
first = ws_tickets.internal_ws_credential()
|
|
_reset_for_tests()
|
|
# The old value no longer validates after reset.
|
|
with pytest.raises(TicketInvalid):
|
|
ws_tickets.consume_internal_credential(first)
|
|
# A fresh mint produces a different value.
|
|
second = ws_tickets.internal_ws_credential()
|
|
assert second != first
|
|
assert ws_tickets.consume_internal_credential(second)["user_id"] == (
|
|
ws_tickets.INTERNAL_USER_ID
|
|
)
|
|
|
|
def test_independent_of_ticket_store(self):
|
|
"""The internal credential is not a ticket — minting tickets doesn't
|
|
touch it, and consuming the credential doesn't consume tickets."""
|
|
cred = ws_tickets.internal_ws_credential()
|
|
ticket = mint_ticket(user_id="u1", provider="nous")
|
|
# Consuming the internal credential leaves the ticket intact.
|
|
ws_tickets.consume_internal_credential(cred)
|
|
assert consume_ticket(ticket)["user_id"] == "u1"
|