diff --git a/.github/workflows/fork-windows-update-test.yml b/.github/workflows/fork-windows-update-test.yml deleted file mode 100644 index 8790ba0519594..0000000000000 --- a/.github/workflows/fork-windows-update-test.yml +++ /dev/null @@ -1,112 +0,0 @@ -# Fork-only: install a Chinese Windows baseline build on a clean Windows runner, then -# drive its real updater against a mock feed carrying a newer Actions artifact. Covers -# discovery, download, the in-app install, the installer's restart, the new version and -# the seeded state the user still sees. It publishes nothing. -# -# gh workflow run fork-windows-update-test.yml -f baseline_run_id= -f baseline_version=0.1.7-cn.2 -f run_id= -f new_version=0.1.7-cn.3 -# -# The new artifact must be a higher Chinese version than the baseline (fork-windows-desktop.yml -# uploads whatever version the build was given), and must contain its `latest.yml` and -# `T3-Code--x64.exe` together. -name: Fork Windows update test - -on: - workflow_dispatch: - inputs: - run_id: - description: "Run id of the workflow_dispatch build that produced the new installer artifact." - type: string - required: true - baseline_run_id: - description: "Run id of the baseline installer build." - type: string - required: true - new_version: - description: "Candidate version, e.g. 0.1.7-cn.3; must be greater than baseline_version." - type: string - required: true - artifact_name: - description: "Artifact name from that build." - type: string - default: t3code-cn-windows-x64 - baseline_version: - description: "Version the baseline installer reports, e.g. 0.1.7-cn.2." - type: string - required: true - -permissions: - actions: read - contents: read - -jobs: - update: - name: Update ${{ inputs.baseline_version }} to ${{ inputs.new_version }} - if: github.repository == 'deepfry666/t3code-cn' - runs-on: windows-2025 - timeout-minutes: 60 - steps: - - name: Checkout - uses: actions/checkout@v6 - with: - sparse-checkout: | - /* - !/.repos/ - sparse-checkout-cone-mode: false - - # setup-vp's own cache restores a Linux-shaped store on Windows (see #7975). - - name: Setup Vite+ - uses: voidzero-dev/setup-vp@v1 - with: - node-version-file: package.json - cache: false - run-install: false - - - name: Install - run: vp install - - - name: Download the baseline installer artifact - uses: actions/download-artifact@v7 - with: - name: ${{ inputs.artifact_name }} - path: ${{ runner.temp }}/baseline - run-id: ${{ inputs.baseline_run_id }} - github-token: ${{ github.token }} - repository: ${{ github.repository }} - - - name: Download the new installer artifact - uses: actions/download-artifact@v7 - with: - name: ${{ inputs.artifact_name }} - path: ${{ runner.temp }}/new-artifact - run-id: ${{ inputs.run_id }} - github-token: ${{ github.token }} - repository: ${{ github.repository }} - - - name: List the new artifact - shell: pwsh - run: | - Get-ChildItem -Recurse -File (Join-Path $env:RUNNER_TEMP "new-artifact") | - Select-Object FullName, Length | Format-Table -AutoSize | Out-String | Write-Host - - - name: Run the update acceptance - shell: pwsh - run: | - $arguments = @( - "scripts/windows-update-acceptance.ts" - "--baseline-installer", (Join-Path $env:RUNNER_TEMP "baseline/T3-Code-${{ inputs.baseline_version }}-x64.exe") - "--new-artifact-dir", (Join-Path $env:RUNNER_TEMP "new-artifact") - "--baseline-version", "${{ inputs.baseline_version }}" - "--new-version", "${{ inputs.new_version }}" - "--artifacts-dir", (Join-Path $env:RUNNER_TEMP "windows-update-acceptance") - ) - node @arguments - if ($LASTEXITCODE -ne 0) { throw "Windows update acceptance failed with exit $LASTEXITCODE." } - - - name: Upload acceptance evidence - if: always() - uses: actions/upload-artifact@v7 - with: - name: windows-update-acceptance - path: ${{ runner.temp }}/windows-update-acceptance - if-no-files-found: warn - retention-days: 14 diff --git a/knip.jsonc b/knip.jsonc index f28cfba216224..fff70ae782731 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -37,7 +37,6 @@ "prove-scenario.ts", "scenarios/*.ts", "setup-worktree.ts", - "fork-macos-update-acceptance.ts", "fork-sign-macos.ts", "sign-macos.ts", "prepare-server-compile-cache.cjs", diff --git a/scripts/fork-macos-update-acceptance.ts b/scripts/fork-macos-update-acceptance.ts deleted file mode 100644 index 07e971255105d..0000000000000 --- a/scripts/fork-macos-update-acceptance.ts +++ /dev/null @@ -1,553 +0,0 @@ -/* oxlint-disable t3code/no-global-process-runtime -- Standalone macOS acceptance has no Effect runtime and must inspect the host. */ -// @effect-diagnostics nodeBuiltinImport:off globalDate:off globalConsole:off globalFetch:off - Host-side macOS automation reads the clock and process table directly. -// Fork-only acceptance for the Chinese macOS update path. It installs a baseline dmg into a -// temporary folder, serves a newer build from a mock feed, and drives the real updater through -// Squirrel.Mac's signature check, install and relaunch, then checks version, data and UI. -// -// node scripts/fork-macos-update-acceptance.ts --baseline-dmg --new-dir release/macos- \ -// --baseline-version 0.1.7-cn.3 --new-version 0.1.7-cn.4 -// -// Squirrel relaunches the app through LaunchServices, which drops this script's environment. -// Both test copies get LSEnvironment (re-signed with the same identity) so the relaunch keeps -// the temporary T3CODE_HOME, but LaunchServices forces the real HOME, so Electron's userData is -// the live app's. The script therefore runs only while the live app holds that profile's -// single-instance lock: the relaunch then hands off to it and quits before touching anything. -import * as NodeChildProcess from "node:child_process"; -import * as NodeCrypto from "node:crypto"; -import * as NodeFS from "node:fs"; -import * as NodeOS from "node:os"; -import * as NodePath from "node:path"; -import * as NodeTimersPromises from "node:timers/promises"; -import * as NodeUtil from "node:util"; -import { _electron, type ElectronApplication, type Page } from "playwright-core"; -import { compareSemverVersions } from "@t3tools/shared/semver"; - -import { serializeUpdateManifest } from "./lib/update-manifest.ts"; - -const FIXTURE_DB = NodePath.join(import.meta.dirname, "fixtures/desktop-launch/statev2.sqlite"); -const THREAD_TITLE = "Release launch check"; -const APP_NAME = "T3 Code (Beta).app"; -const EXECUTABLE = "Contents/MacOS/T3 Code (Beta)"; -const LAUNCH_TIMEOUT_MS = 90_000; -const DOWNLOAD_TIMEOUT_MS = 5 * 60_000; -const INSTALL_TIMEOUT_MS = 5 * 60_000; -const CJK = /[一-鿿]/; -// Packaged builds pin navigator.languages to en-US, so pick Chinese the way Settings stores it. -const LOCALE_STORAGE_KEY = "t3code-cn:locale"; - -interface UpdateState { - readonly enabled: boolean; - readonly status: string; - readonly channel: string; - readonly currentVersion: string; - readonly availableVersion: string | null; - readonly downloadedVersion: string | null; - readonly notAppliedVersion?: string | undefined; - readonly message: string | null; -} - -interface UpdateActionResult { - readonly accepted: boolean; - readonly completed: boolean; - readonly state: UpdateState; -} - -interface DesktopBridgeHandle { - readonly getUpdateState: () => Promise; - readonly checkForUpdate: () => Promise<{ - readonly checked: boolean; - readonly state: UpdateState; - }>; - readonly downloadUpdate: () => Promise; - readonly installUpdate: (expectedVersion?: string) => Promise; -} - -const { values } = NodeUtil.parseArgs({ - options: { - "baseline-dmg": { type: "string" }, - "new-dir": { type: "string" }, - "baseline-version": { type: "string" }, - "new-version": { type: "string" }, - "artifacts-dir": { type: "string" }, - "mock-server-port": { type: "string", default: "3399" }, - "signing-dir": { - type: "string", - default: NodePath.join(NodeOS.homedir(), ".t3code-cn-signing"), - }, - }, -}); -const baselineDmg = values["baseline-dmg"]; -const newDir = values["new-dir"]; -const baselineVersion = values["baseline-version"]; -const newVersion = values["new-version"]; -if (!baselineDmg || !newDir || !baselineVersion || !newVersion) { - throw new Error( - "Usage: fork-macos-update-acceptance.ts --baseline-dmg --new-dir --baseline-version --new-version [--artifacts-dir ]", - ); -} -if (compareSemverVersions(newVersion, baselineVersion) <= 0) { - throw new Error(`The new version ${newVersion} must be greater than ${baselineVersion}.`); -} -const port = Number(values["mock-server-port"]); -const signingDir = values["signing-dir"]!; -const root = NodeFS.realpathSync( - NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-macos-update-acceptance-")), -); -const home = NodePath.join(root, "home"); -const t3Home = NodePath.join(home, ".t3"); -const installDir = NodePath.join(root, "Applications"); -const appPath = NodePath.join(installDir, APP_NAME); -const executable = NodePath.join(appPath, EXECUTABLE); -const feedDir = NodePath.join(root, "update-feed"); -const artifactsDir = NodePath.resolve( - values["artifacts-dir"] ?? NodePath.join("release", "macos-update-acceptance"), -); -const realHome = NodeOS.userInfo().homedir; -const liveProfile = NodePath.join(realHome, "Library/Application Support/t3code-v2"); -const shipItDir = NodePath.join(realHome, "Library/Caches/com.t3tools.t3code.ShipIt"); -const shipItFiles = ["ShipItState.plist", "ShipIt_stderr.log", "ShipIt_stdout.log"]; -const lsregister = - "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister"; -const eventsPath = NodePath.join(artifactsDir, "events.ndjson"); - -const run = (command: string, args: ReadonlyArray): string => - NodeChildProcess.execFileSync(command, args, { encoding: "utf8" }).trim(); - -const record = (step: string, detail?: unknown): void => { - console.log( - `[macos-update-acceptance] ${step}${detail === undefined ? "" : ` ${JSON.stringify(detail)}`}`, - ); - NodeFS.appendFileSync( - eventsPath, - `${JSON.stringify({ at: new Date().toISOString(), step, detail })}\n`, - ); -}; - -const writeJson = (name: string, value: unknown): void => - NodeFS.writeFileSync(NodePath.join(artifactsDir, name), `${JSON.stringify(value, null, 2)}\n`); - -async function waitFor( - label: string, - read: () => T | Promise, - done: (value: T) => boolean, - timeoutMs: number, - intervalMs = 1_000, -): Promise { - const deadline = Date.now() + timeoutMs; - let last: T | undefined; - while (Date.now() < deadline) { - last = await read(); - if (done(last)) return last; - await NodeTimersPromises.setTimeout(intervalMs); - } - throw new Error(`Timed out waiting for ${label}. Last value: ${JSON.stringify(last)}`); -} - -const launchServicesEnvironment = { - T3CODE_HOME: t3Home, - T3CODE_TELEMETRY_ENABLED: "false", - T3CODE_DESKTOP_MOCK_UPDATES: "true", - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT: String(port), -}; -const appEnvironment = { ...launchServicesEnvironment, HOME: home }; - -function signingIdentity(): { identity: string; keychain: string } { - const keychain = NodePath.join(signingDir, "signing.keychain-db"); - const passphrase = NodeFS.readFileSync(NodePath.join(signingDir, "passphrase"), "utf8").trim(); - run("security", ["unlock-keychain", "-p", passphrase, keychain]); - const identity = /^\s*1\) ([0-9A-F]{40}) /m.exec( - run("security", ["find-identity", "-p", "codesigning", keychain]), - )?.[1]; - if (!identity) throw new Error(`No code signing identity in ${keychain}.`); - return { identity, keychain }; -} - -function isolateAndResign(app: string, signing: { identity: string; keychain: string }): void { - const plist = NodePath.join(app, "Contents/Info.plist"); - run("plutil", [ - "-replace", - "LSEnvironment", - "-json", - JSON.stringify(launchServicesEnvironment), - plist, - ]); - run("codesign", [ - "--force", - "--sign", - signing.identity, - "--keychain", - signing.keychain, - "--timestamp=none", - "--preserve-metadata=identifier,entitlements,requirements,flags", - app, - ]); - run("codesign", ["--verify", "--deep", "--strict", app]); -} - -const bundleVersion = (app: string): string => - run("/usr/libexec/PlistBuddy", [ - "-c", - "Print :CFBundleShortVersionString", - NodePath.join(app, "Contents/Info.plist"), - ]); - -const designatedRequirement = (app: string): string => { - const result = NodeChildProcess.spawnSync("codesign", ["-d", "-r-", app], { encoding: "utf8" }); - const line = `${result.stdout}\n${result.stderr}` - .split("\n") - .find((entry) => entry.startsWith("designated => ")); - return line?.slice("designated => ".length) ?? ""; -}; - -// Only processes whose executable lives under this run's private mkdtemp folder, which no -// other app or dev server can share. LaunchServices reports /var rather than /private/var. -function processesUnderRoot(): ReadonlyArray<{ pid: number; command: string }> { - return run("ps", ["-axo", "pid=,comm="]) - .split("\n") - .map((line) => /^\s*(\d+)\s+(.*)$/.exec(line)) - .filter((match): match is RegExpExecArray => match !== null) - .map((match) => ({ pid: Number(match[1]), command: match[2]! })) - .filter((entry) => entry.command.replace(/^\/var\//, "/private/var/").startsWith(`${root}/`)); -} - -async function stopProcessesUnderRoot(label: string): Promise { - const processes = processesUnderRoot(); - if (processes.length === 0) return; - record(`stopping ${label}`, processes); - for (const entry of processes) { - try { - process.kill(entry.pid, "SIGTERM"); - } catch {} - } - await waitFor(`${label} to exit`, processesUnderRoot, (left) => left.length === 0, 30_000); -} - -async function launchApp(): Promise<{ app: ElectronApplication; page: Page }> { - const app = await _electron.launch({ - executablePath: executable, - args: ["--use-mock-keychain"], - env: { ...process.env, ...appEnvironment }, - timeout: LAUNCH_TIMEOUT_MS, - }); - const page = await app.firstWindow({ timeout: LAUNCH_TIMEOUT_MS }); - await page.waitForFunction( - () => Boolean((globalThis as unknown as { desktopBridge?: unknown }).desktopBridge), - undefined, - { timeout: LAUNCH_TIMEOUT_MS }, - ); - return { app, page }; -} - -const readState = (page: Page) => - page.evaluate(() => - ( - globalThis as unknown as { desktopBridge: DesktopBridgeHandle } - ).desktopBridge.getUpdateState(), - ); - -async function checkDataAndLanguage(page: Page, label: string): Promise { - await page - .locator("li[data-thread-item]", { hasText: THREAD_TITLE }) - .waitFor({ timeout: LAUNCH_TIMEOUT_MS }); - const bodyText = await page.locator("body").innerText(); - if (!CJK.test(bodyText)) throw new Error(`The ${label} app does not show a Chinese interface.`); - await page.screenshot({ path: NodePath.join(artifactsDir, `${label}.png`), timeout: 10_000 }); - record(`${label} app shows the seeded thread in Chinese`); -} - -function liveProfileLockHolder(): number | undefined { - try { - const pid = Number( - /-(\d+)$/.exec(NodeFS.readlinkSync(NodePath.join(liveProfile, "SingletonLock")))?.[1], - ); - process.kill(pid, 0); - return pid; - } catch { - return undefined; - } -} - -const main = async (): Promise => { - if (process.platform !== "darwin" || process.arch !== "arm64") { - throw new Error("Run this acceptance on an Apple silicon Mac."); - } - const lockHolder = liveProfileLockHolder(); - if (lockHolder === undefined) { - throw new Error( - `Open the installed T3 Code first: Squirrel's relaunch uses ${liveProfile}, and only the ` + - "running app's single-instance lock makes that relaunch quit without touching it.", - ); - } - NodeFS.rmSync(artifactsDir, { recursive: true, force: true }); - NodeFS.mkdirSync(artifactsDir, { recursive: true }); - const shipItBackup = NodePath.join(root, "shipit-backup"); - NodeFS.mkdirSync(shipItBackup); - for (const file of shipItFiles) { - const source = NodePath.join(shipItDir, file); - if (NodeFS.existsSync(source)) NodeFS.copyFileSync(source, NodePath.join(shipItBackup, file)); - } - let app: ElectronApplication | undefined; - let page: Page | undefined; - let mockServer: NodeChildProcess.ChildProcess | undefined; - const mockLog: string[] = []; - let mountPoint: string | undefined; - try { - record("inputs", { - baselineDmg, - newDir, - baselineVersion, - newVersion, - root, - artifactsDir, - liveProfileLockHolder: lockHolder, - }); - const signing = signingIdentity(); - - NodeFS.mkdirSync(NodePath.join(t3Home, "userdata"), { recursive: true }); - NodeFS.copyFileSync(FIXTURE_DB, NodePath.join(t3Home, "userdata/statev2.sqlite")); - - mountPoint = NodePath.join(root, "dmg"); - run("hdiutil", ["attach", "-nobrowse", "-readonly", "-mountpoint", mountPoint, baselineDmg]); - NodeFS.mkdirSync(installDir, { recursive: true }); - run("ditto", [NodePath.join(mountPoint, APP_NAME), appPath]); - run("hdiutil", ["detach", mountPoint]); - mountPoint = undefined; - const releaseRequirement = designatedRequirement(appPath); - if (bundleVersion(appPath) !== baselineVersion) { - throw new Error(`The baseline dmg holds ${bundleVersion(appPath)}, not ${baselineVersion}.`); - } - isolateAndResign(appPath, signing); - if (designatedRequirement(appPath) !== releaseRequirement) { - throw new Error("Re-signing the test copy changed its designated requirement."); - } - record("baseline installed from the dmg", { appPath, releaseRequirement }); - - const newZip = NodePath.join(newDir, `T3-Code-${newVersion}-arm64.zip`); - const extractDir = NodePath.join(root, "new-app"); - run("ditto", ["-x", "-k", newZip, extractDir]); - const newApp = NodePath.join(extractDir, APP_NAME); - if (designatedRequirement(newApp) !== releaseRequirement) { - throw new Error("The new release is not signed with the baseline's identity."); - } - isolateAndResign(newApp, signing); - NodeFS.mkdirSync(feedDir, { recursive: true }); - const feedZipName = NodePath.basename(newZip); - const feedZip = NodePath.join(feedDir, feedZipName); - run("ditto", ["-c", "-k", "--sequesterRsrc", "--keepParent", newApp, feedZip]); - const sha512 = NodeCrypto.createHash("sha512") - .update(NodeFS.readFileSync(feedZip)) - .digest("base64"); - NodeFS.writeFileSync( - NodePath.join(feedDir, "latest-mac.yml"), - serializeUpdateManifest( - { - version: newVersion, - releaseDate: new Date().toISOString(), - files: [{ url: feedZipName, sha512, size: NodeFS.statSync(feedZip).size }], - extras: { path: feedZipName, sha512 }, - }, - { platformLabel: "macOS" }, - ), - ); - record("mock feed prepared", { feedZipName }); - - mockServer = NodeChildProcess.spawn( - process.execPath, - [NodePath.join(import.meta.dirname, "mock-update-server.ts")], - { - env: { - ...process.env, - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT: String(port), - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_ROOT: feedDir, - }, - stdio: ["ignore", "pipe", "pipe"], - }, - ); - mockServer.stdout?.on("data", (chunk: Buffer) => mockLog.push(chunk.toString())); - mockServer.stderr?.on("data", (chunk: Buffer) => mockLog.push(chunk.toString())); - await waitFor( - "the mock feed", - () => - fetch(`http://localhost:${port}/latest-mac.yml`).then( - (response) => response.status, - () => 0, - ), - (status) => status === 200, - 30_000, - ); - - ({ app, page } = await launchApp()); - const baselinePage = page; - if ( - (await app.evaluate(({ app: electronApp }) => electronApp.getVersion())) !== baselineVersion - ) { - throw new Error("The baseline app reports the wrong version."); - } - await baselinePage.evaluate((key) => localStorage.setItem(key, "zh-CN"), LOCALE_STORAGE_KEY); - await baselinePage.reload(); - await checkDataAndLanguage(baselinePage, "01-baseline"); - - const ready = await waitFor( - "the updater to settle", - () => readState(baselinePage), - (state) => state.enabled || state.status === "disabled", - 60_000, - ); - writeJson("01-update-state.json", ready); - if (!ready.enabled) throw new Error(`Updates are disabled: ${ready.message ?? ready.status}`); - - await baselinePage.evaluate(() => - ( - globalThis as unknown as { desktopBridge: DesktopBridgeHandle } - ).desktopBridge.checkForUpdate(), - ); - const available = await waitFor( - `version ${newVersion} to be offered`, - () => readState(baselinePage), - (state) => state.status === "available" && state.availableVersion === newVersion, - 120_000, - ); - writeJson("02-update-available.json", available); - record("update discovered"); - - const download = await baselinePage.evaluate(() => - ( - globalThis as unknown as { desktopBridge: DesktopBridgeHandle } - ).desktopBridge.downloadUpdate(), - ); - if (!download.accepted) throw new Error(`Download refused: ${JSON.stringify(download)}`); - const downloaded = await waitFor( - `version ${newVersion} to download`, - () => readState(baselinePage), - (state) => - (state.status === "downloaded" && state.downloadedVersion === newVersion) || - state.status === "error", - DOWNLOAD_TIMEOUT_MS, - ); - writeJson("03-update-downloaded.json", downloaded); - if (downloaded.status !== "downloaded") { - throw new Error(`The download failed: ${downloaded.message}`); - } - await baselinePage.screenshot({ - path: NodePath.join(artifactsDir, "02-downloaded.png"), - timeout: 10_000, - }); - record("update downloaded and handed to Squirrel.Mac"); - - const child = app.process(); - const install = baselinePage - .evaluate( - (version) => - ( - globalThis as unknown as { desktopBridge: DesktopBridgeHandle } - ).desktopBridge.installUpdate(version), - newVersion, - ) - .catch((error: unknown) => ({ - quitting: error instanceof Error ? error.message : String(error), - })); - await waitFor( - "the baseline app to quit for the install", - () => child.exitCode !== null || child.signalCode !== null, - Boolean, - INSTALL_TIMEOUT_MS, - 500, - ); - record("baseline quit for the install", await install); - app = undefined; - page = undefined; - - await waitFor( - `the bundle to report ${newVersion}`, - () => bundleVersion(appPath), - (version) => version === newVersion, - INSTALL_TIMEOUT_MS, - 2_000, - ); - const quarantine = NodeChildProcess.spawnSync("xattr", [ - "-p", - "com.apple.quarantine", - appPath, - ]).status; - writeJson("04-installed.json", { - version: bundleVersion(appPath), - designatedRequirement: designatedRequirement(appPath), - quarantined: quarantine === 0, - }); - if (quarantine === 0) throw new Error("The installed update carries a quarantine flag."); - run("codesign", ["--verify", "--deep", "--strict", appPath]); - record("Squirrel.Mac installed the update without quarantine"); - - const launchedLine = `Successfully launched application at ${new URL(`file://${appPath.replace(/^\/private\/var\//, "/var/")}/`).href}`; - const shipItLog = await waitFor( - "Squirrel.Mac to relaunch the app", - () => NodeFS.readFileSync(NodePath.join(shipItDir, "ShipIt_stderr.log"), "utf8"), - (log) => log.includes(launchedLine), - 120_000, - 2_000, - ); - NodeFS.writeFileSync( - NodePath.join(artifactsDir, "shipit.log"), - shipItLog.slice(shipItLog.lastIndexOf("Detected this as an install request")), - ); - await waitFor( - "the relaunched app to hand off to the live app and quit", - processesUnderRoot, - (processes) => processes.length === 0, - 60_000, - ); - record("Squirrel.Mac relaunched the updated app, which handed off to the live app"); - - ({ app, page } = await launchApp()); - const updatedPage = page; - const updatedVersion = await app.evaluate(({ app: electronApp }) => electronApp.getVersion()); - if (updatedVersion !== newVersion) { - throw new Error(`The updated app reports ${updatedVersion}, expected ${newVersion}.`); - } - await checkDataAndLanguage(updatedPage, "03-updated"); - const updatedState = await waitFor( - "the updated app's updater", - () => readState(updatedPage), - (state) => state.enabled || state.status === "disabled", - 60_000, - ); - writeJson("05-updated-state.json", updatedState); - if (updatedState.currentVersion !== newVersion || updatedState.notAppliedVersion) { - throw new Error(`The updated app's updater disagrees: ${JSON.stringify(updatedState)}`); - } - record("acceptance passed", { baselineVersion, updatedVersion }); - } catch (error) { - record("acceptance failed", { - message: error instanceof Error ? error.message : String(error), - stack: error instanceof Error ? error.stack : undefined, - }); - await page - ?.screenshot({ path: NodePath.join(artifactsDir, "failure.png"), timeout: 10_000 }) - .catch(() => {}); - process.exitCode = 1; - } finally { - await Promise.race([app?.close().catch(() => {}), NodeTimersPromises.setTimeout(10_000)]); - await stopProcessesUnderRoot("leftover test processes").catch((error: unknown) => - record("could not stop leftover processes", String(error)), - ); - if (mountPoint) NodeChildProcess.spawnSync("hdiutil", ["detach", "-force", mountPoint]); - mockServer?.kill(); - for (const file of shipItFiles) { - const backup = NodePath.join(root, "shipit-backup", file); - if (NodeFS.existsSync(backup)) NodeFS.copyFileSync(backup, NodePath.join(shipItDir, file)); - } - for (const bundle of [appPath, NodePath.join(root, "new-app", APP_NAME)]) { - NodeChildProcess.spawnSync(lsregister, ["-u", bundle]); - } - NodeFS.writeFileSync(NodePath.join(artifactsDir, "mock-update-server.log"), mockLog.join("")); - for (const logs of [NodePath.join(t3Home, "userdata/logs"), NodePath.join(t3Home, "logs")]) { - if (NodeFS.existsSync(logs)) { - NodeFS.cpSync(logs, NodePath.join(artifactsDir, "t3-logs"), { recursive: true }); - } - } - if (processesUnderRoot().length === 0) NodeFS.rmSync(root, { recursive: true, force: true }); - console.log(`[macos-update-acceptance] evidence in ${artifactsDir}`); - } -}; - -await main(); diff --git a/scripts/windows-update-acceptance.test.ts b/scripts/windows-update-acceptance.test.ts deleted file mode 100644 index f30bfe4d389bc..0000000000000 --- a/scripts/windows-update-acceptance.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -// @effect-diagnostics nodeBuiltinImport:off - builds a disposable artifact directory on disk. -import * as NodeFS from "node:fs"; -import * as NodeOS from "node:os"; -import * as NodePath from "node:path"; -import { expect, it } from "vite-plus/test"; - -import { - assertAcceptanceVersions, - findDesktopArtifact, - parseDesktopArtifactVersion, - renderLatestYml, - validateLatestYml, -} from "./windows-update-acceptance.ts"; - -it("accepts only a strictly newer stable artifact version", () => { - expect(() => assertAcceptanceVersions("0.1.7", "0.1.8")).not.toThrow(); - expect(() => assertAcceptanceVersions("0.1.7", "0.1.7")).toThrow(/greater than the baseline/); - expect(() => assertAcceptanceVersions("0.1.7", "0.1.6")).toThrow(/greater than the baseline/); - expect(() => assertAcceptanceVersions("0.1.7", "0.1.8-nightly.20261108.1")).toThrow( - /not a supported version/, - ); - expect(() => assertAcceptanceVersions("0.1.7-cn.2", "0.1.7-cn.3")).not.toThrow(); - expect(() => assertAcceptanceVersions("0.1.7", "0.1.7-cn.3")).toThrow( - /greater than the baseline/, - ); -}); - -it("reads the version out of the electron-builder installer name", () => { - expect(parseDesktopArtifactVersion("T3-Code-0.1.8-x64.exe")).toBe("0.1.8"); - expect(parseDesktopArtifactVersion("T3-Code-1.2.3-x64.exe.blockmap")).toBeNull(); - expect(parseDesktopArtifactVersion("elevate.exe")).toBeNull(); -}); - -it("finds the one installer for the requested version", () => { - const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-windows-artifact-")); - try { - NodeFS.writeFileSync(NodePath.join(directory, "T3-Code-0.1.8-x64.exe"), ""); - const found = findDesktopArtifact(directory, "0.1.8"); - expect(NodePath.basename(found)).toBe("T3-Code-0.1.8-x64.exe"); - expect(() => findDesktopArtifact(directory, "0.1.9")).toThrow( - /do not report the requested new version 0\.1\.9/, - ); - } finally { - NodeFS.rmSync(directory, { recursive: true, force: true }); - } -}); - -it("renders the electron-updater feed the mock server serves", () => { - const latest = renderLatestYml({ - version: "0.1.8", - fileName: "T3-Code-0.1.8-x64.exe", - sha512: "digest", - size: 42, - releaseDate: "2026-11-09T00:00:00.000Z", - }); - expect(latest).toContain("version: '0.1.8'"); - expect(latest).toContain(" - url: T3-Code-0.1.8-x64.exe"); - expect(latest).toContain(" sha512: digest"); - expect(latest).toContain(" size: 42"); - expect(latest).toContain("path: 'T3-Code-0.1.8-x64.exe'"); - expect(latest).toContain("releaseDate: '2026-11-09T00:00:00.000Z'"); - expect(() => - validateLatestYml(latest, "0.1.8", "T3-Code-0.1.8-x64.exe", "digest", 42), - ).not.toThrow(); - expect(() => validateLatestYml(latest, "0.1.9", "T3-Code-0.1.8-x64.exe", "digest", 42)).toThrow(); - expect(() => validateLatestYml(latest, "0.1.8", "T3-Code-0.1.8-x64.exe", "wrong", 42)).toThrow(); -}); diff --git a/scripts/windows-update-acceptance.ts b/scripts/windows-update-acceptance.ts deleted file mode 100644 index e3bf1b64c404e..0000000000000 --- a/scripts/windows-update-acceptance.ts +++ /dev/null @@ -1,872 +0,0 @@ -/* oxlint-disable t3code/no-global-process-runtime -- Standalone Windows acceptance has no Effect runtime and must inspect the host. */ -// @effect-diagnostics nodeBuiltinImport:off globalDate:off globalConsole:off - Host-side Windows automation reads the clock and process table directly. -// Windows-only acceptance for the installed-app update handoff. It installs the last -// published build, points the app's mock update feed at a newer Actions artifact, then -// checks the app finds, downloads, installs and restarts on it without losing state. -// See .github/workflows/fork-windows-update-test.yml for the runner and inputs. -import * as NodeChildProcess from "node:child_process"; -import * as NodeCrypto from "node:crypto"; -import * as NodeFS from "node:fs"; -import * as NodeHttp from "node:http"; -import * as NodeOS from "node:os"; -import * as NodePath from "node:path"; -import * as NodeTimersPromises from "node:timers/promises"; -import * as NodeUtil from "node:util"; -import { extractFile, uncache } from "@electron/asar"; -import { _electron, type ElectronApplication, type Page } from "playwright-core"; -import { compareSemverVersions } from "@t3tools/shared/semver"; - -import { parseUpdateManifest, serializeUpdateManifest } from "./lib/update-manifest.ts"; - -const FIXTURE_DB = NodePath.join(import.meta.dirname, "fixtures/desktop-launch/statev2.sqlite"); -const THREAD_TITLE = "Release launch check"; -const THREAD_MESSAGE = "The release opens and shows this thread."; -const LAUNCH_TIMEOUT_MS = 90_000; -const DOWNLOAD_TIMEOUT_MS = 5 * 60_000; -const INSTALL_APPLY_TIMEOUT_MS = 10 * 60_000; -const RELAUNCH_TIMEOUT_MS = 3 * 60_000; -const WINDOW_TIMEOUT_MS = 2 * 60_000; - -interface AcceptanceOptions { - readonly baselineInstaller: string; - readonly newArtifactDir: string; - readonly baselineVersion: string; - readonly newVersion: string; - readonly installDir: string; - readonly artifactsDir: string; - readonly mockServerPort: number; -} - -interface UpdateState { - readonly enabled: boolean; - readonly status: string; - readonly channel: string; - readonly currentVersion: string; - readonly availableVersion: string | null; - readonly downloadedVersion: string | null; - readonly notAppliedVersion?: string | undefined; - readonly message: string | null; -} - -interface UpdateActionResult { - readonly accepted: boolean; - readonly completed: boolean; - readonly state: UpdateState; -} - -interface UpdateCheckResult { - readonly checked: boolean; - readonly state: UpdateState; -} - -interface DesktopBridgeHandle { - readonly getUpdateState: () => Promise; - readonly checkForUpdate: () => Promise; - readonly downloadUpdate: () => Promise; - readonly installUpdate: (expectedVersion?: string) => Promise; -} - -interface AppProcess { - readonly pid: number; - readonly window: number; -} - -function parseStableVersion(version: string, label: string): void { - if (!/^\d+\.\d+\.\d+(?:-cn\.\d+)?$/.test(version)) { - throw new Error(`${label} '${version}' is not a supported version.`); - } -} - -export function compareStableVersions(a: string, b: string): number { - parseStableVersion(a, "Version"); - parseStableVersion(b, "Version"); - return compareSemverVersions(a, b); -} - -export function assertAcceptanceVersions(baselineVersion: string, newVersion: string): void { - parseStableVersion(baselineVersion, "Baseline version"); - parseStableVersion(newVersion, "New version"); - if (compareStableVersions(newVersion, baselineVersion) <= 0) { - throw new Error( - `The new artifact version '${newVersion}' must be greater than the baseline '${baselineVersion}'. ` + - "Build the artifact with a higher T3CODE_DESKTOP_VERSION; electron-updater ignores an equal version.", - ); - } -} - -export function parseDesktopArtifactVersion(fileName: string): string | null { - return /^T3-Code-(.+)-x64\.exe$/i.exec(fileName)?.[1] ?? null; -} - -function listFilesRecursive(directory: string): ReadonlyArray { - const files: string[] = []; - const walk = (current: string): void => { - for (const entry of NodeFS.readdirSync(current, { withFileTypes: true })) { - const fullPath = NodePath.join(current, entry.name); - if (entry.isDirectory()) walk(fullPath); - else if (entry.isFile()) files.push(fullPath); - } - }; - walk(directory); - return files; -} - -export function findDesktopArtifact(artifactDir: string, expectedVersion: string): string { - if (!NodeFS.existsSync(artifactDir) || !NodeFS.statSync(artifactDir).isDirectory()) { - throw new Error(`The new artifact directory does not exist: ${artifactDir}`); - } - const files = listFilesRecursive(artifactDir); - const candidates = files - .map((file) => ({ file, version: parseDesktopArtifactVersion(NodePath.basename(file)) })) - .filter( - (candidate): candidate is { file: string; version: string } => candidate.version !== null, - ); - if (candidates.length === 0) { - throw new Error( - `No 'T3-Code--x64.exe' installer in ${artifactDir}. Found: ${files.map((file) => NodePath.basename(file)).join(", ") || "(empty)"}`, - ); - } - const matches = candidates.filter((candidate) => candidate.version === expectedVersion); - if (matches.length === 1) return matches[0]!.file; - throw new Error( - matches.length > 1 - ? `More than one installer reports version ${expectedVersion}: ${matches.map((match) => match.file).join(", ")}` - : `The artifact's installers (${candidates.map((candidate) => candidate.version).join(", ")}) do not report the requested new version ${expectedVersion}.`, - ); -} - -export function renderLatestYml(input: { - readonly version: string; - readonly fileName: string; - readonly sha512: string; - readonly size: number; - readonly releaseDate: string; -}): string { - return serializeUpdateManifest( - { - version: input.version, - releaseDate: input.releaseDate, - files: [{ url: input.fileName, sha512: input.sha512, size: input.size }], - extras: { path: input.fileName, sha512: input.sha512 }, - }, - { platformLabel: "Windows" }, - ); -} - -export function validateLatestYml( - raw: string, - version: string, - fileName: string, - sha512: string, - size: number, -): void { - const manifest = parseUpdateManifest(raw, "latest.yml", "Windows"); - if ( - manifest.version !== version || - manifest.files.length !== 1 || - manifest.files[0]?.url !== fileName || - manifest.files[0]?.sha512 !== sha512 || - manifest.files[0]?.size !== size - ) { - throw new Error( - "The Windows update manifest does not match the installer and requested version.", - ); - } -} - -const resolveOptions = (): AcceptanceOptions => { - const { values } = NodeUtil.parseArgs({ - options: { - "baseline-installer": { type: "string" }, - "new-artifact-dir": { type: "string" }, - "baseline-version": { type: "string", default: "0.1.7" }, - "new-version": { type: "string" }, - "install-dir": { type: "string", default: "C:\\t3code-update-acceptance\\app" }, - "artifacts-dir": { type: "string", default: "windows-update-acceptance" }, - "mock-server-port": { type: "string", default: "3399" }, - }, - }); - const baselineInstaller = values["baseline-installer"]; - const newArtifactDir = values["new-artifact-dir"]; - const newVersion = values["new-version"]; - if (!baselineInstaller || !newArtifactDir || !newVersion) { - throw new Error( - "Usage: windows-update-acceptance.ts --baseline-installer --new-artifact-dir " + - "--new-version [--baseline-version 0.1.7] [--install-dir ] [--artifacts-dir ] [--mock-server-port 3399]", - ); - } - - const mockServerPort = Number(values["mock-server-port"]); - if (!Number.isInteger(mockServerPort) || mockServerPort <= 0 || mockServerPort > 65_535) { - throw new Error(`Invalid --mock-server-port: ${values["mock-server-port"]}`); - } - - return { - baselineInstaller: NodePath.resolve(baselineInstaller), - newArtifactDir: NodePath.resolve(newArtifactDir), - baselineVersion: values["baseline-version"] ?? "0.1.7", - newVersion, - installDir: NodePath.resolve(values["install-dir"] ?? "C:\\t3code-update-acceptance\\app"), - artifactsDir: NodePath.resolve(values["artifacts-dir"] ?? "windows-update-acceptance"), - mockServerPort, - }; -}; - -const sleep = (milliseconds: number) => NodeTimersPromises.setTimeout(milliseconds, undefined); - -async function waitFor( - step: string, - read: () => Promise | T, - predicate: (value: T) => boolean, - timeoutMs: number, - intervalMs = 1_000, -): Promise { - const deadline = Date.now() + timeoutMs; - let lastValue: T | undefined; - let lastError: unknown; - while (Date.now() < deadline) { - try { - lastValue = await read(); - if (predicate(lastValue)) return lastValue; - } catch (error) { - lastError = error; - } - await sleep(intervalMs); - } - throw new Error( - `Timed out after ${Math.round(timeoutMs / 1_000)}s waiting for ${step}. Last value: ${JSON.stringify(lastValue)}${lastError === undefined ? "" : `; last error: ${String(lastError)}`}`, - ); -} - -async function httpStatus(url: string): Promise { - return new Promise((resolve, reject) => { - const request = NodeHttp.get(url, { timeout: 3_000 }, (response) => { - response.resume(); - resolve(response.statusCode ?? 0); - }); - request.on("timeout", () => { - request.destroy(new Error(`Timed out requesting ${url}`)); - }); - request.on("error", reject); - }); -} - -function runPowerShell(script: string, env: Record = {}): string { - const result = NodeChildProcess.spawnSync( - "powershell.exe", - ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script], - { - encoding: "utf8", - env: { ...process.env, ...env }, - timeout: 120_000, - windowsHide: true, - }, - ); - if (result.error) throw result.error; - if (result.status !== 0) { - throw new Error( - `PowerShell failed with exit ${result.status}.\nScript: ${script}\nstdout: ${result.stdout}\nstderr: ${result.stderr}`, - ); - } - return result.stdout.trim(); -} - -const PROCESS_QUERY = ` -$exe = $env:T3_ACCEPT_EXE -$since = $null -if ($env:T3_ACCEPT_SINCE_MS) { - $since = [DateTimeOffset]::FromUnixTimeMilliseconds([int64]$env:T3_ACCEPT_SINCE_MS).LocalDateTime -} -$items = @(Get-CimInstance Win32_Process | Where-Object { $_.ExecutablePath -eq $exe -and ($null -eq $since -or $_.CreationDate -ge $since) } | ForEach-Object { - $process = Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue - $window = 0 - if ($process) { $window = [int64]$process.MainWindowHandle } - [pscustomobject]@{ pid = [int]$_.ProcessId; window = $window } -}) -ConvertTo-Json -InputObject $items -Compress -`; - -function listAppProcesses(executable: string, sinceMs?: number): ReadonlyArray { - const output = runPowerShell(PROCESS_QUERY, { - T3_ACCEPT_EXE: executable, - ...(sinceMs === undefined ? {} : { T3_ACCEPT_SINCE_MS: String(sinceMs) }), - }); - if (output.length === 0) return []; - const parsed: unknown = JSON.parse(output); - const items = Array.isArray(parsed) ? parsed : [parsed]; - return items.flatMap((item): AppProcess[] => { - if (typeof item !== "object" || item === null) return []; - const record = item as Record; - return typeof record.pid === "number" && typeof record.window === "number" - ? [{ pid: record.pid, window: record.window }] - : []; - }); -} - -function closeRestartedWindows(executable: string, sinceMs: number): void { - const pids = listAppProcesses(executable, sinceMs).map((process) => process.pid); - if (pids.length === 0) return; - runPowerShell( - "$env:T3_ACCEPT_PIDS.Split(',') | ForEach-Object { $p = Get-Process -Id ([int]$_) -ErrorAction SilentlyContinue; if ($p -and $p.MainWindowHandle -ne 0) { [void]$p.CloseMainWindow() } }", - { T3_ACCEPT_PIDS: pids.join(",") }, - ); -} - -function readExecutableProductVersion(executable: string): string { - return runPowerShell( - "$version = (Get-Item -LiteralPath $env:T3_ACCEPT_EXE).VersionInfo.ProductVersion\n[Console]::Out.Write($version)", - { T3_ACCEPT_EXE: executable }, - ); -} - -function versionLabelMatches(label: string, expected: string): boolean { - // electron-builder writes ProductVersion in its four-part Windows form (x.y.z.0). - const trimmed = label.trim(); - const expectedLabel = expected.trim(); - return trimmed === expectedLabel || trimmed === `${expectedLabel}.0`; -} - -function readInstalledAppVersion(installDir: string): string { - const archive = NodePath.join(installDir, "resources", "app.asar"); - uncache(archive); - const packageJson = extractFile(archive, "package.json"); - const data: unknown = JSON.parse(packageJson.toString()); - if ( - typeof data !== "object" || - data === null || - !("version" in data) || - typeof data.version !== "string" - ) { - throw new Error("Installed application package has no version."); - } - return data.version; -} - -// Rebranding changes the executable name, so find the app by its version instead of its name. -function findInstalledExecutable(installDir: string, expectedVersion: string): string { - if (!NodeFS.existsSync(installDir)) { - throw new Error(`The install directory does not exist: ${installDir}`); - } - if (readInstalledAppVersion(installDir) !== expectedVersion) { - throw new Error(`Installed app is not version ${expectedVersion}.`); - } - const executables = NodeFS.readdirSync(installDir, { withFileTypes: true }) - .filter((entry) => entry.isFile() && entry.name.toLowerCase().endsWith(".exe")) - .filter((entry) => !entry.name.toLowerCase().startsWith("uninstall")) - .map((entry) => NodePath.join(installDir, entry.name)); - for (const executable of executables) { - try { - if ( - versionLabelMatches( - readExecutableProductVersion(executable), - expectedVersion.split("-")[0]!, - ) - ) { - return executable; - } - } catch { - // A partially written executable during an install is retried by the caller's wait. - } - } - throw new Error( - `No executable in ${installDir} reports version ${expectedVersion}. Candidates: ` + - (executables.length === 0 ? "(none)" : executables.join(", ")), - ); -} - -async function readUpdateState(page: Page): Promise { - return page.evaluate(() => { - const target = globalThis as unknown as { desktopBridge: DesktopBridgeHandle }; - return target.desktopBridge.getUpdateState(); - }); -} - -async function launchApp( - executable: string, - options: AcceptanceOptions, - t3Home: string, -): Promise<{ app: ElectronApplication; page: Page }> { - const app = await _electron.launch({ - executablePath: executable, - args: ["--no-sandbox"], - env: { - ...process.env, - T3CODE_HOME: t3Home, - T3CODE_TELEMETRY_ENABLED: "false", - T3CODE_DESKTOP_MOCK_UPDATES: "true", - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT: String(options.mockServerPort), - }, - timeout: LAUNCH_TIMEOUT_MS, - }); - const page = await app.firstWindow({ timeout: LAUNCH_TIMEOUT_MS }); - await page.waitForFunction( - () => Boolean((globalThis as unknown as { desktopBridge?: unknown }).desktopBridge), - undefined, - { timeout: LAUNCH_TIMEOUT_MS }, - ); - return { app, page }; -} - -async function stopApp( - app: ElectronApplication | undefined, - artifactsDir: string, - label: string, -): Promise { - if (!app) return; - const child = app.process(); - await Promise.race([app.close().catch(() => {}), sleep(10_000)]); - NodeFS.writeFileSync( - NodePath.join(artifactsDir, `${label}-exit.json`), - `${JSON.stringify({ exitCode: child.exitCode, signalCode: child.signalCode })}\n`, - ); - if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL"); -} - -function copyDirectory(from: string, to: string): void { - if (!NodeFS.existsSync(from)) return; - NodeFS.cpSync(from, to, { recursive: true }); -} - -function tail(text: string, lines: number): string { - return text.split("\n").slice(-lines).join("\n"); -} - -const run = async (): Promise => { - if (process.platform !== "win32") { - throw new Error( - `This acceptance installs and updates the Windows desktop app; it cannot run on ${process.platform}. ` + - "Run .github/workflows/fork-windows-update-test.yml through workflow_dispatch instead.", - ); - } - - const options = resolveOptions(); - NodeFS.mkdirSync(options.artifactsDir, { recursive: true }); - assertAcceptanceVersions(options.baselineVersion, options.newVersion); - - const newInstaller = findDesktopArtifact(options.newArtifactDir, options.newVersion); - if (!NodeFS.existsSync(options.baselineInstaller)) { - throw new Error(`The baseline installer does not exist: ${options.baselineInstaller}`); - } - let executable = ""; - const eventsPath = NodePath.join(options.artifactsDir, "events.ndjson"); - const mockLog: string[] = []; - let step = "validating inputs"; - let app: ElectronApplication | undefined; - let page: Page | undefined; - let mockServer: NodeChildProcess.ChildProcess | undefined; - const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-windows-update-acceptance-")); - const t3Home = NodePath.join(home, "t3"); - - const record = (name: string, detail?: unknown): void => { - const event = { - at: new Date().toISOString(), - step: name, - ...(detail === undefined ? {} : { detail }), - }; - step = name; - console.log( - `[windows-update-acceptance] ${name}${detail === undefined ? "" : ` ${JSON.stringify(detail)}`}`, - ); - NodeFS.appendFileSync(eventsPath, `${JSON.stringify(event)}\n`); - }; - - const writeJson = (name: string, value: unknown): void => { - NodeFS.writeFileSync( - NodePath.join(options.artifactsDir, name), - `${JSON.stringify(value, null, 2)}\n`, - ); - }; - - try { - record("inputs validated", { - baselineInstaller: options.baselineInstaller, - baselineVersion: options.baselineVersion, - newInstaller, - newVersion: options.newVersion, - installDir: options.installDir, - mockServerPort: options.mockServerPort, - t3Home, - }); - - step = "seeding an isolated T3 home"; - NodeFS.mkdirSync(NodePath.join(t3Home, "userdata"), { recursive: true }); - NodeFS.copyFileSync(FIXTURE_DB, NodePath.join(t3Home, "userdata/statev2.sqlite")); - record("seeded isolated T3 home", { t3Home }); - - step = "preparing the mock update feed"; - const feedDir = NodePath.join(home, "update-feed"); - NodeFS.mkdirSync(feedDir, { recursive: true }); - const newInstallerName = NodePath.basename(newInstaller); - const feedInstaller = NodePath.join(feedDir, newInstallerName); - NodeFS.copyFileSync(newInstaller, feedInstaller); - const installerBytes = NodeFS.readFileSync(newInstaller); - const sha512 = NodeCrypto.createHash("sha512").update(installerBytes).digest("base64"); - const latestYml = NodeFS.readFileSync( - NodePath.join(NodePath.dirname(newInstaller), "latest.yml"), - "utf8", - ); - validateLatestYml( - latestYml, - options.newVersion, - newInstallerName, - sha512, - installerBytes.byteLength, - ); - NodeFS.writeFileSync(NodePath.join(feedDir, "latest.yml"), latestYml); - NodeFS.writeFileSync(NodePath.join(options.artifactsDir, "latest.yml"), latestYml); - record("prepared mock update feed", { - fileName: newInstallerName, - size: installerBytes.byteLength, - sha512, - }); - - step = "starting the mock update server"; - mockServer = NodeChildProcess.spawn( - process.execPath, - [NodePath.join(import.meta.dirname, "mock-update-server.ts")], - { - env: { - ...process.env, - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT: String(options.mockServerPort), - T3CODE_DESKTOP_MOCK_UPDATE_SERVER_ROOT: feedDir, - }, - stdio: ["ignore", "pipe", "pipe"], - windowsHide: true, - }, - ); - mockServer.stdout?.on("data", (chunk: Buffer) => mockLog.push(chunk.toString())); - mockServer.stderr?.on("data", (chunk: Buffer) => mockLog.push(chunk.toString())); - await waitFor( - "the mock update server to serve latest.yml", - async () => httpStatus(`http://localhost:${options.mockServerPort}/latest.yml`), - (status) => status === 200, - 60_000, - 500, - ); - record("mock update server ready"); - - step = "installing the baseline build"; - const installer = NodeChildProcess.spawnSync( - options.baselineInstaller, - ["/S", `/D=${options.installDir}`], - { stdio: "inherit", timeout: INSTALL_APPLY_TIMEOUT_MS, windowsHide: true }, - ); - if (installer.error) throw installer.error; - if (installer.status !== 0) { - throw new Error(`The baseline installer exited with ${installer.status}.`); - } - if (!NodeFS.existsSync(options.installDir)) { - throw new Error(`The baseline installer did not create ${options.installDir}.`); - } - executable = findInstalledExecutable(options.installDir, options.baselineVersion); - const installedBaselineVersion = readExecutableProductVersion(executable); - record("baseline installed", { executable, productVersion: installedBaselineVersion }); - - step = "launching the baseline app"; - const baselineSession = await launchApp(executable, options, t3Home); - app = baselineSession.app; - page = baselineSession.page; - const baselinePage = baselineSession.page; - await baselinePage - .locator("li[data-thread-item]", { hasText: THREAD_TITLE }) - .waitFor({ timeout: LAUNCH_TIMEOUT_MS }); - record("baseline app opened the seeded thread"); - await baselinePage.screenshot({ - path: NodePath.join(options.artifactsDir, "01-baseline.png"), - timeout: 10_000, - }); - - const baselineAppVersion = await baselineSession.app.evaluate(({ app: electronApp }) => - electronApp.getVersion(), - ); - if (baselineAppVersion !== options.baselineVersion) { - throw new Error( - `The baseline app reports version ${baselineAppVersion}, expected ${options.baselineVersion}.`, - ); - } - - step = "waiting for the updater to be ready"; - const readyState = await waitFor( - "the desktop updater to enable itself", - () => readUpdateState(baselinePage), - (state) => state.enabled || state.status === "disabled", - 60_000, - ); - writeJson("01-baseline-update-state.json", readyState); - if (!readyState.enabled) { - throw new Error( - `Updates are disabled before the check: ${readyState.message ?? readyState.status}`, - ); - } - if (readyState.channel !== "latest") { - throw new Error( - `The baseline app is on the '${readyState.channel}' update channel, expected 'latest'.`, - ); - } - if (readyState.currentVersion !== options.baselineVersion) { - throw new Error( - `The updater reports current version ${readyState.currentVersion}, expected ${options.baselineVersion}.`, - ); - } - - step = "checking the mock feed for the new version"; - let checkResult: UpdateCheckResult | undefined; - for (let attempt = 0; attempt < 5; attempt += 1) { - checkResult = (await baselinePage.evaluate(() => { - const target = globalThis as unknown as { desktopBridge: DesktopBridgeHandle }; - return target.desktopBridge.checkForUpdate(); - })) as UpdateCheckResult; - if (checkResult.checked) break; - await sleep(2_000); - } - writeJson("02-check-result.json", checkResult); - record("update check requested", { checked: checkResult?.checked }); - const availableState = await waitFor( - `the app to offer version ${options.newVersion}`, - () => readUpdateState(baselinePage), - (state) => state.status === "available" && state.availableVersion === options.newVersion, - 120_000, - ); - writeJson("02-update-available.json", availableState); - record("update discovered", { - availableVersion: availableState.availableVersion, - status: availableState.status, - }); - await baselinePage.screenshot({ - path: NodePath.join(options.artifactsDir, "02-update-available.png"), - timeout: 10_000, - }); - - step = "downloading the update"; - let downloadResult: UpdateActionResult | undefined; - for (let attempt = 0; attempt < 5; attempt += 1) { - downloadResult = (await baselinePage.evaluate(() => { - const target = globalThis as unknown as { desktopBridge: DesktopBridgeHandle }; - return target.desktopBridge.downloadUpdate(); - })) as UpdateActionResult; - if (downloadResult.accepted) break; - await sleep(2_000); - } - writeJson("03-download-result.json", downloadResult); - if (!downloadResult?.accepted) { - throw new Error(`The app refused to download the update: ${JSON.stringify(downloadResult)}`); - } - const downloadedState = await waitFor( - `the app to download version ${options.newVersion}`, - () => readUpdateState(baselinePage), - (state) => state.status === "downloaded" && state.downloadedVersion === options.newVersion, - DOWNLOAD_TIMEOUT_MS, - ); - writeJson("03-update-downloaded.json", downloadedState); - await waitFor( - "the mock feed to log the manifest and installer requests", - () => mockLog.join(""), - (log) => log.includes("/latest.yml") && log.includes(`/${newInstallerName}`), - 15_000, - 500, - ); - record("mock feed served the manifest and installer", { - downloadedVersion: downloadedState.downloadedVersion, - }); - await baselinePage.screenshot({ - path: NodePath.join(options.artifactsDir, "03-update-downloaded.png"), - timeout: 10_000, - }); - - step = "installing the update"; - const installStartedAt = Date.now(); - const installAttempt = baselinePage - .evaluate((expectedVersion) => { - const target = globalThis as unknown as { desktopBridge: DesktopBridgeHandle }; - return target.desktopBridge.installUpdate(expectedVersion); - }, options.newVersion) - .then((result: UpdateActionResult) => { - record("install action returned", result); - return result; - }) - .catch((error: unknown) => { - record("install action ended with the app quitting", { - error: error instanceof Error ? error.message : String(error), - }); - return undefined; - }); - const child = baselineSession.app.process(); - await waitFor( - "the app to exit after the install action", - () => child.exitCode !== null || child.signalCode !== null, - (exited) => exited, - INSTALL_APPLY_TIMEOUT_MS, - 500, - ); - record("baseline app exited for the installer", { - exitCode: child.exitCode, - signalCode: child.signalCode, - elapsedMs: Date.now() - installStartedAt, - }); - await installAttempt; - app = undefined; - page = undefined; - - step = "waiting for the installer to replace the app"; - await waitFor( - `the installed executable to report ${options.newVersion}`, - () => { - executable = findInstalledExecutable(options.installDir, options.newVersion); - return readInstalledAppVersion(options.installDir); - }, - (version) => version === options.newVersion, - INSTALL_APPLY_TIMEOUT_MS, - 2_000, - ); - const installedVersion = readExecutableProductVersion(executable); - writeJson("04-installed-executable.json", { executable, productVersion: installedVersion }); - record("installer replaced the app", { productVersion: installedVersion }); - - step = "waiting for the installer to restart the app"; - await waitFor( - "the installer to restart the app", - () => listAppProcesses(executable, installStartedAt), - (processes) => processes.length > 0, - RELAUNCH_TIMEOUT_MS, - 2_000, - ); - record("installer restarted the app", { - processes: listAppProcesses(executable, installStartedAt), - }); - try { - await waitFor( - "the restarted app to show its window", - () => listAppProcesses(executable, installStartedAt), - (processes) => processes.some((process) => process.window !== 0), - WINDOW_TIMEOUT_MS, - 2_000, - ); - record("restarted app showed its window"); - } catch (error) { - record("restarted app did not expose a window handle", { - message: error instanceof Error ? error.message : String(error), - }); - } - - step = "stopping the restarted app before the Playwright check"; - for ( - let attempt = 0; - attempt < 3 && listAppProcesses(executable, installStartedAt).length > 0; - attempt += 1 - ) { - closeRestartedWindows(executable, installStartedAt); - await sleep(2_000); - } - if (listAppProcesses(executable, installStartedAt).length > 0) { - throw new Error( - `Could not stop the restarted app at ${executable}: ${JSON.stringify(listAppProcesses(executable, installStartedAt))}`, - ); - } - record("restarted app stopped"); - - step = "relaunching the updated app to check version and data"; - const updatedSession = await launchApp(executable, options, t3Home); - app = updatedSession.app; - page = updatedSession.page; - const updatedPage = updatedSession.page; - const updatedVersion = await updatedSession.app.evaluate(({ app: electronApp }) => - electronApp.getVersion(), - ); - if (updatedVersion !== options.newVersion) { - throw new Error( - `The updated app reports version ${updatedVersion}, expected ${options.newVersion}.`, - ); - } - const thread = updatedPage.locator("li[data-thread-item]", { hasText: THREAD_TITLE }); - await thread.waitFor({ timeout: LAUNCH_TIMEOUT_MS }); - await thread.click(); - await updatedPage.getByText(THREAD_MESSAGE).waitFor({ timeout: LAUNCH_TIMEOUT_MS }); - record("updated app kept the seeded thread and message", { updatedVersion }); - await updatedPage.screenshot({ - path: NodePath.join(options.artifactsDir, "04-updated-app.png"), - timeout: 10_000, - }); - - const updatedState = await waitFor( - "the updated app's updater state", - () => readUpdateState(updatedPage), - (state) => state.enabled || state.status === "disabled", - 60_000, - ); - writeJson("04-updated-update-state.json", updatedState); - if (!updatedState.enabled) { - throw new Error( - `The updated app has updates disabled: ${updatedState.message ?? updatedState.status}`, - ); - } - if (updatedState.currentVersion !== options.newVersion) { - throw new Error( - `The updated app's updater reports current version ${updatedState.currentVersion}, expected ${options.newVersion}.`, - ); - } - if (updatedState.notAppliedVersion !== undefined) { - throw new Error( - `The updated app says the last install did not apply: ${updatedState.notAppliedVersion}.`, - ); - } - record("acceptance passed", { - baselineVersion: options.baselineVersion, - updatedVersion, - channel: updatedState.channel, - }); - } catch (error) { - record("acceptance failed", { - step, - message: error instanceof Error ? error.message : String(error), - stack: error instanceof Error ? error.stack : undefined, - }); - if (page) { - await page - .screenshot({ path: NodePath.join(options.artifactsDir, "failure.png"), timeout: 10_000 }) - .catch(() => {}); - } - NodeFS.mkdirSync(options.artifactsDir, { recursive: true }); - NodeFS.writeFileSync( - NodePath.join(options.artifactsDir, "mock-update-server.log"), - mockLog.join(""), - ); - copyDirectory( - NodePath.join(t3Home, "userdata/logs"), - NodePath.join(options.artifactsDir, "logs"), - ); - const serverLog = NodePath.join(t3Home, "userdata/logs/server-child.log"); - if (NodeFS.existsSync(serverLog)) { - console.error("\nLast backend output (server-child.log):"); - console.error(tail(NodeFS.readFileSync(serverLog, "utf8"), 40)); - } - console.error(error instanceof Error ? error.stack : error); - process.exitCode = 1; - } finally { - await stopApp(app, options.artifactsDir, "app"); - copyDirectory( - NodePath.join(t3Home, "userdata/logs"), - NodePath.join(options.artifactsDir, "logs"), - ); - if (mockServer) { - mockServer.kill(); - await Promise.race([ - new Promise((resolve) => mockServer?.once("exit", () => resolve())), - sleep(5_000), - ]); - NodeFS.writeFileSync( - NodePath.join(options.artifactsDir, "mock-update-server.log"), - mockLog.join(""), - ); - } - try { - // Only the isolated T3 home this run created; the installed app stays for the runner's lifetime. - NodeFS.rmSync(home, { recursive: true, force: true, maxRetries: 5 }); - } catch (error) { - console.error(`Could not remove the isolated home ${home}: ${String(error)}`); - } - } -}; - -if (import.meta.main) { - await run(); -}