Files
RuneStone/tests/smoke/links/fileDragLinks.smoke.mjs
2026-11-16 04:21:21 -07:00

82 lines
4.7 KiB
JavaScript

// File tree drags into Writer and the Source: documents become escaped links, images embed, and undo restores the source.
import assert from "node:assert/strict";
import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("links/fileDragLinks", async ({ page }) => {
await page.addInitScript(() => localStorage.setItem("runestone.settings", JSON.stringify({ autosaveEnabled: false })));
await mockTauri(page, {
commands: test => {
const svg = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="80"><rect width="100" height="80" fill="#639"/></svg>';
const workspace = test.workspace({
root: "C:\\file-drag-links",
files: { "notes/draft.md": "Before.\n\nAfter.\n", "files/report [Q1] (#1).pdf": "PDF fixture", "images/diagram (#1).svg": btoa(svg) },
});
return {
...workspace,
workspace_read_data_url: ({ rel }) => "data:image/svg+xml;base64," + workspace.files.get(rel),
plugins_snapshot: () => ({ plugins: [], issues: [] }),
spelling_menu_install: () => null,
};
},
});
const row = path => page.locator(`[data-tree-rel="${path}"]`);
async function drag(path, target) {
const from = await row(path).boundingBox(), to = await page.locator(target).boundingBox();
assert.ok(from && to, "The file and editor must be visible");
await page.mouse.move(from.x + Math.min(80, from.width / 2), from.y + from.height / 2);
await page.mouse.down();
await page.mouse.move(to.x + 45, to.y + 12, { steps: 15 });
await page.locator(".image-drop-target").waitFor();
await page.mouse.up();
}
const writerSource = () => page.evaluate(() => window.dragWriter.state.doc.toString());
const ideSource = () => page.evaluate(() => window.dragSource.state.doc.toString());
await page.goto(smokeUrl);
await row("notes").click();
await row("notes/draft.md").dblclick();
await page.locator(".cm-content").waitFor();
await page.evaluate(async () => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
window.dragWriter = EditorView.findFromDOM(document.querySelector(".cm-content"));
});
await row("files").click();
const original = await writerSource();
await drag("files/report [Q1] (#1).pdf", ".cm-content");
await page.waitForFunction(() => window.dragWriter.state.doc.toString().includes(".pdf)"));
const linked = await writerSource();
assert.ok(linked.includes("[report \\[Q1\\] (#1).pdf](../files/report%20%5BQ1%5D%20%28%231%29.pdf)"), linked);
assert.ok(!linked.includes("!["), "A document must be a link, not an image");
await page.evaluate(() => window.dragWriter.focus());
await page.keyboard.press("Control+z");
assert.equal(await writerSource(), original, "Writer undo restores the exact source");
await row("images").click();
await drag("images/diagram (#1).svg", ".cm-content");
await page.locator(".md-image-frame img").waitFor();
assert.ok((await writerSource()).includes("![](../images/diagram%20%28%231%29.svg)"));
await page.evaluate(() => window.dragWriter.focus());
await page.keyboard.press("Control+z");
assert.equal(await writerSource(), original);
console.log("PASS: Writer drags insert escaped document links and image embeds, and undo restores the source");
await page.keyboard.press("Control+e");
await page.locator(".source-editor .cm-content").waitFor();
await page.evaluate(async () => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
window.dragSource = EditorView.findFromDOM(document.querySelector(".source-editor .cm-content"));
});
const originalIde = await ideSource();
await drag("files/report [Q1] (#1).pdf", ".source-editor .cm-content");
await page.waitForFunction(() => window.dragSource.state.doc.toString().includes(".pdf)"));
assert.ok((await ideSource()).includes("[report \\[Q1\\] (#1).pdf](../files/report%20%5BQ1%5D%20%28%231%29.pdf)"));
await page.evaluate(() => window.dragSource.focus());
assert.equal(await page.evaluate(() => window.dragSource.hasFocus), true);
await page.keyboard.press("Control+z");
await page.waitForFunction(expected => window.dragSource.state.doc.toString() === expected, originalIde, { timeout: 3000 });
assert.equal(await ideSource(), originalIde, "Source undo restores the exact source");
await drag("images/diagram (#1).svg", ".source-editor .cm-content");
assert.ok((await ideSource()).includes("![](../images/diagram%20%28%231%29.svg)"));
assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(call => ["workspace_rename", "workspace_write_bytes"].includes(call.command)).length), 0);
console.log("PASS: Source drags insert escaped document links and image embeds, undo restores the source, and no file moves");
}, { timeout: 15000 });