import assert from "node:assert/strict"; import test from "node:test"; import { EditorState } from "@codemirror/state"; import { attachmentRel, dataUrlExtension, hasFileDrag, imageFiles, imageInsertion, mayAnimate, pastedImageSource, workspaceImageLink } from "../../../src/lib/workspace/media.ts"; import { copiedImage, keepCopiedImage } from "../../../src/lib/workspace/copiedImage.ts"; import { imageSource, editImageSource, imageSourceRanges } from "../../../src/lib/editor/imageSource.ts"; import { imageDrops, trackImageDrop } from "../../../src/lib/editor/imageDrop.ts"; test("same-name image imports receive distinct safe attachment paths", () => { const paths = new Set(Array.from({ length: 100 }, () => attachmentRel("photo (1).PNG"))); assert.equal(paths.size, 100); assert.ok([...paths].every(path => /^attachments\/[\w.-]+\.PNG$/.test(path))); }); test("protected file drags and files without MIME metadata are recognized", () => { assert.equal(hasFileDrag({ types: ["Files"], files: [] } as unknown as DataTransfer), true); assert.equal(hasFileDrag({ types: ["text/plain"] } as unknown as DataTransfer), false); const files = [{ name: "PHOTO.PNG", type: "" }, { name: "note.md", type: "text/plain" }]; assert.deepEqual(imageFiles({ files } as unknown as DataTransfer), [files[0]]); }); test("a copied web image names its original file in the clipboard HTML", () => { const copy = (src: string) => `\r\n\r\n\r\n`; assert.equal(pastedImageSource(copy("https://media.example.com/cat.gif")), "https://media.example.com/cat.gif"); assert.equal(pastedImageSource(copy("https://cdn.example.com/a/b.WEBP?width=200&ex=1")), "https://cdn.example.com/a/b.WEBP?width=200&ex=1"); assert.equal(pastedImageSource("x"), "http://example.com/x.avif"); assert.equal(pastedImageSource(""), "https://example.com/x.gif"); assert.equal(pastedImageSource(copy("https://example.com/photo.jpg")), "https://example.com/photo.jpg"); // Firefox copies the element of the page with all of its attributes. assert.equal(pastedImageSource(''), "https://h.example.net/om/1/x.gif"); // A missing address and other schemes keep the clipboard copy. assert.equal(pastedImageSource(copy("data:image/gif;base64,R0lGODlh")), undefined); assert.equal(pastedImageSource(copy("file:///C:/cat.gif")), undefined); assert.equal(pastedImageSource(copy("cat.gif")), undefined); assert.equal(pastedImageSource(''), undefined); assert.equal(pastedImageSource(""), undefined); // A copy of a page part with two images is not a copied image. assert.equal(pastedImageSource(copy("https://example.com/a.gif") + copy("https://example.com/b.gif")), undefined); }); test("only formats with frames get a download, and data URLs name their extension", () => { assert.equal(mayAnimate("https://media.example.com/cat.gif"), true); assert.equal(mayAnimate("https://cdn.example.com/a/b.WEBP?width=200"), true); assert.equal(mayAnimate("http://example.com/x.avif"), true); assert.equal(mayAnimate("https://example.com/photo.jpg"), false); assert.equal(mayAnimate("https://example.com/image?format=gif"), false); assert.equal(dataUrlExtension("data:image/gif;base64,R0lGODlh"), "gif"); assert.equal(dataUrlExtension("data:image/jpeg;base64,/9j/"), "jpg"); assert.equal(dataUrlExtension("data:image/webp;base64,UklG"), "webp"); assert.equal(dataUrlExtension("data:image/svg+xml;base64,PHN2Zz4="), undefined); assert.equal(dataUrlExtension("data:text/html;base64,PGh0bWw+"), undefined); }); test("a paste uses only the original of the image that the RuneStone browser copied last", async () => { assert.equal(await copiedImage("https://example.com/a.gif"), undefined); let finish: (value: string) => void = () => {}; keepCopiedImage("https://example.com/a.gif", new Promise(resolve => { finish = resolve; })); const waiting = copiedImage("https://example.com/a.gif"); finish("data:image/gif;base64,R0lGODlh"); assert.equal(await waiting, "data:image/gif;base64,R0lGODlh"); assert.equal(await copiedImage("https://example.com/b.gif"), undefined); keepCopiedImage("https://example.com/b.gif", Promise.reject(new Error("engine stopped"))); assert.equal(await copiedImage("https://example.com/b.gif"), undefined); assert.equal(await copiedImage("https://example.com/a.gif"), undefined, "a newer copy replaces the older one"); }); test("image batches form independent Markdown blocks without extra blank lines", () => { assert.equal(imageInsertion("beforeafter", 6, 6, ["![](a.png)", "![](b.png)"]), "\n\n![](a.png)\n\n![](b.png)\n\n"); assert.equal(imageInsertion("before\n\nafter", 8, 8, ["![](a.png)"]), "![](a.png)\n\n"); assert.equal(imageInsertion("before\r\n\r\nafter", 10, 10, ["![](a.png)"]), "![](a.png)\n\n"); }); test("pending image drops follow intervening edits instead of the current caret", () => { let state = EditorState.create({ doc: "before\nafter", extensions: [imageDrops] }); const id = Symbol(); state = state.update({ effects: trackImageDrop.of({ id, from: 7, to: 7 }) }).state; state = state.update({ changes: { from: 0, insert: "new\n" }, selection: { anchor: 0 } }).state; assert.deepEqual(state.field(imageDrops).get(id), { from: 11, to: 11 }); }); test("navigator image links are relative and encode Markdown and URL punctuation", () => { assert.equal(workspaceImageLink("notes/drafts/note.md", "images/a (#1).png"), "![](../../images/a%20%28%231%29.png)"); assert.equal(workspaceImageLink("notes/note.md", "notes/photo.png"), "![](photo.png)"); assert.equal(workspaceImageLink("note.md", "日本語/photo%.png"), "![](%E6%97%A5%E6%9C%AC%E8%AA%9E/photo%25.png)"); }); test("image source is explicitly activated, tracks edits, and hides on leaving", () => { let state = EditorState.create({ doc: "![](a.png)\n\nText", extensions: [imageSource] }); state = state.update({ selection: { anchor: 0, head: 10 } }).state; assert.equal(state.field(imageSource), null); state = state.update({ effects: editImageSource.of({ from: 0, to: 10 }) }).state; state = state.update({ changes: { from: 2, insert: "Alt" } }).state; assert.deepEqual(state.field(imageSource), { from: 0, to: 13 }); state = state.update({ selection: { anchor: state.doc.length } }).state; assert.equal(state.field(imageSource), null); }); test("image ranges distinguish embeds from code and retain reference and wiki syntax", () => { const source = "`![](code.png)`\n\nText ![alt]() and ![[a b.png|Photo]]\n\n![ref][photo]\n\n[photo]: x.png\n\n```\n![](code.png)\n```"; assert.deepEqual(imageSourceRanges(source).map(range => source.slice(range.from, range.to)), ["![alt]()", "![[a b.png|Photo]]", "![ref][photo]"]); });