import assert from "node:assert/strict"; import test from "node:test"; import { copySelection, cutSelection, pasteClipboard, writerContextItems, } from "../../../src/lib/editor/writerMenu.ts"; type Harness = { selected: string; inTable: boolean; deleted: boolean; focused: boolean; inserted: string | null; dispatched: boolean; }; function fakeEditor(init?: { selected?: string; inTable?: boolean }) { const harness: Harness = { selected: init?.selected ?? "", inTable: init?.inTable ?? false, deleted: false, focused: false, inserted: null, dispatched: false, }; const tr = { insertText(text: string) { harness.inserted = text; return tr; }, }; const chainApi = { focus() { harness.focused = true; return chainApi; }, deleteSelection() { harness.deleted = true; return chainApi; }, run() { return true; }, }; const editor = { isActive(name: string) { return name === "table" && harness.inTable; }, chain() { return chainApi; }, state: { selection: { from: 0, to: harness.selected.length }, doc: { textBetween() { return harness.selected; }, }, tr, }, view: { dispatch() { harness.dispatched = true; }, }, }; return { editor, harness }; } function withClipboard( impl: { writeText?: (text: string) => Promise; readText?: () => Promise }, run: () => Promise, ): Promise { const previous = Object.getOwnPropertyDescriptor(globalThis, "navigator"); Object.defineProperty(globalThis, "navigator", { configurable: true, value: { clipboard: impl }, }); return run().finally(() => { if (previous) { Object.defineProperty(globalThis, "navigator", previous); } else { Reflect.deleteProperty(globalThis, "navigator"); } }); } function actionLabels(items: ReturnType): string[] { const labels: string[] = []; for (const item of items) { if (item.kind === "action") { labels.push(item.label); } else if (item.kind === "group") { for (const sub of item.items) { if (sub.kind === "action") { labels.push(sub.label); } } } } return labels; } test("paste inserts clipboard text without parsing tags", async () => { const { editor, harness } = fakeEditor(); await withClipboard( { readText: async () => "bold" }, async () => { await pasteClipboard(editor as never); }, ); assert.equal(harness.focused, true); assert.equal(harness.dispatched, true); assert.equal(harness.inserted, "bold"); }); test("paste swallows clipboard errors", async () => { const { editor, harness } = fakeEditor(); await withClipboard( { readText: async () => { throw new Error("denied"); }, }, async () => { await pasteClipboard(editor as never); }, ); assert.equal(harness.dispatched, false); assert.equal(harness.inserted, null); }); test("cut keeps the selection when clipboard write fails", async () => { const { editor, harness } = fakeEditor({ selected: "gone" }); await withClipboard( { writeText: async () => { throw new Error("denied"); }, }, async () => { await cutSelection(editor as never); }, ); assert.equal(harness.deleted, false); }); test("copy swallows clipboard errors", async () => { const { editor } = fakeEditor({ selected: "keep" }); let attempted = false; await withClipboard( { writeText: async () => { attempted = true; throw new Error("denied"); }, }, async () => { await copySelection(editor as never); }, ); assert.equal(attempted, true); }); test("cut writes selected text then deletes it", async () => { const { editor, harness } = fakeEditor({ selected: "clip" }); let written = ""; await withClipboard( { writeText: async (text) => { written = text; }, }, async () => { await cutSelection(editor as never); }, ); assert.equal(written, "clip"); assert.equal(harness.deleted, true); }); test("table row and column actions disable outside a table", () => { const { editor } = fakeEditor({ inTable: false }); const items = writerContextItems(editor as never); const table = items.find((item) => item.kind === "group" && item.label === "Table"); assert.equal(table?.kind, "group"); if (table?.kind !== "group") { return; } const byLabel = new Map( table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]), ); assert.equal(byLabel.get("Insert table")?.disabled, undefined); assert.equal(byLabel.get("Add row above")?.disabled, true); assert.equal(byLabel.get("Delete table")?.disabled, true); }); test("table row and column actions enable inside a table", () => { const { editor } = fakeEditor({ inTable: true }); const items = writerContextItems(editor as never); const table = items.find((item) => item.kind === "group" && item.label === "Table"); assert.equal(table?.kind, "group"); if (table?.kind !== "group") { return; } const byLabel = new Map( table.items.filter((item) => item.kind === "action").map((item) => [item.label, item]), ); assert.equal(byLabel.get("Insert table")?.disabled, undefined); assert.equal(byLabel.get("Add row above")?.disabled, false); assert.equal(byLabel.get("Delete table")?.disabled, false); }); test("style and format actions cover the requested commands", () => { const { editor } = fakeEditor(); const labels = actionLabels(writerContextItems(editor as never)); for (const label of [ "Cut", "Copy", "Paste", "Heading 1", "Heading 6", "Body", "Quote", "Bullet list", "Numbered list", "Task list", "Bold", "Italic", "Strikethrough", "Highlight", "Code", "Math", "Comment", "Clear formatting", "Insert table", ]) { assert.equal(labels.includes(label), true, label); } assert.equal(labels.includes("Superscript"), false); assert.equal(labels.includes("Subscript"), false); });