hermes-agent/tests/gateway/test_scale_to_zero.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

91 lines
2.8 KiB
Python

"""Unit tests for the scale-to-zero idle-detection pure logic (Phase 0).
Behaviour-contract tests (AGENTS.md): each conjunct of the idle predicate and
each clause of the arm-gate is exercised independently, not frozen against a
snapshot. The pure helpers in gateway/scale_to_zero.py take plain inputs so they
test without a live gateway.
"""
from __future__ import annotations
import pytest
from gateway.scale_to_zero import (
DEFAULT_IDLE_TIMEOUT_MINUTES,
SCALE_TO_ZERO_ENV,
is_idle,
messaging_is_relay_only_or_absent,
parse_idle_timeout_seconds,
scale_to_zero_enabled,
should_arm,
)
# ── scale_to_zero_enabled (the Labs HERMES_SCALE_TO_ZERO stamp, D11/Q8=A) ────
@pytest.mark.parametrize("value", ["1", "true", "TRUE", "yes", "on", " On "])
def test_enabled_truthy_values(value):
assert scale_to_zero_enabled({SCALE_TO_ZERO_ENV: value}) is True
# ── parse_idle_timeout_seconds (config.yaml, D2) ─────────────────────────────
def test_timeout_parses_minutes_to_seconds():
assert parse_idle_timeout_seconds(5) == 300.0
assert parse_idle_timeout_seconds(10) == 600.0
assert parse_idle_timeout_seconds("5") == 300.0
# ── messaging_is_relay_only_or_absent (F6/D1) ────────────────────────────────
class _P:
"""Stand-in for a Platform enum member with a ``.value``."""
def __init__(self, value):
self.value = value
def test_relay_only_is_true():
assert messaging_is_relay_only_or_absent([_P("relay")]) is True
def test_no_platform_is_true():
# A Chronos-only / no-messaging-platform agent can scale to zero.
assert messaging_is_relay_only_or_absent([]) is True
# ── should_arm (D1/D11/§3.4(1)) ──────────────────────────────────────────────
def test_arm_blocked_without_wake_url():
# A suspended instance with no wake target is a black hole (§3.4(1)).
assert should_arm(enabled=True, relay_only_or_absent=True, wake_url=None) is False
assert should_arm(enabled=True, relay_only_or_absent=True, wake_url="") is False
# ── is_idle (D2/D3/F7) — each conjunct flips the result ──────────────────────
def _idle_kwargs(**over):
base = dict(
running_agent_count=0,
seconds_since_last_inbound=600.0,
idle_timeout_seconds=300.0,
has_live_background_work=False,
)
base.update(over)
return base
def test_not_idle_with_running_agent():
assert is_idle(**_idle_kwargs(running_agent_count=1)) is False
def test_idle_exactly_at_threshold():
# >= timeout is idle (boundary).
assert is_idle(**_idle_kwargs(seconds_since_last_inbound=300.0)) is True