fix(cli): stabilize wrapped CJK arrow navigation in interactive input

This commit is contained in:
张涔熙
2026-02-08 11:10:03 +08:00
parent 8b1ef77970
commit 342ba2b879
3 changed files with 282 additions and 15 deletions

View File

@@ -4,25 +4,45 @@ import nanobot.cli.commands as commands
def test_read_interactive_input_uses_plain_input(monkeypatch) -> None:
captured: dict[str, object] = {}
def fake_print(*args, **kwargs):
captured["printed"] = args
captured["print_kwargs"] = kwargs
captured: dict[str, str] = {}
def fake_input(prompt: str = "") -> str:
captured["prompt"] = prompt
return "hello"
monkeypatch.setattr(commands.console, "print", fake_print)
monkeypatch.setattr(builtins, "input", fake_input)
monkeypatch.setattr(commands, "_PROMPT_SESSION", None)
monkeypatch.setattr(commands, "_READLINE", None)
value = commands._read_interactive_input()
assert value == "hello"
assert captured["prompt"] == ""
assert captured["print_kwargs"] == {"end": ""}
assert captured["printed"] == ("[bold blue]You:[/bold blue] ",)
assert captured["prompt"] == "You: "
def test_read_interactive_input_prefers_prompt_session(monkeypatch) -> None:
captured: dict[str, object] = {}
class FakePromptSession:
async def prompt_async(self, label: object) -> str:
captured["label"] = label
return "hello"
monkeypatch.setattr(commands, "_PROMPT_SESSION", FakePromptSession())
monkeypatch.setattr(commands, "_PROMPT_SESSION_LABEL", "LBL")
value = __import__("asyncio").run(commands._read_interactive_input_async())
assert value == "hello"
assert captured["label"] == "LBL"
def test_prompt_text_for_readline_modes(monkeypatch) -> None:
monkeypatch.setattr(commands, "_READLINE", object())
monkeypatch.setattr(commands, "_USING_LIBEDIT", True)
assert commands._prompt_text() == "\033[1;34mYou:\033[0m "
monkeypatch.setattr(commands, "_USING_LIBEDIT", False)
assert "\001" in commands._prompt_text()
def test_flush_pending_tty_input_skips_non_tty(monkeypatch) -> None:
@@ -34,4 +54,3 @@ def test_flush_pending_tty_input_skips_non_tty(monkeypatch) -> None:
monkeypatch.setattr(commands.os, "isatty", lambda _fd: False)
commands._flush_pending_tty_input()