| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 6 | |
| 7 | const output = process.env.CAGE_WORKSPACE_QA_OUTPUT || await mkdtemp(join(tmpdir(), "cage-default-workspace-ui-")); |
| 8 | await mkdir(output, { recursive: true }); |
| 9 | const notice = "此历史会话仍使用原来的客机目录,文件保留在虚拟 Mac 内。要使用与主机共享的默认工作区,请从默认工作区新建会话。"; |
| 10 | let allowed = false; |
| 11 | let screenAllowed = false, frameReads = 0, frameReadsAtClick = 0; |
| 12 | const actions = []; |
| 13 | const maintenance = []; |
| 14 | const fixture = await createWorkbench({ |
| 15 | beforeNavigate: async (page, state) => { |
| 16 | const panelImage = await page.evaluate(() => { |
| 17 | const canvas = document.createElement("canvas"); |
| 18 | canvas.width = 480; |
| 19 | canvas.height = 260; |
| 20 | const context = canvas.getContext("2d"); |
| 21 | context.fillStyle = "#eee"; |
| 22 | context.fillRect(0, 0, canvas.width, canvas.height); |
| 23 | return canvas.toDataURL("image/png").split(",")[1]; |
| 24 | }); |
| 25 | state.account.guest_system = state.status.guest_system = "macos"; |
| 26 | state.status.vm_running = state.status.ready = true; |
| 27 | state.status.project_error = "请在虚拟 Mac 的项目授权窗口中选中此项目,然后重新打开"; |
| 28 | state.status.project_errors = [{ path: state.settings.workspace, message: state.status.project_error }]; |
| 29 | await page.route("**/api/projects/open", async route => { |
| 30 | actions.push(route.request().postDataJSON()); |
| 31 | if (allowed) { |
| 32 | state.status.project_error = ""; |
| 33 | state.status.project_errors = []; |
| 34 | } |
| 35 | return route.fulfill({ json: { workspace: state.settings.workspace, ...(allowed ? {} : { approval: "fixture-grant" }) } }); |
| 36 | }); |
| 37 | await page.route("**/api/macos/project-approval", route => { |
| 38 | if (route.request().postDataJSON().action === "click") { |
| 39 | frameReadsAtClick = frameReads; |
| 40 | return route.fulfill({ json: { error: "accessibility_permission" } }); |
| 41 | } |
| 42 | frameReads++; |
| 43 | return route.fulfill({ json: screenAllowed |
| 44 | ? { image: panelImage, window: 1, pid: 5, nonce: "fixture" } |
| 45 | : { error: "project_screen_permission" } }); |
| 46 | }); |
| 47 | await page.route("**/api/macos/maintenance", route => { |
| 48 | const request = route.request().postDataJSON(); |
| 49 | maintenance.push(request); |
| 50 | if (request.action === "open") return route.fulfill({ json: { session: "permission-fixture", maintenance_version: 1 } }); |
| 51 | if (request.action === "frame") return route.fulfill({ json: { |
| 52 | image: panelImage, mime: "image/png", geometry: "permission-window", region: [100, 80, 480, 260], maintenance_version: 1, |
| 53 | } }); |
| 54 | return route.fulfill({ json: { maintenance_version: 1 } }); |
| 55 | }); |
| 56 | await page.route("**/api/cli/sessions/*/account", route => route.fulfill({ json: { supported: false, candidates: [], session: {} } })); |
| 57 | await page.routeWebSocket("**/api/terminals/**", socket => socket.send(Buffer.from("隔离测试终端\r\n/Users/cage/.local/share/cage/workspace $ "))); |
| 58 | }, |
| 59 | }); |
| 60 | const { page, state, browser, errors } = fixture; |
| 61 | try { |
| 62 | await page.getByRole("button", { name: "重新连接项目", exact: true }).click(); |
| 63 | const project = page.getByRole("dialog", { name: "打开项目", exact: true }); |
| 64 | await project.waitFor(); |
| 65 | assert.equal(await project.getByRole("textbox", { name: "新项目文件夹", exact: true }).inputValue(), state.settings.workspace); |
| 66 | await project.getByRole("button", { name: "打开项目", exact: true }).click(); |
| 67 | const permission = page.getByRole("dialog", { name: "允许客机访问所选项目", exact: true }); |
| 68 | await permission.waitFor(); |
| 69 | await permission.getByText(state.settings.workspace, { exact: true }).waitFor(); |
| 70 | await permission.getByRole("status").filter({ hasText: "屏幕与系统音频录制" }).waitFor(); |
| 71 | await page.screenshot({ path: join(output, "default-workspace-approval.png"), animations: "disabled" }); |
| 72 | await permission.getByRole("button", { name: "打开客机授权设置", exact: true }).click(); |
| 73 | const recovery = page.getByRole("dialog", { name: "恢复客机系统权限", exact: true }); |
| 74 | await recovery.getByRole("img").waitFor(); |
| 75 | assert.equal(maintenance.find(request => request.action === "open").maintenance, "screen"); |
| 76 | await recovery.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 77 | await recovery.waitFor({ state: "detached" }); |
| 78 | screenAllowed = true; |
| 79 | await permission.getByRole("img").click(); |
| 80 | const inputError = permission.getByRole("alert").filter({ hasText: "辅助功能" }); |
| 81 | await inputError.waitFor(); |
| 82 | await permission.getByRole("img").waitFor(); |
| 83 | assert.ok(frameReads > frameReadsAtClick, "next guest frame was fetched"); |
| 84 | assert.ok(await inputError.isVisible(), "polling must retain the failed click"); |
| 85 | await permission.getByRole("button", { name: "打开客机授权设置", exact: true }).click(); |
| 86 | await recovery.getByRole("img").waitFor(); |
| 87 | assert.equal(maintenance.filter(request => request.action === "open").at(-1).maintenance, "input"); |
| 88 | await recovery.getByRole("button", { name: "返回工作台", exact: true }).click(); |
| 89 | await recovery.waitFor({ state: "detached" }); |
| 90 | allowed = true; |
| 91 | await permission.getByRole("button", { name: "验证授权", exact: true }).click(); |
| 92 | await permission.waitFor({ state: "hidden" }); |
| 93 | assert.equal(actions.length, 2); |
| 94 | assert.ok(actions.every(item => item.workspace === state.settings.workspace)); |
| 95 | |
| 96 | state.terminals = [{ id: "legacy-default", kind: "claude", title: "历史默认会话", workspace: state.settings.workspace, |
| 97 | cli_session_id: "01234567-1234-4234-8234-0123456789ab", workspace_notice: notice, closed: true }]; |
| 98 | await page.reload(); |
| 99 | await page.getByRole("tab", { name: "历史默认会话", exact: true }).click(); |
| 100 | await page.getByRole("status").filter({ hasText: notice }).waitFor(); |
| 101 | await page.screenshot({ path: join(output, "default-workspace-legacy-notice.png"), animations: "disabled" }); |
| 102 | assert.deepEqual(errors, []); |
| 103 | const result = { passed: true, browser: browser.browserType().name(), actions, checks: ["default workspace reconnect opens existing project authorization", "missing screen permission opens its restricted recovery view", "click permission error survives the next frame and opens input recovery", "permission retry keeps the same host directory", "legacy guest-private workspace notice is visible in the terminal"] }; |
| 104 | await writeFile(join(output, "result.json"), JSON.stringify(result, null, 2)); |
| 105 | console.log(JSON.stringify(result)); |
| 106 | } finally { await browser.close(); } |
| 107 | |
| 108 | { |
| 109 | let grant = "", generation = 0, approved = false; |
| 110 | const f = await createWorkbench({ |
| 111 | initialization: { required: true, tracked: true, started: true, prepared: true }, |
| 112 | async beforeNavigate(page, state) { |
| 113 | state.account.guest_system = "macos"; |
| 114 | Object.assign(state.status, { |
| 115 | guest_system: "macos", ready: true, vm_running: true, |
| 116 | job: { kind: "safety", running: true, message: "正在检查客机网络" }, |
| 117 | safety: { gate: { mode: "open", until: new Date(Date.now() + 3600000).toISOString() } }, |
| 118 | project_errors: [{ path: state.status.environment_directory, recovery: "project-approval" }], |
| 119 | }); |
| 120 | const image = await page.evaluate(() => document.createElement("canvas").toDataURL().split(",")[1]); |
| 121 | await page.route("**/api/projects/open", route => { |
| 122 | const request = route.request().postDataJSON(); |
| 123 | assert.deepEqual(request, { workspace: state.status.environment_directory, add: false, activate: false }); |
| 124 | if (approved) state.status.project_errors = []; |
| 125 | else if (!grant) grant = "setup-grant-" + ++generation; |
| 126 | return route.fulfill({ json: { workspace: request.workspace, approval: approved ? "" : grant } }); |
| 127 | }); |
| 128 | await page.route("**/api/macos/project-approval", route => { |
| 129 | const q = route.request().postDataJSON(); |
| 130 | if (!grant || q.grant !== grant) return route.fulfill({ |
| 131 | status: 409, headers: { "Content-Type": "text/plain; charset=utf-8", "X-Cage-Recovery": "project-approval-ended" }, body: "项目授权请求已失效,请重新请求授权。", |
| 132 | }); |
| 133 | if (q.action === "frame") return route.fulfill({ json: { image, window: 1, pid: 2, nonce: grant } }); |
| 134 | if (q.action === "click") { assert.equal(q.nonce, grant); approved = true; } |
| 135 | if (q.action === "cancel") grant = ""; |
| 136 | return route.fulfill({ json: {} }); |
| 137 | }); |
| 138 | }, |
| 139 | }); |
| 140 | try { |
| 141 | const region = f.page.getByRole("region", { name: "默认工作区访问" }); |
| 142 | const open = region.getByRole("button", { name: "查看并授权" }); |
| 143 | const dialog = f.page.getByRole("dialog", { name: "允许客机访问所选项目" }); |
| 144 | const enter = f.page.getByRole("button", { name: "进入工作台", exact: true }); |
| 145 | await open.waitFor(); |
| 146 | await region.getByText(f.state.status.environment_directory, { exact: true }).waitFor(); |
| 147 | assert.equal(await dialog.count(), 0); |
| 148 | assert.equal(generation, 0, "polling must not open the guest dialog"); |
| 149 | await open.click(); |
| 150 | await dialog.getByText("当前客机操作结束后,会显示项目授权窗口。").waitFor(); |
| 151 | assert.equal(generation, 0); |
| 152 | f.state.status.job.running = false; |
| 153 | await dialog.getByRole("img").waitFor(); |
| 154 | await dialog.getByRole("button", { name: "取消", exact: true }).click(); |
| 155 | await dialog.waitFor({ state: "hidden" }); |
| 156 | assert.equal(grant, ""); |
| 157 | await enter.waitFor(); |
| 158 | assert.ok(await enter.isEnabled(), "optional project approval cannot gate workbench entry"); |
| 159 | await open.click(); |
| 160 | await dialog.getByRole("img").waitFor(); |
| 161 | grant = ""; |
| 162 | await dialog.getByRole("button", { name: "重新请求授权" }).click(); |
| 163 | await dialog.getByRole("img").waitFor(); |
| 164 | assert.equal(generation, 3); |
| 165 | await dialog.getByRole("button", { name: "验证授权" }).click(); |
| 166 | assert.equal(approved, false, "verification must not approve a pending project"); |
| 167 | await dialog.getByRole("img").click(); |
| 168 | await dialog.waitFor({ state: "hidden" }); |
| 169 | await enter.click(); |
| 170 | await f.page.getByRole("complementary", { name: "项目与终端" }).waitFor(); |
| 171 | assert.deepEqual(f.errors, []); |
| 172 | console.log("PASS macOS initialization: default project approval, cancellation, expiry, renewal, verification and workbench entry"); |
| 173 | } finally { await f.browser.close(); } |
| 174 | } |