| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createRequire } from "node:module"; |
| 4 | import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import { join } from "node:path"; |
| 7 | const require = createRequire(import.meta.url); |
| 8 | const playwright = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 9 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 10 | const browser = await playwright[engine].launch({ executablePath: process.env.CAGE_TEST_BROWSER_PATH }); |
| 11 | const output = process.env.CAGE_PHONE_QA_OUTPUT || await mkdtemp(join(tmpdir(), "cage-macos-phone-")); |
| 12 | await mkdir(output, { recursive: true }); |
| 13 | try { |
| 14 | const context = await browser.newContext({ viewport: { width: 390, height: 844 }, hasTouch: true, isMobile: true, userAgent: "CageMobile/1" }); |
| 15 | const page = await context.newPage(); |
| 16 | const errors = [], actions = []; |
| 17 | page.on("pageerror", error => errors.push(error.message)); |
| 18 | const image = await page.evaluate(() => { |
| 19 | const canvas = document.createElement("canvas"); canvas.width = 900; canvas.height = 600; |
| 20 | const ctx = canvas.getContext("2d"); ctx.fillStyle = "#eeeae3"; ctx.fillRect(0, 0, 900, 600); |
| 21 | ctx.fillStyle = "#30312e"; ctx.font = "28px sans-serif"; |
| 22 | ctx.fillText("Claude Desktop · isolated test image", 90, 100); |
| 23 | ctx.fillStyle = "white"; ctx.fillRect(100, 260, 700, 80); |
| 24 | ctx.fillStyle = "#555"; ctx.fillText("Tap the input to continue", 150, 310); |
| 25 | return canvas.toDataURL("image/jpeg").split(",")[1]; |
| 26 | }); |
| 27 | let frame, locked = false; |
| 28 | await page.route("**/api/**", route => { |
| 29 | const request = route.request(), path = new URL(request.url()).pathname; |
| 30 | const input = request.postDataJSON(); |
| 31 | if (path.endsWith("/projects")) return route.fulfill({ json: { host: "隔离测试虚拟 Mac", ready: true, projects: [] } }); |
| 32 | if (path.endsWith("/sessions")) return route.fulfill({ json: { sessions: [] } }); |
| 33 | if (!path.startsWith("/api/mobile/desktop/")) return route.fulfill({ status: 404 }); |
| 34 | const operation = path.split("/").at(-1); |
| 35 | if (operation === "close") return route.fulfill({ json: { id: frame.id } }); |
| 36 | if (operation === "open") { |
| 37 | locked = false; |
| 38 | frame = { ...input, image, document: "mac-window-geometry", focus: "", shared: true, window: "desktop", |
| 39 | content_width: 900, content_height: 600, login_supported: false, pixel_ratio_supported: true, requested_pixel_ratio: input.pixel_ratio || 1 }; |
| 40 | } |
| 41 | if (locked) return route.fulfill({ status: 410, contentType: "text/plain; charset=utf-8", body: "Claude Desktop 会话已结束,请重新打开" }); |
| 42 | if (operation === "action") { |
| 43 | actions.push(input); |
| 44 | if (input.type === "resize") Object.assign(frame, { width: input.width, height: input.height, requested_pixel_ratio: input.pixel_ratio }); |
| 45 | if (input.type === "tap") { assert.equal(input.document, frame.document); frame.focus = "current-ax-input"; } |
| 46 | if (input.type === "text") { assert.equal(input.focus, frame.focus); assert.ok(new TextEncoder().encode(input.text).length <= 8192); } |
| 47 | } |
| 48 | return route.fulfill({ json: operation === "frame" ? frame : { id: frame.id } }); |
| 49 | }); |
| 50 | await page.goto((process.env.CAGE_UI_URL || "http://127.0.0.1:5298") + "/mobile.html"); |
| 51 | await page.getByRole("dialog", { name: "工作区侧栏" }).getByRole("button", { name: "Claude Desktop", exact: true }).click(); |
| 52 | const screen = page.getByRole("img", { name: "远程 Desktop 画面", exact: true }); |
| 53 | await screen.waitFor(); |
| 54 | await page.getByText(/请在电脑的 Cage 中完成 Claude Desktop 登录/).waitFor(); |
| 55 | assert.equal(await page.getByRole("button", { name: "登录窗口", exact: true }).count(), 0); |
| 56 | const keyboard = page.getByRole("button", { name: "输入文字", exact: true }); |
| 57 | assert.ok(await keyboard.isDisabled()); |
| 58 | await screen.tap(); |
| 59 | await keyboard.click(); |
| 60 | const tap = actions.find(action => action.type === "tap"); |
| 61 | assert.ok(Math.abs(tap.x - 450) <= 2 && Math.abs(tap.y - 300) <= 2, JSON.stringify(tap)); |
| 62 | const field = page.getByLabel("待插入文字"); |
| 63 | await field.fill("中".repeat(2731)); |
| 64 | assert.ok(await page.getByRole("button", { name: "插入文字" }).isDisabled()); |
| 65 | await page.getByRole("alert").getByText(/单次输入最多 8192 字节/).waitFor(); |
| 66 | const text = "中".repeat(2730) + "ab"; |
| 67 | await field.fill(text); |
| 68 | await page.getByRole("button", { name: "插入文字" }).click(); |
| 69 | await page.waitForFunction(() => document.querySelector('[aria-label="待插入文字"]')?.value === ""); |
| 70 | assert.equal(actions.filter(action => action.type === "text").at(-1).text, text); |
| 71 | await page.getByRole("button", { name: "收起输入框" }).click(); |
| 72 | await page.screenshot({ path: join(output, "macos-phone-desktop.png") }); |
| 73 | locked = true; |
| 74 | await page.getByRole("button", { name: "重新打开", exact: true }).waitFor(); |
| 75 | assert.equal(await screen.count(), 0); |
| 76 | await page.getByRole("button", { name: "重新打开", exact: true }).click(); |
| 77 | await screen.waitFor(); |
| 78 | assert.ok(await keyboard.isDisabled()); |
| 79 | assert.deepEqual(errors, []); |
| 80 | const retryPage = await context.newPage(); |
| 81 | let opens = 0, frameReads = 0, opened; |
| 82 | await retryPage.route("**/api/**", route => { |
| 83 | const path = new URL(route.request().url()).pathname; |
| 84 | if (path.endsWith("/projects")) return route.fulfill({ json: { host: "隔离测试虚拟 Mac", ready: true, projects: [] } }); |
| 85 | if (path.endsWith("/sessions")) return route.fulfill({ json: { sessions: [] } }); |
| 86 | if (path.endsWith("/open")) { opened = route.request().postDataJSON(); opens++; return route.fulfill({ json: { id: opened.id } }); } |
| 87 | if (path.endsWith("/frame")) { |
| 88 | if (opens === 1) return route.fulfill({ status: ++frameReads === 1 ? 425 : 503, body: "客机浏览器尚未就绪" }); |
| 89 | return route.fulfill({ json: { ...opened, image, document: "browser-page", url: "about:blank" } }); |
| 90 | } |
| 91 | return route.fulfill({ status: 404 }); |
| 92 | }); |
| 93 | await retryPage.goto((process.env.CAGE_UI_URL || "http://127.0.0.1:5298") + "/mobile.html"); |
| 94 | await retryPage.getByRole("dialog", { name: "工作区侧栏" }).getByRole("button", { name: "浏览器", exact: true }).click(); |
| 95 | await retryPage.getByText("正在启动远程浏览器…", { exact: true }).waitFor(); |
| 96 | const reopen = retryPage.getByRole("button", { name: "重新打开", exact: true }); |
| 97 | await reopen.waitFor(); |
| 98 | assert.equal(await retryPage.getByRole("button", { name: "重新读取画面", exact: true }).count(), 0); |
| 99 | await reopen.click(); |
| 100 | await retryPage.getByRole("img", { name: "远程浏览器画面" }).waitFor(); |
| 101 | assert.equal(opens, 2); |
| 102 | assert.equal(frameReads, 2); |
| 103 | const result = { passed: true, engine, actions: actions.map(({ text, ...action }) => ({ ...action, ...(text ? { textBytes: new TextEncoder().encode(text).length } : {}) })), |
| 104 | checks: ["native content geometry maps center", "focus required before typing", "8192-byte UTF-8 limit", "shared login instruction", "expired GUI session removes image and reopens", "browser waits after 425, reopens directly after frame 503"] }; |
| 105 | await writeFile(join(output, "result.json"), JSON.stringify(result, null, 2)); |
| 106 | console.log(JSON.stringify(result)); |
| 107 | } finally { await browser.close(); } |