From c91f253067ffed90ed813d73b4ddb81067b1c8b8 Mon Sep 17 00:00:00 2001 From: lukdz Date: Wed, 11 Nov 2026 15:17:50 +0100 Subject: [PATCH] fix(desktop): show the update dialog after a manual check leaves checking Up-to-date and error are applied in detached updater events, so the menu could read a snapshot that was still checking. Wait for a later state, and do not show a dialog if that wait times out. --- .../src/window/DesktopApplicationMenu.test.ts | 266 +++++++++++++++++- .../src/window/DesktopApplicationMenu.ts | 28 +- 2 files changed, 288 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src/window/DesktopApplicationMenu.test.ts b/apps/desktop/src/window/DesktopApplicationMenu.test.ts index 612ec441ba99b..ddd21ac3dae9a 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.test.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.test.ts @@ -1,8 +1,13 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; +import type { DesktopUpdateState, DesktopUpdateStatus } from "@t3tools/contracts"; import { assert, describe, it } from "@effect/vitest"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Queue from "effect/Queue"; +import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; import * as Electron from "electron"; import { vi } from "vite-plus/test"; @@ -63,11 +68,48 @@ const layerElectronDialog = Layer.succeed(ElectronDialog.ElectronDialog, { showErrorBox: () => Effect.void, } satisfies ElectronDialog.ElectronDialog["Service"]); -const layerDesktopUpdates = Layer.succeed(DesktopUpdates.DesktopUpdates, { +const layerRecordingDialog = (shown: Deferred.Deferred) => + Layer.succeed(ElectronDialog.ElectronDialog, { + pickFolder: () => Effect.succeedNone, + pickFiles: () => Effect.succeed([]), + showMessageBox: (options) => + Deferred.succeed(shown, options).pipe(Effect.as({ response: 0, checkboxChecked: false })), + showErrorBox: () => Effect.void, + } satisfies ElectronDialog.ElectronDialog["Service"]); + +const menuUpdateState = ( + status: DesktopUpdateStatus, + overrides: Partial = {}, +): DesktopUpdateState => ({ + enabled: true, + status, + channel: "latest", + currentVersion: "1.2.3", + hostArch: "arm64", + appArch: "arm64", + runningUnderArm64Translation: false, + availableVersion: null, + downloadedVersion: null, + releaseNotes: [], + omittedReleaseCount: 0, + downloadPercent: null, + checkedAt: null, + message: null, + errorContext: null, + canRetry: false, + ...overrides, +}); + +const desktopUpdatesService = ( + overrides: Partial = {}, +): DesktopUpdates.DesktopUpdates["Service"] => ({ getState: Effect.die("unexpected getState"), isActionActive: Effect.succeed(false), isInstallActive: Effect.succeed(false), - subscribe: Effect.die("unexpected subscribe"), + subscribe: Effect.succeed({ + latest: menuUpdateState("idle"), + changes: Stream.empty, + }), emitState: Effect.void, disabledReason: Effect.succeedNone, configure: Effect.void, @@ -76,7 +118,13 @@ const layerDesktopUpdates = Layer.succeed(DesktopUpdates.DesktopUpdates, { download: Effect.die("unexpected download"), install: Effect.die("unexpected install"), installPrepared: () => Effect.die("unexpected installPrepared"), -} satisfies DesktopUpdates.DesktopUpdates["Service"]); + ...overrides, +}); + +const layerDesktopUpdates = Layer.succeed(DesktopUpdates.DesktopUpdates, desktopUpdatesService()); + +const layerDesktopUpdatesWith = (overrides: Partial) => + Layer.succeed(DesktopUpdates.DesktopUpdates, desktopUpdatesService(overrides)); const layerDesktopWindow = (selectedAction: Deferred.Deferred) => Layer.succeed(DesktopWindow.DesktopWindow, { @@ -113,6 +161,8 @@ const configureMenu = ( selectedAction: Deferred.Deferred, applicationMenuTemplate: Deferred.Deferred, environment: Partial = {}, + updates: Layer.Layer = layerDesktopUpdates, + dialog: Layer.Layer = layerElectronDialog, ) => Effect.gen(function* () { const menu = yield* DesktopApplicationMenu.DesktopApplicationMenu; @@ -122,8 +172,8 @@ const configureMenu = ( DesktopApplicationMenu.layer.pipe( Layer.provideMerge(layerElectronMenu(applicationMenuTemplate)), Layer.provideMerge(layerDesktopWindow(selectedAction)), - Layer.provideMerge(layerDesktopUpdates), - Layer.provideMerge(layerElectronDialog), + Layer.provideMerge(updates), + Layer.provideMerge(dialog), Layer.provideMerge(layerElectronApp), Layer.provideMerge( DesktopEnvironment.layer({ ...environmentInput, ...environment }).pipe( @@ -134,6 +184,18 @@ const configureMenu = ( ), ); +const clickCheckForUpdates = (template: readonly Electron.MenuItemConstructorOptions[]) => { + const helpMenu = template.find((item) => item.role === "help"); + if (!Array.isArray(helpMenu?.submenu)) { + throw new Error("Expected Help menu submenu to be an array."); + } + const checkForUpdates = helpMenu.submenu.find((item) => item.label === "Check for Updates..."); + if (typeof checkForUpdates?.click !== "function") { + throw new Error("Expected Check for Updates menu item to have a click handler."); + } + checkForUpdates.click({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent); +}; + describe("DesktopApplicationMenu", () => { it.effect("leaves the accelerator to Chromium instead of injecting a paste", () => Effect.gen(function* () { @@ -285,4 +347,198 @@ describe("DesktopApplicationMenu", () => { assert.equal(yield* Deferred.await(selectedAction), "main-reload"); }), ); + + it.effect("explains when updates are disabled without checking", () => + Effect.gen(function* () { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + let checked = false; + + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + disabledReason: Effect.succeed(Option.some("This build has no update feed.")), + check: () => + Effect.sync(() => { + checked = true; + }).pipe(Effect.andThen(Effect.die("unexpected check"))), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + const dialog = yield* Deferred.await(shown); + assert.equal(dialog.title, "Updates unavailable"); + assert.equal(dialog.detail, "This build has no update feed."); + assert.isFalse(checked); + }), + ); + + it.effect("shows the settled up-to-date and error dialogs from a manual check", () => + Effect.gen(function* () { + const cases = [ + { + state: menuUpdateState("up-to-date"), + title: "You're up to date!", + message: "T3 Code 1.2.3 is currently the newest version available.", + detail: undefined, + }, + { + state: menuUpdateState("error", { message: "feed down" }), + title: "Update check failed", + message: "Could not check for updates.", + detail: "feed down", + }, + { + state: menuUpdateState("error"), + title: "Update check failed", + message: "Could not check for updates.", + detail: "An unknown error occurred. Please try again later.", + }, + ]; + + for (const expected of cases) { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + check: () => Effect.succeed({ checked: true, state: expected.state }), + subscribe: Effect.die("unexpected subscribe"), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + const dialog = yield* Deferred.await(shown); + assert.equal(dialog.title, expected.title); + assert.equal(dialog.message, expected.message); + assert.equal(dialog.detail, expected.detail); + } + }), + ); + + it.effect("stays quiet when a manual check finds or already has an update", () => + Effect.gen(function* () { + for (const status of ["available", "downloaded"] as const) { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + check: () => Effect.succeed({ checked: true, state: menuUpdateState(status) }), + subscribe: Effect.die("unexpected subscribe"), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + yield* Effect.yieldNow; + assert.isFalse(yield* Deferred.isDone(shown)); + } + }), + ); + + it.effect("waits until a manual check leaves checking before choosing a dialog", () => + Effect.gen(function* () { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + const settled = yield* Queue.unbounded(); + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + check: () => Effect.succeed({ checked: true, state: menuUpdateState("checking") }), + subscribe: Effect.succeed({ + latest: menuUpdateState("checking"), + changes: Stream.fromQueue(settled), + }), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + assert.isFalse(yield* Deferred.isDone(shown)); + yield* Queue.offer(settled, menuUpdateState("error", { message: "feed down" })); + const dialog = yield* Deferred.await(shown); + assert.equal(dialog.title, "Update check failed"); + assert.equal(dialog.detail, "feed down"); + }), + ); + + it.effect("uses the subscribed snapshot when the check result is still checking", () => + Effect.gen(function* () { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + check: () => Effect.succeed({ checked: true, state: menuUpdateState("checking") }), + subscribe: Effect.succeed({ + latest: menuUpdateState("up-to-date"), + changes: Stream.never, + }), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + const dialog = yield* Deferred.await(shown); + assert.equal(dialog.title, "You're up to date!"); + assert.equal(dialog.message, "T3 Code 1.2.3 is currently the newest version available."); + }), + ); + + it.effect("does not invent a dialog when a manual check stays checking", () => + Effect.gen(function* () { + const selectedAction = yield* Deferred.make(); + const applicationMenuTemplate = + yield* Deferred.make(); + const shown = yield* Deferred.make(); + const subscribed = yield* Deferred.make(); + const settled = yield* Queue.unbounded(); + yield* configureMenu( + selectedAction, + applicationMenuTemplate, + {}, + layerDesktopUpdatesWith({ + check: () => Effect.succeed({ checked: true, state: menuUpdateState("checking") }), + subscribe: Deferred.succeed(subscribed, undefined).pipe( + Effect.as({ + latest: menuUpdateState("checking"), + changes: Stream.fromQueue(settled), + }), + ), + }), + layerRecordingDialog(shown), + ); + + clickCheckForUpdates(yield* Deferred.await(applicationMenuTemplate)); + yield* Deferred.await(subscribed); + yield* Effect.yieldNow; + yield* TestClock.adjust("5 seconds"); + yield* Queue.offer(settled, menuUpdateState("up-to-date")); + yield* Effect.yieldNow; + assert.isFalse(yield* Deferred.isDone(shown)); + }), + ); }); diff --git a/apps/desktop/src/window/DesktopApplicationMenu.ts b/apps/desktop/src/window/DesktopApplicationMenu.ts index 44d2c234f8358..3fddacdc33427 100644 --- a/apps/desktop/src/window/DesktopApplicationMenu.ts +++ b/apps/desktop/src/window/DesktopApplicationMenu.ts @@ -3,6 +3,7 @@ import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import * as Electron from "electron"; @@ -41,6 +42,9 @@ const { logInfo: logUpdaterInfo } = makeComponentLogger("desktop-updater"); const { logError: logMenuError } = makeComponentLogger("desktop-menu"); +// Long enough for the detached updater event to publish, short enough that the menu cannot hang. +const MANUAL_UPDATE_CHECK_SETTLE_TIMEOUT = "5 seconds"; + const dispatchMenuAction = Effect.fn("desktop.menu.dispatchMenuAction")(function* ( action: string, ): Effect.fn.Return { @@ -68,7 +72,29 @@ const checkForUpdatesFromMenu = Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; const electronDialog = yield* ElectronDialog.ElectronDialog; const result = yield* updates.check("menu"); - const updateState = result.state; + // Up-to-date and error are applied in detached updater events, so the check + // promise can resolve while this snapshot is still "checking". + const settled = + result.state.status === "checking" + ? yield* Effect.scoped( + Effect.gen(function* () { + const { latest, changes } = yield* updates.subscribe; + if (latest.status !== "checking") { + return Option.some(latest); + } + return yield* changes.pipe( + Stream.filter((state) => state.status !== "checking"), + Stream.take(1), + Stream.runHead, + ); + }), + ).pipe(Effect.timeoutOption(MANUAL_UPDATE_CHECK_SETTLE_TIMEOUT), Effect.map(Option.flatten)) + : Option.some(result.state); + if (Option.isNone(settled)) { + yield* logUpdaterInfo("manual update check was still running when the menu stopped waiting"); + return; + } + const updateState = settled.value; if (updateState.status === "up-to-date") { yield* electronDialog.showMessageBox({