aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/tests/test_completion.py
blob: 1222dc29097e1f99ccc95fc61c0fb91852035f01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
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"]