import assert from "node:assert/strict"; import test from "node:test"; import { EditorState } from "@codemirror/state"; import { attachmentRel, hasFileDrag, imageFiles, imageInsertion, workspaceImageLink } from "../src/lib/workspace/media.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("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]"]); });