| 1 | import assert from "node:assert/strict"; |
| 2 | import { pathToFileURL } from "node:url"; |
| 3 | import path from "node:path"; |
| 4 | import { writeFile } from "node:fs/promises"; |
| 5 | import { execFile } from "node:child_process"; |
| 6 | import { promisify } from "node:util"; |
| 7 | |
| 8 | const { chromium } = await import(pathToFileURL(process.env.CAGE_NATIVE_DISPLAY_PLAYWRIGHT).href); |
| 9 | let input = ""; |
| 10 | for await (const chunk of process.stdin) input += chunk; |
| 11 | const config = JSON.parse(input); |
| 12 | const browser = await chromium.launch({ |
| 13 | executablePath: process.env.CAGE_NATIVE_DISPLAY_CHROMIUM || path.join(process.env["ProgramFiles(x86)"], "Microsoft", "Edge", "Application", "msedge.exe"), |
| 14 | headless: true, |
| 15 | }); |
| 16 | const measurements = [], errors = []; |
| 17 | let received = 0, bytes = 0, opened = 0, closed = 0; |
| 18 | const context = await browser.newContext({ viewport: { width: 1440, height: 950 }, deviceScaleFactor: 1 }); |
| 19 | await context.addCookies(config.cookies.map(cookie => ({ ...cookie, url: config.url, httpOnly: true, sameSite: "Strict" }))); |
| 20 | const page = await context.newPage(); |
| 21 | page.setDefaultTimeout(45000); |
| 22 | page.on("pageerror", error => errors.push(error.message)); |
| 23 | const diagnostics = []; |
| 24 | page.on("console", message => { |
| 25 | if (["error", "warning"].includes(message.type())) diagnostics.push(message.text()); |
| 26 | }); |
| 27 | page.on("response", response => { |
| 28 | if (response.status() >= 400) diagnostics.push(`${response.status()} ${new URL(response.url()).pathname}`); |
| 29 | }); |
| 30 | page.on("websocket", socket => { |
| 31 | if (!new URL(socket.url()).pathname.endsWith("/vnc")) return; |
| 32 | opened++; |
| 33 | socket.on("close", () => { closed++; }); |
| 34 | socket.on("framereceived", event => { received++; bytes += Buffer.byteLength(event.payload); }); |
| 35 | }); |
| 36 | const canvas = page.locator(".remote-canvas canvas:visible").last(); |
| 37 | const execute = promisify(execFile); |
| 38 | async function cpuSample() { |
| 39 | if (!process.env.CAGE_NATIVE_DISPLAY_SAMPLE) return null; |
| 40 | const result = await execute("powershell.exe", ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", process.env.CAGE_NATIVE_DISPLAY_SAMPLE, "-ServiceProcessId", String(process.ppid)]); |
| 41 | return JSON.parse(result.stdout.replace(/^\uFEFF/, "")); |
| 42 | } |
| 43 | async function measure(name, milliseconds) { |
| 44 | const before = { received, bytes, opened, closed }, start = performance.now(); |
| 45 | const cpuBefore = await cpuSample(); |
| 46 | await new Promise(resolve => setTimeout(resolve, milliseconds)); |
| 47 | const cpuAfter = await cpuSample(); |
| 48 | const sample = { name, milliseconds: performance.now() - start, messages: received - before.received, bytes: bytes - before.bytes, opened: opened - before.opened, closed: closed - before.closed }; |
| 49 | if (cpuBefore && cpuAfter) { |
| 50 | sample.guest_cpu_seconds = cpuAfter.guest.cpu - cpuBefore.guest.cpu; |
| 51 | sample.guest_process_cpu_percent_one_core = 100000 * sample.guest_cpu_seconds / (cpuAfter.guest.timestamp - cpuBefore.guest.timestamp); |
| 52 | sample.guest_process_groups = Object.fromEntries(Object.entries(cpuAfter.guest.groups || {}).map(([name, value]) => [name, 100000 * (value - (cpuBefore.guest.groups?.[name] || 0)) / (cpuAfter.guest.timestamp - cpuBefore.guest.timestamp)])); |
| 53 | sample.host_service_cpu_seconds = cpuAfter.hostCPU - cpuBefore.hostCPU; |
| 54 | sample.host_service_cpu_percent_one_core = 100000 * sample.host_service_cpu_seconds / (cpuAfter.timestamp - cpuBefore.timestamp); |
| 55 | } |
| 56 | measurements.push(sample); |
| 57 | return sample; |
| 58 | } |
| 59 | async function point(x, y) { |
| 60 | const position = await canvas.evaluate((element, coordinate) => { |
| 61 | const box = element.getBoundingClientRect(); |
| 62 | return { x: box.x + coordinate.x * 2 / element.width * box.width, y: box.y + coordinate.y * 2 / element.height * box.height }; |
| 63 | }, { x, y }); |
| 64 | await page.mouse.click(position.x, position.y); |
| 65 | } |
| 66 | try { |
| 67 | await page.goto(config.url); |
| 68 | await page.getByRole("navigation", { name: "主导航" }).getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 69 | const address = page.locator(".address-bar input").first(); |
| 70 | await address.fill(config.fixture); |
| 71 | await address.press("Enter"); |
| 72 | await page.waitForFunction(() => [...document.querySelectorAll(".remote-canvas canvas")].some(element => { |
| 73 | if (element.getBoundingClientRect().width < 100 || element.width < 320 || element.height < 740) return false; |
| 74 | const pixel = element.getContext("2d").getImageData(150, 650, 1, 1).data; |
| 75 | return Math.abs(pixel[0] - 220) < 20 && Math.abs(pixel[1] - 80) < 20; |
| 76 | })); |
| 77 | await measure("warmup", 1500); |
| 78 | const idle = await measure("static", 5000); |
| 79 | assert.equal(idle.closed, 0, "static frames disconnected the native bridge"); |
| 80 | assert.equal(idle.opened, 0, "static frames triggered reconnection"); |
| 81 | assert(idle.bytes < 65536, `static frames repeatedly transferred pixels (${idle.bytes} bytes)`); |
| 82 | await page.screenshot({ path: path.join(config.evidence, "windows-browser-static.png") }); |
| 83 | |
| 84 | await point(120, 65); |
| 85 | const typed = performance.now(); |
| 86 | await page.keyboard.type("Cage live 123", { delay: 30 }); |
| 87 | await page.waitForFunction(() => [...document.querySelectorAll(".remote-canvas canvas")].some(element => { |
| 88 | if (element.getBoundingClientRect().width < 100 || element.width < 320) return false; |
| 89 | const pixel = element.getContext("2d").getImageData(20, 20, 1, 1).data; |
| 90 | return Math.abs(pixel[0] - 40) < 20 && Math.abs(pixel[1] - 160) < 20; |
| 91 | })); |
| 92 | measurements.push({ name: "input_to_visible_confirmation", milliseconds: performance.now() - typed }); |
| 93 | await point(600, 100); |
| 94 | await point(120, 175); |
| 95 | await page.waitForFunction(() => [...document.querySelectorAll(".remote-canvas canvas")].some(element => { |
| 96 | if (element.getBoundingClientRect().width < 100 || element.height < 740) return false; |
| 97 | const pixels = element.getContext("2d").getImageData(0, 650, element.width, 1).data; |
| 98 | const red = x => Math.abs(pixels[x * 4] - 220) < 20 && Math.abs(pixels[x * 4 + 1] - 80) < 20; |
| 99 | return !red(90) && Array.from({ length: 80 }, (_, index) => 160 + index * 10).some(x => x < element.width && red(x)); |
| 100 | }), null, { timeout: 5000 }); |
| 101 | const motion = await measure("animation", 4500); |
| 102 | assert(motion.bytes > 65536, "animation produced no changed-frame payload"); |
| 103 | assert.equal(motion.closed, 0); |
| 104 | await page.waitForFunction(() => [...document.querySelectorAll(".remote-canvas canvas")].some(element => { |
| 105 | if (element.getBoundingClientRect().width < 100 || element.height < 740) return false; |
| 106 | const pixel = element.getContext("2d").getImageData(150, 650, 1, 1).data; |
| 107 | return Math.abs(pixel[0] - 220) < 20 && Math.abs(pixel[1] - 80) < 20; |
| 108 | })); |
| 109 | await page.mouse.wheel(0, 650); |
| 110 | const scrolling = await measure("scroll", 800); |
| 111 | assert(scrolling.bytes > 0, "scrolling did not update the rendered page"); |
| 112 | assert.equal(scrolling.closed, 0); |
| 113 | await page.keyboard.press("Control+Home"); |
| 114 | |
| 115 | await page.keyboard.press("Control+f"); |
| 116 | const menu = await measure("native_find", 1200); |
| 117 | assert(menu.bytes > 1000, "native find UI did not update the frame"); |
| 118 | await page.screenshot({ path: path.join(config.evidence, "windows-browser-native-find.png") }); |
| 119 | await page.keyboard.press("Escape"); |
| 120 | const previousWidth = await canvas.evaluate(element => element.width); |
| 121 | await page.setViewportSize({ width: 1280, height: 850 }); |
| 122 | await page.waitForFunction(width => [...document.querySelectorAll(".remote-canvas canvas")].some(element => element.getBoundingClientRect().width > 100 && element.width >= 320 && element.width !== width), previousWidth); |
| 123 | await page.screenshot({ path: path.join(config.evidence, "windows-browser-resized.png") }); |
| 124 | |
| 125 | const edge = await canvas.evaluate(element => ({ x: element.width / 2 - 90, y: element.height / 2 - 42 })); |
| 126 | await point(edge.x, edge.y); |
| 127 | await page.waitForFunction(() => [...document.querySelectorAll(".remote-canvas canvas")].some(element => { |
| 128 | if (element.getBoundingClientRect().width < 100 || element.width < 320 || element.height < 240) return false; |
| 129 | const pixel = element.getContext("2d").getImageData(element.width - 100, element.height - 50, 1, 1).data; |
| 130 | return Math.abs(pixel[0] - 90) < 20 && Math.abs(pixel[1] - 60) < 20 && Math.abs(pixel[2] - 210) < 20; |
| 131 | }), null, { timeout: 5000 }); |
| 132 | measurements.push({ name: "resized_edge_click", confirmed: true }); |
| 133 | |
| 134 | await page.evaluate(() => { window.cageWindowVisible = false; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 135 | await measure("hide_transition", 500); |
| 136 | const hidden = await measure("hidden", 2000); |
| 137 | assert.equal(hidden.messages, 0, "hidden workbench retained its frame stream"); |
| 138 | await page.evaluate(() => { window.cageWindowVisible = true; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 139 | await canvas.waitFor(); |
| 140 | const resumed = await measure("resume", 2000); |
| 141 | assert(resumed.bytes > 0, "restoring the workbench did not deliver pixels"); |
| 142 | if (process.env.CAGE_NATIVE_DISPLAY_DESKTOP === "1") { |
| 143 | await page.getByRole("navigation", { name: "主导航" }).getByRole("button", { name: "Claude Desktop", exact: true }).click(); |
| 144 | await page.waitForFunction(() => [...document.querySelectorAll('section[aria-label="Claude Desktop"] canvas')].some(element => { |
| 145 | if (element.getBoundingClientRect().width < 100 || element.width < 320 || element.height < 240) return false; |
| 146 | const pixels = element.getContext("2d").getImageData(0, 0, element.width, element.height).data; |
| 147 | for (let y = 0; y < element.height; y += 8) for (let x = 0; x < element.width; x += 8) { |
| 148 | const index = (y * element.width + x) * 4; |
| 149 | if (pixels[index] > 180 && pixels[index + 1] > 70 && pixels[index + 1] < 160 && pixels[index + 2] < 130) return true; |
| 150 | } |
| 151 | return false; |
| 152 | }), null, { timeout: 75000 }); |
| 153 | await page.screenshot({ path: path.join(config.evidence, "windows-claude-live.png") }); |
| 154 | const desktop = await measure("claude_visible", 5000); |
| 155 | assert.equal(desktop.closed, 0, "the official Claude window disconnected while visible"); |
| 156 | await page.evaluate(() => { window.cageWindowVisible = false; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 157 | await measure("claude_hide_transition", 500); |
| 158 | const desktopHidden = await measure("claude_hidden", 2000); |
| 159 | assert.equal(desktopHidden.messages, 0); |
| 160 | await page.evaluate(() => { window.cageWindowVisible = true; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 161 | const desktopResumed = await measure("claude_resume", 2000); |
| 162 | assert(desktopResumed.bytes > 0); |
| 163 | } |
| 164 | assert.deepEqual(errors, []); |
| 165 | await writeFile(path.join(config.evidence, "windows-browser-live.json"), JSON.stringify({ measurements, diagnostics, errors, opened, closed }, null, 2)); |
| 166 | console.log(JSON.stringify({ measurements, opened, closed })); |
| 167 | } catch (error) { |
| 168 | await page.screenshot({ path: path.join(config.evidence, "windows-browser-failure.png") }); |
| 169 | await writeFile(path.join(config.evidence, "windows-browser-live-partial.json"), JSON.stringify({ measurements, diagnostics, errors, received, bytes, opened, closed }, null, 2)); |
| 170 | console.log(JSON.stringify({ diagnostics, measurements, errors, received, bytes, opened, closed })); |
| 171 | throw error; |
| 172 | } finally { await browser.close(); } |