fix(desktop): show why an update install failed (#12628)
Signed-off-by: Seydi Charyyev <seydi.charyev@gmail.com>
This commit is contained in:
@@ -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<typeof window.electron.onUpdaterEvent>[0];
|
||||
|
||||
let sendUpdaterEvent: UpdaterCallback;
|
||||
|
||||
const renderDownloadedUpdate = async () => {
|
||||
render(<UpdateSection />, { 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<never>(() => {}));
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -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 <Loader2 className="w-4 h-4 animate-spin" />;
|
||||
case 'success':
|
||||
return <CheckCircle className="w-4 h-4 text-green-500" />;
|
||||
@@ -344,8 +359,13 @@ export default function UpdateSection() {
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{updateStatus === 'ready' && (
|
||||
<Button onClick={installUpdate} variant="default" size="sm">
|
||||
{(updateStatus === 'ready' || updateStatus === 'installing') && (
|
||||
<Button
|
||||
onClick={installUpdate}
|
||||
disabled={updateStatus === 'installing'}
|
||||
variant="default"
|
||||
size="sm"
|
||||
>
|
||||
{intl.formatMessage(i18n.installAndRestart)}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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'),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user