Files
computer-playbook/tests/unit/cli/console/test_runner.py
T
kevinveenbirkenbachandClaude Opus 4.7 54f88275ce feat(cli/console): stateful navigation, ls, and modular split
Reworks the REPL into a stateful shell that tracks a category path: typing a bare category name cd's into it, `/` returns to root, `..` walks one level up (chained as `../..` etc.), path tokens with slashes or a leading `..` navigate, `infinito [path]` always jumps absolute, and `ls` renders the current folder with a 🗂️/⚙️ emoji column, name, and a 30-char-truncated description (folders sorted before commands). Executable leaves (sub-packages with `__main__.py`) run instead of cd-ing into them. The prompt prints `🖥️  infinito <space-separated path>> `; the screen clears (incl. scrollback via `\033[3J`) before each command run, and the run echoes the resolved invocation in the fresh frame so the user sees what is about to execute.

Splits `repl.py` (~250 lines) into focused modules: `constants.py` (sentinels), `navigation.py` (path/category resolve + prompt + normalize), `ls.py` (folder rendering + truncate), `runner.py` (subprocess dispatch + clear_screen), `banner.py` (startup banner). `repl.py` is now a thin `main()` loop with backward-compat re-exports so external consumers keep the `repl.<symbol>` surface. Mirrors the split on the test side into `test_navigation.py`, `test_runner.py`, `test_ls.py`, and a `test_repl.py` that owns only main-loop integration with a shared `_MainLoopHarness` mixin.

Adds `scripts/tests/environment/11_console.sh` as a CI smoke that feeds a navigation sequence through the REPL and asserts every prompt shape plus the `ls` emoji output, so a regression in the prompt format, the path resolver, or the folder rendering is caught by the environment-test orchestrator.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 13:06:38 +02:00

48 lines
1.6 KiB
Python

import signal
import sys
import unittest
from unittest.mock import patch
from cli.console import runner
class TestRunCli(unittest.TestCase):
@patch("cli.console.runner.subprocess.run")
def test_invokes_python_m_cli_with_argv(self, mock_run):
mock_run.return_value.returncode = 0
rc = runner.run_cli(["meta", "env"])
self.assertEqual(rc, 0)
called_argv = mock_run.call_args[0][0]
self.assertEqual(called_argv[0], sys.executable)
self.assertEqual(called_argv[1:], ["-m", "cli", "meta", "env"])
@patch("cli.console.runner.subprocess.run")
@patch("cli.console.runner.clear_screen")
def test_clears_screen_before_invocation(self, mock_clear, mock_run):
mock_run.return_value.returncode = 0
runner.run_cli(["meta", "env"])
mock_clear.assert_called_once_with()
@patch("cli.console.runner.subprocess.run")
def test_restores_sigint_handler(self, mock_run):
mock_run.return_value.returncode = 0
before = signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
runner.run_cli(["x"])
after = signal.getsignal(signal.SIGINT)
self.assertEqual(after, signal.SIG_DFL)
finally:
signal.signal(signal.SIGINT, before)
class TestClearScreen(unittest.TestCase):
def test_noop_when_not_tty(self):
with patch("cli.console.runner.sys.stdout") as mock_stdout:
mock_stdout.isatty.return_value = False
runner.clear_screen()
mock_stdout.write.assert_not_called()
if __name__ == "__main__":
unittest.main()