`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.
137 lines
4.9 KiB
Python
137 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
|