import { describe, expect, it } from "vitest"; import { clearFileTreeClipboardPayload, decodeFileTreeClipboardPayload, encodeFileTreeClipboardPayload, inferPasteTargetDocId, isTextInputTarget, } from "./clipboard"; describe("file-tree clipboard payload", () => { it("可编码/解码", () => { const payload = { type: "mnote-file-tree" as const, version: 1 as const, action: "copy" as const, rowIds: ["doc:a", "asset:x"] }; const text = encodeFileTreeClipboardPayload(payload); expect(decodeFileTreeClipboardPayload(text)).toEqual(payload); const cutPayload = { ...payload, action: "cut" as const }; expect(decodeFileTreeClipboardPayload(encodeFileTreeClipboardPayload(cutPayload))).toEqual(cutPayload); expect( decodeFileTreeClipboardPayload( encodeFileTreeClipboardPayload({ ...payload, action: "move" as any }), ), ).toBeNull(); expect(decodeFileTreeClipboardPayload("not-a-payload")).toBeNull(); }); it("可清空内存剪贴板", async () => { await clearFileTreeClipboardPayload(); }); }); describe("inferPasteTargetDocId", () => { it("focused 为 doc/index/asset 三分支覆盖", () => { const rowById = new Map(); rowById.set("asset:x", { kind: "asset", rowId: "asset:x", docId: "d1", asset: { id: "x" } }); expect(inferPasteTargetDocId({ focusedRowId: "doc:d2", rowById, activeDocId: "active" })).toBe("d2"); expect(inferPasteTargetDocId({ focusedRowId: "index:d3", rowById, activeDocId: "active" })).toBe("d3"); expect(inferPasteTargetDocId({ focusedRowId: "asset:x", rowById, activeDocId: "active" })).toBe("d1"); }); it("无 focused 时回退 activeDocId", () => { const rowById = new Map(); expect(inferPasteTargetDocId({ focusedRowId: null, rowById, activeDocId: "active" })).toBe("active"); expect(inferPasteTargetDocId({ focusedRowId: null, rowById, activeDocId: null })).toBeNull(); }); }); describe("isTextInputTarget", () => { it("input/textarea/contenteditable 不拦截快捷键", () => { const input = document.createElement("input"); const textarea = document.createElement("textarea"); const div = document.createElement("div"); div.setAttribute("contenteditable", "true"); expect(isTextInputTarget(input)).toBe(true); expect(isTextInputTarget(textarea)).toBe(true); expect(isTextInputTarget(div)).toBe(true); expect(isTextInputTarget(document.createElement("button"))).toBe(false); }); });