From efb25b159664b939834ecfbb2bb262715a5ab279 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 13 Aug 2026 17:17:38 -0700 Subject: [PATCH] review: install only from downloaded status, report install failures via errorContext Co-Authored-By: Claude Fable 5 --- .../src/updates/DesktopRemoteUpdates.test.ts | 35 +++++++++++++++++++ .../src/updates/DesktopRemoteUpdates.ts | 7 +++- .../src/updates/remoteUpdateFlow.test.ts | 27 +++++++++++--- apps/desktop/src/updates/remoteUpdateFlow.ts | 6 +++- .../desktop/src/updates/updatesTestHarness.ts | 3 +- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts index 9e034ca5d2..39049b5aa0 100644 --- a/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts +++ b/apps/desktop/src/updates/DesktopRemoteUpdates.test.ts @@ -9,6 +9,7 @@ import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; +import * as ElectronUpdater from "../electron/ElectronUpdater.ts"; import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts"; import * as DesktopRemoteUpdates from "./DesktopRemoteUpdates.ts"; import * as DesktopUpdates from "./DesktopUpdates.ts"; @@ -101,6 +102,40 @@ describe("DesktopRemoteUpdates", () => { ); }); + it.effect("reports a failed outcome when quitAndInstall fails", () => { + const harness = makeHarness({ + quitAndInstall: Effect.fail( + new ElectronUpdater.ElectronUpdaterQuitAndInstallError({ + channel: "latest", + isSilent: true, + isForceRunAfter: true, + cause: new Error("spawn failed"), + }), + ), + }); + + return runRemoteUpdatesTest(harness, ({ reports, requests }) => + Effect.gen(function* () { + yield* Queue.offer(requests, request("req-4")); + yield* settle; + harness.emit("update-available", { version: "1.2.4" }); + yield* settle; + harness.emit("update-downloaded", { version: "1.2.4" }); + yield* settle; + + // Install failures reduce to status "downloaded" + errorContext + // "install"; the run must still end with a failed report after the + // premature "installing" one. + const terminals = terminalReports(reports); + assert.deepEqual( + terminals.map((report) => report.outcome), + ["installing", "failed"], + ); + assert.equal(terminals[1]?.state.errorContext, "install"); + }), + ); + }); + it.effect("reports up-to-date without installing when there is no update", () => { const harness = makeHarness(); diff --git a/apps/desktop/src/updates/DesktopRemoteUpdates.ts b/apps/desktop/src/updates/DesktopRemoteUpdates.ts index 02da2ad050..dc6d78e29c 100644 --- a/apps/desktop/src/updates/DesktopRemoteUpdates.ts +++ b/apps/desktop/src/updates/DesktopRemoteUpdates.ts @@ -97,7 +97,12 @@ export const listen: Effect.Effect< outcome: "failed", reason: "The desktop app could not start the install.", }); - } else if (result.state.status === "error") { + } else if ( + result.state.status === "error" || + result.state.errorContext === "install" + ) { + // Install failures reduce to status "downloaded" with + // errorContext "install", not to "error". yield* publishReport(result.state, { outcome: "failed", reason: result.state.message ?? "The desktop app failed to install the update.", diff --git a/apps/desktop/src/updates/remoteUpdateFlow.test.ts b/apps/desktop/src/updates/remoteUpdateFlow.test.ts index 1bf3b60d9e..3fc56e8350 100644 --- a/apps/desktop/src/updates/remoteUpdateFlow.test.ts +++ b/apps/desktop/src/updates/remoteUpdateFlow.test.ts @@ -46,7 +46,7 @@ describe("nextRemoteDesktopUpdateStep", () => { }); }); - it("installs as soon as a download is present", () => { + it("installs only from the downloaded status the updater will accept", () => { assert.deepEqual( nextRemoteDesktopUpdateStep( makeState({ status: "downloaded", downloadedVersion: "1.2.4" }), @@ -55,16 +55,35 @@ describe("nextRemoteDesktopUpdateStep", () => { ), { action: "install" }, ); - // A downloadedVersion left over from an earlier local download wins even - // when the status has moved on. + // A previous install failure keeps status "downloaded", so a remote run + // retries the install. assert.deepEqual( nextRemoteDesktopUpdateStep( - makeState({ status: "idle", downloadedVersion: "1.2.4" }), + makeState({ + status: "downloaded", + downloadedVersion: "1.2.4", + errorContext: "install", + message: "quitAndInstall failed", + }), NO_ATTEMPTS, null, ), { action: "install" }, ); + // A leftover downloadedVersion on an error state (background updater + // error) must fail instead of reporting an install the updater refuses. + assert.deepEqual( + nextRemoteDesktopUpdateStep( + makeState({ + status: "error", + downloadedVersion: "1.2.4", + message: "background updater error", + }), + NO_ATTEMPTS, + null, + ), + { action: "done", outcome: "failed", reason: "background updater error" }, + ); }); it("rides along while a check or download is already in flight", () => { diff --git a/apps/desktop/src/updates/remoteUpdateFlow.ts b/apps/desktop/src/updates/remoteUpdateFlow.ts index 89e0dc9edb..f84d18509a 100644 --- a/apps/desktop/src/updates/remoteUpdateFlow.ts +++ b/apps/desktop/src/updates/remoteUpdateFlow.ts @@ -42,7 +42,11 @@ export function nextRemoteDesktopUpdateStep( reason: disabledReason ?? "Automatic updates are disabled on this machine.", }; } - if (state.downloadedVersion !== null || state.status === "downloaded") { + // Only "downloaded" is installable: installDownloadedUpdate rejects any + // other status. A leftover downloadedVersion on an "error" state (e.g. a + // background updater error) must fall through to the error branch instead + // of reporting an install that the updater will refuse. + if (state.status === "downloaded") { return { action: "install" }; } if (state.status === "downloading" || state.status === "checking") { diff --git a/apps/desktop/src/updates/updatesTestHarness.ts b/apps/desktop/src/updates/updatesTestHarness.ts index 9e778a30b3..32620e6334 100644 --- a/apps/desktop/src/updates/updatesTestHarness.ts +++ b/apps/desktop/src/updates/updatesTestHarness.ts @@ -26,6 +26,7 @@ export interface UpdatesHarnessOptions { >; readonly setUpdateChannelError?: DesktopAppSettings.DesktopSettingsWriteError; readonly setDisableDifferentialDownload?: Effect.Effect; + readonly quitAndInstall?: Effect.Effect; readonly stopBackend?: Effect.Effect; readonly env?: Record; } @@ -82,7 +83,7 @@ export function makeHarness(options: UpdatesHarnessOptions = {}) { quitAndInstall: () => Effect.sync(() => { quitAndInstallCount += 1; - }), + }).pipe(Effect.andThen(options.quitAndInstall ?? Effect.void)), on: (eventName, listener) => Effect.acquireRelease( Effect.sync(() => {