Files
openidea/src/platform/clipboard.test.ts
T
Bert Verhelstandbertyhell 69d839e123 feat(plugins): read the clipboard from user-invoked commands (API 1.11) (#4047)
Plugins could write the clipboard but not read it. ui.read-clipboard() (SDK host::read_clipboard()) returns the text and a PNG image of the user's clipboard, behind a new clipboard.read capability. Only calls the user started can read: plugin commands and view actions run by the user, not event handlers, web view messages or commands chained through command-runner. The host never reads the clipboard itself: it asks the invoking window (ClipboardRequested / plugin_answer_clipboard), so remote development reads the client's clipboard, and a remote window asks the user first. Plugin API 1.11.0; components rebuilt.

Closes #3524

Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com>
2026-11-18 09:20:09 +08:00

122 lines
4.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commands } from "./ipc";
import { pasteClipboardFiles, readClipboardPng, readClipboardText, writeClipboardText } from "./clipboard";
beforeEach(() => vi.stubGlobal("__TAURI_INTERNALS__", {}));
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
function transfer(text: string, files = true): DataTransfer {
return { getData: () => text, files: [], types: files ? ["Files"] : ["text/plain"] } as unknown as DataTransfer;
}
const cases = [
{ name: "text present", text: "plain text", paths: ["/stale"], expected: "plain text", calls: 0 },
{
name: "files plus list",
text: "",
paths: ["/a b", "/中文", "C:\\folder"],
expected: "/a b\n/中文\nC:\\folder",
calls: 1,
},
{ name: "files without list", text: "", paths: [], expected: "", calls: 1 },
{ name: "backend error", text: "", paths: null, expected: "", calls: 1 },
];
describe("clipboard text", () => {
it.each(cases)("$name", async ({ text, paths, expected, calls }) => {
const list = vi.spyOn(commands, "clipboardFileList");
if (paths) list.mockResolvedValue({ status: "ok", data: paths });
else list.mockRejectedValue(new Error("busy"));
expect(await readClipboardText(transfer(text))).toBe(expected);
expect(list).toHaveBeenCalledTimes(calls);
list.mockClear();
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { readText: async () => text } });
expect(await readClipboardText()).toBe(expected);
expect(list).toHaveBeenCalledTimes(calls);
});
it("does not consult files for an empty text-only event or a browser", async () => {
const list = vi.spyOn(commands, "clipboardFileList");
expect(await readClipboardText(transfer("", false))).toBe("");
vi.unstubAllGlobals();
expect(await readClipboardText(transfer(""))).toBe("");
expect(list).not.toHaveBeenCalled();
});
it("does not intercept text, already handled images, or an absent transfer", () => {
const paste = vi.fn();
for (const data of [transfer("text"), null]) {
const event = new Event("paste", { cancelable: true }) as ClipboardEvent;
Object.defineProperty(event, "clipboardData", { value: data });
expect(pasteClipboardFiles(event, paste)).toBe(false);
}
const event = new Event("paste", { cancelable: true }) as ClipboardEvent;
Object.defineProperty(event, "clipboardData", { value: transfer("") });
event.preventDefault();
expect(pasteClipboardFiles(event, paste)).toBe(false);
expect(paste).not.toHaveBeenCalled();
});
});
describe("clipboard writes", () => {
it.each(["com.acme.A#run", "/p/src/A.java:5", "中文\n😀", ""])(
"writes %j natively without browser activation",
async (text) => {
const native = vi.spyOn(commands, "clipboardWriteText").mockResolvedValue({ status: "ok", data: null });
const browser = vi
.fn()
.mockRejectedValue(
new DOMException(
"The request is not allowed by the user agent or the platform in the current context",
"NotAllowedError",
),
);
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText: browser } });
await Promise.resolve();
await writeClipboardText(text);
expect(native).toHaveBeenCalledExactlyOnceWith(text);
expect(browser).not.toHaveBeenCalled();
},
);
it("reports native errors instead of claiming success", async () => {
vi.spyOn(commands, "clipboardWriteText").mockResolvedValue({
status: "error",
error: { kind: "internal", message: "busy", data: null },
});
await expect(writeClipboardText("text")).rejects.toThrow("busy");
});
it("uses the browser clipboard outside Tauri", async () => {
vi.unstubAllGlobals();
const native = vi.spyOn(commands, "clipboardWriteText");
const browser = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText: browser } });
await writeClipboardText("web preview");
expect(browser).toHaveBeenCalledExactlyOnceWith("web preview");
expect(native).not.toHaveBeenCalled();
});
});
describe("clipboard image", () => {
it("is the PNG's base64, without the data URL prefix", async () => {
const image = vi.spyOn(commands, "clipboardImage");
image.mockResolvedValue({ status: "ok", data: "data:image/png;base64,iVBORw0KGgo=" });
expect(await readClipboardPng()).toBe("iVBORw0KGgo=");
image.mockResolvedValue({ status: "ok", data: null });
expect(await readClipboardPng()).toBeNull();
image.mockRejectedValue(new Error("busy"));
await expect(readClipboardPng()).rejects.toThrow("busy");
});
it("is none outside the desktop app", async () => {
vi.unstubAllGlobals();
const image = vi.spyOn(commands, "clipboardImage");
expect(await readClipboardPng()).toBeNull();
expect(image).not.toHaveBeenCalled();
});
});