|
| 1 | +// Electron entry for verify-background-preview.mjs. CommonJS, because an ESM |
| 2 | +// entry delays `ready` until its top-level await settles. |
| 3 | +const NodeFS = require("node:fs"); |
| 4 | +const NodeOS = require("node:os"); |
| 5 | +const NodePath = require("node:path"); |
| 6 | +const NodeURL = require("node:url"); |
| 7 | +const { app, BrowserWindow, nativeImage } = require("electron"); |
| 8 | + |
| 9 | +const CAPTURE_TIMEOUT_MS = 5_000; |
| 10 | +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 11 | +const withTimeout = (promise, ms) => |
| 12 | + Promise.race([ |
| 13 | + promise.then((value) => ({ ok: true, value })), |
| 14 | + sleep(ms).then(() => ({ ok: false })), |
| 15 | + ]); |
| 16 | +const page = (html) => `data:text/html,${encodeURIComponent(html)}`; |
| 17 | + |
| 18 | +async function captureColor(tab, rgb) { |
| 19 | + await tab.debugger.sendCommand("Runtime.evaluate", { |
| 20 | + expression: `document.body.style.background = 'rgb(${rgb.join(",")})'`, |
| 21 | + }); |
| 22 | + const shot = await withTimeout( |
| 23 | + tab.debugger.sendCommand("Page.captureScreenshot", { format: "png" }), |
| 24 | + CAPTURE_TIMEOUT_MS, |
| 25 | + ); |
| 26 | + if (!shot.ok) return "timed out"; |
| 27 | + const pixels = nativeImage.createFromBuffer(Buffer.from(shot.value.data, "base64")).toBitmap(); |
| 28 | + const got = [pixels[2], pixels[1], pixels[0]]; |
| 29 | + // Color management can shift a channel by a few levels. |
| 30 | + return got.every((value, index) => Math.abs(value - rgb[index]) <= 6) |
| 31 | + ? null |
| 32 | + : `stale: rgb(${got.join(",")})`; |
| 33 | +} |
| 34 | + |
| 35 | +async function main() { |
| 36 | + const { backgroundTabWindowOptions } = await import( |
| 37 | + NodeURL.pathToFileURL(NodePath.join(__dirname, "../src/preview/BackgroundTabWindow.ts")).href |
| 38 | + ); |
| 39 | + const preload = NodePath.join( |
| 40 | + NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-background-preview-")), |
| 41 | + "preload.cjs", |
| 42 | + ); |
| 43 | + NodeFS.writeFileSync(preload, ""); |
| 44 | + // Each state closes its windows, which would otherwise quit the app. |
| 45 | + app.on("window-all-closed", () => {}); |
| 46 | + await app.whenReady(); |
| 47 | + |
| 48 | + const failures = []; |
| 49 | + for (const state of ["hidden", "minimized"]) { |
| 50 | + const appWindow = new BrowserWindow({ width: 480, height: 320, show: true }); |
| 51 | + await appWindow.loadURL(page("<body>T3 Code</body>")); |
| 52 | + await sleep(500); |
| 53 | + if (state === "hidden") appWindow.hide(); |
| 54 | + else appWindow.minimize(); |
| 55 | + await sleep(1_500); |
| 56 | + |
| 57 | + // Created only after the app window is gone, as an agent's tab would be. |
| 58 | + const tabWindow = new BrowserWindow( |
| 59 | + backgroundTabWindowOptions({ |
| 60 | + partition: `t3-background-preview-${state}`, |
| 61 | + preload, |
| 62 | + passkeyBridgeArgument: undefined, |
| 63 | + }), |
| 64 | + ); |
| 65 | + await tabWindow.loadURL(page("<body style='margin:0'></body>")); |
| 66 | + const tab = tabWindow.webContents; |
| 67 | + tab.debugger.attach("1.3"); |
| 68 | + for (const [step, rgb] of [ |
| 69 | + [1, [0, 200, 0]], |
| 70 | + [2, [200, 200, 0]], |
| 71 | + [3, [0, 200, 200]], |
| 72 | + ]) { |
| 73 | + const failure = await captureColor(tab, rgb); |
| 74 | + if (failure) failures.push(`app window ${state}, capture ${step}: ${failure}`); |
| 75 | + await sleep(1_000); |
| 76 | + } |
| 77 | + tabWindow.destroy(); |
| 78 | + appWindow.destroy(); |
| 79 | + } |
| 80 | + |
| 81 | + if (failures.length > 0) { |
| 82 | + console.error("Background preview tabs stopped painting:"); |
| 83 | + for (const failure of failures) console.error(` - ${failure}`); |
| 84 | + app.exit(1); |
| 85 | + return; |
| 86 | + } |
| 87 | + console.log("Background preview tabs paint while the app window is hidden or minimized."); |
| 88 | + app.exit(0); |
| 89 | +} |
| 90 | + |
| 91 | +main().catch((error) => { |
| 92 | + console.error(error); |
| 93 | + app.exit(1); |
| 94 | +}); |
0 commit comments