d10bb2ab6f
`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.
169 lines
5.3 KiB
Python
169 lines
5.3 KiB
Python
"""Local CPR leak reproduction + classic-CLI Application output selection.
|
|
|
|
* Deterministic local-PTY proof that delayed CPR replies leak as
|
|
``ESC[row;colR`` / ``^[[row;colR`` when ``enable_cpr=True`` (no SSH).
|
|
* Integration-level assertion that, with no SSH env vars, classic CLI
|
|
output selection wires a CPR-disabled Output into Application on POSIX.
|
|
* Native Windows keeps prompt_toolkit's default output selection.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import select
|
|
import sys
|
|
import threading
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from cli import (
|
|
_build_cpr_disabled_output,
|
|
_select_classic_cli_pt_output,
|
|
_terminal_may_leak_cpr,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clear_cpr_env(monkeypatch):
|
|
for var in ("SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY", "PROMPT_TOOLKIT_NO_CPR"):
|
|
monkeypatch.delenv(var, raising=False)
|
|
|
|
|
|
class TestClassicCliOutputSelection:
|
|
|
|
|
|
@pytest.mark.windows_only
|
|
def test_windows_preserves_default_output_selection(self):
|
|
assert _terminal_may_leak_cpr() is False
|
|
assert _select_classic_cli_pt_output(sys.stdout) is None
|
|
|
|
@pytest.mark.windows_only
|
|
def test_windows_honors_explicit_no_cpr(self, monkeypatch):
|
|
monkeypatch.setenv("PROMPT_TOOLKIT_NO_CPR", "1")
|
|
assert _terminal_may_leak_cpr() is True
|
|
out = _select_classic_cli_pt_output(sys.stdout)
|
|
# Build may return None if stdout is not a real tty in CI; if it
|
|
# succeeds it must be CPR-disabled.
|
|
assert out is None or out.enable_cpr is False
|
|
|
|
|
|
def _openpty_or_skip():
|
|
import pty
|
|
|
|
try:
|
|
return pty.openpty()
|
|
except OSError as exc:
|
|
pytest.skip(f"no PTY devices available: {exc}")
|
|
|
|
|
|
@pytest.mark.skipif(sys.platform == "win32", reason="POSIX PTY harness")
|
|
class TestDelayedCprLocalPtyLeak:
|
|
def test_delayed_cpr_reply_leaks_when_enable_cpr_true(self):
|
|
"""Local (no SSH) delayed ESC[6n reply lands as ESC[39;1R on stdin."""
|
|
import tty
|
|
|
|
from prompt_toolkit.data_structures import Size
|
|
from prompt_toolkit.output.vt100 import Vt100_Output
|
|
|
|
master, slave = _openpty_or_skip()
|
|
try:
|
|
tty.setraw(slave)
|
|
slave_w = os.fdopen(os.dup(slave), "w", buffering=1)
|
|
stop = threading.Event()
|
|
queries = 0
|
|
|
|
def terminal() -> None:
|
|
nonlocal queries
|
|
buf = b""
|
|
while not stop.is_set():
|
|
try:
|
|
r, _, _ = select.select([master], [], [], 0.05)
|
|
except OSError:
|
|
break
|
|
if not r:
|
|
continue
|
|
try:
|
|
chunk = os.read(master, 4096)
|
|
except OSError:
|
|
break
|
|
if not chunk:
|
|
break
|
|
buf += chunk
|
|
while True:
|
|
idx = buf.find(b"\x1b[6n")
|
|
if idx < 0:
|
|
buf = buf[-8:] if len(buf) > 8 else buf
|
|
break
|
|
buf = buf[idx + 4 :]
|
|
queries += 1
|
|
time.sleep(0.12)
|
|
try:
|
|
os.write(master, b"\x1b[39;1R")
|
|
except OSError:
|
|
pass
|
|
|
|
threading.Thread(target=terminal, daemon=True).start()
|
|
out = Vt100_Output(
|
|
slave_w, lambda: Size(rows=40, columns=80), enable_cpr=True
|
|
)
|
|
out.ask_for_cpr()
|
|
out.flush()
|
|
for i in range(4):
|
|
slave_w.write(f"\rgpt-5.6-sol Q {i}\n")
|
|
slave_w.flush()
|
|
time.sleep(0.02)
|
|
time.sleep(0.3)
|
|
|
|
data = b""
|
|
while True:
|
|
r, _, _ = select.select([slave], [], [], 0.05)
|
|
if not r:
|
|
break
|
|
data += os.read(slave, 4096)
|
|
|
|
stop.set()
|
|
slave_w.close()
|
|
|
|
assert queries >= 1
|
|
assert b"\x1b[39;1R" in data
|
|
finally:
|
|
try:
|
|
os.close(slave)
|
|
except OSError:
|
|
pass
|
|
try:
|
|
os.close(master)
|
|
except OSError:
|
|
pass
|
|
|
|
def test_cpr_disabled_output_sends_no_query(self):
|
|
"""Hermes CPR-disabled builder must not emit ESC[6n."""
|
|
master, slave = _openpty_or_skip()
|
|
try:
|
|
slave_w = os.fdopen(slave, "w", buffering=1)
|
|
out = _build_cpr_disabled_output(slave_w)
|
|
assert out is not None
|
|
assert out.enable_cpr is False
|
|
|
|
seen = b""
|
|
|
|
def reader() -> None:
|
|
nonlocal seen
|
|
r, _, _ = select.select([master], [], [], 0.25)
|
|
if r:
|
|
seen = os.read(master, 4096)
|
|
|
|
threading.Thread(target=reader, daemon=True).start()
|
|
slave_w.write("status ok\n")
|
|
slave_w.flush()
|
|
# Do not call ask_for_cpr — renderer skips it when NOT_SUPPORTED.
|
|
time.sleep(0.3)
|
|
slave_w.close()
|
|
assert b"\x1b[6n" not in seen
|
|
finally:
|
|
try:
|
|
os.close(master)
|
|
except OSError:
|
|
pass
|