-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.test.ts
More file actions
309 lines (263 loc) · 7.93 KB
/
Copy pathmain.test.ts
File metadata and controls
309 lines (263 loc) · 7.93 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { MockInstance } from "vitest";
const mainMockState = vi.hoisted(() => ({
argsResult: {
args: {
help: false,
verbose: false,
},
help: false,
kind: "convert" as const,
} as Record<string, unknown>,
parseError: undefined as Error | undefined,
runCalls: [] as unknown[],
statusRunCalls: 0,
sdpubRunCalls: [] as unknown[],
runError: undefined as Error | undefined,
statusRunError: undefined as Error | undefined,
sdpubRunError: undefined as Error | undefined,
}));
vi.mock("../../src/cli/args.js", () => ({
parseCLIArguments: vi.fn(() => {
if (mainMockState.parseError !== undefined) {
throw mainMockState.parseError;
}
return mainMockState.argsResult;
}),
}));
vi.mock("../../src/cli/convert.js", () => ({
runConvertCommand: vi.fn((args: unknown) => {
mainMockState.runCalls.push(args);
if (mainMockState.runError !== undefined) {
return Promise.reject(mainMockState.runError);
}
return Promise.resolve();
}),
}));
vi.mock("../../src/cli/status.js", () => ({
runStatusCommand: vi.fn(() => {
mainMockState.statusRunCalls += 1;
if (mainMockState.statusRunError !== undefined) {
return Promise.reject(mainMockState.statusRunError);
}
return Promise.resolve();
}),
}));
vi.mock("../../src/cli/sdpub.js", () => ({
runSdpubCommand: vi.fn((args: unknown) => {
mainMockState.sdpubRunCalls.push(args);
if (mainMockState.sdpubRunError !== undefined) {
return Promise.reject(mainMockState.sdpubRunError);
}
return Promise.resolve();
}),
}));
import { main } from "../../src/cli/main.js";
import { LLMPaymentRequiredError } from "../../src/llm/index.js";
describe("cli/main", () => {
const originalExitCode = process.exitCode;
let stdoutWrite: MockInstance;
let stderrWrite: MockInstance;
let stdoutChunks: string[];
let stderrChunks: string[];
beforeEach(() => {
mainMockState.argsResult = {
args: {
help: false,
verbose: false,
},
help: false,
kind: "convert",
};
mainMockState.parseError = undefined;
mainMockState.runCalls.length = 0;
mainMockState.statusRunCalls = 0;
mainMockState.sdpubRunCalls.length = 0;
mainMockState.runError = undefined;
mainMockState.statusRunError = undefined;
mainMockState.sdpubRunError = undefined;
process.exitCode = 0;
stdoutChunks = [];
stderrChunks = [];
stdoutWrite = vi
.spyOn(process.stdout, "write")
.mockImplementation((chunk: string | Uint8Array) => {
stdoutChunks.push(String(chunk));
return true;
});
stderrWrite = vi
.spyOn(process.stderr, "write")
.mockImplementation((chunk: string | Uint8Array) => {
stderrChunks.push(String(chunk));
return true;
});
});
afterEach(() => {
stdoutWrite.mockRestore();
stderrWrite.mockRestore();
process.exitCode = originalExitCode;
});
it("prints help text and skips conversion when --help is used", async () => {
mainMockState.argsResult = {
help: true,
helpText: "CLI HELP",
kind: "convert",
};
await main();
expect(stdoutChunks).toStrictEqual(["CLI HELP\n"]);
expect(stderrChunks).toStrictEqual([]);
expect(mainMockState.runCalls).toHaveLength(0);
expect(mainMockState.statusRunCalls).toBe(0);
expect(mainMockState.sdpubRunCalls).toHaveLength(0);
expect(process.exitCode).toBe(0);
});
it("runs the convert command for normal execution", async () => {
mainMockState.argsResult = {
args: {
help: false,
inputPath: "/tmp/book.txt",
outputPath: "/tmp/out.txt",
verbose: false,
},
help: false,
kind: "convert",
};
await main();
expect(mainMockState.runCalls).toStrictEqual([
{
help: false,
inputPath: "/tmp/book.txt",
outputPath: "/tmp/out.txt",
verbose: false,
},
]);
expect(mainMockState.sdpubRunCalls).toHaveLength(0);
expect(mainMockState.statusRunCalls).toBe(0);
expect(stdoutChunks).toStrictEqual([]);
expect(stderrChunks).toStrictEqual([]);
expect(process.exitCode).toBe(0);
});
it("runs the status command for status execution", async () => {
mainMockState.argsResult = {
help: false,
kind: "status",
};
await main();
expect(mainMockState.runCalls).toHaveLength(0);
expect(mainMockState.statusRunCalls).toBe(1);
expect(mainMockState.sdpubRunCalls).toHaveLength(0);
expect(process.exitCode).toBe(0);
});
it("runs the sdpub command for sdpub subcommands", async () => {
mainMockState.argsResult = {
args: {
inputPath: "/tmp/book.sdpub",
subcommand: "list",
},
help: false,
kind: "sdpub",
};
await main();
expect(mainMockState.runCalls).toHaveLength(0);
expect(mainMockState.sdpubRunCalls).toStrictEqual([
{
inputPath: "/tmp/book.sdpub",
subcommand: "list",
},
]);
expect(process.exitCode).toBe(0);
});
it("writes parse errors to stderr and sets a non-zero exit code", async () => {
mainMockState.parseError = new Error("bad args");
await main();
expect(stderrChunks).toStrictEqual(["bad args\n"]);
expect(mainMockState.runCalls).toHaveLength(0);
expect(mainMockState.statusRunCalls).toBe(0);
expect(process.exitCode).toBe(1);
});
it("writes convert command failures to stderr and sets a non-zero exit code", async () => {
mainMockState.argsResult = {
args: {
help: false,
verbose: false,
},
help: false,
kind: "convert",
};
mainMockState.runError = new Error("convert failed");
await main();
expect(stderrChunks).toStrictEqual(["convert failed\n"]);
expect(mainMockState.runCalls).toStrictEqual([
{
help: false,
verbose: false,
},
]);
expect(process.exitCode).toBe(1);
});
it("writes status command failures to stderr and sets a non-zero exit code", async () => {
mainMockState.argsResult = {
help: false,
kind: "status",
};
mainMockState.statusRunError = new Error("status failed");
await main();
expect(stderrChunks).toStrictEqual(["status failed\n"]);
expect(mainMockState.statusRunCalls).toBe(1);
expect(process.exitCode).toBe(1);
});
it("writes sdpub command failures to stderr and sets a non-zero exit code", async () => {
mainMockState.argsResult = {
args: {
inputPath: "/tmp/book.sdpub",
subcommand: "info",
},
help: false,
kind: "sdpub",
};
mainMockState.sdpubRunError = new Error("sdpub failed");
await main();
expect(stderrChunks).toStrictEqual(["sdpub failed\n"]);
expect(mainMockState.sdpubRunCalls).toStrictEqual([
{
inputPath: "/tmp/book.sdpub",
subcommand: "info",
},
]);
expect(process.exitCode).toBe(1);
});
it("writes the full cause chain to stderr", async () => {
mainMockState.argsResult = {
args: {
help: false,
verbose: false,
},
help: false,
kind: "convert",
};
mainMockState.runError = new Error("convert failed", {
cause: new Error("tls reset"),
});
await main();
expect(stderrChunks).toStrictEqual(["convert failed: tls reset\n"]);
expect(process.exitCode).toBe(1);
});
it("writes a stable payment required message for LLM billing failures", async () => {
mainMockState.argsResult = {
args: {
help: false,
verbose: false,
},
help: false,
kind: "convert",
};
mainMockState.runError = new LLMPaymentRequiredError("provider message", {
cause: new Error("raw provider error"),
});
await main();
expect(stderrChunks).toStrictEqual([
"LLM payment required. Check your provider billing status or account balance.\n",
]);
expect(process.exitCode).toBe(1);
});
});