|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { runCli } from '../cli.ts'; |
| 4 | +import { AppError } from '../utils/errors.ts'; |
| 5 | +import type { DaemonResponse } from '../daemon-client.ts'; |
| 6 | + |
| 7 | +class ExitSignal extends Error { |
| 8 | + public readonly code: number; |
| 9 | + |
| 10 | + constructor(code: number) { |
| 11 | + super(`EXIT_${code}`); |
| 12 | + this.code = code; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +type RunResult = { |
| 17 | + code: number | null; |
| 18 | + stdout: string; |
| 19 | + stderr: string; |
| 20 | + daemonCalls: number; |
| 21 | +}; |
| 22 | + |
| 23 | +async function runCliCapture(argv: string[]): Promise<RunResult> { |
| 24 | + let daemonCalls = 0; |
| 25 | + let stdout = ''; |
| 26 | + let stderr = ''; |
| 27 | + let code: number | null = null; |
| 28 | + |
| 29 | + const originalExit = process.exit; |
| 30 | + const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 31 | + const originalStderrWrite = process.stderr.write.bind(process.stderr); |
| 32 | + |
| 33 | + (process as any).exit = ((nextCode?: number) => { |
| 34 | + throw new ExitSignal(nextCode ?? 0); |
| 35 | + }) as typeof process.exit; |
| 36 | + (process.stdout as any).write = ((chunk: unknown) => { |
| 37 | + stdout += String(chunk); |
| 38 | + return true; |
| 39 | + }) as typeof process.stdout.write; |
| 40 | + (process.stderr as any).write = ((chunk: unknown) => { |
| 41 | + stderr += String(chunk); |
| 42 | + return true; |
| 43 | + }) as typeof process.stderr.write; |
| 44 | + |
| 45 | + const sendToDaemon = async (): Promise<DaemonResponse> => { |
| 46 | + daemonCalls += 1; |
| 47 | + throw new AppError('COMMAND_FAILED', 'Failed to start daemon', { |
| 48 | + infoPath: '/tmp/daemon.json', |
| 49 | + hint: 'stale daemon info', |
| 50 | + }); |
| 51 | + }; |
| 52 | + |
| 53 | + try { |
| 54 | + await runCli(argv, { sendToDaemon }); |
| 55 | + } catch (error) { |
| 56 | + if (error instanceof ExitSignal) code = error.code; |
| 57 | + else throw error; |
| 58 | + } finally { |
| 59 | + process.exit = originalExit; |
| 60 | + process.stdout.write = originalStdoutWrite; |
| 61 | + process.stderr.write = originalStderrWrite; |
| 62 | + } |
| 63 | + |
| 64 | + return { code, stdout, stderr, daemonCalls }; |
| 65 | +} |
| 66 | + |
| 67 | +async function runCliCaptureWithErrorDetails( |
| 68 | + argv: string[], |
| 69 | + details: Record<string, unknown>, |
| 70 | + message = 'Failed to start daemon', |
| 71 | +): Promise<RunResult> { |
| 72 | + let daemonCalls = 0; |
| 73 | + let stdout = ''; |
| 74 | + let stderr = ''; |
| 75 | + let code: number | null = null; |
| 76 | + |
| 77 | + const originalExit = process.exit; |
| 78 | + const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 79 | + const originalStderrWrite = process.stderr.write.bind(process.stderr); |
| 80 | + |
| 81 | + (process as any).exit = ((nextCode?: number) => { |
| 82 | + throw new ExitSignal(nextCode ?? 0); |
| 83 | + }) as typeof process.exit; |
| 84 | + (process.stdout as any).write = ((chunk: unknown) => { |
| 85 | + stdout += String(chunk); |
| 86 | + return true; |
| 87 | + }) as typeof process.stdout.write; |
| 88 | + (process.stderr as any).write = ((chunk: unknown) => { |
| 89 | + stderr += String(chunk); |
| 90 | + return true; |
| 91 | + }) as typeof process.stderr.write; |
| 92 | + |
| 93 | + const sendToDaemon = async (): Promise<DaemonResponse> => { |
| 94 | + daemonCalls += 1; |
| 95 | + throw new AppError('COMMAND_FAILED', message, details); |
| 96 | + }; |
| 97 | + |
| 98 | + try { |
| 99 | + await runCli(argv, { sendToDaemon }); |
| 100 | + } catch (error) { |
| 101 | + if (error instanceof ExitSignal) code = error.code; |
| 102 | + else throw error; |
| 103 | + } finally { |
| 104 | + process.exit = originalExit; |
| 105 | + process.stdout.write = originalStdoutWrite; |
| 106 | + process.stderr.write = originalStderrWrite; |
| 107 | + } |
| 108 | + |
| 109 | + return { code, stdout, stderr, daemonCalls }; |
| 110 | +} |
| 111 | + |
| 112 | +test('close treats daemon startup failure as no-op', async () => { |
| 113 | + const result = await runCliCapture(['close']); |
| 114 | + assert.equal(result.code, null); |
| 115 | + assert.equal(result.daemonCalls, 1); |
| 116 | + assert.equal(result.stdout, ''); |
| 117 | + assert.equal(result.stderr, ''); |
| 118 | +}); |
| 119 | + |
| 120 | +test('close --json treats daemon startup failure as no-op success', async () => { |
| 121 | + const result = await runCliCapture(['close', '--json']); |
| 122 | + assert.equal(result.code, null); |
| 123 | + assert.equal(result.daemonCalls, 1); |
| 124 | + const payload = JSON.parse(result.stdout); |
| 125 | + assert.equal(payload.success, true); |
| 126 | + assert.equal(payload.data.closed, 'session'); |
| 127 | + assert.equal(payload.data.source, 'no-daemon'); |
| 128 | + assert.equal(result.stderr, ''); |
| 129 | +}); |
| 130 | + |
| 131 | +test('close treats lock-only daemon startup failure as no-op', async () => { |
| 132 | + const result = await runCliCaptureWithErrorDetails(['close'], { |
| 133 | + lockPath: '/tmp/daemon.lock', |
| 134 | + hint: 'stale daemon lock', |
| 135 | + }); |
| 136 | + assert.equal(result.code, null); |
| 137 | + assert.equal(result.daemonCalls, 1); |
| 138 | + assert.equal(result.stdout, ''); |
| 139 | + assert.equal(result.stderr, ''); |
| 140 | +}); |
| 141 | + |
| 142 | +test('close treats structured daemon startup failure as no-op without relying on message text', async () => { |
| 143 | + const result = await runCliCaptureWithErrorDetails( |
| 144 | + ['close'], |
| 145 | + { |
| 146 | + kind: 'daemon_startup_failed', |
| 147 | + lockPath: '/tmp/daemon.lock', |
| 148 | + }, |
| 149 | + 'daemon bootstrap failed', |
| 150 | + ); |
| 151 | + assert.equal(result.code, null); |
| 152 | + assert.equal(result.daemonCalls, 1); |
| 153 | + assert.equal(result.stdout, ''); |
| 154 | + assert.equal(result.stderr, ''); |
| 155 | +}); |
0 commit comments