| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir } from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const failure = "联网检查未通过:浏览器语言未通过。语言与已保存的设置不一致。请核对语言设置后重新检测。外网保持关闭。"; |
| 6 | const output = process.env.CAGE_SAFETY_SCREENSHOTS || "/tmp/cage-safety-checklist"; |
| 7 | await mkdir(output, { recursive: true }); |
| 8 | const platforms = process.env.CAGE_TEST_BROWSER === "webkit" ? ["darwin"] : ["darwin", "windows", "linux"]; |
| 9 | |
| 10 | for (const platform of platforms) { |
| 11 | const { browser, page, state, errors } = await createWorkbench({ |
| 12 | clientContext: { mode: "local", connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "disconnected" } }, |
| 13 | |
| 14 | |
| 15 | beforeNavigate: page => page.addInitScript(() => { |
| 16 | const fetch = window.fetch; |
| 17 | window.fetch = (input, ...args) => String(input) === "/api/cli/sessions" |
| 18 | ? Promise.resolve(new Response('{"sessions":[]}', { headers: { "Content-Type": "application/json" } })) |
| 19 | : fetch(input, ...args); |
| 20 | }), |
| 21 | }); |
| 22 | const checklist = page.getByRole("dialog", { name: "网络与隔离检查", exact: true }); |
| 23 | const link = parent => parent.getByRole("button", { name: "查看检查清单", exact: true }); |
| 24 | const showChecklist = async (button, keyboard = false) => { |
| 25 | if (keyboard) { await button.focus(); await button.press("Enter"); } |
| 26 | else await button.click(); |
| 27 | await checklist.waitFor(); |
| 28 | assert.equal(await page.getByRole("dialog").evaluateAll(nodes => nodes.at(-1).getAttribute("aria-label")), "网络与隔离检查"); |
| 29 | await checklist.getByRole("heading", { name: "关键检查", exact: true }).waitFor(); |
| 30 | await checklist.getByText("1 / 2 通过", { exact: true }).waitFor(); |
| 31 | const failed = checklist.locator("details").filter({ hasText: "浏览器语言" }); |
| 32 | await failed.locator("summary").click(); |
| 33 | await failed.getByText("语言与已保存的设置不一致", { exact: true }).waitFor(); |
| 34 | await failed.getByText("请核对语言设置后重新检测", { exact: true }).waitFor(); |
| 35 | }; |
| 36 | const closeChecklist = async () => { |
| 37 | await page.keyboard.press("Escape"); |
| 38 | await checklist.waitFor({ state: "hidden" }); |
| 39 | }; |
| 40 | try { |
| 41 | Object.assign(state.status, { |
| 42 | ready: true, vm_running: true, |
| 43 | job: { id: "failed-check", kind: "safety", running: false, error: failure, message: "环境检查未完成", log: "fixture check log" }, |
| 44 | safety: { |
| 45 | gate: { mode: "closed", reason: failure }, error: failure, |
| 46 | report: { created_at: new Date().toISOString(), checks: [ |
| 47 | { id: "dns", title: "加密 DNS", critical: true, state: "passed", evidence: "DNS 通过指定代理" }, |
| 48 | { id: "browser", title: "浏览器语言", critical: true, state: "failed", evidence: "语言与已保存的设置不一致", remedy: "请核对语言设置后重新检测" }, |
| 49 | ] }, |
| 50 | }, |
| 51 | }); |
| 52 | await page.reload(); |
| 53 | const homeError = page.getByRole("alert").filter({ hasText: failure }); |
| 54 | await link(homeError).waitFor(); |
| 55 | await page.screenshot({ path: `${output}/${platform}-failure.png` }); |
| 56 | const postCount = () => state.requests.filter(r => r.method === "POST").length; |
| 57 | const beforeOpening = postCount(); |
| 58 | await showChecklist(link(homeError), true); |
| 59 | await page.screenshot({ path: `${output}/${platform}-checklist.png` }); |
| 60 | await closeChecklist(); |
| 61 | assert.equal(postCount(), beforeOpening, "viewing a checklist must not start checks or open networking"); |
| 62 | assert.equal(await link(homeError).evaluate(node => node === document.activeElement), true); |
| 63 | |
| 64 | const feedback = page.locator(".status-feedback"); |
| 65 | state.status.job = { ...state.status.job, id: "failed-check-notification" }; |
| 66 | await link(feedback).waitFor(); |
| 67 | await showChecklist(link(feedback)); |
| 68 | await closeChecklist(); |
| 69 | |
| 70 | await page.getByRole("button", { name: "查看操作详情", exact: true }).click(); |
| 71 | const details = page.getByRole("dialog", { name: "操作详情", exact: true }); |
| 72 | await showChecklist(link(details)); |
| 73 | await closeChecklist(); |
| 74 | assert.equal(await details.isVisible(), true, "closing the checklist must preserve operation details"); |
| 75 | await details.getByRole("button", { name: "关闭面板", exact: true }).click(); |
| 76 | |
| 77 | state.status.job.setup_progress = { phase: "services", label: "环境服务" }; |
| 78 | await page.reload(); |
| 79 | const setup = page.getByRole("region", { name: "环境安装进度", exact: true }); |
| 80 | await showChecklist(link(setup)); |
| 81 | await closeChecklist(); |
| 82 | delete state.status.job.setup_progress; |
| 83 | await page.reload(); |
| 84 | |
| 85 | await page.route("**/api/settings/probe", route => route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: failure })); |
| 86 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 87 | await page.getByRole("navigation", { name: "设置分组" }).getByRole("button", { name: "网络与代理", exact: true }).click(); |
| 88 | const settings = page.getByRole("region", { name: "设置", exact: true }); |
| 89 | const dns = settings.getByLabel("加密 DNS", { exact: true }); |
| 90 | await dns.fill("https://example.com/dns-query"); |
| 91 | await settings.getByRole("button", { name: "验证出口", exact: true }).click(); |
| 92 | const settingsError = settings.getByRole("alert").filter({ hasText: failure }); |
| 93 | await showChecklist(link(settingsError), true); |
| 94 | await closeChecklist(); |
| 95 | assert.equal(await dns.inputValue(), "https://example.com/dns-query", "viewing checks must preserve unsaved settings"); |
| 96 | assert.equal(await settings.isVisible(), true); |
| 97 | await dns.fill(state.settings.dns_url); |
| 98 | |
| 99 | await page.route("**/api/proxy/route", route => route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: "关键检测证据不完整,外网保持关闭" })); |
| 100 | await settings.getByRole("button", { name: "重新应用", exact: true }).click(); |
| 101 | const proxyError = settings.getByRole("alert").filter({ hasText: "关键检测证据不完整" }); |
| 102 | await showChecklist(link(proxyError)); |
| 103 | await closeChecklist(); |
| 104 | await settings.getByRole("button", { name: "返回工作台", exact: true }).first().click(); |
| 105 | |
| 106 | await page.route("**/api/projects/open", route => route.fulfill({ status: 409, contentType: "text/plain; charset=utf-8", body: failure })); |
| 107 | await page.getByRole("button", { name: "打开项目", exact: true }).click(); |
| 108 | const projects = page.getByRole("dialog", { name: "打开项目", exact: true }); |
| 109 | await projects.getByRole("button", { name: "打开项目", exact: true }).click(); |
| 110 | await showChecklist(link(projects)); |
| 111 | await closeChecklist(); |
| 112 | assert.equal(await projects.isVisible(), true); |
| 113 | await projects.getByRole("button", { name: "取消", exact: true }).click(); |
| 114 | |
| 115 | for (const viewport of [{ width: 820, height: 558 }, { width: 560, height: 812 }]) { |
| 116 | await page.setViewportSize(viewport); |
| 117 | if (viewport.width === 560) await page.getByRole("button", { name: "收起侧栏", exact: true }).click(); |
| 118 | await showChecklist(link(homeError)); |
| 119 | assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false); |
| 120 | await closeChecklist(); |
| 121 | await page.screenshot({ path: `${output}/${platform}-${viewport.width}.png` }); |
| 122 | } |
| 123 | |
| 124 | state.status.job = { ...state.status.job, id: "unrelated-error", error: "项目文件夹连接失败" }; |
| 125 | await page.reload(); |
| 126 | const unrelated = page.getByRole("alert").filter({ hasText: "项目文件夹连接失败" }); |
| 127 | await unrelated.waitFor(); |
| 128 | assert.equal(await link(unrelated).count(), 0); |
| 129 | assert.equal(state.status.safety.gate.mode, "closed"); |
| 130 | assert.deepEqual(errors, []); |
| 131 | console.log(`PASS ${platform}: home, notification, details, setup, settings, proxy, project, keyboard, return focus, compact layout and no mutations`); |
| 132 | } finally { await browser.close(); } |
| 133 | } |