Files
mnote/wolai-frontend/src/lib/file-tree/clipboard.test.ts
T
lix-2026 384da4e44c feat(tree): checkpoint resource lifecycle work
提交当前顶层 mnote Git 工作区,范围集中在 04-tree-domain 的 resource/trash/filetree 生命周期、mnote-web resource_trash 路由、Convex/Next 兼容接口、sidebar/file-tree 客户端适配、smoke 脚本与对应设计/bug 记录。

不包含被 ignore 的 design/05-editor-mainline/reference-code/leptos-tiptap 嵌套仓库改动。新增 smoke 的测试密码改为运行时读取 MNOTE_E2E_PASSWORD,避免提交明文 credential assignment。
2026-05-16 07:38:45 +08:00

58 lines
2.4 KiB
TypeScript

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<string, any>();
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<string, any>();
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);
});
});