diff --git a/docs/cli-reference.md b/docs/cli-reference.md index c537a0774..7f7b7a15a 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -39,6 +39,9 @@ The Python package and Nanobot Desktop keep separate runtimes, configuration, workspaces, and state. When a running Desktop release publishes its private terminal-access descriptor, an interactive bare `nanobot` or `nanobot webui` asks which installation to use after authenticating a ready Desktop target. +Use `Up`/`Down` to highlight an installation and `Enter` to confirm; `1`/`2` +also move the highlight. `Ctrl+C` cancels with exit code 130 without attaching +to Desktop or starting a Python backend. If Desktop is absent, busy, unavailable, or cannot be authenticated, the command reports the current Python executable and continues normally. A virtual environment's executable is shown without resolving it to its base interpreter. diff --git a/nanobot/cli/desktop_target.py b/nanobot/cli/desktop_target.py index 9e352f676..f84e431e9 100644 --- a/nanobot/cli/desktop_target.py +++ b/nanobot/cli/desktop_target.py @@ -178,21 +178,30 @@ def _print_desktop_error(message: str) -> None: def _choose_target(status: DesktopReply) -> Literal["desktop", "python"]: + import questionary + detail = "" if status.state == "ready" else f" ({status.state})" - print("Nanobot Desktop is running. Choose a target for this invocation:") - print(f" 1. Nanobot Desktop{detail}") - print(f" 2. Current Python environment ({_python_target_label()})") - while True: - try: - answer = input("Target [1/2]: ").strip().lower() - except (EOFError, KeyboardInterrupt) as exc: - print(file=sys.stderr) - raise SystemExit(130) from exc - if answer in {"1", "desktop", "d"}: - return "desktop" - if answer in {"2", "python", "p"}: - return "python" - print("Enter 1 for Desktop or 2 for the current Python environment.") + try: + answer = questionary.select( + "Nanobot Desktop is running. Choose a target for this invocation:", + choices=[ + questionary.Choice(f"Nanobot Desktop{detail}", value="desktop"), + questionary.Choice( + f"Current Python environment ({_python_target_label()})", value="python" + ), + ], + default="desktop", + use_shortcuts=True, + instruction="(↑/↓ to move, Enter to confirm, Ctrl+C to cancel)", + ).unsafe_ask() + except (EOFError, KeyboardInterrupt): + answer = None + if answer == "desktop": + return "desktop" + if answer == "python": + return "python" + _print_desktop_error("Target selection cancelled.") + raise SystemExit(130) def _desktop_terminal_directory() -> Path | None: diff --git a/tests/cli/test_desktop_target.py b/tests/cli/test_desktop_target.py index 13de9617a..f329247ae 100644 --- a/tests/cli/test_desktop_target.py +++ b/tests/cli/test_desktop_target.py @@ -7,6 +7,9 @@ import time from pathlib import Path import pytest +from prompt_toolkit.application import create_app_session +from prompt_toolkit.input import create_pipe_input +from prompt_toolkit.output import DummyOutput from nanobot.cli import desktop_target from nanobot.cli.desktop_target import ( @@ -208,6 +211,62 @@ def test_absent_desktop_selects_current_python(monkeypatch, capsys) -> None: assert "Using current Python environment:" in capsys.readouterr().out +@pytest.mark.parametrize(("keys", "expected"), [ + ("\r", "desktop"), + ("\x1b[B\r", "python"), + ("\x1b[B\x1b[A\r", "desktop"), + ("\x1b[A\r", "python"), + ("2\r", "python"), + ("21\r", "desktop"), +]) +def test_target_picker_accepts_real_navigation_keys(keys, expected): + with create_pipe_input() as pipe_input: + with create_app_session(input=pipe_input, output=DummyOutput()): + pipe_input.send_text(keys) + assert desktop_target._choose_target( + DesktopReply("ready", frozenset({"webui", "tui"})) + ) == expected + + +@pytest.mark.parametrize("args", [[], ["webui"]]) +@pytest.mark.parametrize("cancel", ["interrupt", "eof"]) +def test_target_picker_cancellation_exits_without_attachment(monkeypatch, capsys, args, cancel): + calls: list[str] = [] + + class Target: + def request(self, operation: str) -> DesktopReply: + calls.append(operation) + assert operation == "status", "cancellation must not request attachment credentials" + return DesktopReply("ready", frozenset({"webui", "tui"})) + + monkeypatch.setattr(desktop_target, "_interactive_shell", lambda: True) + monkeypatch.setattr(desktop_target, "discover_desktop_target", Target) + monkeypatch.setattr( + "nanobot.cli.desktop_tui.launch_desktop_tui", + lambda *_: pytest.fail("cancellation started the TUI"), + ) + monkeypatch.setattr( + "nanobot.cli.webui_support._launch_browser", + lambda *_: pytest.fail("cancellation opened a browser"), + ) + + with create_pipe_input() as pipe_input: + with create_app_session(input=pipe_input, output=DummyOutput()): + if cancel == "interrupt": + # Cancel even after moving the highlight to the Python option. + pipe_input.send_text("\x1b[B\x03") + else: + pipe_input.close() + with pytest.raises(SystemExit) as exc: + dispatch_bare_desktop_target(args) + + assert exc.value.code == 130 + assert calls == ["status"] + output = capsys.readouterr() + assert "Using current Python environment:" not in output.out + assert "No backend was started" in output.err + + def test_python_choice_does_not_request_desktop_operation(monkeypatch, capsys) -> None: calls: list[str] = [] diff --git a/tests/cli/test_desktop_target_entrypoints.py b/tests/cli/test_desktop_target_entrypoints.py index 70f7c38ec..bc7362082 100644 --- a/tests/cli/test_desktop_target_entrypoints.py +++ b/tests/cli/test_desktop_target_entrypoints.py @@ -1,6 +1,9 @@ """The picker covers retained launchers without intercepting explicit commands.""" import pytest +from prompt_toolkit.application import create_app_session +from prompt_toolkit.input import create_pipe_input +from prompt_toolkit.output import DummyOutput from typer.testing import CliRunner from nanobot.cli import commands, desktop_target, entry @@ -26,6 +29,34 @@ def test_retained_launcher_attaches_without_starting_python(monkeypatch, args): assert seen == [args] +@pytest.mark.parametrize("args", [[], ["webui"]]) +@pytest.mark.parametrize("launcher", ["modern", "retained"]) +def test_launcher_picker_ctrl_c_does_not_start_python(monkeypatch, args, launcher): + class Target: + def request(self, operation): + assert operation == "status", "cancelled picker must not request attachment" + return desktop_target.DesktopReply("ready", frozenset({"webui", "tui"})) + + monkeypatch.setattr(desktop_target, "_interactive_shell", lambda: True) + monkeypatch.setattr(desktop_target, "discover_desktop_target", Target) + monkeypatch.setattr(entry, "_run_agent", lambda *_a, **_kw: pytest.fail("agent started")) + monkeypatch.setattr( + "nanobot.cli.webui._resolve_webui_config_path", + lambda *_a, **_kw: pytest.fail("Python WebUI started"), + ) + monkeypatch.setattr(entry.sys, "argv", ["nanobot", *args]) + with create_pipe_input() as pipe_input: + with create_app_session(input=pipe_input, output=DummyOutput()): + pipe_input.send_text("\x03") + if launcher == "retained": + result = CliRunner().invoke(commands.app, args) + assert result.exit_code == 130, result.output + else: + with pytest.raises(SystemExit) as exc: + entry.main() + assert exc.value.code == 130 + + @pytest.mark.parametrize("args", [["agent", "--help"], ["--help"], ["--version"], ["webui", "--no-open"], ["webui", "--port", "8765"]]) def test_retained_launcher_explicit_commands_never_discover(monkeypatch, args):