hermes-agent/tests/cli/test_bracketed_paste_timeout.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

136 lines
4.9 KiB
Python

"""Tests for bracketed-paste timeout safety valve (#16263).
Verifies the production helper in cli.py monkey-patches prompt_toolkit's
Vt100Parser.feed() so the parser auto-escapes from bracketed-paste mode when
the ESC[201~ end mark is never received.
"""
import ast
import importlib
import logging
import time
from pathlib import Path
from unittest.mock import MagicMock
from prompt_toolkit.keys import Keys
ROOT = Path(__file__).resolve().parents[2]
CLI_PATH = ROOT / "cli.py"
def _load_production_patch_helper():
"""Load cli._apply_bracketed_paste_timeout_patch without importing cli.
Importing cli.py pulls optional runtime deps that aren't required for this
parser-level regression. AST-loading the exact helper keeps the test tied
to production code while avoiding unrelated import side effects. If the
production helper is removed, this test fails.
"""
source = CLI_PATH.read_text(encoding="utf-8")
tree = ast.parse(source)
helper_node = next(
(
node
for node in tree.body
if isinstance(node, ast.FunctionDef)
and node.name == "_apply_bracketed_paste_timeout_patch"
),
None,
)
assert helper_node is not None, (
"cli.py must define _apply_bracketed_paste_timeout_patch()"
)
helper_source = ast.get_source_segment(source, helper_node)
namespace = {"time": time, "logger": logging.getLogger("test.cli")}
exec(helper_source, namespace)
return namespace["_apply_bracketed_paste_timeout_patch"]
def _reset_and_apply_production_patch():
"""Reload prompt_toolkit's parser and apply Hermes' production patch."""
import prompt_toolkit.input.vt100_parser as vt100_mod
vt100_mod = importlib.reload(vt100_mod)
# importlib.reload() preserves module dict entries that the reloaded source
# does not redefine, so clear Hermes' sentinel before re-applying.
if hasattr(vt100_mod, "_hermes_bp_timeout_patched"):
delattr(vt100_mod, "_hermes_bp_timeout_patched")
_load_production_patch_helper()()
assert getattr(vt100_mod, "_hermes_bp_timeout_patched", False)
return vt100_mod
class TestBracketedPasteTimeout:
"""Verify the Vt100Parser monkey-patch prevents frozen bracketed-paste."""
def _make_parser(self):
"""Create a Vt100Parser after applying the production patch."""
vt100_mod = _reset_and_apply_production_patch()
callback = MagicMock()
parser = vt100_mod.Vt100Parser(callback)
return parser, callback
def test_normal_bracketed_paste_works(self):
"""A complete bracketed-paste sequence should work normally."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~hello world\x1b[201~")
callback.assert_called_once()
call_args = callback.call_args[0][0]
assert call_args.data == "hello world"
def test_incomplete_paste_times_out(self):
"""If ESC[201~ is never received, parser should recover after timeout."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~some pasted text")
assert parser._in_bracketed_paste
parser._hermes_bp_start = time.monotonic() - 3.0
parser.feed("more data")
assert not parser._in_bracketed_paste
assert callback.called
def test_normal_keys_after_timeout_recovery(self):
"""After timeout recovery, normal key processing should resume."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~stuck")
parser._hermes_bp_start = time.monotonic() - 3.0
parser.feed("")
assert not parser._in_bracketed_paste
callback.reset_mock()
parser.feed("a")
assert not parser._in_bracketed_paste
def test_subsequent_data_after_incomplete_paste(self):
"""Data arriving after a stuck paste should be processable."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~content")
parser._hermes_bp_start = time.monotonic() - 5.0
parser.feed("x")
assert not parser._in_bracketed_paste
assert callback.call_count >= 1
def test_torn_end_mark_recovers(self):
"""If end mark arrives split across feeds within timeout, it still works."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~some content\x1b[20")
assert parser._in_bracketed_paste
parser.feed("1~")
assert not parser._in_bracketed_paste
callback.assert_called_once()
assert callback.call_args[0][0].data == "some content"
def test_no_timeout_under_threshold(self):
"""Bracketed-paste mode should not timeout within the 2s window."""
parser, callback = self._make_parser()
parser.feed("\x1b[200~waiting")
parser._hermes_bp_start = time.monotonic() - 0.5
parser.feed("more waiting")
assert parser._in_bracketed_paste
assert not callback.called