// Project Management card images: a GIF copied in a web browser keeps its original file. A failed download keeps the still copy. import assert from "node:assert/strict"; import { runSmoke } from "../_harness/index.mjs"; import { cardPath, fileText, loadWorkspace, openBoard, pane, waitForFile } from "../_harness/pmFixture.mjs"; const imageFile = id => `.runestone/pm/images/${id}.json`; await runSmoke("features/pmImages", async ({ page }) => { await loadWorkspace(page); await openBoard(page, "Roadmap"); await pane(page).locator(".pm-card-title", { hasText: /^Card A$/ }).click(); const editor = page.locator("dialog.pm-card-editor[open]"); await editor.waitFor(); await editor.getByRole("button", { name: "Edit", exact: true }).click(); const description = editor.locator('textarea[aria-label="Details (Markdown)"]'); await description.fill(""); // A browser puts a still PNG on the clipboard, with HTML that names the original GIF. async function pasteWebImage(src) { await description.evaluate((element, src) => { const png = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="), c => c.charCodeAt(0)); const data = new DataTransfer(); data.items.add(new File([png], "image.png", { type: "image/png" })); data.setData("text/html", ``); element.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData: data })); }, src); } const reference = name => new RegExp(`!\\[${name.replace(".", "\\.")}\\]\\(pm-image:([0-9a-f-]{36})\\)`); await pasteWebImage("https://media.example.com/cat.gif"); await page.waitForFunction(() => /!\[image\.gif\]\(pm-image:/.test(document.querySelector('dialog.pm-card-editor[open] textarea[aria-label="Details (Markdown)"]').value)); const download = await page.evaluate(() => window.__TEST__.calls.find(call => call.command === "workspace_download_image").args); assert.equal(download.url, "https://media.example.com/cat.gif"); // When the download fails, the card gets the still PNG. await pasteWebImage("https://offline.example.com/cat.gif"); await page.waitForFunction(() => /!\[image\.png\]\(pm-image:/.test(document.querySelector('dialog.pm-card-editor[open] textarea[aria-label="Details (Markdown)"]').value)); await editor.getByText("The website refused the original image, so RuneStone added only its first frame.", { exact: false }).waitFor(); // The RuneStone browser keeps the bytes of the image that it copied. The website gets no second request. await page.evaluate(async () => (await import("/src/lib/workspace/copiedImage.ts")).keepCopiedImage("https://offline.example.com/copied.gif", Promise.resolve("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"))); const downloads = () => page.evaluate(() => window.__TEST__.calls.filter(call => call.command === "workspace_download_image").length); const downloadsBefore = await downloads(); await pasteWebImage("https://offline.example.com/copied.gif"); await page.waitForFunction(() => (document.querySelector('dialog.pm-card-editor[open] textarea[aria-label="Details (Markdown)"]').value.match(/!\[image\.gif\]/g) || []).length === 2); assert.equal(await downloads(), downloadsBefore); assert.equal(await editor.getByText("The website refused the original image", { exact: false }).count(), 0, "A kept original clears the message"); // An empty file from a browser drag gives a message, not the size limit. await description.evaluate(element => { const data = new DataTransfer(); data.items.add(new File([], "viprsr188.gif", { type: "image/gif" })); element.dispatchEvent(new DragEvent("drop", { bubbles: true, cancelable: true, dataTransfer: data })); }); await editor.getByText("The image file is empty.", { exact: false }).waitFor(); assert.equal(await editor.getByText("Use an image between 1 byte and 50 MiB.").count(), 0); await editor.locator(".pm-description-actions").getByRole("button", { name: "Save", exact: true }).click(); await waitForFile(page, cardPath("card-a"), /pm-image:.*\n?[\s\S]*pm-image:/); const body = await fileText(page, cardPath("card-a")); const gif = reference("image.gif").exec(body)[1], png = reference("image.png").exec(body)[1]; assert.match(await fileText(page, imageFile(gif)), /"data":"data:image\/gif;base64,R0lGOD/); assert.match(await fileText(page, imageFile(png)), /"data":"data:image\/png;base64,iVBOR/); console.log("PASS: a pasted web GIF keeps its original file, a copied original needs no download, and failures explain the cause"); });