fix(cli): make Desktop target selection keyboard navigable [skip ci]
Reuse the existing questionary selector so users can highlight a target with arrow keys and confirm with Enter. Retain numeric shortcuts and the current Python executable label without changing discovery or attachment behavior. Keep cancellation fail-closed with exit code 130. Exercise real navigation, EOF and Ctrl+C through both CLI launchers so cancelling cannot start a Python backend or attach to Desktop. Document the controls.
This commit is contained in:
1 parent
9a594f74ff
commit
cf74d1c7e5
4 files changed
+116
-14
No files matched your search
@@ -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] = []
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user