From c2e7a4f543e346f47d12cb118be0c25f97bcc58b Mon Sep 17 00:00:00 2001 From: Seydi Charyyev Date: Mon, 19 Oct 2026 23:31:51 +0000 Subject: [PATCH] fix(desktop): show why an update install failed (#12628) Signed-off-by: Seydi Charyyev --- .../settings/app/UpdateSection.test.tsx | 65 ++++++++++++++++ .../components/settings/app/UpdateSection.tsx | 28 ++++++- ui/desktop/src/preload.ts | 6 +- ui/desktop/src/utils/autoUpdater.ts | 76 ++++++++++--------- 4 files changed, 134 insertions(+), 41 deletions(-) create mode 100644 ui/desktop/src/components/settings/app/UpdateSection.test.tsx diff --git a/ui/desktop/src/components/settings/app/UpdateSection.test.tsx b/ui/desktop/src/components/settings/app/UpdateSection.test.tsx new file mode 100644 index 000000000..5f3c4d3ed --- /dev/null +++ b/ui/desktop/src/components/settings/app/UpdateSection.test.tsx @@ -0,0 +1,65 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import { act, render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import UpdateSection from './UpdateSection'; +import { IntlTestWrapper } from '../../../i18n/test-utils'; + +type UpdaterCallback = Parameters[0]; + +let sendUpdaterEvent: UpdaterCallback; + +const renderDownloadedUpdate = async () => { + render(, { wrapper: IntlTestWrapper }); + await act(async () => { + sendUpdaterEvent({ event: 'update-downloaded', data: { version: '1.52.0' } }); + }); + return screen.getByRole('button', { name: 'Install & Restart' }); +}; + +describe('UpdateSection install', () => { + beforeEach(() => { + window.electron.getVersion = vi.fn(() => '1.51.0'); + window.electron.getUpdateState = vi.fn().mockResolvedValue(null); + window.electron.getAutoDownloadDisabled = vi.fn().mockResolvedValue(false); + window.electron.onUpdaterEvent = vi.fn((callback: UpdaterCallback) => { + sendUpdaterEvent = callback; + }); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it('shows why the install failed and brings the button back', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }); + window.electron.installUpdate = vi.fn().mockResolvedValue({ + success: false, + error: 'Refusing to auto-update: C:\\Goose does not look like an app install directory', + }); + const button = await renderDownloadedUpdate(); + + await user.click(button); + + expect(await screen.findByText(/does not look like an app install directory/)).toBeVisible(); + + act(() => { + vi.advanceTimersByTime(5000); + }); + + expect(screen.queryByText(/does not look like an app install directory/)).toBeNull(); + expect(screen.getByRole('button', { name: 'Install & Restart' })).toBeEnabled(); + }); + + it('disables the button while the install runs', async () => { + window.electron.installUpdate = vi.fn(() => new Promise(() => {})); + const user = userEvent.setup(); + const button = await renderDownloadedUpdate(); + + await user.click(button); + await user.click(button); + + expect(button).toBeDisabled(); + expect(window.electron.installUpdate).toHaveBeenCalledTimes(1); + }); +}); diff --git a/ui/desktop/src/components/settings/app/UpdateSection.tsx b/ui/desktop/src/components/settings/app/UpdateSection.tsx index daec80764..286d0a9ed 100644 --- a/ui/desktop/src/components/settings/app/UpdateSection.tsx +++ b/ui/desktop/src/components/settings/app/UpdateSection.tsx @@ -235,8 +235,21 @@ export default function UpdateSection() { } }; - const installUpdate = () => { - window.electron.installUpdate(); + const installUpdate = async () => { + setUpdateStatus('installing'); + try { + const result = await window.electron.installUpdate(); + if (result.error) { + throw new Error(result.error); + } + } catch (error) { + setUpdateInfo((prev) => ({ + ...prev, + error: errorMessage(error, 'Failed to install update'), + })); + setUpdateStatus('error'); + setTimeout(() => setUpdateStatus('ready'), 5000); + } }; const downloadUpdate = async () => { @@ -270,6 +283,7 @@ export default function UpdateSection() { case 'downloading': return intl.formatMessage(i18n.downloadingProgress, { percent: Math.round(progress) }); case 'ready': + case 'installing': return intl.formatMessage(i18n.downloadReady); case 'success': return updateInfo.isUpdateAvailable === false @@ -289,6 +303,7 @@ export default function UpdateSection() { switch (updateStatus) { case 'checking': case 'downloading': + case 'installing': return ; case 'success': return ; @@ -344,8 +359,13 @@ export default function UpdateSection() { )} - {updateStatus === 'ready' && ( - )} diff --git a/ui/desktop/src/preload.ts b/ui/desktop/src/preload.ts index 34d99953f..4044cd4bf 100644 --- a/ui/desktop/src/preload.ts +++ b/ui/desktop/src/preload.ts @@ -163,7 +163,7 @@ type ElectronAPI = { getVersion: () => string; checkForUpdates: () => Promise<{ updateInfo: unknown; error: string | null }>; downloadUpdate: () => Promise<{ success: boolean; error: string | null }>; - installUpdate: () => void; + installUpdate: () => Promise<{ success: boolean; error: string | null }>; restartApp: () => void; onUpdaterEvent: (callback: (event: UpdaterEvent) => void) => void; getUpdateState: () => Promise<{ updateAvailable: boolean; latestVersion?: string } | null>; @@ -311,8 +311,8 @@ const electronAPI: ElectronAPI = { downloadUpdate: (): Promise<{ success: boolean; error: string | null }> => { return ipcRenderer.invoke('download-update'); }, - installUpdate: (): void => { - ipcRenderer.invoke('install-update'); + installUpdate: (): Promise<{ success: boolean; error: string | null }> => { + return ipcRenderer.invoke('install-update'); }, restartApp: (): void => { ipcRenderer.send('restart-app'); diff --git a/ui/desktop/src/utils/autoUpdater.ts b/ui/desktop/src/utils/autoUpdater.ts index c9f6b3de7..6029bdf63 100644 --- a/ui/desktop/src/utils/autoUpdater.ts +++ b/ui/desktop/src/utils/autoUpdater.ts @@ -275,42 +275,50 @@ export function registerUpdateIpcHandlers() { }); ipcMain.handle('install-update', async () => { - if (isUsingGitHubFallback) { - log.info('Installing update from GitHub fallback...'); + try { + if (isUsingGitHubFallback) { + log.info('Installing update from GitHub fallback...'); - const downloadPath = githubUpdateInfo.downloadPath; - if (!downloadPath) { - throw new Error('Update file path not found. Please download the update first.'); + const downloadPath = githubUpdateInfo.downloadPath; + if (!downloadPath) { + throw new Error('Update file path not found. Please download the update first.'); + } + + try { + await fs.access(downloadPath); + } catch { + throw new Error('Update file not found. Please download the update first.'); + } + + trackUpdateInstallInitiated( + githubUpdateInfo.latestVersion || 'unknown', + 'github-fallback', + 'auto_swap_and_relaunch' + ); + + const result = await githubUpdater.installUpdate(downloadPath); + if (!result.success) { + throw new Error(result.error || 'Failed to install update'); + } + + log.info('Quitting app so the update swap can complete...'); + setTimeout(() => app.quit(), 0); + } else { + // Use electron-updater's built-in install + trackUpdateInstallInitiated( + lastUpdateState?.latestVersion || 'unknown', + 'electron-updater', + 'quit_and_install' + ); + autoUpdater.quitAndInstall(false, true); } - - try { - await fs.access(downloadPath); - } catch { - throw new Error('Update file not found. Please download the update first.'); - } - - trackUpdateInstallInitiated( - githubUpdateInfo.latestVersion || 'unknown', - 'github-fallback', - 'auto_swap_and_relaunch' - ); - - const result = await githubUpdater.installUpdate(downloadPath); - if (!result.success) { - log.error('Error installing GitHub update:', result.error); - throw new Error(result.error || 'Failed to install update'); - } - - log.info('Quitting app so the update swap can complete...'); - setTimeout(() => app.quit(), 0); - } else { - // Use electron-updater's built-in install - trackUpdateInstallInitiated( - lastUpdateState?.latestVersion || 'unknown', - 'electron-updater', - 'quit_and_install' - ); - autoUpdater.quitAndInstall(false, true); + return { success: true, error: null }; + } catch (error) { + log.error('Error installing update:', error); + return { + success: false, + error: errorMessage(error, 'Unknown error'), + }; } });