"""The index as the Files tab draws it, and names printed safely.""" import pytest from meshbay_cli.client.tree import Tree from meshbay_cli.errors import NOT_FOUND, CliError from meshbay_cli.output import human_size, safe INDEX = { "roots": [{"name": "share", "available": True, "writable": True}, {"name": "usb", "available": False, "writable": False}], "dirs": ["share", "share/2024", "share/empty", "usb", "usb/old"], "entries": [ {"id": "a" * 64, "name": "notes.txt", "path": "share", "size": 6, "type": "document"}, {"id": "b" * 64, "name": "x.flac", "path": "share/2024", "size": 9, "type": "audio"}, # A folder the node's walk did not list still exists through its file. {"id": "c" * 64, "name": "y.jpg", "path": "share/2024/late", "size": 1, "type": "image"}, {"id": "d" * 64, "name": "z.txt", "path": "usb/old", "size": 1, "type": "document"}, ], } def _names(items): return sorted(i.name for i in items) def test_the_top_is_the_roots(): tree = Tree.from_index(INDEX) top = tree.children("") assert _names(top) == ["share", "usb"] assert {i.name: i.available for i in top} == {"share": True, "usb": False} def test_a_folder_holds_its_folders_and_files(): tree = Tree.from_index(INDEX) assert _names(tree.children("share")) == ["2024", "empty", "notes.txt"] assert _names(tree.children("share/2024")) == ["late", "x.flac"] def test_an_unavailable_root_shows_nothing_unless_asked(): tree = Tree.from_index(INDEX) assert tree.children("usb") == [] assert _names(tree.children("usb/old", include_unavailable=True)) == ["z.txt"] def test_walk_visits_every_folder(): tree = Tree.from_index(INDEX) assert [p for p, _ in tree.walk("share")] == [ "share", "share/2024", "share/2024/late", "share/empty"] def test_a_missing_folder_is_not_found(): with pytest.raises(CliError) as e: Tree.from_index(INDEX).children("share/nope") assert e.value.code == NOT_FOUND def test_a_file_is_found_by_its_path(): tree = Tree.from_index(INDEX) assert tree.file("share/2024/x.flac")["id"] == "b" * 64 assert tree.file("share/2024/nope") is None def test_control_characters_and_bidi_overrides_are_escaped(): assert safe("evil\x1b[31mred") == "evil\\x1b[31mred" assert safe("a\nb\x7fc\x9b") == "a\\x0ab\\x7fc\\x9b" assert safe("photo‮gpj.exe") == "photo\\u202egpj.exe" assert safe("été 🎵 l'ouest") == "été 🎵 l'ouest" def test_sizes(): assert human_size(6) == "6 B" assert human_size(2_500_000) == "2.4 MB"