Merge branch 'fix/win-updater-unable-to-close' into develop

This commit is contained in:
Wxw-Gu committed 2026-11-20 14:24:19 +08:00
commit bc3a560830
15 files changed
+801 -102

No files matched your search

+49
View File
@@ -179,4 +179,53 @@ describe('AppUpdateService', () => {
expect(service.install()).toEqual({ success: true })
expect(mocks.updater.quitAndInstall).toHaveBeenCalledOnce()
})
it('waits for beforeQuitAndInstall cleanup before invoking quitAndInstall', async () => {
let releaseCleanup!: () => void
const cleanupPromise = new Promise<void>((resolve) => {
releaseCleanup = resolve
})
const beforeQuitAndInstall = vi.fn(() => cleanupPromise)
const service = new AppUpdateService({
platform: 'win32',
packagedRuntime: () => true,
currentVersion: () => '2.7.0',
beforeQuitAndInstall
})
mocks.updater.downloadUpdate.mockImplementationOnce(async () => {
mocks.emit('update-downloaded', { version: '2.7.1' })
})
await service.download()
expect(service.install()).toEqual({ success: true })
await Promise.resolve()
expect(beforeQuitAndInstall).toHaveBeenCalledOnce()
expect(mocks.updater.quitAndInstall).not.toHaveBeenCalled()
expect(service.getState().status).toBe('installing')
releaseCleanup()
await cleanupPromise
await new Promise((resolve) => setTimeout(resolve, 0))
expect(mocks.updater.quitAndInstall).toHaveBeenCalledOnce()
})
it('still invokes quitAndInstall when beforeQuitAndInstall rejects', async () => {
const service = new AppUpdateService({
platform: 'win32',
packagedRuntime: () => true,
currentVersion: () => '2.7.0',
beforeQuitAndInstall: async () => {
throw new Error('cleanup failed')
}
})
mocks.updater.downloadUpdate.mockImplementationOnce(async () => {
mocks.emit('update-downloaded', { version: '2.7.1' })
})
await service.download()
expect(service.install()).toEqual({ success: true })
await new Promise((resolve) => setTimeout(resolve, 0))
expect(mocks.updater.quitAndInstall).toHaveBeenCalledOnce()
})
})
+70
View File
@@ -0,0 +1,70 @@
import { EventEmitter } from 'node:events'
import { describe, expect, it, vi } from 'vitest'
import { terminateChildProcess, waitForChildExit } from '../../src/main/process-tree'
class FakeChildProcess extends EventEmitter {
pid: number | undefined = 4242
killed = false
exitCode: number | null = null
signalCode: NodeJS.Signals | null = null
kill = vi.fn((signal?: NodeJS.Signals | number) => {
this.killed = true
this.signalCode = typeof signal === 'string' ? signal : 'SIGTERM'
queueMicrotask(() => this.emit('exit', null, this.signalCode))
return true
})
}
describe('process-tree', () => {
it('waitForChildExit resolves immediately for already exited process', async () => {
const child = new FakeChildProcess()
child.exitCode = 0
await expect(waitForChildExit(child, 50)).resolves.toBe(true)
})
it('waitForChildExit waits for exit event and returns false on timeout', async () => {
const child = new FakeChildProcess()
const pending = waitForChildExit(child, 300)
setTimeout(() => {
child.exitCode = 0
child.emit('exit', 0, null)
}, 10)
await expect(pending).resolves.toBe(true)
const hanging = new FakeChildProcess()
await expect(waitForChildExit(hanging, 30)).resolves.toBe(false)
})
it('terminateChildProcess falls back to child.kill when killTree returns fallback', async () => {
const child = new FakeChildProcess()
const killTree = vi.fn(async () => 'fallback' as const)
const exited = await terminateChildProcess(child, {
timeoutMs: 300,
killTree
})
expect(exited).toBe(true)
expect(killTree).toHaveBeenCalledOnce()
expect(child.kill).toHaveBeenCalledOnce()
})
it('terminateChildProcess waits for exit after killTree succeeds', async () => {
const child = new FakeChildProcess()
const killTree = vi.fn(async () => {
setTimeout(() => {
child.exitCode = 0
child.emit('exit', 0, null)
}, 15)
return 'taskkill' as const
})
const exited = await terminateChildProcess(child, {
timeoutMs: 300,
killTree
})
expect(exited).toBe(true)
expect(child.kill).not.toHaveBeenCalled()
})
})
+30
View File
@@ -0,0 +1,30 @@
import { existsSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
const ROOT = path.resolve(__dirname, '../..')
describe('Windows NSIS installer configuration', () => {
it('wires build/installer.nsh in electron-builder configs', () => {
const winConfig = readFileSync(path.join(ROOT, 'electron-builder.win.yml'), 'utf8')
const baseConfig = readFileSync(path.join(ROOT, 'electron-builder.yml'), 'utf8')
expect(winConfig).toContain('include: build/installer.nsh')
expect(baseConfig).toContain('include: build/installer.nsh')
expect(existsSync(path.join(ROOT, 'build', 'installer.nsh'))).toBe(true)
})
it('kills process trees and handles cross-drive updates in build/installer.nsh', () => {
const script = readFileSync(path.join(ROOT, 'build', 'installer.nsh'), 'utf8')
expect(script).toContain('!macro customCheckAppRunning')
expect(script).toContain('taskkill /f /t /im')
expect(script).toContain('find.exe" /I')
expect(script).toContain('Function tmPrepareCrossDriveUpdateTemp')
expect(script).toContain('Function tmRestoreCrossDriveUpdateTemp')
expect(script).toContain('!macro customInstall')
expect(script).toContain('!macro customRemoveFiles')
expect(script).toContain('un.tmAtomicMoveToBackup')
expect(script).toContain('un.tmRestoreFromBackup')
})
})