Files
computer-playbook/tests/unit/cli/console/test_ls.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

65 lines
2.2 KiB
Python

import io
import unittest
from unittest.mock import patch
from cli.console import ls
from cli.console.constants import LS_DESC_LIMIT
class TestTruncate(unittest.TestCase):
def test_short_passthrough(self):
self.assertEqual(ls.truncate("short", 30), "short")
def test_exact_limit_passthrough(self):
text = "x" * 30
self.assertEqual(ls.truncate(text, 30), text)
def test_over_limit_uses_ellipsis(self):
text = "x" * 50
out = ls.truncate(text, 30)
self.assertEqual(len(out), 30)
self.assertTrue(out.endswith("…"))
class TestDoLs(unittest.TestCase):
def test_lists_root_categories(self):
with patch("sys.stdout", new_callable=io.StringIO) as out:
ls.do_ls([])
text = out.getvalue()
self.assertIn("administration", text)
self.assertIn("meta", text)
def test_entries_get_emoji_prefix(self):
from cli.core.help import _COMMAND_EMOJI, _FOLDER_EMOJI
with patch("sys.stdout", new_callable=io.StringIO) as out:
ls.do_ls([])
text = out.getvalue()
self.assertIn(_FOLDER_EMOJI, text)
self.assertIn(_COMMAND_EMOJI, text)
def test_folders_listed_before_apps(self):
with patch("sys.stdout", new_callable=io.StringIO) as out:
ls.do_ls([])
lines = [line for line in out.getvalue().splitlines() if line.strip()]
admin_idx = next(i for i, line in enumerate(lines) if "administration" in line)
console_idx = next(i for i, line in enumerate(lines) if "console" in line)
self.assertLess(admin_idx, console_idx)
def test_truncates_description_to_30_chars(self):
with patch("sys.stdout", new_callable=io.StringIO) as out:
ls.do_ls([])
for line in out.getvalue().splitlines():
if not line.strip():
continue
visible = line.replace("\033[2m", "").replace("\033[0m", "")
# Layout: ` <emoji> <name> <desc>`; desc is the segment
# past the two-space separator following the padded name.
parts = visible.split(" ")
desc = parts[-1].strip()
self.assertLessEqual(len(desc), LS_DESC_LIMIT)
if __name__ == "__main__":
unittest.main()