Files
hermes-agent/tests/hermes_cli/test_chat_q_exit_clear.py
T
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

150 lines
4.7 KiB
Python

"""Regression tests for #53009: chat -q final response erased by exit-summary clear."""
from types import SimpleNamespace
import pytest
import cli as cli_mod
# ── A3.1 Test-First: verify _clear_terminal_on_exit gating ──────────────────
def test_print_exit_summary_clears_screen_by_default(monkeypatch):
"""Default behavior: _print_exit_summary() calls _clear_terminal_on_exit()."""
calls = []
class FakeCLI:
conversation_history = []
session_start = None
def _clear_terminal_on_exit(self):
calls.append("clear")
monkeypatch.setattr(cli_mod, "datetime", SimpleNamespace(
now=lambda: SimpleNamespace(
__sub__=lambda self, other: SimpleNamespace(
total_seconds=lambda: 0
)
)
))
fake = FakeCLI()
cli_mod.HermesCLI._print_exit_summary(fake) # default clear_screen=True
assert "clear" in calls, "_clear_terminal_on_exit should be called by default"
def test_print_exit_summary_skips_clear_when_clear_screen_false(monkeypatch):
"""With clear_screen=False, _print_exit_summary() does NOT clear."""
calls = []
class FakeCLI:
conversation_history = []
session_start = None
def _clear_terminal_on_exit(self):
calls.append("clear")
monkeypatch.setattr(cli_mod, "datetime", SimpleNamespace(
now=lambda: SimpleNamespace(
__sub__=lambda self, other: SimpleNamespace(
total_seconds=lambda: 0
)
)
))
fake = FakeCLI()
cli_mod.HermesCLI._print_exit_summary(fake, clear_screen=False)
assert "clear" not in calls, (
"_clear_terminal_on_exit should NOT be called when clear_screen=False"
)
# ── Production-path test: single-query -q path skips the clear ──────────────
def test_single_query_main_skips_clear_on_exit_summary(monkeypatch):
"""The single-query (-q) path calls _print_exit_summary without clearing."""
calls = []
clear_calls = []
class FakeCLI:
def __init__(self, **_kwargs):
self.console = SimpleNamespace(print=lambda *_a, **_kw: calls.append("query-label"))
self.session_id = "sq-test"
self.agent = SimpleNamespace(
session_id="sq-test",
platform="cli",
)
def _claim_active_session(self, surface, *, stderr=False):
calls.append(("claim", surface, stderr))
return True
def _show_security_advisories(self):
calls.append("advisories")
def chat(self, query, images=None):
calls.append(("chat", query, images))
return "done"
def _print_exit_summary(self, clear_screen=True):
calls.append(("summary", clear_screen))
if clear_screen:
clear_calls.append("CLEARED") # should NOT happen
monkeypatch.setattr(cli_mod, "HermesCLI", FakeCLI)
monkeypatch.setattr(cli_mod.atexit, "register", lambda *_a, **_kw: None)
monkeypatch.setattr(
cli_mod,
"_finalize_single_query",
lambda fake_cli: calls.append(("finalize", fake_cli.session_id)),
)
cli_mod.main(query="hello", quiet=False, toolsets="terminal")
assert calls == [
("claim", "cli", False),
"query-label",
"advisories",
("chat", "hello", None),
("summary", False), # <-- clear_screen=False for single-query
("finalize", "sq-test"),
]
assert len(clear_calls) == 0, (
"_clear_terminal_on_exit must NOT be called in single-query mode"
)
# ── Verify interactive mode still clears ────────────────────────────────────
def test_print_exit_summary_still_clears_in_interactive_path(monkeypatch):
"""Interactive mode should still clear the screen (preserving #38928)."""
from datetime import datetime as real_datetime
calls = []
class FakeCLI:
conversation_history = [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi"},
]
session_start = real_datetime(2026, 1, 1, 12, 0, 0)
session_id = "test-session"
_session_db = None
agent = None
def _clear_terminal_on_exit(self):
calls.append("clear")
monkeypatch.setattr(cli_mod, "datetime", SimpleNamespace(
now=lambda: real_datetime(2026, 1, 1, 12, 1, 0) # 1 min elapsed
))
fake = FakeCLI()
cli_mod.HermesCLI._print_exit_summary(fake) # default clear_screen=True
assert "clear" in calls, (
"Interactive mode should still clear the screen (regression test for #38928)"
)