hermes-agent/tests/tui_gateway/test_tui_gateway_loop_noise.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

88 lines
2.5 KiB
Python

"""Tests for tui_gateway.loop_noise — the WS peer-hangup teardown filter (#50005)."""
from __future__ import annotations
import asyncio
import pytest
from tui_gateway.loop_noise import (
_is_benign_teardown,
install_loop_noise_filter,
)
class _FakeConnectionLostCallback:
"""Stand-in whose repr matches asyncio's ``_call_connection_lost`` flood."""
def __repr__(self) -> str:
return "<Handle _ProactorBasePipeTransport._call_connection_lost(None)>"
def test_benign_teardown_matches_reset_in_connection_lost():
ctx = {
"exception": ConnectionResetError(10054, "forcibly closed"),
"handle": _FakeConnectionLostCallback(),
}
assert _is_benign_teardown(ctx) is True
def test_benign_teardown_matches_aborted_and_broken_pipe():
for exc in (
ConnectionAbortedError(10053, "aborted"),
BrokenPipeError("epipe"),
):
ctx = {"exception": exc, "callback": _FakeConnectionLostCallback()}
assert _is_benign_teardown(ctx) is True
def test_reset_outside_connection_lost_is_not_suppressed():
# Same error type, but NOT from the connection-lost teardown path — must
# fall through to the default handler.
ctx = {
"exception": ConnectionResetError("reset in a real handler"),
"handle": "<Handle some_other_handler()>",
}
assert _is_benign_teardown(ctx) is False
def test_unrelated_exception_is_not_suppressed():
ctx = {
"exception": ValueError("boom"),
"handle": _FakeConnectionLostCallback(),
}
assert _is_benign_teardown(ctx) is False
def test_no_exception_is_not_suppressed():
assert _is_benign_teardown({"message": "loop warning, no exc"}) is False
def test_install_suppresses_flood_and_forwards_real_errors():
loop = asyncio.new_event_loop()
try:
forwarded: list[dict] = []
loop.set_exception_handler(lambda _loop, ctx: forwarded.append(ctx))
install_loop_noise_filter(loop)
# Benign teardown flood → swallowed, not forwarded.
loop.call_exception_handler(
{
"exception": ConnectionResetError(10054, "forcibly closed"),
"handle": _FakeConnectionLostCallback(),
}
)
assert forwarded == []
# Genuine loop error → forwarded to the previous handler unchanged.
real_ctx = {"exception": RuntimeError("genuine loop bug")}
loop.call_exception_handler(real_ctx)
assert len(forwarded) == 1
assert forwarded[0] is real_ctx
finally:
loop.close()