|
| 1 | +/** |
| 2 | + * @file src/main/clients/__tests__/client-install.service.test.ts |
| 3 | + * |
| 4 | + * @description Unit tests for ClientInstallService fallback chains and |
| 5 | + * failure classifications. |
| 6 | + */ |
| 7 | + |
| 8 | +import { EventEmitter } from 'events' |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +type Platform = typeof process.platform |
| 12 | + |
| 13 | +const withMockPlatform = (platform: Platform): void => { |
| 14 | + Object.defineProperty(process, 'platform', { |
| 15 | + configurable: true, |
| 16 | + value: platform, |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +interface SpawnPlan { |
| 21 | + readonly command: string |
| 22 | + readonly exitCode?: number |
| 23 | + readonly stdout?: string |
| 24 | + readonly stderr?: string |
| 25 | + readonly error?: string |
| 26 | +} |
| 27 | + |
| 28 | +const spawnPlanQueue = vi.hoisted(() => [] as SpawnPlan[]) |
| 29 | +const managerAvailability = vi.hoisted( |
| 30 | + () => ({ winget: true, choco: true, npm: true }) as Record<string, boolean>, |
| 31 | +) |
| 32 | + |
| 33 | +const spawnMock = vi.hoisted(() => |
| 34 | + vi.fn((command: string) => { |
| 35 | + const plan = spawnPlanQueue.shift() |
| 36 | + const effectivePlan: SpawnPlan = plan ?? { command, exitCode: 0 } |
| 37 | + |
| 38 | + const child = new EventEmitter() as EventEmitter & { |
| 39 | + stdout: EventEmitter |
| 40 | + stderr: EventEmitter |
| 41 | + } |
| 42 | + child.stdout = new EventEmitter() |
| 43 | + child.stderr = new EventEmitter() |
| 44 | + |
| 45 | + queueMicrotask(() => { |
| 46 | + if (effectivePlan.stdout) child.stdout.emit('data', effectivePlan.stdout) |
| 47 | + if (effectivePlan.stderr) child.stderr.emit('data', effectivePlan.stderr) |
| 48 | + if (effectivePlan.error) { |
| 49 | + child.emit('error', new Error(effectivePlan.error)) |
| 50 | + } else { |
| 51 | + child.emit('close', effectivePlan.exitCode ?? 0) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + return child |
| 56 | + }), |
| 57 | +) |
| 58 | + |
| 59 | +vi.mock('cross-spawn', () => ({ |
| 60 | + default: spawnMock, |
| 61 | +})) |
| 62 | + |
| 63 | +vi.mock('../windows-detection.util', () => ({ |
| 64 | + hasWindowsCommandOnPath: (commandNames: readonly string[]) => { |
| 65 | + const key = commandNames[0] |
| 66 | + if (!key) return false |
| 67 | + return managerAvailability[key] ?? false |
| 68 | + }, |
| 69 | +})) |
| 70 | + |
| 71 | +import { ClientInstallService } from '../client-install.service' |
| 72 | + |
| 73 | +describe('ClientInstallService', () => { |
| 74 | + beforeEach(() => { |
| 75 | + withMockPlatform('win32') |
| 76 | + spawnMock.mockClear() |
| 77 | + spawnPlanQueue.length = 0 |
| 78 | + managerAvailability.winget = true |
| 79 | + managerAvailability.choco = true |
| 80 | + managerAvailability.npm = true |
| 81 | + }) |
| 82 | + |
| 83 | + it('uses fallback chain when winget fails and choco succeeds', async () => { |
| 84 | + spawnPlanQueue.push( |
| 85 | + { command: 'winget', exitCode: 1, stderr: 'failed' }, |
| 86 | + { command: 'choco', exitCode: 0, stdout: 'ok' }, |
| 87 | + ) |
| 88 | + const service = new ClientInstallService() |
| 89 | + |
| 90 | + const result = await service.install('cursor') |
| 91 | + |
| 92 | + expect(result.success).toBe(true) |
| 93 | + expect(result.installedWith).toBe('choco') |
| 94 | + expect(result.attempts).toHaveLength(2) |
| 95 | + expect(result.attempts[0]?.manager).toBe('winget') |
| 96 | + expect(result.attempts[0]?.success).toBe(false) |
| 97 | + expect(result.attempts[1]?.manager).toBe('choco') |
| 98 | + expect(result.attempts[1]?.success).toBe(true) |
| 99 | + expect(spawnMock).toHaveBeenNthCalledWith(1, 'winget', expect.any(Array), expect.any(Object)) |
| 100 | + expect(spawnMock).toHaveBeenNthCalledWith(2, 'choco', expect.any(Array), expect.any(Object)) |
| 101 | + }) |
| 102 | + |
| 103 | + it('returns no_available_manager when none of the managers are present', async () => { |
| 104 | + managerAvailability.winget = false |
| 105 | + managerAvailability.choco = false |
| 106 | + managerAvailability.npm = false |
| 107 | + const service = new ClientInstallService() |
| 108 | + |
| 109 | + const result = await service.install('codex-cli') |
| 110 | + |
| 111 | + expect(result.success).toBe(false) |
| 112 | + expect(result.failureReason).toBe('no_available_manager') |
| 113 | + expect(result.attempts).toHaveLength(3) |
| 114 | + expect(result.attempts.every((attempt) => attempt.skipped === true)).toBe(true) |
| 115 | + expect(spawnMock).not.toHaveBeenCalled() |
| 116 | + }) |
| 117 | + |
| 118 | + it('returns requires_elevation without trying to elevate automatically', async () => { |
| 119 | + spawnPlanQueue.push({ |
| 120 | + command: 'winget', |
| 121 | + exitCode: 1, |
| 122 | + stderr: 'This operation requires elevation. Run as administrator.', |
| 123 | + }) |
| 124 | + managerAvailability.choco = false |
| 125 | + managerAvailability.npm = false |
| 126 | + const service = new ClientInstallService() |
| 127 | + |
| 128 | + const result = await service.install('codex-gui') |
| 129 | + |
| 130 | + expect(result.success).toBe(false) |
| 131 | + expect(result.failureReason).toBe('requires_elevation') |
| 132 | + expect(result.attempts).toHaveLength(1) |
| 133 | + expect(result.attempts[0]?.command).toBe('winget') |
| 134 | + expect(spawnMock).toHaveBeenCalledTimes(1) |
| 135 | + }) |
| 136 | + |
| 137 | + it('returns manual_install_required for manual-only clients', async () => { |
| 138 | + const service = new ClientInstallService() |
| 139 | + |
| 140 | + const result = await service.install('jetbrains') |
| 141 | + |
| 142 | + expect(result.success).toBe(false) |
| 143 | + expect(result.failureReason).toBe('manual_install_required') |
| 144 | + expect(result.docsUrl).toContain('jetbrains.com') |
| 145 | + expect(result.attempts).toHaveLength(0) |
| 146 | + expect(spawnMock).not.toHaveBeenCalled() |
| 147 | + }) |
| 148 | + |
| 149 | + it('returns unsupported_platform on non-Windows', async () => { |
| 150 | + withMockPlatform('linux') |
| 151 | + const service = new ClientInstallService() |
| 152 | + |
| 153 | + const result = await service.install('cursor') |
| 154 | + |
| 155 | + expect(result.success).toBe(false) |
| 156 | + expect(result.failureReason).toBe('unsupported_platform') |
| 157 | + expect(spawnMock).not.toHaveBeenCalled() |
| 158 | + }) |
| 159 | +}) |
0 commit comments