| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { execFileSync } from "node:child_process"; |
| 4 | import { createHash, randomUUID } from "node:crypto"; |
| 5 | import fs from "node:fs/promises"; |
| 6 | import { createRequire } from "node:module"; |
| 7 | import os from "node:os"; |
| 8 | import path from "node:path"; |
| 9 | import { seed, verify } from "./personal-data-fixture.mjs"; |
| 10 | |
| 11 | const source = process.argv[2]; |
| 12 | if (process.platform !== "darwin" || !source || !path.isAbsolute(source) || !source.endsWith(".app")) { |
| 13 | throw new Error("Usage on macOS: node scripts/test-macos-install.mjs /absolute/path/to/Cage.app"); |
| 14 | } |
| 15 | const require = createRequire(import.meta.url); |
| 16 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 17 | const root = await fs.mkdtemp(path.join(os.tmpdir(), "cage-macos-install-")); |
| 18 | const qaHome = path.join(root, "user"); |
| 19 | const manifest = path.join(root, "personal-data.json"); |
| 20 | |
| 21 | const profile = path.join(root, "runtime-profile"); |
| 22 | const env = { ...process.env, HOME: qaHome, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }; |
| 23 | const installed = path.join(qaHome, "Applications", "Cage.app"); |
| 24 | const mount = path.join(root, "mount"); |
| 25 | const image = path.join(root, "manual-install.dmg"); |
| 26 | const stage = path.join(root, "volume"); |
| 27 | const run = (exe, args, options = {}) => execFileSync(exe, args, { encoding: "utf8", timeout: 180000, ...options }); |
| 28 | const cli = (...args) => run(path.join(installed, "Contents/Resources/cage"), args, { env }); |
| 29 | const hash = async file => createHash("sha256").update(await fs.readFile(file)).digest("hex"); |
| 30 | const password = randomUUID() + "7z"; |
| 31 | const username = "install_smoke"; |
| 32 | const evidence = process.env.CAGE_REMOTE_QA_DIR; |
| 33 | let browser, mounted = false, running = false; |
| 34 | |
| 35 | try { |
| 36 | await seed(qaHome, manifest); |
| 37 | await fs.mkdir(path.dirname(installed), { recursive: true }); |
| 38 | await fs.mkdir(stage); |
| 39 | await fs.mkdir(mount); |
| 40 | run("ditto", [source, path.join(stage, "Cage.app")]); |
| 41 | run("hdiutil", ["create", "-quiet", "-volname", "Cage-Isolated-QA", "-srcfolder", stage, "-format", "UDZO", image]); |
| 42 | run("hdiutil", ["attach", "-nobrowse", "-readonly", "-mountpoint", mount, image]); |
| 43 | mounted = true; |
| 44 | const result = { |
| 45 | dmg_sha256: await hash(image), |
| 46 | backend_sha256: await hash(path.join(source, "Contents/Resources/cage")), |
| 47 | launcher_sha256: await hash(path.join(source, "Contents/MacOS/Cage")), |
| 48 | }; |
| 49 | |
| 50 | if (evidence) await fs.mkdir(evidence, { recursive: true }); |
| 51 | browser = await chromium.launch({ headless: true, executablePath: process.env.CAGE_TEST_BROWSER_PATH }); |
| 52 | let savedAccountHash; |
| 53 | for (const phase of ["install", "reinstall"]) { |
| 54 | if (phase === "reinstall") await fs.rm(installed, { recursive: true }); |
| 55 | run("ditto", [path.join(mount, "Cage.app"), installed]); |
| 56 | if (savedAccountHash) assert.equal(await hash(path.join(profile, "account.json")), savedAccountHash, "reinstall changed the account file"); |
| 57 | run("codesign", ["--verify", "--deep", "--strict", installed]); |
| 58 | assert.equal(await hash(path.join(installed, "Contents/Resources/cage")), result.backend_sha256); |
| 59 | cli("verify-web-assets"); |
| 60 | if (process.arch === "arm64") cli("verify-macos-guest"); |
| 61 | running = true; |
| 62 | const entry = JSON.parse(cli("desktop", "--json")); |
| 63 | assert.equal(entry.status, "ready"); |
| 64 | const page = await browser.newPage(); |
| 65 | const errors = []; |
| 66 | page.on("pageerror", error => errors.push(error.message)); |
| 67 | try { |
| 68 | await page.goto(entry.url); |
| 69 | if (phase === "install") { |
| 70 | await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor(); |
| 71 | await page.getByRole("checkbox", { name: /^记住我/ }).uncheck(); |
| 72 | await page.getByLabel("用户名", { exact: true }).fill(username); |
| 73 | await page.getByLabel("设置密码", { exact: true }).fill(password); |
| 74 | await page.getByLabel("确认密码", { exact: true }).fill(password); |
| 75 | await page.getByRole("button", { name: "创建本地账户", exact: true }).click(); |
| 76 | } else { |
| 77 | await page.getByLabel("账户密码", { exact: true }).fill(password); |
| 78 | await page.getByRole("button", { name: "解锁 Cage", exact: true }).click(); |
| 79 | } |
| 80 | await page.getByRole("heading", { name: "为 Cage 准备独立环境", exact: true }).waitFor(); |
| 81 | assert.equal(await page.getByRole("navigation", { name: "主导航" }).count(), 0, "workbench must remain hidden before initialization"); |
| 82 | if (evidence) await page.screenshot({ animations: "disabled", path: path.join(evidence, `${phase}-initialization.png`) }); |
| 83 | await page.getByRole("button", { name: "下一步:配置代理", exact: true }).click(); |
| 84 | await page.getByRole("heading", { name: "连接你的网络代理", exact: true }).waitFor(); |
| 85 | assert.ok(await page.getByRole("button", { name: "开始安装", exact: true }).isDisabled(), "a fresh profile must require a proxy before installing"); |
| 86 | |
| 87 | await page.getByRole("button", { name: "上一步", exact: true }).click(); |
| 88 | await page.getByRole("heading", { name: "为 Cage 准备独立环境", exact: true }).waitFor(); |
| 89 | const account = await page.evaluate(async () => { |
| 90 | const context = await (await fetch("/api/client/context")).json(); |
| 91 | return (await fetch("/api/account", { headers: { "X-Cage-Connection": context.connection_id } })).json(); |
| 92 | }); |
| 93 | assert.equal(account.username, username); |
| 94 | assert.equal(account.unlocked, true); |
| 95 | const status = await page.evaluate(async () => (await fetch("/api/client/local-status")).json()); |
| 96 | assert.ok(status.known && status.service_running && !status.running, "installation QA must leave the VM stopped"); |
| 97 | assert.deepEqual(errors, []); |
| 98 | result[phase] = { original_account: true, initialization_wizard: true, proxy_required: true, vm_stopped: true, preserved: await verify(qaHome, manifest) }; |
| 99 | } finally { |
| 100 | await page.close(); |
| 101 | } |
| 102 | cli("prepare-update", "--json"); |
| 103 | running = false; |
| 104 | savedAccountHash = await hash(path.join(profile, "account.json")); |
| 105 | } |
| 106 | const accountHash = await hash(path.join(profile, "account.json")); |
| 107 | await fs.rm(installed, { recursive: true }); |
| 108 | assert.equal(await hash(path.join(profile, "account.json")), accountHash); |
| 109 | result.uninstall = { account_preserved: true, preserved: await verify(qaHome, manifest) }; |
| 110 | if (evidence) await fs.writeFile(path.join(evidence, "result.json"), JSON.stringify(result, null, 2)); |
| 111 | console.log(JSON.stringify(result, null, 2)); |
| 112 | } finally { |
| 113 | try { |
| 114 | if (running) cli("prepare-update", "--json"); |
| 115 | } finally { |
| 116 | try { |
| 117 | if (browser) await browser.close(); |
| 118 | } finally { |
| 119 | if (mounted) run("hdiutil", ["detach", mount]); |
| 120 | await fs.rm(root, { recursive: true }); |
| 121 | } |
| 122 | } |
| 123 | } |