| 1 | import assert from "node:assert/strict"; |
| 2 | import { randomBytes } from "node:crypto"; |
| 3 | import fs from "node:fs/promises"; |
| 4 | import os from "node:os"; |
| 5 | import path from "node:path"; |
| 6 | import { execFile } from "node:child_process"; |
| 7 | import { promisify } from "node:util"; |
| 8 | import { createRequire } from "node:module"; |
| 9 | |
| 10 | const run = promisify(execFile); |
| 11 | const target = process.argv[2]; |
| 12 | assert.ok(path.isAbsolute(target || ""), "supply an absolute Cage.app or full Cage helper path"); |
| 13 | const packaged = target.endsWith(".app"); |
| 14 | const cage = packaged ? path.join(target, "Contents/Resources/cage") : target; |
| 15 | const evidence = process.env.CAGE_REMOTE_QA_DIR; |
| 16 | if (evidence) await fs.mkdir(evidence, { recursive: true }); |
| 17 | const profile = await fs.mkdtemp(path.join(os.tmpdir(), "cage-packaged-")); |
| 18 | const env = { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }; |
| 19 | const require = createRequire(import.meta.url); |
| 20 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 21 | let browser; |
| 22 | try { |
| 23 | const { stdout } = await run(cage, ["desktop", "--json"], { env }); |
| 24 | const entry = JSON.parse(stdout); |
| 25 | assert.equal(entry.status, "ready"); |
| 26 | assert.equal(entry.controller.version, (await fs.readFile(new URL("../../VERSION", import.meta.url), "utf8")).trim()); |
| 27 | browser = await chromium.launch({ headless: true, executablePath: process.env.CAGE_TEST_BROWSER_PATH }); |
| 28 | const page = await browser.newPage(); |
| 29 | const errors = []; |
| 30 | page.on("pageerror", (error) => errors.push(error.message)); |
| 31 | await page.goto(entry.url); |
| 32 | await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor(); |
| 33 | await page.getByLabel("用户名",{exact:true}).waitFor(); |
| 34 | const initial = await page.evaluate(async () => (await fetch("/api/client/local-status")).json()); |
| 35 | assert.ok(initial.known && initial.service_running && !initial.running, "opening the local home must not boot the VM"); |
| 36 | assert.equal(await page.getByRole("heading",{name:"选择工作环境"}).count(),0); |
| 37 | if (evidence) await page.screenshot({ path:path.join(evidence,"desktop-first-open.png") }); |
| 38 | const password = randomBytes(24).toString("base64url"); |
| 39 | await page.getByLabel("用户名",{exact:true}).fill("packaged_smoke"); |
| 40 | await page.getByLabel("设置密码",{exact:true}).fill(password); |
| 41 | await page.getByLabel("确认密码",{exact:true}).fill(password); |
| 42 | const statusResponse = page.waitForResponse((response) => response.url().endsWith("/api/status") && response.ok()); |
| 43 | await page.getByRole("button",{name:"创建本地账户",exact:true}).click(); |
| 44 | const status = await (await statusResponse).json(); |
| 45 | assert.equal(status.vm_running, false); |
| 46 | await page.getByRole("heading", { name: "为 Cage 准备独立环境", exact: true }).waitFor(); |
| 47 | const nav = page.getByRole("navigation", {name:"主导航"}); |
| 48 | assert.equal(await nav.count(), 0, "fresh profile must not mount workbench navigation"); |
| 49 | await page.getByRole("button",{name:"只连接 SSH 主机",exact:true}).click(); |
| 50 | await page.getByRole("complementary",{name:"SSH 主机"}).waitFor(); |
| 51 | if (evidence) await page.screenshot({ path:path.join(evidence,"local-ssh-sidebar.png") }); |
| 52 | await page.getByRole("button",{name:"返回工作台侧栏"}).click(); |
| 53 | assert.equal(new URL(page.url()).hash, "", "launch ticket was not removed"); |
| 54 | await page.getByRole("button", { name: "下一步:配置代理", exact: true }).click(); |
| 55 | await page.getByRole("heading",{name:"连接你的网络代理",exact:true}).waitFor(); |
| 56 | assert.ok(await page.getByRole("button",{name:"开始安装",exact:true}).isDisabled(), "proxy must be applied before installation"); |
| 57 | await page.getByText("添加来源", { exact: true }).first().waitFor(); |
| 58 | if(evidence) await page.screenshot({ path:path.join(evidence,"initialization-proxy.png") }); |
| 59 | assert.deepEqual(errors, []); |
| 60 | console.log(JSON.stringify({ checks: ["standalone initialization after account setup", "VM stopped throughout", "no workbench navigation before initialization", "SSH-only alternative", "proxy required before installation", packaged ? "packaged helper bootstrap" : "development helper bootstrap", "real session and APIs", "embedded production UI loaded", "private profile onboarding", "launch ticket removed"], version: entry.controller.version })); |
| 61 | } finally { |
| 62 | await browser?.close(); |
| 63 | await run(cage, ["prepare-update", "--json"], { env }); |
| 64 | await fs.rm(profile, { recursive: true, force: true }); |
| 65 | await fs.rm(profile + "-workspace", { recursive: true, force: true }); |
| 66 | } |