| 1 | import { pathToFileURL } from "node:url"; |
| 2 | const { chromium } = await import( |
| 3 | process.env.CAGE_WINDOWS_QA_PLAYWRIGHT |
| 4 | ? pathToFileURL(process.env.CAGE_WINDOWS_QA_PLAYWRIGHT).href |
| 5 | : "playwright" |
| 6 | ); |
| 7 | import path from "node:path"; |
| 8 | let input = ""; |
| 9 | for await (const chunk of process.stdin) input += chunk; |
| 10 | const config = JSON.parse(input); |
| 11 | const browser = await chromium.launch({ |
| 12 | executablePath: path.join( |
| 13 | process.env["ProgramFiles(x86)"], |
| 14 | "Microsoft", |
| 15 | "Edge", |
| 16 | "Application", |
| 17 | "msedge.exe", |
| 18 | ), |
| 19 | headless: true, |
| 20 | }); |
| 21 | try { |
| 22 | const ctx = await browser.newContext({ |
| 23 | viewport: { width: 1440, height: 900 }, |
| 24 | deviceScaleFactor: 1, |
| 25 | }); |
| 26 | await ctx.addCookies( |
| 27 | config.cookies.map((c) => ({ |
| 28 | ...c, |
| 29 | url: config.url, |
| 30 | httpOnly: true, |
| 31 | sameSite: "Strict", |
| 32 | })), |
| 33 | ); |
| 34 | const page = await ctx.newPage(); |
| 35 | page.setDefaultTimeout(30000); |
| 36 | const pointers = []; |
| 37 | page.on("websocket", (socket) => { |
| 38 | if (new URL(socket.url()).pathname !== "/api/claude-desktop/vnc") return; |
| 39 | socket.on("framesent", ({ payload }) => { |
| 40 | if (!Buffer.isBuffer(payload) || payload.length !== 6 || payload[0] !== 5) |
| 41 | return; |
| 42 | if (pointers.length < 24) |
| 43 | pointers.push([ |
| 44 | payload[1], |
| 45 | payload.readUInt16BE(2), |
| 46 | payload.readUInt16BE(4), |
| 47 | ]); |
| 48 | }); |
| 49 | }); |
| 50 | await page.goto(config.url); |
| 51 | const nav = page.getByRole("navigation", { name: "主导航" }); |
| 52 | await nav |
| 53 | .getByRole("button", { name: "Claude Desktop", exact: true }) |
| 54 | .click(); |
| 55 | const canvas = page |
| 56 | .locator('section[aria-label="Claude Desktop"] canvas:visible') |
| 57 | .last(); |
| 58 | await canvas.waitFor({ timeout: 45000 }); |
| 59 | |
| 60 | |
| 61 | function onboardingScreen(requireSignIn) { |
| 62 | const el = Array.from( |
| 63 | document.querySelectorAll('section[aria-label="Claude Desktop"] canvas'), |
| 64 | ).find( |
| 65 | (c) => |
| 66 | c.getBoundingClientRect().width > 100 && |
| 67 | c.width >= 1100 && |
| 68 | c.height >= 240, |
| 69 | ); |
| 70 | if (!el) return; |
| 71 | const { width, height } = el; |
| 72 | const pixels = el.getContext("2d").getImageData(0, 0, width, height).data; |
| 73 | const near = (x, y, red, green, blue, tolerance) => { |
| 74 | const index = (y * width + x) * 4; |
| 75 | return ( |
| 76 | Math.abs(pixels[index] - red) <= tolerance && |
| 77 | Math.abs(pixels[index + 1] - green) <= tolerance && |
| 78 | Math.abs(pixels[index + 2] - blue) <= tolerance |
| 79 | ); |
| 80 | }; |
| 81 | const cx = Math.floor(width / 2); |
| 82 | let logo = 0, |
| 83 | googleBlue = 0, |
| 84 | googleGreen = 0, |
| 85 | googleRed = 0; |
| 86 | for (let y = Math.floor(height * 0.1); y < height * 0.8; y += 2) { |
| 87 | for ( |
| 88 | let x = Math.max(0, cx - 220); |
| 89 | x < Math.min(width, cx + 220); |
| 90 | x += 2 |
| 91 | ) { |
| 92 | if (near(x, y, 220, 115, 77, 22)) logo++; |
| 93 | if (near(x, y, 66, 133, 244, 24)) googleBlue++; |
| 94 | if (near(x, y, 52, 168, 83, 24)) googleGreen++; |
| 95 | if (near(x, y, 234, 67, 53, 24)) googleRed++; |
| 96 | } |
| 97 | } |
| 98 | const signIn = |
| 99 | googleBlue >= 1 && googleRed >= 1 && googleGreen >= 1 && logo < 80; |
| 100 | if (!signIn && (requireSignIn || logo < 100)) return; |
| 101 | const lower = signIn ? 0.33 : 0.6, |
| 102 | upper = signIn ? 0.83 : 0.999; |
| 103 | let first = -1, |
| 104 | last = -1; |
| 105 | for (let y = Math.floor(height * lower); y < height * upper; y++) { |
| 106 | if ( |
| 107 | !near(cx - 45, y, 24, 24, 24, 15) || |
| 108 | !near(cx + 45, y, 24, 24, 24, 15) |
| 109 | ) |
| 110 | continue; |
| 111 | let left = cx - 45, |
| 112 | right = cx + 45; |
| 113 | while (left > 0 && near(left, y, 24, 24, 24, 15)) left--; |
| 114 | while (right < width - 1 && near(right, y, 24, 24, 24, 15)) right++; |
| 115 | if ( |
| 116 | right - left < 230 || |
| 117 | right - left > 820 || |
| 118 | Math.abs((right + left) / 2 - cx) > 6 |
| 119 | ) |
| 120 | continue; |
| 121 | if (first < 0) first = y; |
| 122 | last = y; |
| 123 | } |
| 124 | if (first < 0 || last - first < 20 || last - first > 100) return; |
| 125 | const bounds = el.getBoundingClientRect(); |
| 126 | return { |
| 127 | signIn, |
| 128 | x: bounds.left + bounds.width / 2, |
| 129 | y: bounds.top + bounds.height * ((first + last) / 2 / height), |
| 130 | }; |
| 131 | } |
| 132 | const screenshot = async (name) => |
| 133 | page.screenshot({ |
| 134 | clip: { x: 0, y: 0, width: 1440, height: 855 }, |
| 135 | path: path.join(config.evidence, name + ".png"), |
| 136 | }); |
| 137 | const state = await ( |
| 138 | await page.waitForFunction(onboardingScreen, false, { timeout: 60000 }) |
| 139 | ).jsonValue(); |
| 140 | if (!state.signIn) { |
| 141 | await screenshot("windows-workbench-welcome"); |
| 142 | |
| 143 | |
| 144 | await page.waitForTimeout(1000); |
| 145 | const live = await page.evaluate(onboardingScreen, false); |
| 146 | if (!live || live.signIn) |
| 147 | throw new Error("Welcome button changed before clicking"); |
| 148 | await page.mouse.click(live.x, live.y); |
| 149 | try { |
| 150 | await page.waitForFunction(onboardingScreen, true, { timeout: 80000 }); |
| 151 | } catch { |
| 152 | await screenshot("windows-workbench-after-click"); |
| 153 | throw new Error( |
| 154 | "Login form was not observed; welcome point=" + |
| 155 | JSON.stringify(live) + |
| 156 | "; guest pointer coordinates=" + |
| 157 | JSON.stringify(pointers), |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | await screenshot("windows-workbench-signin"); |
| 162 | console.log( |
| 163 | "Verified the official Google/email form inside the shared Windows workbench.", |
| 164 | ); |
| 165 | } finally { |
| 166 | await browser.close(); |
| 167 | } |