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