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

152 lines
5.7 KiB
Python

"""Tests for the /update slash command in the classic CLI and TUI launcher.
Verifies that ``HermesCLI._handle_update_command`` correctly:
- Refuses to run under a managed install (Homebrew, Docker, etc.)
- Sets ``_pending_relaunch`` and returns ``True`` on confirmation
- Cancels cleanly on a "no"-shaped answer or unrecognized input
- Cancels cleanly when ``_prompt_text_input_modal`` returns None (timeout /
modal dismissed)
Also verifies that ``main_tui_launch._launch_tui`` correctly handles exit
code 42 (the TUI's signal to trigger an update) by calling
``relaunch(["update"], preserve_inherited=False)`` from the Python wrapper
side. The companion Vitest (``ui-tui/src/__tests__/createSlashHandler.test.ts``)
covers the TypeScript slash-handler that *emits* code 42; this file covers
the Python wrapper branch that *acts on* it.
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from cli import HermesCLI
from hermes_cli import main_tui_launch
def _bound(fn, instance):
"""Bind an unbound method to a stand-in instance."""
return fn.__get__(instance, type(instance))
def _make_self(modal_response):
"""Build a minimal stand-in 'self' for ``_handle_update_command``.
Uses the same SimpleNamespace pattern as ``test_destructive_slash_confirm``
so we don't need a full ``HermesCLI`` construction.
``_prompt_text_input_modal`` is stubbed to return *modal_response*
directly so tests can drive the entire confirmation branch without
touching stdin or prompt_toolkit internals.
"""
self_ = SimpleNamespace(
_app=None,
_pending_relaunch=None,
_prompt_text_input_modal=lambda **_kw: modal_response,
)
self_._normalize_slash_confirm_choice = _bound(
HermesCLI._normalize_slash_confirm_choice, self_
)
return self_
def _call(self_):
"""Invoke the real ``_handle_update_command`` on the stub."""
return HermesCLI._handle_update_command(self_)
# ---------------------------------------------------------------------------
# Managed-install guard
# ---------------------------------------------------------------------------
def test_managed_install_refuses_and_does_not_set_pending_relaunch(capsys):
"""Under a managed install (brew/docker), /update prints a hint and
returns without setting ``_pending_relaunch``."""
self_ = SimpleNamespace(
_app=None,
_pending_relaunch=None,
# Use pytest.fail so any unexpected modal invocation surfaces as a failure.
_prompt_text_input_modal=lambda **_kw: pytest.fail("Modal should not be called"),
)
self_._normalize_slash_confirm_choice = _bound(
HermesCLI._normalize_slash_confirm_choice, self_
)
with (
patch("hermes_cli.config.is_managed", return_value=True),
patch(
"hermes_cli.config.format_managed_message",
return_value="Use `sudo nixos-rebuild switch` to update.",
),
):
result = _call(self_)
out = capsys.readouterr().out
assert "sudo nixos-rebuild switch" in out
assert self_._pending_relaunch is None
assert not result
# ---------------------------------------------------------------------------
# Confirmation proceeds only on recognised affirmative responses
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("answer", ["y", "Y", "yes", "YES", "1", "ok"])
def test_affirmative_answer_sets_pending_relaunch_and_returns_true(answer, capsys):
"""Recognised affirmative answers ("y", "yes", "1", "ok") set
``_pending_relaunch = ["update"]`` and return ``True`` so the caller
(process_command) can trigger the main-thread app-exit path."""
self_ = _make_self(modal_response=answer)
with patch("hermes_cli.config.is_managed", return_value=False):
result = _call(self_)
assert self_._pending_relaunch == ["update"]
assert result is True
assert "Launching update" in capsys.readouterr().out
# ---------------------------------------------------------------------------
# Cancellation paths — _pending_relaunch must stay None
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("answer", ["n", "N", "no", "NO", " no "])
def test_negative_answer_cancels(answer, capsys):
"""Any "no"-shaped answer cancels without setting ``_pending_relaunch``."""
self_ = _make_self(modal_response=answer)
with patch("hermes_cli.config.is_managed", return_value=False):
result = _call(self_)
assert self_._pending_relaunch is None
assert not result
assert "Launching update" not in capsys.readouterr().out
def test_none_response_cancels(capsys):
"""``None`` from the modal (timeout or dismiss) cancels cleanly."""
self_ = _make_self(modal_response=None)
with patch("hermes_cli.config.is_managed", return_value=False):
result = _call(self_)
assert self_._pending_relaunch is None
assert not result
@pytest.mark.parametrize("answer", ["nope", "cancel", "sure", "2", "3", "abort", ""])
def test_unrecognized_or_cancel_input_cancels(answer, capsys):
"""Unrecognised input and explicit "cancel" do not proceed.
Previously the implementation treated any non-"n/no" answer as approval,
which meant typos like "nope" or "cancel" would launch the update.
Now only confirmed affirmative aliases ("y", "yes", "1", "ok") proceed;
everything else (including empty string, "cancel", typos) cancels.
"""
self_ = _make_self(modal_response=answer)
with patch("hermes_cli.config.is_managed", return_value=False):
result = _call(self_)
assert self_._pending_relaunch is None
assert not result