| 1 | |
| 2 | |
| 3 | import process from "node:process"; |
| 4 | import { mkdir, readFile } from "node:fs/promises"; |
| 5 | import path from "node:path"; |
| 6 | import assert from "node:assert/strict"; |
| 7 | const chunks = []; |
| 8 | for await (const chunk of process.stdin) chunks.push(chunk); |
| 9 | const entry = Buffer.concat(chunks).toString("utf8").trim(); |
| 10 | const engine = process.env.CAGE_TEST_MOBILE_BROWSER; |
| 11 | if (!["chromium", "webkit"].includes(engine)) throw Error("Choose chromium or webkit"); |
| 12 | const playwright = await import(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 13 | const browser = await playwright[engine].launch({headless:true}); |
| 14 | try { |
| 15 | const page = await browser.newPage({viewport:{width:390,height:844},deviceScaleFactor:1,isMobile:true,hasTouch:true}); |
| 16 | page.setDefaultTimeout(18000); |
| 17 | |
| 18 | |
| 19 | await page.route("**/api/mobile/cli/projects", async route => { |
| 20 | const response = await route.fetch(); |
| 21 | const value = await response.json(); |
| 22 | delete value.shared_sessions; |
| 23 | await route.fulfill({response, json:value}); |
| 24 | }); |
| 25 | await page.route("**/api/mobile/cli/sessions", async route => { |
| 26 | if (route.request().method() !== "POST") return route.continue(); |
| 27 | |
| 28 | |
| 29 | await route.fulfill({status:200,contentType:"application/json",body:process.env.CAGE_TEST_MOBILE_LEGACY_SESSION}); |
| 30 | }); |
| 31 | await page.goto(entry); |
| 32 | await page.getByRole("dialog", {name:"工作区侧栏"}).getByRole("button", {name:/ 新建会话$/}).first().click(); |
| 33 | await page.getByRole("dialog", {name:"工作区侧栏"}).waitFor({state:"hidden"}); |
| 34 | const message = page.getByRole("textbox", {name:"消息"}); |
| 35 | await message.fill("initial mobile prompt"); |
| 36 | await page.getByRole("button", {name:"发送",exact:true}).click(); |
| 37 | const approve = page.getByRole("button",{name:"允许一次"}); |
| 38 | await approve.waitFor(); |
| 39 | assert.match(await page.getByLabel("完整工具参数").innerText(), /mobile-approved\.txt/); |
| 40 | await assert.rejects(readFile(process.env.CAGE_TEST_MOBILE_MARKER), {code:"ENOENT"}); |
| 41 | const out = process.env.CAGE_TEST_MOBILE_CAPTURE; |
| 42 | if(out) { |
| 43 | await mkdir(out,{recursive:true}); |
| 44 | await page.screenshot({path:path.join(out,engine+"-cli-approval.png")}); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | await page.reload(); |
| 49 | |
| 50 | |
| 51 | let offline = false; |
| 52 | const sessionRequests = "**/api/mobile/cli/sessions/**"; |
| 53 | await page.route(sessionRequests, async route => { |
| 54 | if (route.request().method() === "POST" && route.request().url().endsWith("/approval")) offline = true; |
| 55 | if (offline) await route.abort("internetdisconnected"); |
| 56 | else await route.continue(); |
| 57 | }); |
| 58 | await approve.click(); |
| 59 | await page.getByRole("alert").getByRole("button", {name:"重新读取"}).waitFor(); |
| 60 | await page.unroute(sessionRequests); |
| 61 | await page.getByText("主机仍在等待这次审批。请核对参数后再次选择。", {exact:true}).waitFor(); |
| 62 | await assert.rejects(readFile(process.env.CAGE_TEST_MOBILE_MARKER), {code:"ENOENT"}); |
| 63 | await approve.click(); |
| 64 | await page.getByText("isolated tool completed", {exact:true}).waitFor(); |
| 65 | const tools = page.locator(".tool-history"); |
| 66 | await tools.locator("summary").click(); |
| 67 | await tools.getByText("Bash", {exact:true}).waitFor(); |
| 68 | await tools.getByText("已完成", {exact:true}).waitFor(); |
| 69 | assert.equal(await readFile(process.env.CAGE_TEST_MOBILE_MARKER,"utf8"),"approved"); |
| 70 | await message.fill("follow-up"); |
| 71 | await page.getByRole("button", {name:"发送",exact:true}).click(); |
| 72 | await page.getByText("isolated continuation",{exact:true}).waitFor(); |
| 73 | if(out) await page.screenshot({path:path.join(out,engine+"-cli-result.png")}); |
| 74 | } catch(error) { |
| 75 | throw Error(String(error).replace(/(link-bootstrap)#[A-Za-z0-9_-]+/g,"$1#[redacted]")); |
| 76 | } finally { |
| 77 | await browser.close(); |
| 78 | } |