// Project Management description drafts with images: an image pasted in an unsaved description survives a close, // a reopen, and a reload of the card. Save writes the image file. Cancel removes the image data and writes no image file. import assert from "node:assert/strict"; import { runSmoke } from "../_harness/index.mjs"; import { cardPath, draftPath, fileText, loadWorkspace, openBoard, pane, waitForFile } from "../_harness/pmFixture.mjs"; const imageFile = id => `.runestone/pm/images/${id}.json`; const draftImagesPath = id => draftPath(id).replace(/\.json$/, ".images.json"); const json = async (page, path) => JSON.parse(await fileText(page, path) ?? "null"); await runSmoke("features/pmDraftImages", async ({ page }) => { await loadWorkspace(page); await openBoard(page, "Roadmap"); const editor = page.locator("dialog.pm-card-editor[open]"); const description = editor.locator('textarea[aria-label="Details (Markdown)"]'); const descriptionAction = name => editor.locator(".pm-description-actions").getByRole("button", { name, exact: true }); const openCard = async title => { await pane(page).locator(".pm-card-title", { hasText: new RegExp(`^${title}$`) }).click(); await editor.waitFor(); await editor.getByRole("button", { name: "Edit", exact: true }).click(); }; const closeCard = async () => { await editor.getByRole("button", { name: "Close card", exact: true }).click(); await editor.waitFor({ state: "detached" }); }; // Paste a 1 by 1 PNG into the description. Returns the image ID of the new reference. const pasteImage = async name => { await description.evaluate((element, name) => { const png = Uint8Array.from(atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="), c => c.charCodeAt(0)); const data = new DataTransfer(); data.items.add(new File([png], name, { type: "image/png" })); element.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData: data })); }, name); const reference = new RegExp(`!\\[${name.replace(".", "\\.")}\\]\\(pm-image:([0-9a-f-]{36})\\)`); await page.waitForFunction(source => new RegExp(source).test(document.querySelector('dialog.pm-card-editor[open] textarea[aria-label="Details (Markdown)"]').value), reference.source); return reference.exec(await description.inputValue())[1]; }; // Wait until the gallery thumbnail of `name` shows the decoded image. const imageLoads = async name => { await page.waitForFunction(alt => { const image = document.querySelector(`dialog.pm-card-editor[open] .pm-card-images img[alt="${alt}"]`); return !!image?.complete && image.naturalWidth > 0; }, name); assert.equal(await editor.locator(".pm-card-images .pm-error").allInnerTexts().then(texts => texts.join("")), ""); }; // A pasted image stays with the draft when the card closes. The image folder gets no file yet. await openCard("Card A"); const first = await pasteImage("dot.png"); await imageLoads("dot.png"); await closeCard(); await waitForFile(page, draftPath("card-a"), new RegExp(`pm-image:${first}`)); assert.match((await json(page, draftImagesPath("card-a"))).images[first], /^data:image\/png;base64,iVBOR/); assert.equal(await fileText(page, imageFile(first)), undefined, "Only a save writes the image file"); assert.match(await fileText(page, cardPath("card-a")), /\nBody of Card A\.\n$/, "Close keeps the saved description"); // The reopened card restores the draft and shows its image. Save writes the image file and ends the draft. await openCard("Card A"); await imageLoads("dot.png"); assert.match(await description.inputValue(), new RegExp(`pm-image:${first}`)); await descriptionAction("Save").click(); await waitForFile(page, cardPath("card-a"), new RegExp(`!\\[dot\\.png\\]\\(pm-image:${first}\\)`)); assert.match(await fileText(page, imageFile(first)), /"data":"data:image\/png;base64,iVBOR/); assert.equal((await json(page, draftPath("card-a"))).body, null); assert.deepEqual((await json(page, draftImagesPath("card-a"))).images, {}); await closeCard(); await openCard("Card A"); await imageLoads("dot.png"); await closeCard(); // The draft saves after the paste, without another edit, so the image survives a reload of the app. await openCard("Card B"); const second = await pasteImage("second.png"); await waitForFile(page, draftImagesPath("card-b"), new RegExp(second)); await waitForFile(page, draftPath("card-b"), new RegExp(`pm-image:${second}`)); await page.reload(); await page.locator('[data-tree-rel="note.md"]').waitFor(); await openBoard(page, "Roadmap"); await openCard("Card B"); await imageLoads("second.png"); // Cancel removes the draft and its image data. The image folder gets no file. await descriptionAction("Cancel").click(); await page.waitForFunction(rel => /"body":null/.test(window.__TEST__.files.get(rel) ?? ""), draftPath("card-b")); assert.deepEqual((await json(page, draftImagesPath("card-b"))).images, {}); assert.equal(await fileText(page, imageFile(second)), undefined); assert.doesNotMatch(await fileText(page, cardPath("card-b")), /pm-image:/); await closeCard(); console.log("PASS: a draft image survives a close, a reopen, and a reload. Save writes its file. Cancel leaves no image data."); });