|
| 1 | +"""Regression tests for classic-CLI mid-run /background dispatch. |
| 2 | +
|
| 3 | +Background |
| 4 | +---------- |
| 5 | +``/background`` (``/bg``, ``/btw``) exists to start independent work while |
| 6 | +the current turn keeps running. Typed while the agent was busy it went into |
| 7 | +``self._pending_input`` like ordinary input, and ``process_loop`` is blocked |
| 8 | +inside ``self.chat()`` for the whole run, so the background task only started |
| 9 | +once the foreground turn had finished. That is the one moment it was not |
| 10 | +needed (#75221). |
| 11 | +
|
| 12 | +``/steer`` had the identical problem and was fixed by dispatching inline on |
| 13 | +the UI thread; the command's own ``CommandDef`` already declares |
| 14 | +``busy_policy="dispatch"``, which the gateway honours and the classic CLI |
| 15 | +never consulted. |
| 16 | +
|
| 17 | +These tests exercise the detector without starting a prompt_toolkit app, |
| 18 | +mirroring tests/cli/test_cli_steer_busy_path.py. |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import importlib |
| 24 | +import sys |
| 25 | +from unittest.mock import MagicMock, patch |
| 26 | + |
| 27 | + |
| 28 | +def _make_cli(): |
| 29 | + """Create a HermesCLI instance with prompt_toolkit stubbed out.""" |
| 30 | + _clean_config = { |
| 31 | + "model": { |
| 32 | + "default": "anthropic/claude-opus-4.6", |
| 33 | + "base_url": "https://openrouter.ai/api/v1", |
| 34 | + "provider": "auto", |
| 35 | + }, |
| 36 | + "display": {"compact": False, "tool_progress": "all"}, |
| 37 | + "agent": {}, |
| 38 | + "terminal": {"env_type": "local"}, |
| 39 | + } |
| 40 | + clean_env = {"LLM_MODEL": "", "HERMES_MAX_ITERATIONS": ""} |
| 41 | + prompt_toolkit_stubs = { |
| 42 | + "prompt_toolkit": MagicMock(), |
| 43 | + "prompt_toolkit.history": MagicMock(), |
| 44 | + "prompt_toolkit.styles": MagicMock(), |
| 45 | + "prompt_toolkit.patch_stdout": MagicMock(), |
| 46 | + "prompt_toolkit.application": MagicMock(), |
| 47 | + "prompt_toolkit.layout": MagicMock(), |
| 48 | + "prompt_toolkit.layout.processors": MagicMock(), |
| 49 | + "prompt_toolkit.filters": MagicMock(), |
| 50 | + "prompt_toolkit.layout.dimension": MagicMock(), |
| 51 | + "prompt_toolkit.layout.menus": MagicMock(), |
| 52 | + "prompt_toolkit.widgets": MagicMock(), |
| 53 | + "prompt_toolkit.key_binding": MagicMock(), |
| 54 | + "prompt_toolkit.completion": MagicMock(), |
| 55 | + "prompt_toolkit.formatted_text": MagicMock(), |
| 56 | + "prompt_toolkit.auto_suggest": MagicMock(), |
| 57 | + } |
| 58 | + with patch.dict(sys.modules, prompt_toolkit_stubs), patch.dict( |
| 59 | + "os.environ", clean_env, clear=False |
| 60 | + ): |
| 61 | + import cli as _cli_mod |
| 62 | + |
| 63 | + _cli_mod = importlib.reload(_cli_mod) |
| 64 | + with patch.object(_cli_mod, "get_tool_definitions", return_value=[]), patch.dict( |
| 65 | + _cli_mod.__dict__, {"CLI_CONFIG": _clean_config} |
| 66 | + ): |
| 67 | + return _cli_mod.HermesCLI() |
| 68 | + |
| 69 | + |
| 70 | +class TestBackgroundInlineDetector: |
| 71 | + def test_detects_background_when_agent_running(self): |
| 72 | + cli = _make_cli() |
| 73 | + cli._agent_running = True |
| 74 | + assert cli._should_handle_background_command_inline( |
| 75 | + "/background inspect the test failures" |
| 76 | + ) is True |
| 77 | + |
| 78 | + def test_detects_both_aliases(self): |
| 79 | + cli = _make_cli() |
| 80 | + cli._agent_running = True |
| 81 | + assert cli._should_handle_background_command_inline("/bg do work") is True |
| 82 | + assert cli._should_handle_background_command_inline("/btw do work") is True |
| 83 | + |
| 84 | + def test_ignores_background_when_agent_idle(self): |
| 85 | + """Idle input falls through to the normal process_loop dispatch.""" |
| 86 | + cli = _make_cli() |
| 87 | + cli._agent_running = False |
| 88 | + assert cli._should_handle_background_command_inline("/bg do work") is False |
| 89 | + |
| 90 | + def test_ignores_non_slash_input(self): |
| 91 | + cli = _make_cli() |
| 92 | + cli._agent_running = True |
| 93 | + assert cli._should_handle_background_command_inline("bg without slash") is False |
| 94 | + assert cli._should_handle_background_command_inline("") is False |
| 95 | + |
| 96 | + def test_ignores_other_slash_commands(self): |
| 97 | + cli = _make_cli() |
| 98 | + cli._agent_running = True |
| 99 | + assert cli._should_handle_background_command_inline("/steer hello") is False |
| 100 | + assert cli._should_handle_background_command_inline("/queue hello") is False |
| 101 | + assert cli._should_handle_background_command_inline("/stop") is False |
| 102 | + |
| 103 | + def test_ignores_background_with_attached_images(self): |
| 104 | + """Image payloads take the normal path.""" |
| 105 | + cli = _make_cli() |
| 106 | + cli._agent_running = True |
| 107 | + assert cli._should_handle_background_command_inline( |
| 108 | + "/bg look at this", has_images=True |
| 109 | + ) is False |
| 110 | + |
| 111 | + def test_case_and_whitespace_tolerant(self): |
| 112 | + cli = _make_cli() |
| 113 | + cli._agent_running = True |
| 114 | + assert cli._should_handle_background_command_inline("/BG do work") is True |
| 115 | + |
| 116 | + |
| 117 | +class TestBackgroundBusyPolicyContract: |
| 118 | + """The registry already declares the intent this detector implements.""" |
| 119 | + |
| 120 | + def test_background_declares_dispatch_while_busy(self): |
| 121 | + from hermes_cli.commands import resolve_command |
| 122 | + |
| 123 | + cmd = resolve_command("background") |
| 124 | + assert cmd is not None |
| 125 | + assert cmd.busy_policy == "dispatch" |
| 126 | + |
| 127 | + def test_aliases_resolve_to_background(self): |
| 128 | + from hermes_cli.commands import resolve_command |
| 129 | + |
| 130 | + for alias in ("bg", "btw"): |
| 131 | + cmd = resolve_command(alias) |
| 132 | + assert cmd is not None and cmd.name == "background" |
0 commit comments