63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { handlePreviewClipboard } from "../src/preview-clipboard.ts";
|
|
|
|
test("clipboard broker accepts only current preview frames and bounded user-initiated text", async () => {
|
|
const results: unknown[] = [];
|
|
const source = { postMessage: (value: unknown) => results.push(value) } as unknown as Window;
|
|
const frames = [{ contentWindow: source, src: "https://example.test/preview/current" }];
|
|
const event = {
|
|
source,
|
|
data: {
|
|
type: "wrn-studio-copy",
|
|
id: "copy-1",
|
|
path: "/preview/current",
|
|
text: "npm install @wrnexus/ui",
|
|
},
|
|
};
|
|
const copied: string[] = [];
|
|
const write = async (text: string) => {
|
|
copied.push(text);
|
|
};
|
|
expect(await handlePreviewClipboard(event, frames, write)).toBe(true);
|
|
expect(copied).toEqual(["npm install @wrnexus/ui"]);
|
|
expect(results).toEqual([{ type: "wrn-studio-copy-result", id: "copy-1", ok: true }]);
|
|
expect(await handlePreviewClipboard({ ...event, source: null }, frames, write)).toBe(false);
|
|
expect(
|
|
await handlePreviewClipboard(
|
|
{ ...event, data: { ...event.data, path: "/preview/old" } },
|
|
frames,
|
|
write,
|
|
),
|
|
).toBe(false);
|
|
expect(
|
|
await handlePreviewClipboard(
|
|
{ ...event, data: { ...event.data, text: "x".repeat(16385) } },
|
|
frames,
|
|
write,
|
|
),
|
|
).toBe(false);
|
|
expect(await handlePreviewClipboard(event, frames, write, false)).toBe(true);
|
|
expect(copied).toHaveLength(1);
|
|
expect(results.at(-1)).toEqual({ type: "wrn-studio-copy-result", id: "copy-1", ok: false });
|
|
});
|
|
|
|
test("clipboard denial reports failure rather than claiming a copy succeeded", async () => {
|
|
let result: any;
|
|
const source = {
|
|
postMessage: (value: unknown) => {
|
|
result = value;
|
|
},
|
|
} as unknown as Window;
|
|
await handlePreviewClipboard(
|
|
{
|
|
source,
|
|
data: { type: "wrn-studio-copy", id: "copy-2", path: "/preview/current", text: "test" },
|
|
},
|
|
[{ contentWindow: source, src: "https://example.test/preview/current" }],
|
|
async () => {
|
|
throw Error("denied");
|
|
},
|
|
);
|
|
expect(result.ok).toBe(false);
|
|
});
|