Files
hermes-agent/tests/hermes_cli/test_ctrl_enter_newline.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

249 lines
8.5 KiB
Python

"""Regression tests for issue #22379 — Ctrl+Enter newline over SSH/WSL.
prompt_toolkit treats c-j (LF) as Enter on POSIX so thin PTYs (docker exec,
some BSD ssh) that send LF for plain Enter still work. But Windows Terminal
(native, WSL, and SSH-forwarded sessions) sends Ctrl+Enter as bare LF — same
byte. Without environment-aware gating, binding c-j to submit means
Ctrl+Enter submits instead of inserting a newline.
These tests pin the gating predicate and the resulting binding behavior.
``_preserve_ctrl_enter_newline()`` short-circuits to True on native Windows
before it ever looks at the environment, so the env-driven cases below are
POSIX assertions and run on the Linux job. The native-Windows short-circuit
is marked ``windows_only`` and asserted on the real host — patching
``sys.platform`` to ``"win32"`` here would only re-assert the literal in the
``if``, on an interpreter where none of the Windows terminal behaviour it
exists for is present.
"""
from __future__ import annotations
import os
from unittest.mock import patch
import pytest
import sys
class FakeKeyBindings:
def __init__(self):
self.bound = []
def add(self, *keys, **_kwargs):
def _decorator(handler):
self.bound.append(keys)
return handler
return _decorator
def _bind_submit_keys_for_local_linux(cli_mod, *, multiline_shortcuts_enabled):
with patch.object(sys, "platform", "linux"):
with patch.dict(os.environ, {}, clear=True):
with patch("builtins.open", side_effect=OSError("no /proc")):
kb = FakeKeyBindings()
cli_mod._bind_prompt_submit_keys(
kb,
lambda _event: None,
multiline_shortcuts_enabled=multiline_shortcuts_enabled,
)
return kb
@pytest.mark.windows_only
def test_native_windows_preserves_newline():
import cli as cli_mod
assert cli_mod._preserve_ctrl_enter_newline() is True
def test_ssh_tty_alone_preserves_newline():
import cli as cli_mod
# Strip out anything that might leak truth
with patch.dict(os.environ, {"SSH_TTY": "/dev/pts/0"}, clear=True):
assert cli_mod._preserve_ctrl_enter_newline() is True
def test_windows_terminal_session_preserves_newline():
import cli as cli_mod
with patch.dict(os.environ, {"WT_SESSION": "abc-def"}, clear=True):
assert cli_mod._preserve_ctrl_enter_newline() is True
def test_ghostty_tmux_session_preserves_ctrl_j_newline():
"""Ghostty-inherited env survives tmux even when TERM_PROGRAM becomes tmux."""
import cli as cli_mod
with patch.dict(
os.environ,
{"TERM": "tmux-256color", "TERM_PROGRAM": "tmux", "GHOSTTY_RESOURCES_DIR": "/usr/share/ghostty"},
clear=True,
):
assert cli_mod._preserve_ctrl_enter_newline() is True
def test_cli_multiline_shortcuts_default_on():
"""Hermes should default to the common harness behavior: Ctrl+J newline.
Claude Code documents Ctrl+J as a no-setup newline shortcut, OpenCode's
default input_newline includes ctrl+j, and Codex exposes Ctrl+J/keymap
newline behavior. Keep Hermes aligned unless the user opts out.
"""
import cli as cli_mod
assert cli_mod._cli_multiline_shortcuts_enabled({"display": {}}) is True
def test_cli_multiline_shortcuts_can_be_disabled():
import cli as cli_mod
assert cli_mod._cli_multiline_shortcuts_enabled(
{"display": {"cli_multiline_shortcuts": False}}
) is False
def test_ctrl_j_is_not_submit_when_multiline_shortcuts_enabled():
"""With the default setting, c-j is reserved for the newline handler.
This fixes local terminals like iTerm2 where Ctrl+J reaches prompt_toolkit
as c-j but the legacy POSIX fallback bound it to submit.
"""
import cli as cli_mod
kb = _bind_submit_keys_for_local_linux(
cli_mod,
multiline_shortcuts_enabled=True,
)
assert ("enter",) in kb.bound
assert ("c-j",) not in kb.bound
def test_ctrl_j_legacy_submit_when_multiline_shortcuts_disabled():
"""Users can opt out to preserve Enter-as-LF submit fallback on odd PTYs."""
import cli as cli_mod
kb = _bind_submit_keys_for_local_linux(
cli_mod,
multiline_shortcuts_enabled=False,
)
assert ("enter",) in kb.bound
assert ("c-j",) in kb.bound
def test_backslash_enter_continuation_replaces_marker_with_newline():
import cli as cli_mod
assert cli_mod._apply_backslash_line_continuation("first line\\") == "first line\n"
assert cli_mod._apply_backslash_line_continuation("first line\\ ") == "first line\n"
def test_iterm_is_allowlisted_for_extended_enter_keys():
"""iTerm2 needs the app to request extended keys before Shift+Enter is distinct."""
import cli as cli_mod
assert cli_mod._terminal_supports_extended_enter_keys({"TERM_PROGRAM": "iTerm.app"}) is True
def test_unknown_terminal_does_not_enable_extended_enter_keys():
import cli as cli_mod
assert cli_mod._terminal_supports_extended_enter_keys({"TERM_PROGRAM": "unknown"}) is False
# ---------------------------------------------------------------------------
# Ghostty: must push ONLY modifyOtherKeys, not the Kitty keyboard protocol —
# see cli._is_ghostty_terminal for the full rationale (#87630).
# ---------------------------------------------------------------------------
class _FakeOutput:
"""Minimal output object with write_raw + flush for _enable_extended_enter_keys."""
def __init__(self):
self.written = b""
def write_raw(self, data):
self.written += data.encode() if isinstance(data, str) else data
def flush(self):
pass
def test_ghostty_uses_modify_other_keys_only():
"""Ghostty must NOT push the Kitty keyboard protocol (CSI >1u)."""
import cli as cli_mod
out = _FakeOutput()
result = cli_mod._enable_extended_enter_keys(
output=out,
env={"TERM_PROGRAM": "ghostty", "TERM": "xterm-ghostty"},
)
assert result is True
# Must contain modifyOtherKeys push ...
assert b"\x1b[>4;2m" in out.written
# ... but NOT the Kitty protocol push.
assert b"\x1b[>1u" not in out.written
def test_ghostty_via_term_var_uses_modify_other_keys_only():
"""xterm-ghostty TERM (without TERM_PROGRAM) also skips Kitty protocol."""
import cli as cli_mod
out = _FakeOutput()
result = cli_mod._enable_extended_enter_keys(
output=out,
env={"TERM": "xterm-ghostty"},
)
assert result is True
assert b"\x1b[>4;2m" in out.written
assert b"\x1b[>1u" not in out.written
def test_non_ghostty_terminals_still_push_kitty_protocol():
"""iTerm2 and others still get the full dual-protocol push."""
import cli as cli_mod
out = _FakeOutput()
result = cli_mod._enable_extended_enter_keys(
output=out,
env={"TERM_PROGRAM": "iTerm.app", "TERM": "xterm-256color"},
)
assert result is True
assert b"\x1b[>1u" in out.written
assert b"\x1b[>4;2m" in out.written
@pytest.mark.linux_only
def test_proc_version_microsoft_marker_preserves_newline():
"""WSL detection via /proc when env vars are scrubbed (sudo etc.).
``linux_only``: the fallback reads ``/proc/version`` — a Linux-only
interface, and the WSL kernels it sniffs for are Linux kernels.
"""
import cli as cli_mod
from io import StringIO
with patch.dict(os.environ, {}, clear=True):
real_open = open
def _fake_open(path, *args, **kwargs):
if "/proc/version" in str(path) or "/proc/sys/kernel/osrelease" in str(path):
return StringIO("Linux version 5.15.167.4-microsoft-standard-WSL2")
return real_open(path, *args, **kwargs)
with patch("builtins.open", side_effect=_fake_open):
assert cli_mod._preserve_ctrl_enter_newline() is True
# ---------------------------------------------------------------------------
# install_ctrl_enter_alias() — ANSI sequence mappings for enhanced terminals
# ---------------------------------------------------------------------------
def test_is_ghostty_terminal_detection_paths():
"""_is_ghostty_terminal matches exactly the two allowlist conditions."""
import cli as cli_mod
assert cli_mod._is_ghostty_terminal({"TERM_PROGRAM": "ghostty"}) is True
assert cli_mod._is_ghostty_terminal({"TERM": "xterm-ghostty"}) is True
assert cli_mod._is_ghostty_terminal({"TERM": "XTERM-GHOSTTY"}) is True
assert cli_mod._is_ghostty_terminal({"TERM_PROGRAM": "iTerm.app"}) is False
assert cli_mod._is_ghostty_terminal({}) is False