import assert from "node:assert/strict"; import test from "node:test"; import { selectTreePath, treeSelectionEdges, visibleTreePaths, type TreeSelection } from "../../../src/lib/workspace/treeSelection.ts"; import type { TreeEntry } from "../../../src/lib/workspace/types.ts"; const entry = (rel_path: string, kind: "file" | "folder" = "file"): TreeEntry => ({ rel_path, name: rel_path.split("/").pop()!, kind }); const children = { "": [entry("notes", "folder"), entry("src", "folder"), entry("z.md")], notes: [entry("notes/b.md"), entry("notes/a.md"), entry("notes/nested", "folder")], "notes/nested": [entry("notes/nested/c.md")], src: [entry("src/main.ts")], }; const visible = visibleTreePaths(children, new Set(["notes", "notes/nested"])); const empty = (): TreeSelection => ({ paths: new Set(), anchor: null }); test("selection outlines omit shared edges and close around gaps and single rows", () => { const rows = ["a.md", "b.md", "c.md", "d.md", "e.md"]; assert.deepEqual([...treeSelectionEdges(rows, new Set(["a.md", "b.md", "c.md", "e.md"]))], [ ["a.md", { top: null, bottom: 0 }], ["b.md", { top: 0, bottom: 0 }], ["c.md", { top: 0, bottom: null }], ["e.md", { top: null, bottom: null }], ]); assert.deepEqual([...treeSelectionEdges(rows, new Set(["a.md", "c.md", "e.md"]))].map(([, edges]) => edges), Array.from({ length: 3 }, () => ({ top: null, bottom: null }))); }); test("nested selection outlines retain only exposed indentation steps in visible order", () => { const edges = treeSelectionEdges(visible, new Set(visible)); assert.deepEqual(edges.get("notes"), { top: null, bottom: 1 }); assert.deepEqual(edges.get("notes/b.md"), { top: 0, bottom: 0 }); assert.deepEqual(edges.get("notes/nested"), { top: 0, bottom: 1 }); assert.deepEqual(edges.get("notes/nested/c.md"), { top: 0, bottom: 0 }); assert.deepEqual(edges.get("src"), { top: 2, bottom: 0 }); assert.deepEqual(edges.get("z.md"), { top: 0, bottom: null }); const collapsed = visibleTreePaths(children, new Set()); assert.deepEqual([...treeSelectionEdges(collapsed, new Set(visible))], [ ["notes", { top: null, bottom: 0 }], ["src", { top: 0, bottom: 0 }], ["z.md", { top: 0, bottom: null }], ]); }); test("visible order preserves custom sorting and only visits expanded folders", () => { assert.deepEqual(visible, ["notes", "notes/b.md", "notes/a.md", "notes/nested", "notes/nested/c.md", "src", "z.md"]); assert.deepEqual(visibleTreePaths(children, new Set()), ["notes", "src", "z.md"]); }); test("Ctrl-click toggles files and folders independently; plain click replaces the selection", () => { const first = selectTreePath(empty(), "notes", visible); const added = selectTreePath(first, "z.md", visible, { ctrlKey: true }); assert.deepEqual([...added.paths], ["notes", "z.md"]); assert.deepEqual([...first.paths], ["notes"]); const removed = selectTreePath(added, "notes", visible, { ctrlKey: true }); assert.deepEqual([...removed.paths], ["z.md"]); assert.deepEqual([...selectTreePath(removed, "z.md", visible, { ctrlKey: true }).paths], []); assert.deepEqual([...selectTreePath(added, "src", visible).paths], ["src"]); }); test("Shift-click selects an inclusive range in both directions across files and folders", () => { const first = selectTreePath(empty(), "notes/b.md", visible); const forward = selectTreePath(first, "src", visible, { shiftKey: true }); assert.deepEqual([...forward.paths], visible.slice(1, 6)); const reverse = selectTreePath(selectTreePath(empty(), "src", visible), "notes/b.md", visible, { shiftKey: true }); assert.deepEqual([...reverse.paths], visible.slice(1, 6)); }); test("repeated Shift-click keeps the anchor and shrinks or reverses the range", () => { const first = selectTreePath(empty(), "notes/a.md", visible); const extended = selectTreePath(first, "z.md", visible, { shiftKey: true }); const shrunk = selectTreePath(extended, "notes/nested", visible, { shiftKey: true }); assert.deepEqual([...shrunk.paths], ["notes/a.md", "notes/nested"]); assert.equal(shrunk.anchor, "notes/a.md"); assert.deepEqual([...selectTreePath(shrunk, "notes", visible, { shiftKey: true }).paths], visible.slice(0, 3)); }); test("Ctrl+Shift adds a range and Command-click supports the same toggle behavior", () => { let selection = selectTreePath(empty(), "z.md", visible); selection = selectTreePath(selection, "notes/b.md", visible, { metaKey: true }); selection = selectTreePath(selection, "notes/nested", visible, { ctrlKey: true, shiftKey: true }); assert.deepEqual([...selection.paths], ["z.md", "notes/b.md", "notes/a.md", "notes/nested"]); assert.deepEqual([...selectTreePath(selection, "z.md", visible, { metaKey: true }).paths], ["notes/b.md", "notes/a.md", "notes/nested"]); }); test("a missing or hidden anchor makes Shift-click select only the clicked row", () => { assert.deepEqual(selectTreePath(empty(), "src", visible, { shiftKey: true }), { paths: new Set(["src"]), anchor: "src" }); const hidden = selectTreePath(empty(), "notes/b.md", visible); assert.deepEqual(selectTreePath(hidden, "z.md", ["notes", "src", "z.md"], { shiftKey: true }), { paths: new Set(["z.md"]), anchor: "z.md" }); });