| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | const epoch = new Date("2026-11-07T14:00:00Z"); |
| 5 | for (const platform of ["darwin", "windows", "linux"]) { |
| 6 | const calls = []; |
| 7 | let update = platform === "linux" ? { phase: "manual", message: "使用系统包更新" } : { |
| 8 | phase: "failed", message: "更新源请求受限,请稍后重试", currentVersion: "0.1.0", latestVersion: "0.2.0", |
| 9 | retryable: true, retryAt: new Date(+epoch + 60*60*1000).toISOString(), |
| 10 | }; |
| 11 | const fixture = await createWorkbench({ |
| 12 | clientContext: { mode: "local", connection_id: "local", client_platform: platform, server_platform: platform, connection: { state: "disconnected" } }, |
| 13 | beforeNavigate: page => page.clock.install({ time: epoch }), |
| 14 | nativeRequest: async body => { |
| 15 | if (body.action !== "appUpdate") return null; |
| 16 | calls.push(body.operation); |
| 17 | if (body.operation === "check") update = { phase: "current", currentVersion: "0.1.0", retryAt: "" }; |
| 18 | return update; |
| 19 | }, |
| 20 | }); |
| 21 | const { page, browser, errors } = fixture; |
| 22 | try { |
| 23 | await page.waitForFunction(() => document.querySelector(".app-update-button") || !document.querySelector(".loading-screen")); |
| 24 | if (platform === "linux") { |
| 25 | await page.clock.fastForward(7*60*60*1000); |
| 26 | assert.deepEqual(calls, ["status"], "Linux must keep system package updates"); |
| 27 | } else { |
| 28 | await page.getByRole("button", { name: "等待重试更新", exact: true }).waitFor(); |
| 29 | assert.ok(await page.getByRole("button", { name: "等待重试更新", exact: true }).isDisabled()); |
| 30 | assert.deepEqual(calls, ["status"], "startup must respect the native retry deadline"); |
| 31 | await page.clock.fastForward(6*60*1000); |
| 32 | await page.evaluate(() => window.dispatchEvent(new Event("focus"))); |
| 33 | assert.deepEqual(calls, ["status"], "the five-minute timer and focus must not bypass cooldown"); |
| 34 | await page.clock.fastForward(53*60*1000); |
| 35 | assert.deepEqual(calls, ["status"], "must not retry before the server deadline"); |
| 36 | await page.clock.fastForward(2*60*1000); |
| 37 | await page.waitForFunction(() => !document.querySelector(".app-update-button")); |
| 38 | assert.deepEqual(calls, ["status", "check"], "retry once when the server deadline passes"); |
| 39 | } |
| 40 | assert.deepEqual(errors, []); |
| 41 | console.log(`PASS: ${platform} update cooldown`); |
| 42 | } finally { await browser.close(); } |
| 43 | } |