| 1 | import assert from "node:assert/strict"; |
| 2 | import { createRequire } from "node:module"; |
| 3 | import { join } from "node:path"; |
| 4 | import { smokeCaptureDirectory } from "./smoke-captures.mjs"; |
| 5 | |
| 6 | const require = createRequire(import.meta.url); |
| 7 | const engines = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 8 | const engine = process.env.CAGE_TEST_BROWSER || "chromium"; |
| 9 | const browser = await engines[engine].launch({executablePath:process.env.CAGE_TEST_BROWSER_PATH}); |
| 10 | const output = await smokeCaptureDirectory("mobile-inline-select"); |
| 11 | const origin = process.env.CAGE_UI_URL || "http://127.0.0.1:5399"; |
| 12 | const errors = []; |
| 13 | try { |
| 14 | const page = await browser.newPage({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }); |
| 15 | page.on("pageerror", error => errors.push(error.message)); |
| 16 | const projects = [{ path: "/work/alpha", name: "alpha" }, { path: "/work/beta", name: "beta" }, { path: "/work/gamma", name: "gamma" }]; |
| 17 | const sessions = []; |
| 18 | let creations = 0; |
| 19 | await page.route("**/api/**", route => { |
| 20 | const request = route.request(), path = new URL(request.url()).pathname; |
| 21 | if (path.endsWith("/projects")) return route.fulfill({ json: { host: "隔离主机", ready: true, projects, shared_sessions: true } }); |
| 22 | if (path.endsWith("/sessions")) { |
| 23 | if (request.method() === "POST") { |
| 24 | creations++; |
| 25 | const input = request.postDataJSON(); |
| 26 | const session = { ...input, title: "测试会话", revision: 1, generation: input.id, turns: [] }; |
| 27 | sessions.push(session); |
| 28 | return route.fulfill({ json: session }); |
| 29 | } |
| 30 | return route.fulfill({ json: { sessions } }); |
| 31 | } |
| 32 | if (path.endsWith("/account")) return route.fulfill({ json: { session: sessions[0], supported: false, candidates: [] } }); |
| 33 | return route.fulfill({ json: sessions[0] || {} }); |
| 34 | }); |
| 35 | await page.goto(origin + "/mobile.html"); |
| 36 | const picker = page.getByRole("combobox", { name: "选择项目", exact: true }); |
| 37 | await picker.waitFor(); |
| 38 | const create = page.getByRole("button", { name: "新建会话", exact: true }); |
| 39 | const collapsed = await create.boundingBox(); |
| 40 | await picker.press("Enter"); |
| 41 | assert.ok(Math.abs((await create.boundingBox()).y-collapsed.y)<1); |
| 42 | await picker.press("End"); |
| 43 | await picker.press("Enter"); |
| 44 | await picker.getByText("gamma", { exact: true }).waitFor(); |
| 45 | assert.equal(await picker.getAttribute("aria-expanded"), "false"); |
| 46 | assert.equal(await picker.evaluate(element => document.activeElement === element), true); |
| 47 | await picker.press("ArrowDown"); |
| 48 | await picker.press("Escape"); |
| 49 | assert.equal(await picker.getAttribute("aria-expanded"), "false"); |
| 50 | assert.equal((await create.boundingBox()).y, collapsed.y); |
| 51 | await picker.tap(); |
| 52 | await picker.tap(); |
| 53 | await create.tap(); |
| 54 | await page.getByRole("textbox", { name: "消息", exact: true }).waitFor(); |
| 55 | assert.equal(creations, 1, "one tap after closing the project selector must create exactly one session"); |
| 56 | assert.equal(sessions[0].workspace, "/work/gamma"); |
| 57 | await page.screenshot({ path: join(output, `${engine}-new-session.png`) }); |
| 58 | await page.close(); |
| 59 | |
| 60 | const phone = await browser.newPage({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, userAgent: "CageMobile/1" }); |
| 61 | phone.on("pageerror", error => errors.push(error.message)); |
| 62 | const image = await phone.evaluate(() => { |
| 63 | const canvas = document.createElement("canvas"); |
| 64 | canvas.width = 390; canvas.height = 600; |
| 65 | return canvas.toDataURL("image/jpeg").split(",")[1]; |
| 66 | }); |
| 67 | let frame, writes = 0; |
| 68 | const favorites = { folders: [{ id: "work", title: "工作" }, { id: "reference", title: "资料" }], items: [] }; |
| 69 | await phone.route("**/api/**", route => { |
| 70 | const request = route.request(), path = new URL(request.url()).pathname, body = request.postDataJSON(); |
| 71 | if (path.endsWith("/projects")) return route.fulfill({ json: { host: "隔离测试电脑", ready: true, projects: [] } }); |
| 72 | if (path.endsWith("/sessions")) return route.fulfill({ json: { sessions: [] } }); |
| 73 | if (path.endsWith("/favorites")) { |
| 74 | if (body) { writes++; favorites.items.push({ id: "saved", ...body }); } |
| 75 | return route.fulfill({ json: favorites }); |
| 76 | } |
| 77 | if (path.endsWith("/open")) { |
| 78 | frame = { ...body, image, document: "fixture-document", url: "https://example.com/", title: "测试网页" }; |
| 79 | return route.fulfill({ json: { id: frame.id } }); |
| 80 | } |
| 81 | if (path.endsWith("/frame")) return route.fulfill({ json: frame }); |
| 82 | return route.fulfill({ json: { id: frame?.id } }); |
| 83 | }); |
| 84 | await phone.goto(origin + "/mobile.html"); |
| 85 | await phone.getByRole("dialog", { name: "工作区侧栏" }).getByRole("button", { name: "浏览器", exact: true }).click(); |
| 86 | await phone.getByRole("img", { name: "远程浏览器画面" }).waitFor(); |
| 87 | await phone.getByRole("button", { name: "收藏夹", exact: true }).tap(); |
| 88 | await phone.getByRole("button", { name: "收藏此页", exact: true }).tap(); |
| 89 | await phone.getByRole("combobox", { name: "保存到", exact: true }).tap(); |
| 90 | await phone.getByRole("combobox", { name: "保存到", exact: true }).tap(); |
| 91 | await phone.getByRole("button", { name: "保存收藏", exact: true }).tap(); |
| 92 | await phone.getByRole("button", { name: "取消此页收藏", exact: true }).waitFor(); |
| 93 | assert.equal(writes, 1, "one tap after closing the folder selector must save exactly once"); |
| 94 | await phone.screenshot({ path: join(output, `${engine}-saved-favorite.png`) }); |
| 95 | assert.deepEqual(errors, []); |
| 96 | console.log(`${engine}: floating layout, keyboard selection, Escape, focus restoration and session/favorite mutations passed.`); |
| 97 | } finally { |
| 98 | await browser.close(); |
| 99 | } |