Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/app/pages/client/DesktopUpdater.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ function makeUpdate(version: string) {
return {
version,
body: `changelog ${version}`,
download: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
install: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
downloadAndInstall: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
close: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
};
}
Expand Down Expand Up @@ -132,4 +131,56 @@ describe('DesktopUpdater', () => {
await waitFor(() => expect(update1.close).toHaveBeenCalledTimes(1));
expect(update2.close).not.toHaveBeenCalled();
});

it('downloads then installs and prompts to restart', async () => {
const update = makeUpdate('3.0.0');
checkFn.mockResolvedValue(update);

render(
<Provider>
<DesktopUpdatePill />
<DesktopUpdater />
<BannersProbe />
</Provider>
);

const pill = await screen.findByRole('button', { name: 'Update Available' });
fireEvent.click(pill);
await screen.findByTestId('banner-desktop-update-ready');

fireEvent.click(screen.getByRole('button', { name: 'Download & Install' }));
await waitFor(() => expect(update.downloadAndInstall).toHaveBeenCalled());
await screen.findByTestId('banner-desktop-update-restart');
expect(screen.getByRole('button', { name: 'Restart Now' })).toBeInTheDocument();
});

it('retries when downloadAndInstall fails', async () => {
const update = makeUpdate('4.0.0');
update.downloadAndInstall
.mockRejectedValueOnce(new Error('eacces'))
.mockResolvedValueOnce(undefined);
checkFn.mockResolvedValue(update);

render(
<Provider>
<DesktopUpdatePill />
<DesktopUpdater />
<BannersProbe />
</Provider>
);

const pill = await screen.findByRole('button', { name: 'Update Available' });
fireEvent.click(pill);
await screen.findByTestId('banner-desktop-update-ready');

fireEvent.click(screen.getByRole('button', { name: 'Download & Install' }));
await waitFor(() => expect(update.downloadAndInstall).toHaveBeenCalledTimes(1));
await waitFor(() => {
expect(screen.getByRole('button', { name: 'Download & Install' })).toBeInTheDocument();
});

fireEvent.click(screen.getByRole('button', { name: 'Download & Install' }));
await waitFor(() => expect(update.downloadAndInstall).toHaveBeenCalledTimes(2));
await screen.findByTestId('banner-desktop-update-restart');
});
});
27 changes: 8 additions & 19 deletions src/app/pages/client/DesktopUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ export function DesktopUpdater() {
const [bannerVisible, setBannerVisible] = useAtom(updateBannerVisibleAtom);
const setLastChecked = useSetAtom(desktopUpdateLastCheckedAtom);
const [updateInfo, setUpdateInfo] = useState<Update | null>(null);
const [isDownloaded, setIsDownloaded] = useState(false);
const [isDownloading, setIsDownloading] = useState(false);
const [isInstalled, setIsInstalled] = useState(false);
const [isInstalling, setIsInstalling] = useState(false);
const [dismissed, setDismissed] = useState(false);
const [useCustomTitleBar] = useDesktopSetting('useCustomTitleBar');
const hasUpdateRef = useRef(false);
const runningRef = useRef(false);
const downloadedRef = useRef(false);
const pendingUpdateRef = useRef<Update | null>(null);
const installStartedRef = useRef(false);

Expand All @@ -49,18 +47,17 @@ export function DesktopUpdater() {

let mounted = true;

if (!downloadedRef.current) {
if (!installStartedRef.current) {
setPhase({ type: 'checking' });
}

async function run() {
if (runningRef.current || downloadedRef.current || installStartedRef.current) return;
if (runningRef.current || installStartedRef.current) return;
runningRef.current = true;
try {
if (fakeDesktopUpdate()) {
log.log('Fake update: simulating available update');
setUpdateInfo({ version: '9.9.9', body: 'Fake changelog for testing.' } as Update);
setIsDownloaded(false);
setPhase({ type: 'ready', version: '9.9.9' });
setLastChecked(new Date().toISOString());
if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true);
Expand All @@ -78,7 +75,6 @@ export function DesktopUpdater() {
closePendingUpdate(pendingUpdateRef.current);
pendingUpdateRef.current = null;
setUpdateInfo(null);
setIsDownloaded(false);
hasUpdateRef.current = false;
setPhase({ type: 'idle' });
setLastChecked(new Date().toISOString());
Expand All @@ -89,7 +85,6 @@ export function DesktopUpdater() {
pendingUpdateRef.current = update;
log.log(`Desktop update ${update.version} available`);
setUpdateInfo(update);
setIsDownloaded(false);
setIsInstalled(false);
setDismissed(false);
hasUpdateRef.current = true;
Expand Down Expand Up @@ -150,19 +145,19 @@ export function DesktopUpdater() {
if (!updateInfo || installStartedRef.current) return;
installStartedRef.current = true;
try {
setIsDownloading(!isDownloaded);
if (!isDownloaded) setPhase({ type: 'downloading', progress: 0 });
setIsDownloading(true);
setPhase({ type: 'downloading', progress: 0 });

if (fakeDesktopUpdate()) {
for (let pct = 0; pct <= 100; pct += 2) {
// eslint-disable-next-line no-await-in-loop
await new Promise((r) => setTimeout(r, 40));
setPhase({ type: 'downloading', progress: pct });
}
} else if (!isDownloaded) {
} else {
let downloadedBytes = 0;
let contentLength = 0;
await updateInfo.download((event) => {
await updateInfo.downloadAndInstall((event) => {
if (event.event === 'Started') {
contentLength = event.data.contentLength ?? 0;
} else if (event.event === 'Progress') {
Expand All @@ -174,12 +169,9 @@ export function DesktopUpdater() {
}

setIsDownloading(false);
setIsDownloaded(true);
downloadedRef.current = true;
setIsInstalling(true);
setPhase({ type: 'installing' });
if (fakeDesktopUpdate()) await new Promise((r) => setTimeout(r, 1500));
else await updateInfo.install();

setPhase({ type: 'ready', version: updateInfo.version });
setIsInstalling(false);
Expand All @@ -192,7 +184,7 @@ export function DesktopUpdater() {
setIsInstalling(false);
installStartedRef.current = false;
}
}, [closePendingUpdate, isDownloaded, updateInfo, setPhase]);
}, [closePendingUpdate, updateInfo, setPhase]);

const handleRestart = useCallback(async () => {
try {
Expand Down Expand Up @@ -242,9 +234,7 @@ export function DesktopUpdater() {
? 'Downloading...'
: isInstalling
? 'Installing...'
: isDownloaded
? 'Install & Update'
: 'Download & Install',
: 'Download & Install',
variant: 'Primary',
onClick: handleInstall,
},
Expand All @@ -258,7 +248,6 @@ export function DesktopUpdater() {
bannerVisible,
updateInfo,
dismissed,
isDownloaded,
isDownloading,
isInstalled,
isInstalling,
Expand Down
Loading