The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { ComposerContextId, EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | COMPOSER_CONTEXT_CLIPBOARD_MIME, |
| 4 | decodeComposerContextClipboardHtml, |
| 5 | encodeComposerContextFragment, |
| 6 | } from "@t3tools/shared/composerContextClipboard"; |
| 7 | import { expect, it } from "vite-plus/test"; |
| 8 | |
| 9 | import { copySnapshot } from "./copySnapshot"; |
| 10 | |
| 11 | it.each([false, true])( |
| 12 | "preserves image bytes and snapshot metadata, custom format rejected: %s", |
| 13 | async (rejectCustom) => { |
| 14 | class Item { |
| 15 | constructor(readonly data: Record<string, Blob | Promise<Blob>>) {} |
| 16 | } |
| 17 | let clipboard: Item | undefined; |
| 18 | const write = async ([item]: Item[]) => { |
| 19 | if (rejectCustom && item?.data[COMPOSER_CONTEXT_CLIPBOARD_MIME]) |
| 20 | throw new Error("Unsupported format"); |
| 21 | clipboard = item; |
| 22 | await Promise.all(Object.values(item!.data)); |
| 23 | }; |
| 24 | const originals = { |
| 25 | navigator: Object.getOwnPropertyDescriptor(globalThis, "navigator"), |
| 26 | ClipboardItem: Object.getOwnPropertyDescriptor(globalThis, "ClipboardItem"), |
| 27 | }; |
| 28 | using _globals = { |
| 29 | [Symbol.dispose]() { |
| 30 | for (const key of ["navigator", "ClipboardItem"] as const) { |
| 31 | const descriptor = originals[key]; |
| 32 | if (descriptor) Object.defineProperty(globalThis, key, descriptor); |
| 33 | else Reflect.deleteProperty(globalThis, key); |
| 34 | } |
| 35 | }, |
| 36 | }; |
| 37 | Object.defineProperty(globalThis, "navigator", { |
| 38 | configurable: true, |
| 39 | value: { clipboard: { write } }, |
| 40 | }); |
| 41 | Object.defineProperty(globalThis, "ClipboardItem", { configurable: true, value: Item }); |
| 42 | const fragment = encodeComposerContextFragment({ |
| 43 | version: 1, |
| 44 | source: { environmentId: EnvironmentId.make("snapshot-source") }, |
| 45 | records: [ |
| 46 | { |
| 47 | version: 1, |
| 48 | kind: "image", |
| 49 | contextId: ComposerContextId.make("image_snapshot"), |
| 50 | label: "Snapshot", |
| 51 | attachmentId: "snapshot", |
| 52 | name: "snapshot.png", |
| 53 | mimeType: "image/png", |
| 54 | sizeBytes: 3, |
| 55 | source: { |
| 56 | kind: "snap-shot", |
| 57 | capturedAt: "2026-01-01T00:00:00.000Z", |
| 58 | appName: "Finder", |
| 59 | windowTitle: "Downloads", |
| 60 | accessibleText: "Downloads", |
| 61 | }, |
| 62 | }, |
| 63 | ], |
| 64 | })!; |
| 65 | const image = "data:image/png;base64,AQID"; |
| 66 | await copySnapshot(image, "Snapshot", fragment); |
| 67 | const png = await clipboard!.data["image/png"]!; |
| 68 | expect(png.type).toBe("image/png"); |
| 69 | expect([...new Uint8Array(await png.arrayBuffer())]).toEqual([1, 2, 3]); |
| 70 | expect( |
| 71 | decodeComposerContextClipboardHtml(await (await clipboard!.data["text/html"]!).text()), |
| 72 | ).toEqual(JSON.parse(fragment)); |
| 73 | expect(await (await clipboard!.data["text/plain"]!).text()).toBe("Snapshot"); |
| 74 | }, |
| 75 | ); |