|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { |
| 4 | + launchWindowsUpdateInstaller, |
| 5 | + UPDATE_INSTALLER_ARGS, |
| 6 | + type SpawnInstaller, |
| 7 | + type SpawnProcess |
| 8 | +} from './update-installer-launcher'; |
| 9 | + |
| 10 | +class FakeSpawnProcess implements SpawnProcess { |
| 11 | + pid = 1234; |
| 12 | + readonly unref = vi.fn(); |
| 13 | + private readonly errorListeners: Array<(error: Error) => void> = []; |
| 14 | + private readonly exitListeners: Array<(code: number | null, signal: NodeJS.Signals | null) => void> = []; |
| 15 | + |
| 16 | + once(event: 'error', listener: (error: Error) => void): void; |
| 17 | + once(event: 'exit', listener: (code: number | null, signal: NodeJS.Signals | null) => void): void; |
| 18 | + once( |
| 19 | + event: 'error' | 'exit', |
| 20 | + listener: ((error: Error) => void) | ((code: number | null, signal: NodeJS.Signals | null) => void) |
| 21 | + ): void { |
| 22 | + if (event === 'error') { |
| 23 | + this.errorListeners.push(listener as (error: Error) => void); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + this.exitListeners.push(listener as (code: number | null, signal: NodeJS.Signals | null) => void); |
| 28 | + } |
| 29 | + |
| 30 | + emitError(error = new Error('failed')): void { |
| 31 | + for (const listener of this.errorListeners) { |
| 32 | + listener(error); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + emitExit(code: number | null): void { |
| 37 | + for (const listener of this.exitListeners) { |
| 38 | + listener(code, null); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +describe('launchWindowsUpdateInstaller', () => { |
| 44 | + afterEach(() => { |
| 45 | + vi.useRealTimers(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns installer_unavailable when installer path is null', async () => { |
| 49 | + await expect(launch({ installerPath: null })).resolves.toEqual({ |
| 50 | + ok: false, |
| 51 | + reason: 'installer_unavailable' |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns installer_unavailable when installer file is missing', async () => { |
| 56 | + await expect( |
| 57 | + launch({ |
| 58 | + fileExists: (path) => path.endsWith('elevate.exe') |
| 59 | + }) |
| 60 | + ).resolves.toEqual({ ok: false, reason: 'installer_unavailable' }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns elevation_helper_unavailable when elevate.exe is missing', async () => { |
| 64 | + await expect( |
| 65 | + launch({ |
| 66 | + fileExists: (path) => path.endsWith('installer.exe') |
| 67 | + }) |
| 68 | + ).resolves.toEqual({ ok: false, reason: 'elevation_helper_unavailable' }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('spawns elevate.exe with installer arguments', async () => { |
| 72 | + vi.useFakeTimers(); |
| 73 | + const child = new FakeSpawnProcess(); |
| 74 | + const spawnInstaller = vi.fn<SpawnInstaller>(() => child); |
| 75 | + |
| 76 | + const resultPromise = launch({ child, spawnInstaller, settleMs: 10 }); |
| 77 | + await vi.advanceTimersByTimeAsync(10); |
| 78 | + |
| 79 | + await expect(resultPromise).resolves.toEqual({ ok: true, pid: 1234 }); |
| 80 | + expect(spawnInstaller).toHaveBeenCalledWith( |
| 81 | + join('resources', 'elevate.exe'), |
| 82 | + [join('cache', 'installer.exe'), ...UPDATE_INSTALLER_ARGS], |
| 83 | + { |
| 84 | + detached: true, |
| 85 | + stdio: 'ignore', |
| 86 | + windowsHide: false |
| 87 | + } |
| 88 | + ); |
| 89 | + expect(child.unref).toHaveBeenCalledTimes(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns installer_launch_failed if spawn throws', async () => { |
| 93 | + await expect( |
| 94 | + launch({ |
| 95 | + spawnInstaller: () => { |
| 96 | + throw new Error('spawn failed'); |
| 97 | + } |
| 98 | + }) |
| 99 | + ).resolves.toEqual({ ok: false, reason: 'installer_launch_failed' }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns installer_launch_failed if the child emits error before settling', async () => { |
| 103 | + vi.useFakeTimers(); |
| 104 | + const child = new FakeSpawnProcess(); |
| 105 | + const resultPromise = launch({ child, settleMs: 10 }); |
| 106 | + |
| 107 | + child.emitError(); |
| 108 | + |
| 109 | + await expect(resultPromise).resolves.toEqual({ ok: false, reason: 'installer_launch_failed' }); |
| 110 | + expect(child.unref).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns installer_launch_failed if the child exits non-zero before settling', async () => { |
| 114 | + vi.useFakeTimers(); |
| 115 | + const child = new FakeSpawnProcess(); |
| 116 | + const resultPromise = launch({ child, settleMs: 10 }); |
| 117 | + |
| 118 | + child.emitExit(1); |
| 119 | + |
| 120 | + await expect(resultPromise).resolves.toEqual({ ok: false, reason: 'installer_launch_failed' }); |
| 121 | + expect(child.unref).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns success if the child exits cleanly before settling', async () => { |
| 125 | + vi.useFakeTimers(); |
| 126 | + const child = new FakeSpawnProcess(); |
| 127 | + const resultPromise = launch({ child, settleMs: 10 }); |
| 128 | + |
| 129 | + child.emitExit(0); |
| 130 | + |
| 131 | + await expect(resultPromise).resolves.toEqual({ ok: true, pid: 1234 }); |
| 132 | + expect(child.unref).toHaveBeenCalledTimes(1); |
| 133 | + }); |
| 134 | + |
| 135 | + it('returns success if the child survives the settle period', async () => { |
| 136 | + vi.useFakeTimers(); |
| 137 | + const child = new FakeSpawnProcess(); |
| 138 | + const resultPromise = launch({ child, settleMs: 10 }); |
| 139 | + |
| 140 | + await vi.advanceTimersByTimeAsync(10); |
| 141 | + |
| 142 | + await expect(resultPromise).resolves.toEqual({ ok: true, pid: 1234 }); |
| 143 | + expect(child.unref).toHaveBeenCalledTimes(1); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +function launch({ |
| 148 | + installerPath = join('cache', 'installer.exe'), |
| 149 | + resourcesPath = 'resources', |
| 150 | + settleMs = 0, |
| 151 | + child = new FakeSpawnProcess(), |
| 152 | + spawnInstaller = (() => child) as SpawnInstaller, |
| 153 | + fileExists = () => true |
| 154 | +}: { |
| 155 | + installerPath?: string | null; |
| 156 | + resourcesPath?: string; |
| 157 | + settleMs?: number; |
| 158 | + child?: FakeSpawnProcess; |
| 159 | + spawnInstaller?: SpawnInstaller; |
| 160 | + fileExists?: (path: string) => boolean; |
| 161 | +} = {}): Promise<ReturnType<typeof launchWindowsUpdateInstaller> extends Promise<infer Result> ? Result : never> { |
| 162 | + return launchWindowsUpdateInstaller({ |
| 163 | + installerPath, |
| 164 | + resourcesPath, |
| 165 | + settleMs, |
| 166 | + spawnInstaller, |
| 167 | + fileExists |
| 168 | + }); |
| 169 | +} |
0 commit comments