""" Tab completion's candidates: from the parser for actions, verbs and options, from the hub and the node for remote paths — never a question, never an error. """ import pytest from fake_hub import PASSPHRASE, USERNAME, FakeHub from fake_node import FakeNode from meshbay_cli import __main__ as cli from meshbay_cli import complete from meshbay_cli.client import hub as hub_module from meshbay_cli.client import session as session_module from meshbay_cli.commands import context def _c(*words, shell="bash"): return complete.candidates(shell, len(words), ["meshbay-cli", *words]) def test_actions_verbs_options_and_choices_come_from_the_parser(): assert "account" in _c("") and "mkdir" in _c("") and "completion" in _c("") assert _c("ac") == ["account"] assert _c("account", "") == ["show", "register", "verify", "passphrase", "reset", "email", "sign-out-everywhere", "delete"] assert _c("account", "pa") == ["passphrase"] assert _c("devices", "a") == ["approve"] assert _c("browser-access", "") == ["on", "off"] assert _c("completion", "") == ["bash", "powershell"] assert _c("help", "mk") == ["mkdir"] assert "--parents" in _c("mkdir", "--") and "--yes" in _c("rm", "--") assert _c("ls", "--sort", "") == ["name", "size", "date"] # A value, then the slots go on: `get -o DIR PATH`. assert _c("get", "-o", "") == [complete.FILES] assert _c("--hub", "https://x", "ac") == ["account"] @pytest.mark.parametrize("typed, shell, plain", [ ("Some\\ Saga", "bash", "Some Saga"), ("'Some Saga", "bash", "Some Saga"), ('"Some Saga"', "bash", "Some Saga"), ("'it''s'", "powershell", "it's"), ("C:\\Users\\x", "powershell", "C:\\Users\\x"), ]) def test_quoting_is_taken_off_as_each_shell_wrote_it(typed, shell, plain): assert complete.unquote(typed, shell) == plain def test_nothing_signed_in_gives_no_candidate_and_no_error(capsys): assert cli.main(["__complete", "bash", "2", "meshbay-cli", "ls", "Fam"]) == 0 out = capsys.readouterr() assert out.out == "" and out.err == "" assert cli.main(["__complete", "garbage"]) == 0 @pytest.fixture def world(monkeypatch): hub, node = FakeHub(), FakeNode() node.add("share/2024/été", "plage.jpg", b"x") node.add("share", "notes.txt", b"n") monkeypatch.setattr(context, "Hub", lambda url: hub) monkeypatch.setattr(hub_module, "Hub", lambda url: hub) async def group_nodes(token, gid): return [{"node_id": "node-1", "pk_node": "pk"}] hub.group_nodes = group_nodes opened = [] async def connect(account, gid, node_row, join_code=None, *, rewrap=None): assert rewrap is not None, "completion must never make keys" opened.append(gid) return node.session() monkeypatch.setattr(session_module, "connect", connect) assert cli.main(["--hub", hub.url, "-u", USERNAME, "--password", PASSPHRASE, "login"]) == 0 return opened def test_remote_paths_from_the_hub_and_the_node(world, capsys): capsys.readouterr() assert _c("ls", "fa") == ["Family@alice-test/"] assert _c("ls", "Family@alice-test/") == ["Family@alice-test/share/"] assert _c("get", "Family@alice-test/share/") == [ "Family@alice-test/share/2024/", "Family@alice-test/share/notes.txt"] assert _c("get", "Family@alice-test/share/2024/é") == [ "Family@alice-test/share/2024/été/"] assert _c("mkdir", "Family@alice-test/share/") == ["Family@alice-test/share/2024/"] assert _c("chat", "F") == ["Family@alice-test"] assert _c("put", "x", "Family@alice-test/sh") == [complete.FILES, "Family@alice-test/share/"] assert _c("ls", "Nope/") == [] # The node was asked once; the next Tabs read what it said. assert world == ["g1"]