| 1 | import assert from "node:assert/strict"; |
| 2 | import fs from "node:fs/promises"; |
| 3 | import os from "node:os"; |
| 4 | import path from "node:path"; |
| 5 | import { execFile } from "node:child_process"; |
| 6 | import { promisify } from "node:util"; |
| 7 | import { createRequire } from "node:module"; |
| 8 | const run = promisify(execFile); |
| 9 | const cage = path.resolve(process.argv[2] || "_output/diagnostics-qa/cage-backend"); |
| 10 | const require = createRequire(import.meta.url); |
| 11 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 12 | const root = await fs.mkdtemp(path.join(os.tmpdir(), "cage-diagnostics-")); |
| 13 | const profile = path.join(root, "profile"); |
| 14 | const env = { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }; |
| 15 | let browser; |
| 16 | const reportDirectory = process.env.CAGE_DIAGNOSTIC_REPORT |
| 17 | ? path.resolve(process.env.CAGE_DIAGNOSTIC_REPORT) |
| 18 | : await fs.mkdtemp(path.join(os.tmpdir(), "cage-diagnostics-report-")); |
| 19 | await fs.mkdir(reportDirectory, { recursive: true }); |
| 20 | try { |
| 21 | const { stdout } = await run(cage, ["desktop", "--json"], { env }); |
| 22 | const entry = JSON.parse(stdout); |
| 23 | assert.equal(entry.status, "ready"); |
| 24 | browser = await chromium.launch({ headless: true }); |
| 25 | const page = await browser.newPage({ acceptDownloads: true, viewport: { width: 1280, height: 900 } }); |
| 26 | await page.goto(entry.url); |
| 27 | await page.getByRole("heading", { name: "注册 Cage 账户", exact: true }).waitFor(); |
| 28 | await page.getByLabel("用户名", { exact: true }).fill("diagnostics_qa"); |
| 29 | await page.getByLabel("设置密码", { exact: true }).fill("Test-only-password-8712"); |
| 30 | await page.getByLabel("确认密码", { exact: true }).fill("Test-only-password-8712"); |
| 31 | await page.getByRole("button", { name: "创建本地账户", exact: true }).click(); |
| 32 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 33 | await page.getByRole("button", { name: "环境与恢复", exact: true }).click(); |
| 34 | await page.getByRole("heading", { name: "日志与诊断" }).waitFor(); |
| 35 | |
| 36 | const reports = Promise.all(["diagnostic-ui-sentinel", "diagnostic-rejection-sentinel"].map(marker => |
| 37 | page.waitForResponse(response => response.url().endsWith("/api/diagnostics/events") && response.request().postData()?.includes(marker) && response.ok()))); |
| 38 | await page.evaluate(() => { |
| 39 | window.dispatchEvent(new ErrorEvent("error", { error: new Error("password=ui-private-secret diagnostic-ui-sentinel") })); |
| 40 | window.dispatchEvent(new PromiseRejectionEvent("unhandledrejection", { promise: Promise.resolve(), reason: new Error("token=rejection-secret diagnostic-rejection-sentinel") })); |
| 41 | }); |
| 42 | await reports; |
| 43 | const downloaded = page.waitForEvent("download"); |
| 44 | await page.getByRole("button", { name: "导出诊断包", exact: true }).click(); |
| 45 | await (await downloaded).saveAs(path.join(root, "browser.zip")); |
| 46 | await page.getByRole("status").filter({ hasText: "诊断包已下载" }).waitFor(); |
| 47 | await page.screenshot({ path: path.join(reportDirectory, "settings.png") }); |
| 48 | |
| 49 | const nativeOutput = path.join(root, "native.zip"); |
| 50 | const saved = await run(cage, ["diagnostics", "--json", nativeOutput], { env }); |
| 51 | assert.equal(JSON.parse(saved.stdout).status, "ok"); |
| 52 | const selected = await page.evaluate(async () => (await fetch("/api/client/context")).json()); |
| 53 | const selectedOutput = path.join(root, "selected-native.zip"); |
| 54 | const selectedSave = await run(cage, ["diagnostics", "--json", "--connection", selected.connection_id, selectedOutput], { env }); |
| 55 | assert.equal(JSON.parse(selectedSave.stdout).status, "ok"); |
| 56 | |
| 57 | await run(cage, ["prepare-update", "--json"], { env }); |
| 58 | const offline = await run(cage, ["diagnostics", "--json", path.join(root, "offline.zip")], { env }); |
| 59 | assert.equal(JSON.parse(offline.stdout).status, "ok"); |
| 60 | const checks = []; |
| 61 | for (const filename of ["browser.zip", "native.zip", "selected-native.zip", "offline.zip"]) { |
| 62 | const { stdout: contents } = await run("uv", ["run", "--no-project", "python", "-c", "import sys,zipfile,json; z=zipfile.ZipFile(sys.argv[1]); print(json.dumps({'names':z.namelist(),'text':'\\n'.join(z.read(n).decode() for n in z.namelist())}))", path.join(root, filename)]); |
| 63 | const parsed = JSON.parse(contents); |
| 64 | assert.match(parsed.text, /diagnostic-ui-sentinel/); |
| 65 | assert.match(parsed.text, /diagnostic-rejection-sentinel/); |
| 66 | for (const secret of ["ui-private-secret", "rejection-secret", "Test-only-password-8712"]) assert.ok(!parsed.text.includes(secret), `secret in ${filename}`); |
| 67 | assert.ok(parsed.names.every(name => ["manifest.json", "README.txt", "desktop/manifest.json", "desktop/README.txt"].includes(name) || name.startsWith("logs/") || name.startsWith("desktop/logs/"))); |
| 68 | assert.ok(parsed.names.some(name => name.startsWith("desktop/logs/")), `desktop log owner missing from ${filename}`); |
| 69 | checks.push(`${filename}: valid redacted ZIP`); |
| 70 | } |
| 71 | checks.push("real browser settings export", "frontend error and rejection captured", "offline CLI export", "no user files/config/terminal content"); |
| 72 | await fs.writeFile(path.join(reportDirectory, "browser-result.json"), JSON.stringify({ success: true, checks }, null, 2)); |
| 73 | console.log(JSON.stringify({ success: true, reportDirectory, checks })); |
| 74 | } finally { |
| 75 | await browser?.close(); |
| 76 | await run(cage, ["prepare-update", "--json"], { env }).catch(() => {}); |
| 77 | await fs.rm(root, { recursive: true, force: true }); |
| 78 | } |