| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import http from "node:http"; |
| 5 | import {mkdir, readFile} from "node:fs/promises"; |
| 6 | import {join} from "node:path"; |
| 7 | import {fileURLToPath} from "node:url"; |
| 8 | const playwright = await import(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 9 | const engine = process.env.CAGE_TEST_MOBILE_BROWSER || "chromium"; |
| 10 | const resources = new URL("../../mobile/.build/resources/mobile-ui/", import.meta.url); |
| 11 | const httpd = http.createServer(async (req, res) => { |
| 12 | const path = new URL(req.url, "http://localhost").pathname; |
| 13 | try { |
| 14 | if (path !== "/" && path !== "/mobile.html" && !/^\/assets\/[\w.-]+$/.test(path)) throw Error(); |
| 15 | const file = new URL(path.slice(1) || "mobile.html", resources); |
| 16 | const mime = {js:"text/javascript", html:"text/html", png:"image/png", css:"text/css"}[file.pathname.split(".").pop()]; |
| 17 | res.setHeader("Content-Type", mime); |
| 18 | res.end(await readFile(fileURLToPath(file))); |
| 19 | } catch {res.writeHead(404).end();} |
| 20 | }); |
| 21 | await new Promise(resolve => httpd.listen(0, "127.0.0.1", resolve)); |
| 22 | let browser; |
| 23 | try { |
| 24 | browser = await playwright[engine].launch(); |
| 25 | const context = await browser.newContext({viewport:{width:390, height:844}, hasTouch:true, isMobile:true, deviceScaleFactor:1, userAgent:"CageMobile/1"}); |
| 26 | await context.addInitScript(() => { |
| 27 | window.__cageDevices = {current:"a", devices:[{id:"a",name:"Cage 工作电脑"},{id:"b",name:"另一台电脑"}]}; |
| 28 | window.deviceCommands = []; |
| 29 | window.CageDevices = {postMessage(raw) { |
| 30 | const message = JSON.parse(raw); window.deviceCommands.push(message); |
| 31 | if (message.action === "select") { |
| 32 | window.__cageDevices = {...window.__cageDevices,current:message.id}; |
| 33 | window.dispatchEvent(new CustomEvent("cage-devices",{detail:window.__cageDevices})); |
| 34 | } |
| 35 | }}; |
| 36 | }); |
| 37 | const page = await context.newPage(); |
| 38 | page.setDefaultTimeout(8000); |
| 39 | const image = await page.evaluate(() => { |
| 40 | const canvas = document.createElement("canvas"); |
| 41 | canvas.width = 12; canvas.height = 16; |
| 42 | const c = canvas.getContext("2d"); c.fillStyle = "#555"; c.fillRect(0,0,12,16); |
| 43 | return canvas.toDataURL("image/jpeg").split(",")[1]; |
| 44 | }); |
| 45 | const homeNotice = "电脑的新标签页无法在手机浏览器打开,请输入其他网址。"; |
| 46 | let enabled = false, frame, opens = 0, closes = 0, reads = 0; |
| 47 | let dropNextText = false, resizeHold, reloadHold; |
| 48 | let modern = false, readGate, writeGate, failedScroll, disconnectNavigation, idOnly; |
| 49 | let tag = 0; |
| 50 | const frameTag = () => tag.toString(16).padStart(32,"0"); |
| 51 | const responseFrame = () => ({...frame, frame_tag:frameTag(), ...(modern && {action_frame:true})}); |
| 52 | const gate = () => { |
| 53 | let enter, release; |
| 54 | const entered = new Promise(resolve => {enter = resolve;}); |
| 55 | const hold = new Promise(resolve => {release = resolve;}); |
| 56 | return {entered,release,wait:() => {enter(); return hold;}}; |
| 57 | }; |
| 58 | let desktopEnabled = false, desktopStartupReads = 0; |
| 59 | let favoritesAvailable = false, favoritesGate; |
| 60 | const favoriteWrites = []; |
| 61 | const favorites = {folders:[{id:"ref",title:"参考资料"}],items:[ |
| 62 | {id:"one",title:"设计指引",url:"https://design.example/",folder_id:"ref"}, |
| 63 | {id:"another",title:"日历",url:"https://calendar.example/",folder_id:""}, |
| 64 | ]}; |
| 65 | const sessionList = [{id:"70d7194d-33c0-428d-867e-f9e4cb9f902e",workspace:"/work/cage", |
| 66 | title:"移动端导航",generation:"qa",revision:1,turns:[],updated_at:new Date().toISOString()}]; |
| 67 | const graphicCalls = []; |
| 68 | const holdResize = () => { |
| 69 | let release; |
| 70 | resizeHold = new Promise(resolve => {release = resolve;}); |
| 71 | return release; |
| 72 | }; |
| 73 | const actions = []; |
| 74 | const pairedHost = async route => { |
| 75 | const req = route.request(); |
| 76 | const path = new URL(req.url()).pathname, body = req.postDataJSON(); |
| 77 | const reply = (value) => route.fulfill({json: value}); |
| 78 | if (path === "/api/mobile/cli/projects") return reply({host:"Cage 工作电脑", ready:true, |
| 79 | projects:[{path:"/work/cage",name:"Cage"},{path:"/work/web",name:"个人网站"}]}); |
| 80 | if (path === "/api/mobile/cli/sessions" && body) { |
| 81 | const session = {id:body.id, workspace:body.workspace, title:"测试会话", generation:"qa", revision:1, turns:[], updated_at:new Date().toISOString()}; |
| 82 | sessionList.push(session); |
| 83 | return reply(session); |
| 84 | } |
| 85 | if (path === "/api/mobile/cli/sessions") return reply({sessions:sessionList}); |
| 86 | if (path.startsWith("/api/mobile/cli/sessions/")) return reply(sessionList.find(s => path.endsWith(s.id))); |
| 87 | if (path.startsWith("/api/mobile/desktop/")) { |
| 88 | if (!desktopEnabled) return route.fulfill({status:403,contentType:"text/plain",body:"请在电脑上允许 Claude Desktop。"}); |
| 89 | const op = path.split("/").at(-1); |
| 90 | if (op !== "frame") graphicCalls.push({tool:"desktop",op,body}); |
| 91 | if (op === "open") {frame = {...body, document:"desktop-doc", focus:"", window:"desktop", image}; return reply({id:frame.id});} |
| 92 | assert.ok(path.includes("/" + frame.id + "/")); |
| 93 | if (op === "frame" && !desktopStartupReads++) return route.fulfill({status:425}); |
| 94 | if (op === "frame") return reply(frame); |
| 95 | if (op === "action") { |
| 96 | assert.equal(body.document, frame.document); |
| 97 | if (body.type === "resize") Object.assign(frame,{width:body.width,height:body.height, |
| 98 | ...(body.pixel_ratio && {requested_pixel_ratio:body.pixel_ratio,pixel_ratio:body.pixel_ratio})}); |
| 99 | if (body.type === "tap") frame.focus = "compose"; |
| 100 | if (body.type === "login") Object.assign(frame,{window:"login",document:"login-doc",focus:"credential",password:true}); |
| 101 | if (body.type === "key" || body.type === "text") assert.equal(frame.focus,body.focus); |
| 102 | if (body.type === "key") await new Promise(resolve => setTimeout(resolve,65)); |
| 103 | } |
| 104 | return reply({id:frame.id}); |
| 105 | } |
| 106 | if (!path.startsWith("/api/mobile/browser/")) return route.fulfill({status:404}); |
| 107 | if (path === "/api/mobile/browser/favorites") { |
| 108 | if (!favoritesAvailable) return route.fulfill({status:404}); |
| 109 | if (body) { |
| 110 | favoriteWrites.push(body); |
| 111 | if (body.action === "save") favorites.items.push({id:"new",...body}); |
| 112 | else if (body.action === "delete") favorites.items = favorites.items.filter(item => item.id !== body.id); |
| 113 | else throw Error("unexpected favorite mutation"); |
| 114 | } |
| 115 | const result = structuredClone(favorites); |
| 116 | const pending = favoritesGate; favoritesGate = null; |
| 117 | if (pending) await pending.wait(); |
| 118 | return reply(result); |
| 119 | } |
| 120 | if (!enabled) return route.fulfill({status:403, contentType:"text/plain", body:"请先在电脑上允许远程浏览器。"}); |
| 121 | if (path.endsWith("/open")) { |
| 122 | opens++; |
| 123 | graphicCalls.push({tool:"browser",op:"open",body}); |
| 124 | frame = {...body, document:"doc-1", focus:"", password:false, url:"", image}; |
| 125 | tag++; |
| 126 | return reply({id:body.id, ...(modern && {action_frame:true,home_notice:homeNotice})}); |
| 127 | } |
| 128 | assert.ok(path.includes("/" + frame.id + "/")); |
| 129 | if (path.endsWith("/close")) {closes++; return reply({id:frame.id});} |
| 130 | if (path.endsWith("/frame")) { |
| 131 | reads++; |
| 132 | const after = new URL(req.url()).searchParams.get("after"); |
| 133 | if (!modern) assert.equal(after,null,"a tag from an older host must not enable conditional requests"); |
| 134 | const snapshot = responseFrame(); |
| 135 | const pending = readGate; readGate = null; |
| 136 | if (pending) { |
| 137 | await pending.wait(); |
| 138 | return reply({...snapshot,url:"https://stale.example/",document:"obsolete-doc"}); |
| 139 | } |
| 140 | return reply(after === frameTag() ? {id:frame.id,unchanged:true,frame_tag:after} : snapshot); |
| 141 | } |
| 142 | assert.ok(path.endsWith("/action")); |
| 143 | actions.push(body); |
| 144 | assert.equal(body.return_frame,modern || undefined,"only a capable host accepts the new action field"); |
| 145 | assert.equal(body.document, frame.document); |
| 146 | if (body.type === "resize") { |
| 147 | const hold = resizeHold; |
| 148 | resizeHold = null; |
| 149 | if (hold) await hold; |
| 150 | Object.assign(frame, {width:body.width, height:body.height, document:"viewport-" + actions.length}); |
| 151 | } |
| 152 | if (body.type === "reload" && reloadHold) await reloadHold; |
| 153 | if (body.type === "navigate") Object.assign(frame, {url:body.url,document:"doc-2"}); |
| 154 | if (body.type === "tap") frame.focus = "email-field"; |
| 155 | if (body.type === "text" || body.type === "key") assert.equal(body.focus,frame.focus); |
| 156 | if (body.type === "text" && dropNextText) { |
| 157 | dropNextText = false; |
| 158 | return route.abort("internetdisconnected"); |
| 159 | } |
| 160 | tag++; |
| 161 | if (modern) { |
| 162 | const snapshot = responseFrame(); |
| 163 | const pending = writeGate; writeGate = null; |
| 164 | if (pending) await pending.wait(); |
| 165 | if (failedScroll && body.type === "scroll") {failedScroll = false; return route.abort("internetdisconnected");} |
| 166 | if (disconnectNavigation && body.type === "navigate") { |
| 167 | disconnectNavigation = false; |
| 168 | frame.url = "https://recovered.example/"; |
| 169 | return reply({...snapshot,url:"https://obsolete-write.example/"}); |
| 170 | } |
| 171 | if (idOnly) {idOnly = false; return reply({id:frame.id});} |
| 172 | return reply(snapshot); |
| 173 | } |
| 174 | return reply({id:frame.id}); |
| 175 | }; |
| 176 | const origin = "http://127.0.0.1:" + httpd.address().port; |
| 177 | await page.route("**/api/mobile/**",pairedHost); |
| 178 | await page.goto(origin); |
| 179 | const drawer = page.getByRole("dialog",{name:"工作区侧栏"}); |
| 180 | await drawer.waitFor(); |
| 181 | for (const name of ["浏览器","Claude.ai","Claude Desktop"]) assert.equal(await drawer.getByRole("button",{name,exact:true}).count(),1); |
| 182 | await drawer.getByRole("heading",{name:"Claude Code",exact:true}).waitFor(); |
| 183 | assert.equal(await drawer.getByRole("button",{name:"Claude Code",exact:true}).count(),0); |
| 184 | assert.equal(await drawer.getByRole("combobox").count(),0); |
| 185 | const menu = page.getByRole("button",{name:"展开侧栏",exact:true}); |
| 186 | const firstFolder = drawer.getByRole("button",{name:"项目 Cage",exact:true}); |
| 187 | const secondFolder = drawer.getByRole("button",{name:"项目 个人网站",exact:true}); |
| 188 | const exit = drawer.getByRole("link",{name:"退出",exact:true}); |
| 189 | await drawer.getByRole("button",{name:"移动端导航",exact:true}).waitFor(); |
| 190 | if (process.env.CAGE_TEST_MOBILE_CAPTURE) { |
| 191 | await mkdir(process.env.CAGE_TEST_MOBILE_CAPTURE,{recursive:true}); |
| 192 | await page.screenshot({path:join(process.env.CAGE_TEST_MOBILE_CAPTURE,engine+"-workspace-drawer.png")}); |
| 193 | } |
| 194 | await page.setViewportSize({width:390,height:300}); |
| 195 | const scroll = drawer.locator(".drawer-content"); |
| 196 | await page.waitForFunction(() => {const el = document.querySelector(".drawer-content"); return el.scrollHeight > el.clientHeight;}); |
| 197 | const footerTop = (await exit.boundingBox()).y; |
| 198 | await scroll.evaluate(el => {el.scrollTop = el.scrollHeight;}); |
| 199 | assert.ok(await scroll.evaluate(el => el.scrollTop > 0)); |
| 200 | assert.equal((await exit.boundingBox()).y,footerTop); |
| 201 | assert.ok(footerTop > 200 && footerTop < 300); |
| 202 | await page.setViewportSize({width:390,height:844}); |
| 203 | assert.equal(await drawer.getByRole("button",{name:"收起侧栏"}).count(),0); |
| 204 | await page.touchscreen.tap(380, 400); |
| 205 | await drawer.waitFor({state:"hidden"}); |
| 206 | assert.equal(await page.locator(".topbar button").count(),1); |
| 207 | assert.ok(await menu.getByRole("img",{name:"Cage"}).count()); |
| 208 | await page.getByRole("img",{name:"Claude Code",exact:true}).waitFor(); |
| 209 | await menu.click(); |
| 210 | await page.waitForFunction(() => document.querySelector(".workspace-drawer")?.getAnimations().some(animation => animation.transitionProperty === "transform")); |
| 211 | await page.keyboard.press("Escape"); |
| 212 | await drawer.waitFor({state:"hidden"}); |
| 213 | await menu.click(); |
| 214 | await drawer.getByRole("button", {name:"浏览器",exact:true}).click(); |
| 215 | await drawer.waitFor({state:"hidden"}); |
| 216 | await page.getByRole("img",{name:"浏览器",exact:true}).waitFor(); |
| 217 | await page.getByText("请先在电脑上允许远程浏览器。").waitFor(); |
| 218 | enabled = true; |
| 219 | await page.getByRole("button",{name:"重新打开",exact:true}).click(); |
| 220 | const imageView = page.getByRole("img",{name:"远程浏览器画面"}); |
| 221 | const ready = async () => { |
| 222 | await imageView.waitFor(); |
| 223 | await page.waitForFunction(() => !document.querySelector('button[aria-label="重新加载网页"]')?.disabled); |
| 224 | }; |
| 225 | await ready(); |
| 226 | assert.equal(await page.locator(".topbar input").count(),1); |
| 227 | assert.equal(Math.round((await imageView.boundingBox()).y), Math.round((await page.locator(".topbar").boundingBox()).height)); |
| 228 | |
| 229 | await page.reload(); |
| 230 | await ready(); |
| 231 | assert.equal(opens,1); |
| 232 | const address = page.getByRole("textbox",{name:"远程网页地址"}); |
| 233 | |
| 234 | |
| 235 | const duringResize = async (height, url) => { |
| 236 | const release = holdResize(); |
| 237 | const resize = page.waitForRequest(req => req.url().endsWith("/action") && req.postDataJSON()?.type === "resize"); |
| 238 | await address.focus(); |
| 239 | await page.setViewportSize({width:390, height}); |
| 240 | await resize; |
| 241 | await address.fill(url); |
| 242 | await address.press("Enter"); |
| 243 | await page.waitForFunction(() => document.querySelector('[aria-label="前往网址"]').getAttribute("aria-busy") === "true"); |
| 244 | assert.equal(actions.filter(action => action.type === "navigate" && action.url === url).length,0); |
| 245 | return release; |
| 246 | }; |
| 247 | const target = "https://example.org/"; |
| 248 | const navigation = page.waitForResponse(res => res.url().endsWith("/action") && res.request().postDataJSON()?.type === "navigate"); |
| 249 | const release = await duringResize(682, target); |
| 250 | release(); |
| 251 | await navigation; |
| 252 | await ready(); |
| 253 | assert.equal(actions.filter(action => action.type === "navigate" && action.url === target).length,1); |
| 254 | |
| 255 | let releaseReload; |
| 256 | reloadHold = new Promise(resolve => {releaseReload = resolve;}); |
| 257 | await page.getByRole("button",{name:"重新加载网页"}).click(); |
| 258 | await address.fill("https://busy.example.org/"); |
| 259 | await address.press("Enter"); |
| 260 | await page.getByText("上一项操作尚未完成,请稍后再次前往。").waitFor(); |
| 261 | releaseReload(); |
| 262 | await ready(); |
| 263 | assert.equal(actions.filter(action => action.url === "https://busy.example.org/").length, 0); |
| 264 | const matchingFrame = page.waitForResponse(async response => { |
| 265 | if (!response.url().endsWith("/frame") || !response.ok()) return false; |
| 266 | const captured = await response.json(), rect = await imageView.boundingBox(); |
| 267 | return rect && Math.round(rect.width) === captured.width && Math.round(rect.height) === captured.height; |
| 268 | }); |
| 269 | await address.press("Escape"); |
| 270 | await matchingFrame; |
| 271 | await ready(); |
| 272 | const box = await imageView.boundingBox(); |
| 273 | await page.touchscreen.tap(box.x + box.width * .25,box.y + box.height * .45); |
| 274 | const keyboard = page.getByRole("button",{name:"输入文字",exact:true}); |
| 275 | await keyboard.click(); |
| 276 | const tap = actions.find(a => a.type === "tap"); |
| 277 | assert.ok(Math.abs(tap.x / Math.round(box.width) - .25) < .01 && Math.abs(tap.y / Math.round(box.height) - .45) < .01); |
| 278 | const field = page.getByLabel("待插入文字"); |
| 279 | await field.fill("手机中文输入"); |
| 280 | const initialField = await field.elementHandle(); |
| 281 | |
| 282 | |
| 283 | for (const size of [{width:390,height:575}, {width:844,height:390}, {width:390,height:844}]) { |
| 284 | const before = frame.document; |
| 285 | const fresh = page.waitForResponse(async response => { |
| 286 | if (!response.url().endsWith("/frame") || !response.ok()) return false; |
| 287 | const value = await response.json(); |
| 288 | const stage = await imageView.boundingBox(); |
| 289 | return stage && value.document !== before && value.focus === "email-field" && |
| 290 | value.width === Math.round(stage.width) && value.height === Math.round(stage.height); |
| 291 | }); |
| 292 | await page.setViewportSize(size); |
| 293 | await fresh; |
| 294 | await ready(); |
| 295 | assert.equal(await field.evaluate((el, original) => el === original && document.activeElement === el, initialField), true); |
| 296 | assert.equal(await field.inputValue(), "手机中文输入"); |
| 297 | assert.equal(frame.width,size.width); |
| 298 | assert.equal(await page.getByRole("navigation",{name:"远程网页操作"}).count(),0); |
| 299 | } |
| 300 | await initialField.dispose(); |
| 301 | await page.getByRole("button",{name:"插入文字",exact:true}).click(); |
| 302 | await page.getByRole("button",{name:"发送回车",exact:true}).click(); |
| 303 | assert.deepEqual(actions.filter(a => a.type === "text").map(a => a.text),["手机中文输入"]); |
| 304 | assert.deepEqual(actions.filter(a => a.type === "key").map(a => a.key),["Enter"]); |
| 305 | |
| 306 | |
| 307 | await field.fill("uncertain"); |
| 308 | dropNextText = true; |
| 309 | await page.getByRole("button",{name:"插入文字",exact:true}).click(); |
| 310 | await page.getByText("输入结果待确认", {exact:false}).waitFor(); |
| 311 | assert.equal(await imageView.count(),0); |
| 312 | await page.getByRole("button",{name:"重新读取画面",exact:true}).click(); |
| 313 | await ready(); |
| 314 | assert.equal(actions.filter(a => a.text === "uncertain").length,1); |
| 315 | assert.equal(opens,1); |
| 316 | if (await field.count()) assert.equal(await field.inputValue(),""); |
| 317 | |
| 318 | if (!await field.count()) await keyboard.click(); |
| 319 | await field.fill("draft for email only"); |
| 320 | frame.focus = "password-field"; |
| 321 | frame.password = true; |
| 322 | await field.waitFor({state:"detached"}); |
| 323 | await keyboard.click(); |
| 324 | assert.equal(await field.getAttribute("type"), "password"); |
| 325 | assert.equal(await field.inputValue(), ""); |
| 326 | await field.fill("synthetic password"); |
| 327 | await page.evaluate(() => window.dispatchEvent(new Event("pagehide"))); |
| 328 | await imageView.waitFor({state:"detached"}); |
| 329 | assert.equal(await field.count(), 0); |
| 330 | const suspended = reads; |
| 331 | await page.waitForTimeout(900); |
| 332 | assert.equal(reads, suspended); |
| 333 | await page.evaluate(() => window.dispatchEvent(new Event("pageshow"))); |
| 334 | await ready(); |
| 335 | assert.equal(await field.count(), 0); |
| 336 | assert.equal(actions.filter(a => a.text === "synthetic password").length, 0); |
| 337 | await page.setViewportSize({width:844,height:390}); |
| 338 | await page.waitForFunction(() => !document.querySelector('button[aria-label="重新加载网页"]').disabled); |
| 339 | for (let i=0; i<40 && !actions.some(a => a.type === "resize" && a.width > 600); i++) await page.waitForTimeout(100); |
| 340 | assert.ok(actions.some(a => a.type === "resize" && a.width > 600)); |
| 341 | await ready(); |
| 342 | const before = await imageView.boundingBox(); |
| 343 | const xy = {x:before.x+before.width/2, y:before.y+before.height*.7}; |
| 344 | await page.mouse.move(xy.x,xy.y); |
| 345 | await page.mouse.down(); |
| 346 | await page.mouse.move(xy.x,xy.y-45); |
| 347 | await page.mouse.up(); |
| 348 | for (let i=0; i<30 && !actions.some(a => a.type === "scroll"); i++) await page.waitForTimeout(100); |
| 349 | assert.ok(actions.some(a => a.type === "scroll" && a.dy > 0)); |
| 350 | const abandoned = "https://abandoned.example.org/"; |
| 351 | const afterClose = await duringResize(777, abandoned); |
| 352 | await page.getByRole("button",{name:"关闭远程浏览器",exact:true}).click(); |
| 353 | await page.getByRole("button",{name:"浏览器",exact:true}).waitFor(); |
| 354 | afterClose(); |
| 355 | const count = reads; |
| 356 | await page.waitForTimeout(900); |
| 357 | assert.equal(reads,count); |
| 358 | assert.equal(closes,1); |
| 359 | assert.equal(actions.filter(action => action.type === "navigate" && action.url === abandoned).length,0); |
| 360 | await drawer.waitFor(); |
| 361 | assert.equal(await firstFolder.getAttribute("aria-expanded"),"true"); |
| 362 | assert.equal(await secondFolder.getAttribute("aria-expanded"),"false"); |
| 363 | await secondFolder.click(); |
| 364 | assert.equal(await firstFolder.getAttribute("aria-expanded"),"true"); |
| 365 | assert.equal(await secondFolder.getAttribute("aria-expanded"),"true"); |
| 366 | await firstFolder.focus(); await firstFolder.press("Enter"); |
| 367 | assert.equal(await firstFolder.evaluate(el => el === document.activeElement),true); |
| 368 | assert.equal(await drawer.getByRole("button",{name:"移动端导航",exact:true}).count(),0); |
| 369 | assert.equal(await secondFolder.getAttribute("aria-expanded"),"true"); |
| 370 | await drawer.getByRole("button",{name:"在 个人网站 新建会话",exact:true}).click(); |
| 371 | await drawer.waitFor({state:"hidden"}); |
| 372 | const composer = page.getByRole("textbox",{name:"消息"}); |
| 373 | await composer.fill("尚未发送的草稿"); |
| 374 | assert.equal(sessionList.at(-1).workspace,"/work/web"); |
| 375 | await menu.click(); await drawer.getByRole("button",{name:"Claude.ai",exact:true}).click(); |
| 376 | await ready(); |
| 377 | await page.getByRole("img",{name:"Claude.ai",exact:true}).waitFor(); |
| 378 | assert.equal(await address.count(),0); |
| 379 | const claudeBox = await imageView.boundingBox(); |
| 380 | assert.equal(claudeBox.width,390); |
| 381 | assert.ok(claudeBox.height > 667); |
| 382 | await page.getByRole("button",{name:"网页地址",exact:true}).click(); |
| 383 | assert.equal(await address.count(),1); |
| 384 | assert.equal((await imageView.boundingBox()).height,claudeBox.height); |
| 385 | assert.equal(graphicCalls.filter(call => call.tool === "browser" && call.op === "open").at(-1).body.url, "https://claude.ai/"); |
| 386 | await menu.click(); |
| 387 | await firstFolder.click(); |
| 388 | assert.equal(await firstFolder.getAttribute("aria-expanded"),"true"); |
| 389 | assert.equal(await secondFolder.getAttribute("aria-expanded"),"true"); |
| 390 | await page.getByRole("img",{name:"Claude.ai",exact:true}).waitFor(); |
| 391 | await drawer.getByRole("button",{name:"测试会话",exact:true}).click(); |
| 392 | await composer.waitFor(); |
| 393 | assert.equal(await composer.inputValue(), "尚未发送的草稿"); |
| 394 | await menu.click(); await drawer.getByRole("button",{name:"Claude Desktop",exact:true}).click(); |
| 395 | await page.getByRole("img",{name:"Claude Desktop",exact:true}).waitFor(); |
| 396 | await page.getByText("请在电脑上允许 Claude Desktop。").waitFor(); |
| 397 | desktopEnabled = true; |
| 398 | await page.getByRole("button",{name:"重新打开",exact:true}).click(); |
| 399 | await page.getByText("正在启动远程 Claude Desktop…").waitFor(); |
| 400 | const desktop = page.getByRole("img",{name:"远程 Desktop 画面"}); |
| 401 | await desktop.waitFor(); |
| 402 | const logicalWidth = frame.width; |
| 403 | const fit = await desktop.boundingBox(); |
| 404 | const desktopResizes = () => graphicCalls.filter(call => call.tool === "desktop" && call.body?.type === "resize").length; |
| 405 | const priorResizes = desktopResizes(); |
| 406 | await page.getByRole("button",{name:"放大阅读",exact:true}).click(); |
| 407 | assert.ok((await desktop.boundingBox()).width > fit.width * 1.5); |
| 408 | const stage = page.locator(".remote-stage"); |
| 409 | const priorLeft = await stage.evaluate(el => el.scrollLeft); |
| 410 | await page.mouse.move(fit.x + fit.width/2, fit.y + fit.height/2); await page.mouse.down(); |
| 411 | await page.mouse.move(fit.x + fit.width/2 - 50, fit.y + fit.height/2); await page.mouse.up(); |
| 412 | assert.ok(await stage.evaluate(el => el.scrollLeft) > priorLeft); |
| 413 | await page.getByRole("button",{name:"全幅",exact:true}).click(); |
| 414 | assert.equal(frame.width,logicalWidth); assert.equal(desktopResizes(),priorResizes); |
| 415 | await desktop.tap(); |
| 416 | await page.getByRole("button",{name:"输入文字",exact:true}).click(); |
| 417 | await field.fill("Desktop 中文"); |
| 418 | await page.getByRole("button",{name:"插入文字",exact:true}).click(); |
| 419 | await page.getByRole("button",{name:"收起输入框",exact:true}).click(); |
| 420 | await page.getByRole("button",{name:"登录窗口",exact:true}).click(); |
| 421 | await page.getByRole("button",{name:"返回 Desktop",exact:true}).waitFor(); |
| 422 | await page.getByRole("button",{name:"输入文字",exact:true}).click(); |
| 423 | const loginField = page.getByLabel("逐键登录输入"); |
| 424 | await loginField.pressSequentially("Ab2"); |
| 425 | assert.equal(await loginField.inputValue(),""); |
| 426 | for (let i=0; i<40 && graphicCalls.filter(call => call.body?.type === "key").length < 3; i++) await page.waitForTimeout(100); |
| 427 | assert.deepEqual(graphicCalls.filter(call => call.body?.type === "key").map(call=>call.body.key),["A","b","2"]); |
| 428 | assert.equal(graphicCalls.filter(call => call.body?.type === "text").length,1); |
| 429 | await page.getByRole("button",{name:"收起输入框",exact:true}).click(); |
| 430 | Object.assign(frame, {shared:true}); |
| 431 | await page.getByRole("button",{name:"返回 Desktop",exact:true}).waitFor({state:"hidden"}); |
| 432 | assert.equal(await page.getByRole("button",{name:"登录窗口",exact:true}).count(),0); |
| 433 | await page.getByRole("button",{name:"断开远程桌面",exact:true}).waitFor(); |
| 434 | assert.equal(graphicCalls.filter(call => call.body?.pixel_ratio).length,0); |
| 435 | Object.assign(frame,{pixel_ratio_supported:true,requested_pixel_ratio:2,pixel_ratio:2}); |
| 436 | for (const ratio of [3.5,2.625]) { |
| 437 | await page.evaluate(value => Object.defineProperty(window,"devicePixelRatio",{configurable:true,value}),ratio); |
| 438 | for (let i=0; i<60 && frame.requested_pixel_ratio!==ratio; i++) await page.waitForTimeout(100); |
| 439 | assert.equal(frame.requested_pixel_ratio,ratio); |
| 440 | assert.equal(graphicCalls.filter(call=>call.body?.pixel_ratio===ratio).length,1); |
| 441 | } |
| 442 | await menu.click(); await exit.click(); |
| 443 | await page.waitForFunction(() => window.deviceCommands.some(m => m.action === "home")); |
| 444 | await desktop.waitFor({state:"detached"}); |
| 445 | for (let i=0; i<20 && !graphicCalls.some(c => c.tool === "desktop" && c.op === "close"); i++) await page.waitForTimeout(100); |
| 446 | assert.equal(graphicCalls.filter(c => c.tool === "desktop" && c.op === "close").length,1); |
| 447 | assert.ok(await page.evaluate(() => window.deviceCommands.every(m => m.action !== "disconnect" && m.action !== "select"))); |
| 448 | modern = true; |
| 449 | await drawer.getByRole("button",{name:"浏览器",exact:true}).click(); |
| 450 | await drawer.waitFor({state:"hidden"}); |
| 451 | await ready(); |
| 452 | await page.getByText(homeNotice).waitFor(); |
| 453 | assert.equal(await address.isEnabled(),true); |
| 454 | assert.equal(await page.getByRole("button",{name:"收藏夹",exact:true}).isEnabled(),true); |
| 455 | |
| 456 | |
| 457 | const oldRead = readGate = gate(); |
| 458 | await oldRead.entered; |
| 459 | const latestAction = page.waitForResponse(res => res.url().endsWith("/action") && res.request().postDataJSON()?.url === "https://latest.example/"); |
| 460 | await address.fill("https://latest.example/"); await address.press("Enter"); |
| 461 | await latestAction; |
| 462 | await page.waitForFunction(() => document.activeElement !== document.querySelector('[aria-label="远程网页地址"]')); |
| 463 | oldRead.release(); |
| 464 | await page.waitForTimeout(160); |
| 465 | assert.equal(await address.inputValue(),"https://latest.example/"); |
| 466 | assert.equal(await page.getByText(homeNotice).count(),0); |
| 467 | const pausedWrite = writeGate = gate(); |
| 468 | disconnectNavigation = true; |
| 469 | await address.fill("https://background.example/"); await address.press("Enter"); |
| 470 | await pausedWrite.entered; |
| 471 | await page.evaluate(() => window.dispatchEvent(new Event("pagehide"))); |
| 472 | await imageView.waitFor({state:"detached"}); |
| 473 | await page.evaluate(() => window.dispatchEvent(new Event("pageshow"))); |
| 474 | pausedWrite.release(); |
| 475 | await ready(); |
| 476 | assert.equal(await address.inputValue(),"https://recovered.example/"); |
| 477 | const motion = await imageView.boundingBox(); |
| 478 | const firstWrite = writeGate = gate(); |
| 479 | failedScroll = true; |
| 480 | const startActions = actions.length; |
| 481 | const x = motion.x + motion.width/2, y = motion.y + motion.height*.7; |
| 482 | await page.mouse.move(x,y); await page.mouse.down(); await page.mouse.move(x,y-24); |
| 483 | await firstWrite.entered; |
| 484 | await page.mouse.move(x,y-56); await page.mouse.move(x,y-84); await page.mouse.up(); |
| 485 | firstWrite.release(); |
| 486 | await page.getByText("输入结果待确认",{exact:false}).waitFor(); |
| 487 | assert.deepEqual(actions.slice(startActions).map(a=>a.dy),[24]); |
| 488 | await page.getByRole("button",{name:"重新读取画面"}).click(); await ready(); |
| 489 | await page.waitForTimeout(330); |
| 490 | assert.deepEqual(actions.slice(startActions).map(a=>a.dy),[24],"unsent movement must be discarded after uncertain input"); |
| 491 | |
| 492 | |
| 493 | const interrupted = writeGate = gate(); |
| 494 | const firstDrag = actions.length; |
| 495 | await page.mouse.move(x,y); await page.mouse.down(); await page.mouse.move(x,y-20); |
| 496 | await interrupted.entered; |
| 497 | await page.mouse.move(x,y-55); await page.mouse.up(); |
| 498 | await page.mouse.move(x+64,y); await page.mouse.down(); await page.mouse.move(x+64,y-18); await page.mouse.up(); |
| 499 | interrupted.release(); |
| 500 | for (let i=0; i<25 && actions.length < firstDrag+2; i++) await page.waitForTimeout(50); |
| 501 | assert.deepEqual(actions.slice(firstDrag).map(a=>[a.x,a.dy]),[[Math.floor(x-motion.x),20],[Math.floor(x+64-motion.x),18]]); |
| 502 | |
| 503 | |
| 504 | idOnly = true; |
| 505 | const once = actions.length; |
| 506 | const fallback = page.waitForResponse(res => res.url().endsWith("/frame") && res.ok()); |
| 507 | const longAddress = "https://ack-only.example/" + "a".repeat(5000); |
| 508 | await address.fill(longAddress); await address.press("Enter"); |
| 509 | await fallback; |
| 510 | await ready(); |
| 511 | assert.deepEqual(actions.slice(once).map(a=>a.type),["navigate"]); |
| 512 | assert.equal(await address.inputValue(),longAddress); |
| 513 | const bookmarks = page.getByRole("button",{name:"收藏夹",exact:true}); |
| 514 | const panel = page.getByRole("region",{name:"电脑收藏夹",exact:true}); |
| 515 | await bookmarks.click(); |
| 516 | await panel.getByText("这台电脑尚不支持共享收藏夹,请更新电脑端 Cage。").waitFor(); |
| 517 | favoritesAvailable = true; |
| 518 | await panel.getByRole("button",{name:"刷新收藏夹"}).click(); |
| 519 | const folder = panel.getByRole("button",{name:"参考资料",exact:true}); |
| 520 | const rootFavorite = panel.getByRole("button",{name:/^日历/}); |
| 521 | const rootTop = (await rootFavorite.boundingBox()).y; |
| 522 | await folder.focus(); await folder.press("Enter"); |
| 523 | assert.ok((await rootFavorite.boundingBox()).y >= rootTop + 40); |
| 524 | await folder.press("Space"); |
| 525 | assert.equal((await rootFavorite.boundingBox()).y,rootTop); |
| 526 | await panel.getByRole("button",{name:"收藏此页"}).click(); |
| 527 | const destination = panel.getByRole("combobox",{name:"保存到"}); |
| 528 | const saveBookmark = panel.getByRole("button",{name:"保存收藏"}); |
| 529 | const saveTop = (await saveBookmark.boundingBox()).y; |
| 530 | await destination.focus(); await destination.press("Enter"); |
| 531 | assert.ok((await saveBookmark.boundingBox()).y >= saveTop + 72); |
| 532 | await destination.press("ArrowDown"); await destination.press("Enter"); |
| 533 | assert.equal(await destination.evaluate(el => document.activeElement === el),true); |
| 534 | assert.equal((await saveBookmark.boundingBox()).y,saveTop); |
| 535 | await saveBookmark.click(); |
| 536 | await panel.getByRole("button",{name:"取消此页收藏",exact:true}).waitFor(); |
| 537 | assert.deepEqual(favoriteWrites[0],{action:"save",title:"ack-only.example",url:longAddress,folder_id:"ref"}); |
| 538 | assert.equal(await folder.getAttribute("aria-expanded"),"true"); |
| 539 | favorites.items.push({id:"from-computer",title:"电脑新加入",url:"https://added.example/",folder_id:"ref"}); |
| 540 | await panel.getByRole("button",{name:"刷新收藏夹"}).click(); |
| 541 | const added = panel.getByRole("button",{name:/^电脑新加入/}); |
| 542 | await added.waitFor(); |
| 543 | if (process.env.CAGE_TEST_MOBILE_CAPTURE) await page.screenshot({path:join(process.env.CAGE_TEST_MOBILE_CAPTURE,engine+"-phone-favorites.png")}); |
| 544 | const opensBefore = actions.filter(a=>a.type === "navigate").length; |
| 545 | await added.click(); |
| 546 | await panel.waitFor({state:"detached"}); |
| 547 | assert.equal(frame.url,"https://added.example/"); |
| 548 | assert.equal(actions.filter(a=>a.type === "navigate").length,opensBefore+1); |
| 549 | assert.equal(new URL(page.url()).origin,origin); |
| 550 | await bookmarks.click(); |
| 551 | await panel.getByRole("button",{name:"取消此页收藏",exact:true}).click(); |
| 552 | await panel.getByRole("button",{name:"收藏此页",exact:true}).waitFor(); |
| 553 | assert.deepEqual(favoriteWrites.at(-1),{action:"delete",id:"from-computer"}); |
| 554 | const oldFavorites = favoritesGate = gate(); |
| 555 | await panel.getByRole("button",{name:"刷新收藏夹"}).click(); await oldFavorites.entered; |
| 556 | await page.evaluate(() => window.dispatchEvent(new Event("offline"))); |
| 557 | await panel.waitFor({state:"detached"}); |
| 558 | oldFavorites.release(); |
| 559 | await page.getByRole("button",{name:"重新读取画面"}).click(); await ready(); |
| 560 | assert.equal(await panel.count(),0); |
| 561 | favorites.items = []; |
| 562 | await bookmarks.click(); await folder.waitFor(); |
| 563 | assert.equal(await rootFavorite.count(),0,"favorites from a previous connection must not reappear"); |
| 564 | await panel.getByRole("button",{name:"返回网页"}).click(); |
| 565 | await panel.waitFor({state:"detached"}); |
| 566 | await page.getByRole("button",{name:"关闭远程浏览器"}).click(); |
| 567 | const legacy = await browser.newPage({viewport:{width:850,height:720}}); |
| 568 | await legacy.route("**/api/mobile/**",pairedHost); |
| 569 | await legacy.goto(origin + "/mobile.html"); |
| 570 | const project = legacy.getByRole("combobox",{name:"选择项目"}); |
| 571 | const newSession = legacy.getByRole("button",{name:"新建会话",exact:true}); |
| 572 | await project.waitFor(); |
| 573 | assert.equal(await legacy.getByRole("dialog").count(),0); |
| 574 | const headingY = (await newSession.boundingBox()).y; |
| 575 | await project.focus(); await project.press("Enter"); |
| 576 | assert.ok((await newSession.boundingBox()).y > headingY + 50); |
| 577 | await project.press("ArrowDown"); await project.press("Enter"); |
| 578 | assert.equal(await project.getAttribute("aria-expanded"),"false"); |
| 579 | assert.equal(await project.evaluate(el => el === document.activeElement),true); |
| 580 | assert.ok(Math.abs((await newSession.boundingBox()).y - headingY) <= 1); |
| 581 | await legacy.getByRole("button",{name:"测试会话",exact:true}).click(); |
| 582 | await legacy.getByRole("textbox",{name:"消息"}).waitFor(); |
| 583 | console.log(engine + ": logo, project tree, drafts, fixed exit, shared favorites, viewport, Desktop pan and login keyboard passed"); |
| 584 | } finally { |
| 585 | await browser?.close(); |
| 586 | await new Promise(resolve => httpd.close(resolve)); |
| 587 | } |