199 lines
11 KiB
JavaScript
199 lines
11 KiB
JavaScript
// File tree actions on multiple selections: clipboard paths, marks, copies, deletion, group moves, and reordering.
|
||
import assert from "node:assert/strict";
|
||
import { mockTauri, runSmoke, settle as settleFrames, smokeUrl } from "../_harness/index.mjs";
|
||
|
||
await runSmoke("tree/actions", async ({ page }) => {
|
||
await mockTauri(page, {
|
||
commands: test => {
|
||
const contains = (parent, path) => path === parent || path.startsWith(parent + "/");
|
||
const ws = test.workspace({
|
||
root: "C:\\tree-actions-test",
|
||
files: { "notes/a.md": "# A", "notes/nested/b.md": "# B", "target/a.md": "# Existing", "a.md": "# A", "b.md": "# B", "c.md": "# C" },
|
||
});
|
||
const files = ws.files;
|
||
Object.assign(test, { files, answer: "Ok", fail: "", clipboard: "" });
|
||
Object.defineProperty(navigator, "clipboard", { value: { writeText: async text => { test.clipboard = text; } } });
|
||
return {
|
||
...ws,
|
||
workspace_list: args => ws.workspace_list(args).filter(entry => !entry.rel_path.startsWith(".runestone")),
|
||
workspace_delete: ({ rel }) => {
|
||
if (rel === test.fail) throw new Error("Access denied");
|
||
for (const path of [...files.keys()]) if (contains(rel, path)) files.delete(path);
|
||
},
|
||
workspace_copy: ({ rel }) => {
|
||
const to = rel + "-copy";
|
||
for (const [path, text] of [...files]) if (contains(rel, path)) files.set(to + path.slice(rel.length), text);
|
||
return { name: to.split("/").pop(), rel_path: to, kind: files.has(to) ? "file" : "folder" };
|
||
},
|
||
workspace_rename: ({ from, to }) => {
|
||
if ([...files.keys()].some(path => contains(to, path))) throw new Error("Destination exists");
|
||
for (const [path, text] of [...files]) if (contains(from, path)) {
|
||
files.delete(path);
|
||
files.set(to + path.slice(from.length), text);
|
||
}
|
||
},
|
||
workspace_reveal: () => null,
|
||
"plugin:dialog|message": () => test.answer,
|
||
};
|
||
},
|
||
});
|
||
const row = rel => page.locator(`[data-tree-rel="${rel}"]`);
|
||
const selected = () => page.locator('[role="treeitem"][aria-selected="true"]').evaluateAll(nodes => nodes.map(node => node.dataset.treeRel));
|
||
const calls = command => page.evaluate(command => window.__TEST__.calls.filter(call => call.command === command).map(call => call.args), command);
|
||
const settle = () => settleFrames(page);
|
||
const marks = () => page.evaluate(() => JSON.parse(window.__TEST__.files.get(".runestone/marks.json")));
|
||
// Mutations name the workspace root they expect (src/lib/workspace/api.ts:63).
|
||
const expectedRoot = "C:\\tree-actions-test";
|
||
async function reset() {
|
||
await page.goto(smokeUrl);
|
||
await row("notes").waitFor();
|
||
}
|
||
// A name of the form "Copy path › Workspace path" opens a submenu first.
|
||
async function action(path, name) {
|
||
await row(path).click({ button: "right" });
|
||
const [first, second] = name.split(" › ");
|
||
if (second) await page.getByRole("menuitem", { name: first, exact: true }).hover();
|
||
await page.getByRole("menuitem", { name: second ?? first, exact: true }).click();
|
||
await settle();
|
||
}
|
||
async function selectFiles() {
|
||
await row("a.md").click();
|
||
await page.getByRole("tab", { name: "a.md", exact: true }).waitFor();
|
||
await settle();
|
||
await row("b.md").click({ modifiers: ["Control"] });
|
||
await settle();
|
||
}
|
||
async function drag(sourcePath, targetPath, place = "inside") {
|
||
const source = await row(sourcePath).boundingBox();
|
||
const target = await row(targetPath).boundingBox();
|
||
await page.mouse.move(source.x + source.width / 2, source.y + source.height / 2);
|
||
await page.mouse.down();
|
||
await page.mouse.move(target.x + target.width / 2, target.y + target.height * (place === "before" ? 0.1 : place === "after" ? 0.9 : 0.5), { steps: 10 });
|
||
await page.mouse.up();
|
||
await settle();
|
||
}
|
||
|
||
await reset();
|
||
await selectFiles();
|
||
await row("a.md").click({ button: "right" });
|
||
assert.deepEqual(await selected(), ["a.md", "b.md"]);
|
||
assert.equal(await page.getByRole("menuitem", { name: "Rename", exact: true }).isDisabled(), true);
|
||
await page.keyboard.press("Escape");
|
||
await action("a.md", "Copy path › Workspace path");
|
||
assert.equal(await page.evaluate(() => window.__TEST__.clipboard), "a.md\nb.md");
|
||
await action("b.md", "Copy path › System path");
|
||
assert.equal(await page.evaluate(() => window.__TEST__.clipboard), "C:\\tree-actions-test\\a.md\nC:\\tree-actions-test\\b.md");
|
||
await action("a.md", "Show in Explorer");
|
||
assert.deepEqual((await calls("workspace_reveal")).map(args => args.rel), ["a.md", "b.md"]);
|
||
await action("a.md", "Bookmark");
|
||
await action("b.md", "Pin");
|
||
assert.deepEqual((await marks()).bookmarks, ["a.md", "b.md"]);
|
||
assert.deepEqual((await marks()).pins, ["a.md", "b.md"]);
|
||
await action("a.md", "Unpin");
|
||
await action("a.md", "Remove bookmarks");
|
||
await action("a.md", "Make copies (2)");
|
||
assert.deepEqual((await calls("workspace_copy")).map(args => args.rel), ["a.md", "b.md"]);
|
||
await row("c.md").click({ button: "right" });
|
||
assert.deepEqual(await selected(), ["c.md"]);
|
||
assert.equal(await page.getByRole("menuitem", { name: "Rename", exact: true }).isDisabled(), false);
|
||
await page.keyboard.press("Escape");
|
||
console.log("PASS: selection, clipboard paths, marks, copies, reveal, and single-item menus");
|
||
|
||
// Delete moves items to the trash (`permanent: false`). Several items still need one confirmation.
|
||
await reset();
|
||
await selectFiles();
|
||
await page.evaluate(() => { window.__TEST__.answer = "Cancel"; });
|
||
await action("a.md", "Delete (2)");
|
||
assert.equal((await calls("workspace_delete")).length, 0);
|
||
assert.deepEqual(await selected(), ["a.md", "b.md"]);
|
||
await page.evaluate(() => { window.__TEST__.answer = "Ok"; window.__TEST__.fail = "a.md"; });
|
||
await row("c.md").click({ modifiers: ["Control"] });
|
||
await action("a.md", "Delete (3)");
|
||
await row("c.md").waitFor({ state: "detached" });
|
||
assert.deepEqual(await calls("workspace_delete"), ["a.md", "b.md", "c.md"].map(rel => ({ rel, permanent: false })));
|
||
assert.match(await page.locator(".tree .error").textContent(), /a.md: Access denied/);
|
||
// A failed move to the trash offers a permanent delete only through Shift+Delete.
|
||
assert.match(await page.locator(".tree .error").textContent(), /press Shift\+Delete/);
|
||
assert.deepEqual(await selected(), ["a.md"]);
|
||
console.log("PASS: cancellation, one confirmation per batch, partial failure, and selection after deletion");
|
||
|
||
// Changed for the move to the trash: one file needs no confirmation. A permanent delete always asks.
|
||
await reset();
|
||
await action("c.md", "Delete");
|
||
await row("c.md").waitFor({ state: "detached" });
|
||
assert.deepEqual(await calls("workspace_delete"), [{ rel: "c.md", permanent: false }]);
|
||
assert.equal((await calls("plugin:dialog|message")).length, 0);
|
||
await page.evaluate(() => { window.__TEST__.answer = "Cancel"; });
|
||
await action("b.md", "Delete permanently");
|
||
assert.equal((await calls("plugin:dialog|message")).length, 1);
|
||
assert.equal((await calls("workspace_delete")).length, 1);
|
||
await page.evaluate(() => { window.__TEST__.answer = "Ok"; });
|
||
await action("b.md", "Delete permanently");
|
||
await row("b.md").waitFor({ state: "detached" });
|
||
assert.deepEqual((await calls("workspace_delete")).at(-1), { rel: "b.md", permanent: true });
|
||
console.log("PASS: one file moves to the trash without confirmation, and a permanent delete asks first");
|
||
|
||
await reset();
|
||
await row("a.md").click();
|
||
await row("a.md").focus();
|
||
await page.keyboard.press("Delete");
|
||
await row("a.md").waitFor({ state: "detached" });
|
||
assert.deepEqual(await calls("workspace_delete"), [{ rel: "a.md", permanent: false }]);
|
||
await row("b.md").click();
|
||
await row("b.md").focus();
|
||
await page.keyboard.press("Shift+Delete");
|
||
await row("b.md").waitFor({ state: "detached" });
|
||
assert.deepEqual((await calls("workspace_delete")).at(-1), { rel: "b.md", permanent: true });
|
||
assert.equal((await calls("plugin:dialog|message")).length, 1);
|
||
console.log("PASS: Delete moves the selection to the trash, and Shift+Delete deletes it permanently after confirmation");
|
||
|
||
await reset();
|
||
await row("notes").click();
|
||
await row("notes/nested").click();
|
||
await row("notes").click({ modifiers: ["Control"] });
|
||
await row("notes/a.md").click({ modifiers: ["Control"] });
|
||
await row("notes/nested/b.md").click({ modifiers: ["Control"] });
|
||
await row("c.md").click({ modifiers: ["Control"] });
|
||
await action("notes/a.md", "Make copies (5)");
|
||
assert.deepEqual((await calls("workspace_copy")).map(args => args.rel), ["notes", "c.md"]);
|
||
await action("notes/nested/b.md", "Delete (5)");
|
||
await row("notes").waitFor({ state: "detached" });
|
||
assert.deepEqual((await calls("workspace_delete")).map(args => args.rel), ["notes", "c.md"]);
|
||
assert.equal((await calls("plugin:dialog|message")).length, 1);
|
||
console.log("PASS: recursive copy and delete process selected folders and children once");
|
||
|
||
await reset();
|
||
await selectFiles();
|
||
await drag("a.md", "target");
|
||
await row("target/b.md").waitFor();
|
||
assert.deepEqual(await calls("workspace_rename"), [{ from: "a.md", to: "target/a 2.md", expectedRoot }, { from: "b.md", to: "target/b.md", expectedRoot }]);
|
||
assert.deepEqual(await selected(), ["target/a 2.md", "target/b.md"]);
|
||
console.log("PASS: group move into a collapsed folder preserves an existing same-name file");
|
||
|
||
await reset();
|
||
await row("notes").click();
|
||
await row("notes/a.md").click({ modifiers: ["Control"] });
|
||
await row("c.md").click({ modifiers: ["Control"] });
|
||
await drag("notes/a.md", "notes/nested");
|
||
assert.equal((await calls("workspace_rename")).length, 0);
|
||
await drag("notes/a.md", "target");
|
||
await row("target/notes").waitFor();
|
||
assert.deepEqual(await calls("workspace_rename"), [{ from: "notes", to: "target/notes", expectedRoot }, { from: "c.md", to: "target/c.md", expectedRoot }]);
|
||
assert.deepEqual(await selected(), ["target/notes", "target/c.md"]);
|
||
await row("target/notes").click();
|
||
await row("target/notes/a.md").waitFor();
|
||
console.log("PASS: mixed folder and file moves skip selected descendants and reject drops into the same folder");
|
||
|
||
await reset();
|
||
await selectFiles();
|
||
await drag("a.md", "c.md", "after");
|
||
assert.deepEqual((await marks()).order[""].filter(name => name.endsWith(".md")), ["c.md", "a.md", "b.md"]);
|
||
await drag("b.md", "c.md", "before");
|
||
assert.deepEqual((await marks()).order[""].filter(name => name.endsWith(".md")), ["a.md", "b.md", "c.md"]);
|
||
if (process.env.SCREENSHOT_PATH) {
|
||
await row("a.md").click({ button: "right" });
|
||
await page.screenshot({ path: process.env.SCREENSHOT_PATH });
|
||
}
|
||
console.log("PASS: group reorder preserves selected row order in both directions");
|
||
});
|