| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import { mkdir } from "node:fs/promises"; |
| 5 | import { join, resolve } from "node:path"; |
| 6 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 7 | |
| 8 | const reports = resolve( |
| 9 | process.env.CAGE_REMOTE_QA_DIR || "ui/_output/desktop-environments", |
| 10 | ); |
| 11 | await mkdir(reports, { recursive: true }); |
| 12 | const host = { |
| 13 | id: "fixture-host", |
| 14 | name: "开发云服务器", |
| 15 | host: "dev.example", |
| 16 | user: "cage", |
| 17 | port: 22, |
| 18 | auth: { kind: "agent" }, |
| 19 | }; |
| 20 | const second = { |
| 21 | ...host, |
| 22 | id: "fixture-second", |
| 23 | name: "第二台开发机", |
| 24 | host: "test.example", |
| 25 | }; |
| 26 | let generation = 0; |
| 27 | const view = { |
| 28 | desktop_gateway: true, |
| 29 | mode: "none", |
| 30 | connection_id: "view-0", |
| 31 | client_platform: "darwin", |
| 32 | server_platform: "darwin", |
| 33 | connection: { state: "disconnected" }, |
| 34 | }; |
| 35 | const local = { |
| 36 | available: true, |
| 37 | known: true, |
| 38 | service_running: false, |
| 39 | running: false, |
| 40 | busy: false, |
| 41 | live_terminals: 0, |
| 42 | controller_id: "", |
| 43 | }; |
| 44 | let failSSH = true, |
| 45 | holdSSH = true, |
| 46 | staleStop = false; |
| 47 | const actions = []; |
| 48 | function switchTo(mode, hostID) { |
| 49 | const target = hostID === second.id ? second : host; |
| 50 | Object.assign(view, { |
| 51 | mode, |
| 52 | connection_id: "view-" + ++generation, |
| 53 | candidate: null, |
| 54 | host_id: mode === "remote" ? target.id : "", |
| 55 | name: mode === "remote" ? target.name : "", |
| 56 | server_platform: mode === "remote" ? "linux" : "darwin", |
| 57 | instance_id: mode === "remote" ? "1".repeat(64) : "", |
| 58 | connection: { state: mode === "none" ? "disconnected" : "connected" }, |
| 59 | }); |
| 60 | } |
| 61 | function stopLocal() { |
| 62 | Object.assign(local, { |
| 63 | service_running: false, |
| 64 | running: false, |
| 65 | live_terminals: 0, |
| 66 | controller_id: "", |
| 67 | }); |
| 68 | } |
| 69 | const { browser, page, state, errors } = await createWorkbench({ |
| 70 | clientContext: view, |
| 71 | clientRequest: async (route, path, body) => { |
| 72 | const reply = (value) => route.fulfill({ json: value }); |
| 73 | if (path === "/client/hosts") return reply({ hosts: [host, second] }); |
| 74 | if (path === "/client/local-status") return reply(local); |
| 75 | if (path === "/client/context") { |
| 76 | if (!holdSSH && view.candidate?.connection.state === "connecting") { |
| 77 | Object.assign( |
| 78 | view.candidate.connection, |
| 79 | failSSH |
| 80 | ? { state: "failed", message: "无法连接云端服务" } |
| 81 | : { state: "connected" }, |
| 82 | ); |
| 83 | } |
| 84 | return reply(view); |
| 85 | } |
| 86 | assert.equal(body.connection_id, view.connection_id, "stale client intent"); |
| 87 | actions.push([path, body]); |
| 88 | if (path === "/client/candidate") { |
| 89 | view.candidate = { |
| 90 | id: "candidate-" + ++generation, |
| 91 | mode: body.mode, |
| 92 | connection: { |
| 93 | host_id: body.host_id, |
| 94 | state: body.mode === "local" ? "connected" : "connecting", |
| 95 | }, |
| 96 | }; |
| 97 | return reply(view.candidate); |
| 98 | } |
| 99 | if (path === "/client/cancel") { |
| 100 | assert.equal(body.candidate_id, view.candidate.id); |
| 101 | view.candidate = null; |
| 102 | return reply(view); |
| 103 | } |
| 104 | if (path === "/client/commit") { |
| 105 | assert.equal(body.candidate_id, view.candidate.id); |
| 106 | assert.equal(view.candidate.connection.state, "connected"); |
| 107 | if (body.local_action === "stop") { |
| 108 | assert.equal(body.local_controller_id, local.controller_id); |
| 109 | if (staleStop) |
| 110 | return route.fulfill({ status: 423, body: "请切回本机解锁" }); |
| 111 | stopLocal(); |
| 112 | } |
| 113 | switchTo(view.candidate.mode, view.candidate.connection.host_id); |
| 114 | return reply(view); |
| 115 | } |
| 116 | if (path === "/client/local-stop") { |
| 117 | assert.equal(body.local_controller_id, local.controller_id); |
| 118 | if (staleStop) |
| 119 | return route.fulfill({ status: 423, body: "请切回本机解锁" }); |
| 120 | stopLocal(); |
| 121 | return reply(view); |
| 122 | } |
| 123 | if (path === "/client/disconnect") { |
| 124 | switchTo("none"); |
| 125 | return reply(view); |
| 126 | } |
| 127 | throw new Error("Unexpected client API " + path); |
| 128 | }, |
| 129 | }); |
| 130 | page.setDefaultTimeout(7000); |
| 131 | const click = (name) => page.getByRole("button", { name, exact: true }).click(); |
| 132 | const visible = (name) => |
| 133 | page.getByText(name, { exact: true }).first().waitFor(); |
| 134 | try { |
| 135 | await page.getByRole("heading", { name: "先启动 Cage 环境" }).waitFor(); |
| 136 | assert.equal(view.mode, "local"); |
| 137 | assert.equal(state.requests.some(r => r.path === "/start"), false); |
| 138 | await click("SSH 主机"); |
| 139 | |
| 140 | await page.getByRole("heading", { name: "先启动 Cage 环境" }).waitFor(); |
| 141 | await click("返回工作台侧栏"); |
| 142 | await page.getByRole("navigation", { name: "主导航" }).waitFor(); |
| 143 | await click("SSH 主机"); |
| 144 | await click("连接此主机"); |
| 145 | await page.getByText("正在验证…", { exact: true }).waitFor(); |
| 146 | assert.equal(await page.getByRole("dialog").count(), 0); |
| 147 | holdSSH = false; |
| 148 | await visible("无法连接云端服务"); |
| 149 | assert.equal(view.mode, "local"); |
| 150 | failSSH = false; |
| 151 | Object.assign(local, { |
| 152 | service_running: true, |
| 153 | running: true, |
| 154 | live_terminals: 2, |
| 155 | controller_id: "local-identity", |
| 156 | }); |
| 157 | await click("连接此主机"); |
| 158 | const decision = page.getByRole("dialog", { name: "切换到 SSH 主机" }); |
| 159 | await decision.waitFor(); |
| 160 | await page.keyboard.press("Escape"); |
| 161 | await decision.waitFor({ state: "hidden" }); |
| 162 | assert.equal(view.mode, "local"); |
| 163 | assert.equal(local.running, true); |
| 164 | await click("连接此主机"); |
| 165 | await decision.waitFor(); |
| 166 | await page.screenshot({ path: join(reports, "ssh-switch-confirmation.png") }); |
| 167 | await click("保留本机运行"); |
| 168 | await visible("本机也在运行"); |
| 169 | await visible("SSH · 云端运行"); |
| 170 | assert.equal(view.mode, "remote"); |
| 171 | assert.equal(local.running, true); |
| 172 | await click("停止本机环境"); |
| 173 | staleStop = true; |
| 174 | await click("确认停止本机"); |
| 175 | await visible("本机环境已锁定。请先返回本机解锁,再停止本机环境。"); |
| 176 | assert.equal(view.mode, "remote"); |
| 177 | assert.equal(state.account.unlocked, true); |
| 178 | await click("返回本机解锁"); |
| 179 | await page.locator(".client-toolbar").waitFor({state: "hidden"}); |
| 180 | await page.getByRole("navigation", {name: "主导航"}).waitFor(); |
| 181 | assert.equal(view.mode, "local"); |
| 182 | staleStop = false; |
| 183 | await click("SSH 主机"); |
| 184 | await click("连接此主机"); |
| 185 | await decision.waitFor(); |
| 186 | await click("保留本机运行"); |
| 187 | await visible("SSH · 云端运行"); |
| 188 | await click("SSH 主机"); |
| 189 | await page.screenshot({ path: join(reports, "ssh-sidebar-background.png") }); |
| 190 | await page.getByRole("button", { name: /第二台开发机/ }).click(); |
| 191 | await click("连接此主机"); |
| 192 | await decision.waitFor(); |
| 193 | assert.equal(view.host_id, host.id, "A remains selected before B's commit"); |
| 194 | staleStop = true; |
| 195 | await click("停止本机并连接"); |
| 196 | await decision.getByRole("button", { name: "返回本机解锁" }).waitFor(); |
| 197 | assert.equal(view.host_id, host.id); |
| 198 | const cancelled = view.candidate.id; |
| 199 | await click("返回本机解锁"); |
| 200 | await page.locator(".client-toolbar").waitFor({state: "hidden"}); |
| 201 | await page.getByRole("navigation", {name: "主导航"}).waitFor(); |
| 202 | assert.ok( |
| 203 | actions.some( |
| 204 | ([path, body]) => |
| 205 | path === "/client/cancel" && body.candidate_id === cancelled, |
| 206 | ), |
| 207 | ); |
| 208 | staleStop = false; |
| 209 | await click("SSH 主机"); |
| 210 | await click("连接此主机"); |
| 211 | await decision.waitFor(); |
| 212 | await click("保留本机运行"); |
| 213 | await visible("SSH · 云端运行"); |
| 214 | await click("SSH 主机"); |
| 215 | await page.getByRole("button", { name: /第二台开发机/ }).click(); |
| 216 | await click("连接此主机"); |
| 217 | await decision.waitFor(); |
| 218 | await click("停止本机并连接"); |
| 219 | await visible("SSH · 云端运行"); |
| 220 | assert.equal(view.host_id, second.id); |
| 221 | assert.equal(local.running, false); |
| 222 | Object.assign(local, { |
| 223 | running: true, |
| 224 | live_terminals: 1, |
| 225 | controller_id: "new-local-identity", |
| 226 | }); |
| 227 | await click("停止本机环境"); |
| 228 | await click("确认停止本机"); |
| 229 | await visible("SSH · 云端运行"); |
| 230 | assert.equal(view.mode, "remote"); |
| 231 | assert.equal(local.running, false); |
| 232 | await click("SSH 主机"); |
| 233 | await click("断开连接"); |
| 234 | await page.getByRole("complementary", { name: "SSH 主机" }).waitFor(); |
| 235 | assert.equal(view.mode, "none"); |
| 236 | assert.equal( |
| 237 | actions.some( |
| 238 | ([path, body]) => |
| 239 | path === "/client/local-stop" && |
| 240 | body.local_controller_id === "new-local-identity", |
| 241 | ), |
| 242 | true, |
| 243 | ); |
| 244 | assert.deepEqual(errors, []); |
| 245 | console.log( |
| 246 | "PASS: on-demand workbench, SSH sidebar round trip, candidate failure, cancel/keep/stop, locked local stop, confirmed background stop and disconnect.", |
| 247 | reports, |
| 248 | ); |
| 249 | } catch (error) { |
| 250 | await page.screenshot({ path: join(reports, "failure.png") }).catch(() => {}); |
| 251 | throw error; |
| 252 | } finally { |
| 253 | await browser.close(); |
| 254 | } |