122 lines
5.2 KiB
TypeScript
122 lines
5.2 KiB
TypeScript
// tests/main/services/update-service-status.test.ts
|
|
// 설정 정보 탭·타이틀바 칩이 보는 업데이트 상태(getStatus / status-changed)와 수동 다운로드·설치.
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import type { AppUpdateStatus } from '@d3ro/core/types'
|
|
import { DEFAULT_UPDATE_POLICY, type UpdatePolicy } from '../../../src/main/update-policy'
|
|
import type { UpdaterEventHandlers, UpdaterPort } from '../../../src/main/services/UpdateService'
|
|
|
|
vi.mock('../../../src/main/services/LoggerService', () => ({
|
|
getLogger: () => ({ info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }),
|
|
}))
|
|
|
|
const store = vi.hoisted(() => ({ values: {} as Record<string, unknown> }))
|
|
vi.mock('../../../src/main/services/ConfigService', () => ({
|
|
configGet: vi.fn((key: string) => store.values[key]),
|
|
configSet: vi.fn((key: string, value: unknown) => {
|
|
store.values[key] = value
|
|
}),
|
|
configGetAll: vi.fn(() => store.values),
|
|
}))
|
|
|
|
type ServiceModule = typeof import('../../../src/main/services/UpdateService')
|
|
let UpdateService: ServiceModule['UpdateService']
|
|
|
|
function harness(opts: { policy?: Partial<UpdatePolicy>; noUpdater?: boolean } = {}) {
|
|
let handlers: UpdaterEventHandlers | null = null
|
|
const updater: UpdaterPort = {
|
|
checkForUpdates: vi.fn(async () => undefined),
|
|
downloadUpdate: vi.fn(async () => undefined),
|
|
quitAndInstall: vi.fn(),
|
|
setChannel: vi.fn(),
|
|
setDifferentialDisabled: vi.fn(),
|
|
setAutoInstallOnQuit: vi.fn(),
|
|
}
|
|
const statuses: AppUpdateStatus[] = []
|
|
const service = new UpdateService({
|
|
createUpdater: (h) => {
|
|
handlers = h
|
|
return opts.noUpdater ? null : updater
|
|
},
|
|
policySource: { load: vi.fn(async () => ({ ...DEFAULT_UPDATE_POLICY, ...opts.policy })) },
|
|
prompter: { askConsent: vi.fn(async () => 'later' as const), askRestart: vi.fn(async () => 'later' as const) },
|
|
currentVersion: () => '1.10.0',
|
|
now: () => 5_000,
|
|
})
|
|
service.on('status-changed', (status) => statuses.push(status))
|
|
service.initialize()
|
|
service.dispose()
|
|
return { service, updater, statuses, handlers: () => handlers as UpdaterEventHandlers }
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
vi.resetModules()
|
|
store.values = { updateDeviceId: 'device-1' }
|
|
UpdateService = (await import('../../../src/main/services/UpdateService')).UpdateService
|
|
})
|
|
|
|
describe('UpdateService 상태', () => {
|
|
it('업데이터를 만들 수 없는 빌드는 disabled 로 보인다', () => {
|
|
const h = harness({ noUpdater: true })
|
|
expect(h.service.getStatus().phase).toBe('disabled')
|
|
})
|
|
|
|
it('확인 중 → 최신 버전, 마지막 확인 시각을 남긴다', () => {
|
|
const h = harness()
|
|
h.handlers().onChecking()
|
|
expect(h.service.getStatus().phase).toBe('checking')
|
|
h.handlers().onNotAvailable('1.10.0')
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'up-to-date', lastCheckedAt: 5_000, currentVersion: '1.10.0' })
|
|
})
|
|
|
|
it('새 버전 → 다운로드 → 진행률 → 설치 대기 → 재시작 설치', async () => {
|
|
const h = harness()
|
|
await h.service.handleUpdateAvailable({ version: '1.10.2' })
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'available', version: '1.10.2', isMandatory: false })
|
|
|
|
await h.service.startDownload()
|
|
expect(h.updater.downloadUpdate).toHaveBeenCalledTimes(1)
|
|
expect(h.service.getStatus().phase).toBe('downloading')
|
|
|
|
h.handlers().onProgress({ percent: 42.4, bytesPerSecond: 1, transferred: 42, total: 100 })
|
|
expect(h.service.getStatus().progress?.percent).toBe(42.4)
|
|
|
|
h.handlers().onDownloaded('1.10.2')
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'downloaded', version: '1.10.2', progress: null })
|
|
|
|
await h.service.installNow()
|
|
expect(h.updater.quitAndInstall).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('건너뛴 버전도 화면에 남기고, 직접 받기를 누르면 건너뛰기를 풀고 받는다', async () => {
|
|
store.values.skippedUpdateVersion = '1.10.2'
|
|
const h = harness()
|
|
await h.service.handleUpdateAvailable({ version: '1.10.2' })
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'available', skipped: true, version: '1.10.2' })
|
|
|
|
await h.service.startDownload()
|
|
expect(store.values.skippedUpdateVersion).toBeNull()
|
|
expect(h.updater.downloadUpdate).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('강제 설치 하한 아래면 묻지 않고 받는 중으로 바뀐다', async () => {
|
|
const h = harness({ policy: { forceInstallBelow: '1.10.1' } })
|
|
await h.service.checkForUpdates()
|
|
await h.service.handleUpdateAvailable({ version: '1.10.2' })
|
|
await vi.waitFor(() => expect(h.updater.downloadUpdate).toHaveBeenCalledTimes(1))
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'downloading', isMandatory: true })
|
|
})
|
|
|
|
it('받기 전이면 설치 요청은 아무것도 하지 않는다', async () => {
|
|
const h = harness()
|
|
await h.service.installNow()
|
|
expect(h.updater.quitAndInstall).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('오류는 메시지와 함께 error 단계로, 상태가 바뀔 때마다 알린다', () => {
|
|
const h = harness()
|
|
h.handlers().onError('net::ERR_INTERNET_DISCONNECTED')
|
|
expect(h.service.getStatus()).toMatchObject({ phase: 'error', error: 'net::ERR_INTERNET_DISCONNECTED' })
|
|
expect(h.statuses.at(-1)?.phase).toBe('error')
|
|
})
|
|
})
|