|
| 1 | +import { Command } from 'commander' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +// Mock child_process |
| 5 | +vi.mock('node:child_process', () => ({ |
| 6 | + spawn: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +// Mock chalk to avoid colors in tests |
| 10 | +vi.mock('chalk', () => ({ |
| 11 | + default: { |
| 12 | + green: vi.fn((text: string) => text), |
| 13 | + red: vi.fn((text: string) => text), |
| 14 | + dim: vi.fn((text: string) => text), |
| 15 | + }, |
| 16 | +})) |
| 17 | + |
| 18 | +// Mock spinner — pass through to the callback |
| 19 | +vi.mock('../lib/spinner.js', () => ({ |
| 20 | + withSpinner: vi.fn((_opts: unknown, fn: () => Promise<unknown>) => fn()), |
| 21 | +})) |
| 22 | + |
| 23 | +import { spawn } from 'node:child_process' |
| 24 | +import { registerUpdateCommand } from '../commands/update.js' |
| 25 | + |
| 26 | +const mockSpawn = vi.mocked(spawn) |
| 27 | + |
| 28 | +function createProgram() { |
| 29 | + const program = new Command() |
| 30 | + program.exitOverride() |
| 31 | + registerUpdateCommand(program) |
| 32 | + return program |
| 33 | +} |
| 34 | + |
| 35 | +function mockFetch(version: string) { |
| 36 | + vi.stubGlobal( |
| 37 | + 'fetch', |
| 38 | + vi.fn().mockResolvedValue({ |
| 39 | + ok: true, |
| 40 | + json: () => Promise.resolve({ version }), |
| 41 | + }), |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +function mockFetchError(status: number) { |
| 46 | + vi.stubGlobal( |
| 47 | + 'fetch', |
| 48 | + vi.fn().mockResolvedValue({ |
| 49 | + ok: false, |
| 50 | + status, |
| 51 | + }), |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +function mockFetchNetworkError(message: string) { |
| 56 | + vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error(message))) |
| 57 | +} |
| 58 | + |
| 59 | +function mockSpawnSuccess() { |
| 60 | + mockSpawn.mockReturnValue({ |
| 61 | + on: vi.fn((event: string, cb: (arg?: unknown) => void) => { |
| 62 | + if (event === 'close') cb(0) |
| 63 | + }), |
| 64 | + } as never) |
| 65 | +} |
| 66 | + |
| 67 | +function mockSpawnFailure(exitCode: number) { |
| 68 | + mockSpawn.mockReturnValue({ |
| 69 | + on: vi.fn((event: string, cb: (arg?: unknown) => void) => { |
| 70 | + if (event === 'close') cb(exitCode) |
| 71 | + }), |
| 72 | + } as never) |
| 73 | +} |
| 74 | + |
| 75 | +function mockSpawnPermissionError() { |
| 76 | + mockSpawn.mockReturnValue({ |
| 77 | + on: vi.fn((event: string, cb: (arg?: unknown) => void) => { |
| 78 | + if (event === 'error') { |
| 79 | + const err = Object.assign(new Error('EACCES'), { code: 'EACCES' }) |
| 80 | + cb(err) |
| 81 | + } |
| 82 | + }), |
| 83 | + } as never) |
| 84 | +} |
| 85 | + |
| 86 | +describe('update command', () => { |
| 87 | + let consoleSpy: ReturnType<typeof vi.spyOn> |
| 88 | + let consoleErrorSpy: ReturnType<typeof vi.spyOn> |
| 89 | + |
| 90 | + beforeEach(() => { |
| 91 | + consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) |
| 92 | + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 93 | + process.exitCode = undefined |
| 94 | + }) |
| 95 | + |
| 96 | + afterEach(() => { |
| 97 | + vi.restoreAllMocks() |
| 98 | + vi.unstubAllGlobals() |
| 99 | + process.exitCode = undefined |
| 100 | + }) |
| 101 | + |
| 102 | + describe('already up to date', () => { |
| 103 | + it('prints up-to-date message when versions match', async () => { |
| 104 | + const { version } = await import('../../package.json') |
| 105 | + mockFetch(version) |
| 106 | + |
| 107 | + const program = createProgram() |
| 108 | + await program.parseAsync(['node', 'td', 'update']) |
| 109 | + |
| 110 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 111 | + expect.anything(), |
| 112 | + expect.stringContaining('Already up to date'), |
| 113 | + ) |
| 114 | + expect(mockSpawn).not.toHaveBeenCalled() |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + describe('--check flag', () => { |
| 119 | + it('shows version info without installing when update available', async () => { |
| 120 | + mockFetch('99.99.99') |
| 121 | + |
| 122 | + const program = createProgram() |
| 123 | + await program.parseAsync(['node', 'td', 'update', '--check']) |
| 124 | + |
| 125 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Update available')) |
| 126 | + expect(mockSpawn).not.toHaveBeenCalled() |
| 127 | + }) |
| 128 | + |
| 129 | + it('shows up-to-date message when already current', async () => { |
| 130 | + const { version } = await import('../../package.json') |
| 131 | + mockFetch(version) |
| 132 | + |
| 133 | + const program = createProgram() |
| 134 | + await program.parseAsync(['node', 'td', 'update', '--check']) |
| 135 | + |
| 136 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 137 | + expect.anything(), |
| 138 | + expect.stringContaining('Already up to date'), |
| 139 | + ) |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + describe('update available', () => { |
| 144 | + it('spawns npm install and reports success', async () => { |
| 145 | + mockFetch('99.99.99') |
| 146 | + mockSpawnSuccess() |
| 147 | + |
| 148 | + const program = createProgram() |
| 149 | + await program.parseAsync(['node', 'td', 'update']) |
| 150 | + |
| 151 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 152 | + 'npm', |
| 153 | + ['install', '-g', '@doist/todoist-cli@latest'], |
| 154 | + { stdio: 'inherit' }, |
| 155 | + ) |
| 156 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 157 | + expect.anything(), |
| 158 | + expect.stringContaining('Updated to v99.99.99'), |
| 159 | + ) |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + describe('registry errors', () => { |
| 164 | + it('handles HTTP errors from registry', async () => { |
| 165 | + mockFetchError(503) |
| 166 | + |
| 167 | + const program = createProgram() |
| 168 | + await program.parseAsync(['node', 'td', 'update']) |
| 169 | + |
| 170 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 171 | + expect.anything(), |
| 172 | + expect.stringContaining('Failed to check for updates'), |
| 173 | + ) |
| 174 | + expect(process.exitCode).toBe(1) |
| 175 | + }) |
| 176 | + |
| 177 | + it('handles network failures', async () => { |
| 178 | + mockFetchNetworkError('getaddrinfo ENOTFOUND registry.npmjs.org') |
| 179 | + |
| 180 | + const program = createProgram() |
| 181 | + await program.parseAsync(['node', 'td', 'update']) |
| 182 | + |
| 183 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 184 | + expect.anything(), |
| 185 | + expect.stringContaining('Failed to check for updates'), |
| 186 | + ) |
| 187 | + expect(process.exitCode).toBe(1) |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + describe('install errors', () => { |
| 192 | + it('suggests sudo on permission error', async () => { |
| 193 | + mockFetch('99.99.99') |
| 194 | + mockSpawnPermissionError() |
| 195 | + |
| 196 | + const program = createProgram() |
| 197 | + await program.parseAsync(['node', 'td', 'update']) |
| 198 | + |
| 199 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 200 | + expect.anything(), |
| 201 | + expect.stringContaining('Permission denied'), |
| 202 | + ) |
| 203 | + expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('sudo')) |
| 204 | + expect(process.exitCode).toBe(1) |
| 205 | + }) |
| 206 | + |
| 207 | + it('handles non-zero exit code from npm', async () => { |
| 208 | + mockFetch('99.99.99') |
| 209 | + mockSpawnFailure(1) |
| 210 | + |
| 211 | + const program = createProgram() |
| 212 | + await program.parseAsync(['node', 'td', 'update']) |
| 213 | + |
| 214 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 215 | + expect.anything(), |
| 216 | + expect.stringContaining('exited with code 1'), |
| 217 | + ) |
| 218 | + expect(process.exitCode).toBe(1) |
| 219 | + }) |
| 220 | + }) |
| 221 | +}) |
0 commit comments