You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
4.7 KiB
123 lines
4.7 KiB
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { EventEmitter } from 'node:events'
|
|
import type { autoUpdater } from 'electron-updater'
|
|
vi.mock('electron-updater', () => ({ autoUpdater: {} }))
|
|
import { UpdateService } from '@template/desktop/updates'
|
|
const services: UpdateService[] = []
|
|
afterEach(() => {
|
|
for (const service of services.splice(0)) service.dispose()
|
|
vi.useRealTimers()
|
|
})
|
|
function fixture(options: Partial<ConstructorParameters<typeof UpdateService>[4]> = {}) {
|
|
const backend = Object.assign(new EventEmitter(), {
|
|
setFeedURL: vi.fn(),
|
|
checkForUpdates: vi.fn(),
|
|
downloadUpdate: vi.fn(),
|
|
quitAndInstall: vi.fn(),
|
|
autoDownload: true,
|
|
autoInstallOnAppQuit: true,
|
|
allowDowngrade: true,
|
|
allowPrerelease: true,
|
|
disableWebInstaller: false,
|
|
logger: {}
|
|
})
|
|
const cancel = vi.fn()
|
|
backend.checkForUpdates.mockImplementation(async () => {
|
|
backend.emit('checking-for-update')
|
|
backend.emit('update-available', { version: '0.2.0' })
|
|
return { cancellationToken: { cancel } }
|
|
})
|
|
const changed = vi.fn()
|
|
const failure = vi.fn()
|
|
const service = new UpdateService('https://releases.example.com/stable', true, changed, failure, {
|
|
...options,
|
|
backend: backend as unknown as typeof autoUpdater
|
|
})
|
|
services.push(service)
|
|
return { service, backend, cancel, changed, failure }
|
|
}
|
|
describe('release update lifecycle', () => {
|
|
it('handles installer errors even after application resource disposal', async () => {
|
|
const failed = vi.fn()
|
|
const { service, backend, failure } = fixture({ onInstallFailure: failed })
|
|
await service.check()
|
|
backend.emit('update-downloaded', { version: '0.2.0' })
|
|
backend.quitAndInstall.mockImplementation(() => {
|
|
service.dispose()
|
|
backend.emit('error', new Error('installer failed'))
|
|
})
|
|
await service.install()
|
|
expect(failed).toHaveBeenCalledOnce()
|
|
expect(failure).toHaveBeenCalledOnce()
|
|
})
|
|
it('checks at startup and periodically without auto-downloading; removes timers and listeners', async () => {
|
|
vi.useFakeTimers()
|
|
const { service, backend } = fixture({ checkIntervalMinutes: 15 })
|
|
expect(backend).toMatchObject({
|
|
autoDownload: false,
|
|
autoInstallOnAppQuit: false,
|
|
allowDowngrade: false,
|
|
allowPrerelease: false,
|
|
disableWebInstaller: true,
|
|
logger: null
|
|
})
|
|
service.start()
|
|
service.start()
|
|
await vi.advanceTimersByTimeAsync(10000)
|
|
expect(backend.checkForUpdates).toHaveBeenCalledTimes(1)
|
|
await vi.advanceTimersByTimeAsync(890000)
|
|
expect(backend.checkForUpdates).toHaveBeenCalledTimes(2)
|
|
expect(backend.downloadUpdate).not.toHaveBeenCalled()
|
|
service.dispose()
|
|
await vi.advanceTimersByTimeAsync(900000)
|
|
expect(backend.checkForUpdates).toHaveBeenCalledTimes(2)
|
|
expect(backend.eventNames()).toHaveLength(0)
|
|
})
|
|
it('keeps a failed download observable and can retry after a fresh check', async () => {
|
|
const { service, backend, failure } = fixture()
|
|
await service.check()
|
|
backend.downloadUpdate.mockRejectedValueOnce(new Error('private URL failed'))
|
|
await service.download()
|
|
expect(service.get()).toMatchObject({ status: 'error' })
|
|
expect(service.get().message).not.toContain('private URL')
|
|
expect(failure).toHaveBeenCalled()
|
|
await service.check()
|
|
backend.downloadUpdate.mockImplementationOnce(async () => {
|
|
backend.emit('update-downloaded', { version: '0.2.0' })
|
|
})
|
|
await service.download()
|
|
expect(service.get()).toMatchObject({ status: 'ready', version: '0.2.0' })
|
|
})
|
|
it('cancels a download without turning cancellation into a failure or late ready state', async () => {
|
|
let resolve!: () => void
|
|
const pending = new Promise<void>((yes) => {
|
|
resolve = yes
|
|
})
|
|
const { service, backend, cancel } = fixture()
|
|
await service.check()
|
|
backend.downloadUpdate.mockReturnValue(pending)
|
|
const download = service.download()
|
|
await expect(service.check()).rejects.toThrow('active')
|
|
service.cancel()
|
|
backend.emit('update-downloaded', { version: '0.2.0' })
|
|
resolve()
|
|
await download
|
|
expect(cancel).toHaveBeenCalledOnce()
|
|
expect(service.get().status).toBe('cancelled')
|
|
})
|
|
it('waits for application cleanup before invoking the installer', async () => {
|
|
let resolve!: () => void
|
|
const pending = new Promise<void>((yes) => {
|
|
resolve = yes
|
|
})
|
|
const { service, backend } = fixture({ beforeInstall: () => pending })
|
|
await service.check()
|
|
backend.emit('update-downloaded', { version: '0.2.0' })
|
|
const install = service.install()
|
|
expect(service.get().status).toBe('installing')
|
|
expect(backend.quitAndInstall).not.toHaveBeenCalled()
|
|
resolve()
|
|
await install
|
|
expect(backend.quitAndInstall).toHaveBeenCalledWith(false, true)
|
|
})
|
|
})
|
|
|