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
5 changes: 5 additions & 0 deletions .changeset/fix-desktop-update-restart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pymodel/pythinker-code": patch
---

Fix desktop update prompts so one action downloads, closes, installs, and restarts the app.
3 changes: 3 additions & 0 deletions apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { isAbsolute, join, resolve } from 'node:path'
import {
app,
autoUpdater,
BrowserWindow,
dialog,
ipcMain,
Expand Down Expand Up @@ -401,6 +402,8 @@ async function boot(): Promise<void> {
if (!app.requestSingleInstanceLock()) {
app.quit()
} else {
// Update-triggered quits close windows before app.before-quit, so start teardown here.
autoUpdater.on('before-quit-for-update', () => { void requestAppQuit() })
app.on('second-instance', showWindowSafely)
app.on('activate', showWindowSafely)
app.on('window-all-closed', () => {
Expand Down
21 changes: 20 additions & 1 deletion apps/desktop/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let initialCheckTimer: ReturnType<typeof setTimeout> | undefined
let checkInterval: ReturnType<typeof setInterval> | undefined
let listenersWired = false
let initialized = false
let installWhenDownloaded = false
let updateTelemetryTrack: UpdateTelemetryTrack = () => {}

export function trackUpdateTransition(
Expand Down Expand Up @@ -92,6 +93,7 @@ function emitUpdateTelemetry(previous: UpdateState, next: UpdateState): void {
}

function stateError(error: unknown): void {
installWhenDownloaded = false
updateState({
status: 'error',
message: error instanceof Error ? error.message : String(error),
Expand Down Expand Up @@ -166,6 +168,7 @@ function wireUpdaterEvents(): void {
})
autoUpdater.on('update-downloaded', (info) => {
updateState({ status: 'downloaded', version: info.version, percent: 100, message: undefined })
if (installWhenDownloaded) installDownloadedUpdate()
})
autoUpdater.on('error', stateError)
listenersWired = true
Expand Down Expand Up @@ -278,10 +281,26 @@ export function quitAndInstallNow(): UpdateState {
} catch {
// Telemetry must never delay update installation.
}
if (state.status === 'available') {
installWhenDownloaded = true
updateState({ status: 'downloading', percent: 0, message: undefined })
try {
void autoUpdater.downloadUpdate().catch(stateError)
} catch (error) {
stateError(error)
}
return state
}
if (state.status !== 'downloaded') return state
installDownloadedUpdate()
return state
}

function installDownloadedUpdate(): void {
installWhenDownloaded = false
try {
autoUpdater.quitAndInstall()
} catch (error) {
stateError(error)
}
return state
}
31 changes: 31 additions & 0 deletions apps/desktop/tests/updater.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vi.mock('electron-updater', () => ({
autoUpdater: {
on: vi.fn(),
checkForUpdates: vi.fn(),
downloadUpdate: vi.fn(() => Promise.resolve([])),
quitAndInstall: vi.fn(),
},
},
Expand Down Expand Up @@ -118,3 +119,33 @@ describe('packaged builds without update metadata', () => {
expect(app.once).not.toHaveBeenCalled()
})
})

describe('installing an update', () => {
it('downloads an available update and installs it when the download completes', async () => {
vi.resetModules()
const directory = temporaryDirectory()
writeFileSync(join(directory, 'app-update.yml'), '', 'utf8')
const { app: localApp } = await import('electron')
const { default: localElectronUpdater } = await import('electron-updater')
const {
initUpdater: initLocalUpdater,
quitAndInstallNow,
} = await import('../src/updater')
const localAutoUpdater = localElectronUpdater.autoUpdater
vi.mocked(localApp.getPath).mockReturnValue(directory)
Object.defineProperty(localApp, 'isPackaged', { configurable: true, value: true })
Object.defineProperty(process, 'resourcesPath', { configurable: true, value: directory })

initLocalUpdater(() => undefined)
const available = vi.mocked(localAutoUpdater.on).mock.calls.find(([event]) => event === 'update-available')?.[1] as ((info: { version: string }) => void) | undefined
const downloaded = vi.mocked(localAutoUpdater.on).mock.calls.find(([event]) => event === 'update-downloaded')?.[1] as ((info: { version: string }) => void) | undefined
available?.({ version: '1.2.3' })

expect(quitAndInstallNow()).toMatchObject({ status: 'downloading' })
expect(localAutoUpdater.downloadUpdate).toHaveBeenCalledOnce()
expect(localAutoUpdater.quitAndInstall).not.toHaveBeenCalled()

downloaded?.({ version: '1.2.3' })
expect(localAutoUpdater.quitAndInstall).toHaveBeenCalledOnce()
})
})
9 changes: 2 additions & 7 deletions apps/pythinker-web/src/components/UpdateToast.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ const title = computed(() =>
: t('update.available'),
);

const primaryLabel = computed(() =>
state.value?.status === 'downloaded' ? t('settings.desktop.restartToUpdate') : t('update.download'),
);

async function primary(): Promise<void> {
if (bridge === undefined || busy.value) return;
busy.value = true;
try {
state.value =
state.value?.status === 'downloaded' ? await bridge.quitAndInstall() : await bridge.checkForUpdates();
state.value = await bridge.quitAndInstall();
} finally {
busy.value = false;
}
Expand Down Expand Up @@ -97,7 +92,7 @@ onUnmounted(() => {
</div>
<div class="acts">
<button type="button" class="skip" @click="skip">{{ t('update.skip') }}</button>
<button type="button" class="go" :disabled="busy" @click="void primary()">{{ primaryLabel }}</button>
<button type="button" class="go" :disabled="busy" @click="void primary()">{{ t('update.install') }}</button>
</div>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion apps/pythinker-web/src/i18n/locales/en/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export default {
available: 'A new version is available',
availableVersion: 'Version {version} is available',
prompt: 'Install it now, or skip this version.',
download: 'Download update',
install: 'Update',
skip: 'Skip',
} as const;
8 changes: 4 additions & 4 deletions apps/pythinker-web/test/update-toast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ describe('UpdateToast', () => {
const bridge = installBridge({ status: 'downloaded', version: '1.2.3', autoUpdate: true });
const wrapper = await mountToast();
expect(wrapper.get('.title').text()).toContain('1.2.3');
expect(wrapper.get('.go').text()).toBe(enSettings.desktop.restartToUpdate);
expect(wrapper.get('.go').text()).toBe(enUpdate.install);
await wrapper.get('.go').trigger('click');
expect(bridge.quitAndInstall).toHaveBeenCalledTimes(1);
});

it('offers a manual download when automatic updates are off', async () => {
it('starts the complete update flow when automatic downloads are off', async () => {
const bridge = installBridge({ status: 'available', version: '1.2.3', autoUpdate: false });
const wrapper = await mountToast();
expect(wrapper.get('.go').text()).toBe(enUpdate.download);
expect(wrapper.get('.go').text()).toBe(enUpdate.install);
await wrapper.get('.go').trigger('click');
expect(bridge.checkForUpdates).toHaveBeenCalledTimes(1);
expect(bridge.quitAndInstall).toHaveBeenCalledTimes(1);
});

it('hides the prompt and remembers the skipped version', async () => {
Expand Down
4 changes: 4 additions & 0 deletions docs/guides/desktop.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ The desktop application updates itself. Open **Settings** in the application to
- **Check for updates** — check immediately.
- **Restart to update** — appears when an update is downloaded and ready to install.

When a new version is available, the application also shows an update prompt. Choose **Update** to
download the version if needed, close the application, install it, and restart. Choose **Skip** to
ignore that version.

The update controls apply to installed builds only. A development build shows them as unavailable.

## The local Host port
Expand Down
Loading