The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /// <reference lib="dom" /> |
| 2 | // Holds pasted-image compression open in the page, so a scenario can act while an image is |
| 3 | // still preparing, then tells it when every image decode, encode and read has settled. |
| 4 | import { expect } from "e2e"; |
| 5 | import type { Browser } from "@e2e-dev/web"; |
| 6 | |
| 7 | interface CompressionHold { |
| 8 | readonly release: () => void; |
| 9 | readonly releaseNext: () => void; |
| 10 | readonly held: Array<() => void>; |
| 11 | released: boolean; |
| 12 | releasedDecodes: number; |
| 13 | compressionDecodes: number; |
| 14 | inFlight: number; |
| 15 | } |
| 16 | |
| 17 | declare global { |
| 18 | interface Window { |
| 19 | compressionHold?: CompressionHold; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /** Installs the hold before the app loads; call before `t3.open`. */ |
| 24 | export const holdImageCompression = (browser: Browser) => |
| 25 | browser.addInitScript(() => { |
| 26 | const hold: CompressionHold = { |
| 27 | release: () => { |
| 28 | hold.released = true; |
| 29 | for (const resume of hold.held.splice(0)) resume(); |
| 30 | }, |
| 31 | releaseNext: () => { |
| 32 | hold.releasedDecodes++; |
| 33 | hold.held.shift()?.(); |
| 34 | }, |
| 35 | held: [], |
| 36 | released: false, |
| 37 | releasedDecodes: 0, |
| 38 | compressionDecodes: 0, |
| 39 | inFlight: 0, |
| 40 | }; |
| 41 | window.compressionHold = hold; |
| 42 | const track = async <T>(work: () => Promise<T>) => { |
| 43 | hold.inFlight++; |
| 44 | try { |
| 45 | return await work(); |
| 46 | } finally { |
| 47 | hold.inFlight--; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | const decode = window.createImageBitmap.bind(window); |
| 52 | window.createImageBitmap = ((source: ImageBitmapSource, ...rest: [ImageBitmapOptions?]) => |
| 53 | track(async () => { |
| 54 | // Only attachment compression decodes with an orientation; placeholder thumbnails do not. |
| 55 | if (rest[0]?.imageOrientation === undefined) return decode(source, ...rest); |
| 56 | if (!hold.released) await new Promise<void>((resume) => hold.held.push(resume)); |
| 57 | const bitmap = await decode(source, ...rest); |
| 58 | hold.compressionDecodes++; |
| 59 | return bitmap; |
| 60 | })) as typeof createImageBitmap; |
| 61 | |
| 62 | const convertToBlob = OffscreenCanvas.prototype.convertToBlob; |
| 63 | OffscreenCanvas.prototype.convertToBlob = function (options) { |
| 64 | return track(() => convertToBlob.call(this, options)); |
| 65 | }; |
| 66 | const readAsDataURL = FileReader.prototype.readAsDataURL; |
| 67 | FileReader.prototype.readAsDataURL = function (blob) { |
| 68 | hold.inFlight++; |
| 69 | this.addEventListener("loadend", () => hold.inFlight--, { once: true }); |
| 70 | readAsDataURL.call(this, blob); |
| 71 | }; |
| 72 | }); |
| 73 | |
| 74 | /** Pastes a PNG wider than the 2000 px attachment limit into the focused composer. */ |
| 75 | export const pasteWideScreenshot = (browser: Browser) => |
| 76 | browser.evaluate(async () => { |
| 77 | const canvas = new OffscreenCanvas(2200, 20); |
| 78 | canvas.getContext("2d")!.fillRect(0, 0, 2200, 20); |
| 79 | const file = new File([await canvas.convertToBlob({ type: "image/png" })], "screenshot.png", { |
| 80 | type: "image/png", |
| 81 | }); |
| 82 | const clipboardData = new DataTransfer(); |
| 83 | clipboardData.items.add(file); |
| 84 | const editor = document.querySelector('[contenteditable="true"]'); |
| 85 | if (!editor) throw new Error("No composer editor to paste into"); |
| 86 | return editor.dispatchEvent( |
| 87 | new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }), |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | /** |
| 92 | * Lets the held compression finish and waits until it has. What follows the last tracked step |
| 93 | * is synchronous or microtask-only, so the app has applied the result by the next evaluate. |
| 94 | */ |
| 95 | export const finishImageCompression = async (browser: Browser) => { |
| 96 | await browser.evaluate(() => { |
| 97 | window.compressionHold!.release(); |
| 98 | return null; |
| 99 | }); |
| 100 | await expect |
| 101 | .poll(() => |
| 102 | browser.evaluate(() => { |
| 103 | const hold = window.compressionHold!; |
| 104 | return hold.released && hold.compressionDecodes > 0 && hold.inFlight === 0; |
| 105 | }), |
| 106 | ) |
| 107 | .toBe(true); |
| 108 | }; |
| 109 | |
| 110 | /** Waits until `count` compressions are held, in the order their images were pasted. */ |
| 111 | export const expectHeldImageCompressions = (browser: Browser, count: number) => |
| 112 | expect.poll(() => browser.evaluate(() => window.compressionHold!.held.length)).toBe(count); |
| 113 | |
| 114 | /** Lets the oldest held compression finish, keeping the rest held, and waits until it has. */ |
| 115 | export const finishNextImageCompression = async (browser: Browser) => { |
| 116 | await browser.evaluate(() => { |
| 117 | window.compressionHold!.releaseNext(); |
| 118 | return null; |
| 119 | }); |
| 120 | await expect |
| 121 | .poll(() => |
| 122 | browser.evaluate(() => { |
| 123 | const hold = window.compressionHold!; |
| 124 | return ( |
| 125 | hold.compressionDecodes === hold.releasedDecodes && hold.inFlight === hold.held.length |
| 126 | ); |
| 127 | }), |
| 128 | ) |
| 129 | .toBe(true); |
| 130 | }; |