| 1 | import assert from "node:assert/strict"; |
| 2 | import fs from "node:fs/promises"; |
| 3 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | |
| 5 | const output = process.env.CAGE_UPDATE_QA_OUTPUT || "/tmp/cage-update-diagnosis/ui"; |
| 6 | await fs.mkdir(output, { recursive: true }); |
| 7 | const calls = []; |
| 8 | let update = { phase: "idle", currentVersion: "0.1.3-alpha.1" }; |
| 9 | const fixture = await createWorkbench({ nativeRequest: async body => { |
| 10 | if (body.action !== "appUpdate") return body.action === "workbenchReady" ? true : null; |
| 11 | calls.push(body.operation); |
| 12 | if (body.operation === "check") update = { ...update, phase: "current", message: "已是最新版本" }; |
| 13 | if (body.operation === "install") update = { ...update, phase: "downloading", receivedBytes: 50, totalBytes: 100 }; |
| 14 | return update; |
| 15 | } }); |
| 16 | const { page, browser, errors } = fixture; |
| 17 | const publish = async value => { |
| 18 | update = value; |
| 19 | await page.evaluate(detail => window.dispatchEvent(new CustomEvent("cage-app-update", { detail })), value); |
| 20 | }; |
| 21 | const count = operation => calls.filter(value => value === operation).length; |
| 22 | try { |
| 23 | await page.clock.install(); |
| 24 | calls.length = 0; |
| 25 | update = { phase: "idle", currentVersion: "0.1.3-alpha.1" }; |
| 26 | await page.reload(); |
| 27 | await page.getByRole("heading", { name: "先启动 Cage 环境" }).waitFor(); |
| 28 | await page.exposeFunction("updateCheckCount", () => count("check")); |
| 29 | await page.waitForFunction(async () => await window.updateCheckCount() === 1); |
| 30 | await page.waitForFunction(() => !document.querySelector(".app-update-button")); |
| 31 | assert.deepEqual(calls, ["status", "check"], "startup must check without a user click"); |
| 32 | await page.clock.fastForward(6 * 60 * 60 * 1000 + 60000); |
| 33 | await page.waitForFunction(async () => await window.updateCheckCount() === 2); |
| 34 | await page.waitForFunction(() => !document.querySelector(".app-update-button")); |
| 35 | assert.equal(count("check"), 2, "periodic checks run while the window is visible"); |
| 36 | await publish({ phase: "failed", currentVersion: update.currentVersion, message: "所选代理未运行" }); |
| 37 | await page.locator(".app-update-button").waitFor({ state: "hidden" }); |
| 38 | assert.equal(await page.locator(".app-update-button").count(), 0, "background check failure must not advertise an update"); |
| 39 | await page.clock.fastForward(5 * 60 * 1000 + 60000); |
| 40 | await page.waitForFunction(async () => await window.updateCheckCount() === 3); |
| 41 | assert.equal(count("check"), 3, "a failed background check retries after the proxy can recover"); |
| 42 | await publish({ phase: "available", currentVersion: "0.1.3-alpha.1", latestVersion: "0.1.3-alpha.2" }); |
| 43 | const button = page.getByRole("button", { name: "下载更新", exact: true }); |
| 44 | await button.waitFor(); |
| 45 | assert.match(await button.getAttribute("title"), /0.1.3-alpha.2.*保存工作.*自动停止环境/); |
| 46 | await page.screenshot({ path: `${output}/available.png` }); |
| 47 | await button.click(); |
| 48 | await page.getByRole("button", { name: "正在下载更新 50%", exact: true }).waitFor(); |
| 49 | assert.equal(count("install"), 1); |
| 50 | assert.equal(await page.getByRole("progressbar", { name: "更新下载进度" }).getAttribute("aria-valuenow"), "50"); |
| 51 | assert.equal(await page.locator(".app-update-percent").innerText(), "50%"); |
| 52 | assert.equal(await page.getByRole("dialog", { name: "Cage 更新" }).count(), 0); |
| 53 | assert.ok(await page.getByRole("button", { name: "正在下载更新 50%", exact: true }).isDisabled()); |
| 54 | await page.screenshot({ path: `${output}/downloading.png` }); |
| 55 | await publish({ ...update, receivedBytes: 110 }); |
| 56 | await page.getByRole("button", { name: "正在下载更新 100%", exact: true }).waitFor(); |
| 57 | await publish({ ...update, totalBytes: 0 }); |
| 58 | assert.equal(await page.getByRole("progressbar").getAttribute("aria-valuenow"), null, "unknown totals must stay indeterminate"); |
| 59 | await publish({ ...update, phase: "verifying" }); |
| 60 | await page.getByRole("button", { name: "正在验证更新", exact: true }).waitFor(); |
| 61 | await publish({ ...update, phase: "installing" }); |
| 62 | await page.getByRole("button", { name: "正在安装更新,完成后自动重启", exact: true }).waitFor(); |
| 63 | await publish({ ...update, phase: "failed", message: "下载中断,请检查代理后重试", retryable: true }); |
| 64 | await page.getByRole("alert").filter({ hasText: "下载中断" }).waitFor(); |
| 65 | const retry = page.getByRole("button", { name: "重试更新", exact: true }); |
| 66 | await retry.waitFor(); |
| 67 | await retry.click(); |
| 68 | await page.getByRole("button", { name: "正在下载更新 50%", exact: true }).waitFor(); |
| 69 | assert.equal(count("install"), 2); |
| 70 | await page.getByRole("button", { name: "收起侧栏", exact: true }).click(); |
| 71 | assert.equal(await page.locator(".app-update-button").count(), 1, "collapsed sidebar keeps one progress indicator"); |
| 72 | await publish({ phase: "current", currentVersion: "0.1.3-alpha.2" }); |
| 73 | await page.locator(".app-update-button").waitFor({ state: "hidden" }); |
| 74 | assert.equal(await page.locator(".app-update-button").count(), 0, "a current version leaves no update affordance"); |
| 75 | await publish({ phase: "manual", currentVersion: "0.1.3-alpha.2" }); |
| 76 | await page.locator(".app-update-button").waitFor({ state: "hidden" }); |
| 77 | assert.equal(await page.locator(".app-update-button").count(), 0, "system package updates must not offer an automatic installation"); |
| 78 | assert.deepEqual(errors, []); |
| 79 | const result = { checks: ["automatic startup and periodic checks", "failed background check retry", "latest version hidden", "single click without update dialog", "ring and percentage", "indeterminate progress", "verification and installation", "visible failure and retry", "collapsed sidebar", "Linux manual update remains separate"], calls }; |
| 80 | await fs.writeFile(`${output}/checks.json`, JSON.stringify(result, null, 2)); |
| 81 | console.log(JSON.stringify(result)); |
| 82 | } finally { await browser.close(); } |
| 83 | |
| 84 | const recovered = await createWorkbench({ nativeRequest: async body => { |
| 85 | if (body.action !== "appUpdate") return null; |
| 86 | return body.operation === "status" |
| 87 | ? { phase: "failed", message: "新版本工作台未能就绪,已恢复原应用", retryable: true } |
| 88 | : { phase: "current", currentVersion: "0.1.3-alpha.1" }; |
| 89 | } }); |
| 90 | try { |
| 91 | await recovered.page.getByRole("alert").filter({ hasText: "已恢复原应用" }).waitFor(); |
| 92 | assert.equal(await recovered.page.locator(".app-update-button").count(), 0, "rollback failure stays visible without advertising an unavailable update"); |
| 93 | assert.deepEqual(recovered.errors, []); |
| 94 | console.log("PASS: persisted update failure remains visible while the startup check completes"); |
| 95 | } finally { await recovered.browser.close(); } |