Skip to content

Commit e6fa64e

Browse files
committed
Fix eslint and prettier violations in test files
- Fix syntax error in type cast (mock-openai.test.ts:282) - Remove unused 'result' variable and 'reject' param (server.test.ts) - Remove unused 'res' assignment (mock-openai.test.ts) - Apply prettier formatting to all changed test files
1 parent e0c8490 commit e6fa64e

4 files changed

Lines changed: 128 additions & 134 deletions

File tree

‎src/__tests__/cli.test.ts‎

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,155 +8,155 @@ const CLI_PATH = resolve(__dirname, "../../dist/cli.js");
88

99
/** Spawn the CLI and collect stdout/stderr/exit code. */
1010
function runCli(
11-
args: string[],
12-
opts: { timeout?: number } = {},
11+
args: string[],
12+
opts: { timeout?: number } = {},
1313
): Promise<{ stdout: string; stderr: string; code: number | null }> {
14-
const timeout = opts.timeout ?? 5000;
15-
return new Promise((res) => {
16-
const cp = execFile("node", [CLI_PATH, ...args], { timeout }, (err, stdout, stderr) => {
17-
const code = cp.exitCode ?? (err && "code" in err ? (err as { code: number }).code : null);
18-
res({ stdout, stderr, code });
19-
});
14+
const timeout = opts.timeout ?? 5000;
15+
return new Promise((res) => {
16+
const cp = execFile("node", [CLI_PATH, ...args], { timeout }, (err, stdout, stderr) => {
17+
const code = cp.exitCode ?? (err && "code" in err ? (err as { code: number }).code : null);
18+
res({ stdout, stderr, code });
2019
});
20+
});
2121
}
2222

2323
/**
2424
* Spawn the CLI expecting a long-running server. Returns the child
2525
* process plus helpers to read accumulated output and send signals.
2626
*/
2727
function spawnCli(args: string[]): {
28-
cp: ChildProcess;
29-
stdout: () => string;
30-
stderr: () => string;
31-
kill: (signal?: NodeJS.Signals) => void;
32-
waitForOutput: (match: RegExp, timeoutMs?: number) => Promise<void>;
28+
cp: ChildProcess;
29+
stdout: () => string;
30+
stderr: () => string;
31+
kill: (signal?: NodeJS.Signals) => void;
32+
waitForOutput: (match: RegExp, timeoutMs?: number) => Promise<void>;
3333
} {
34-
let out = "";
35-
let err = "";
36-
const cp = execFile("node", [CLI_PATH, ...args]);
37-
cp.stdout?.on("data", (d) => {
38-
out += d;
39-
});
40-
cp.stderr?.on("data", (d) => {
41-
err += d;
34+
let out = "";
35+
let err = "";
36+
const cp = execFile("node", [CLI_PATH, ...args]);
37+
cp.stdout?.on("data", (d) => {
38+
out += d;
39+
});
40+
cp.stderr?.on("data", (d) => {
41+
err += d;
42+
});
43+
44+
const waitForOutput = (match: RegExp, timeoutMs = 5000): Promise<void> =>
45+
new Promise((resolve, reject) => {
46+
const deadline = setTimeout(() => {
47+
reject(new Error(`Timed out waiting for ${match} — stdout: ${out}, stderr: ${err}`));
48+
}, timeoutMs);
49+
50+
const check = () => {
51+
if (match.test(out) || match.test(err)) {
52+
clearTimeout(deadline);
53+
resolve();
54+
return;
55+
}
56+
setTimeout(check, 50);
57+
};
58+
check();
4259
});
4360

44-
const waitForOutput = (match: RegExp, timeoutMs = 5000): Promise<void> =>
45-
new Promise((resolve, reject) => {
46-
const deadline = setTimeout(() => {
47-
reject(new Error(`Timed out waiting for ${match} — stdout: ${out}, stderr: ${err}`));
48-
}, timeoutMs);
49-
50-
const check = () => {
51-
if (match.test(out) || match.test(err)) {
52-
clearTimeout(deadline);
53-
resolve();
54-
return;
55-
}
56-
setTimeout(check, 50);
57-
};
58-
check();
59-
});
60-
61-
return {
62-
cp,
63-
stdout: () => out,
64-
stderr: () => err,
65-
kill: (signal: NodeJS.Signals = "SIGTERM") => cp.kill(signal),
66-
waitForOutput,
67-
};
61+
return {
62+
cp,
63+
stdout: () => out,
64+
stderr: () => err,
65+
kill: (signal: NodeJS.Signals = "SIGTERM") => cp.kill(signal),
66+
waitForOutput,
67+
};
6868
}
6969

7070
function makeTmpDir(): string {
71-
return mkdtempSync(join(tmpdir(), "cli-test-"));
71+
return mkdtempSync(join(tmpdir(), "cli-test-"));
7272
}
7373

7474
function writeFixture(dir: string, name: string): string {
75-
const filePath = join(dir, name);
76-
writeFileSync(
77-
filePath,
78-
JSON.stringify({
79-
fixtures: [
80-
{
81-
match: { userMessage: "hello" },
82-
response: { content: "Hello from test fixture!" },
83-
},
84-
],
85-
}),
86-
"utf-8",
87-
);
88-
return filePath;
75+
const filePath = join(dir, name);
76+
writeFileSync(
77+
filePath,
78+
JSON.stringify({
79+
fixtures: [
80+
{
81+
match: { userMessage: "hello" },
82+
response: { content: "Hello from test fixture!" },
83+
},
84+
],
85+
}),
86+
"utf-8",
87+
);
88+
return filePath;
8989
}
9090

9191
/* ================================================================== */
9292

9393
describe("CLI: --help", () => {
94-
it("prints usage text and exits with code 0", async () => {
95-
const { stdout, code } = await runCli(["--help"]);
96-
expect(stdout).toContain("Usage: mock-openai");
97-
expect(stdout).toContain("--port");
98-
expect(stdout).toContain("--fixtures");
99-
expect(code).toBe(0);
100-
});
94+
it("prints usage text and exits with code 0", async () => {
95+
const { stdout, code } = await runCli(["--help"]);
96+
expect(stdout).toContain("Usage: mock-openai");
97+
expect(stdout).toContain("--port");
98+
expect(stdout).toContain("--fixtures");
99+
expect(code).toBe(0);
100+
});
101101
});
102102

103103
describe("CLI: argument validation", () => {
104-
it("rejects --port 99999 (out of range)", async () => {
105-
const { stderr, code } = await runCli(["--port", "99999"]);
106-
expect(stderr).toContain("Invalid port");
107-
expect(code).toBe(1);
108-
});
109-
110-
it("rejects --port=-1 (negative)", async () => {
111-
const { stderr, code } = await runCli(["--port=-1"]);
112-
expect(stderr).toContain("Invalid port");
113-
expect(code).toBe(1);
114-
});
115-
116-
it("rejects --latency=-5 (negative)", async () => {
117-
const { stderr, code } = await runCli(["--latency=-5"]);
118-
expect(stderr).toContain("Invalid latency");
119-
expect(code).toBe(1);
120-
});
121-
122-
it("rejects --chunk-size 0 (below minimum)", async () => {
123-
const { stderr, code } = await runCli(["--chunk-size", "0"]);
124-
expect(stderr).toContain("Invalid chunk-size");
125-
expect(code).toBe(1);
126-
});
104+
it("rejects --port 99999 (out of range)", async () => {
105+
const { stderr, code } = await runCli(["--port", "99999"]);
106+
expect(stderr).toContain("Invalid port");
107+
expect(code).toBe(1);
108+
});
109+
110+
it("rejects --port=-1 (negative)", async () => {
111+
const { stderr, code } = await runCli(["--port=-1"]);
112+
expect(stderr).toContain("Invalid port");
113+
expect(code).toBe(1);
114+
});
115+
116+
it("rejects --latency=-5 (negative)", async () => {
117+
const { stderr, code } = await runCli(["--latency=-5"]);
118+
expect(stderr).toContain("Invalid latency");
119+
expect(code).toBe(1);
120+
});
121+
122+
it("rejects --chunk-size 0 (below minimum)", async () => {
123+
const { stderr, code } = await runCli(["--chunk-size", "0"]);
124+
expect(stderr).toContain("Invalid chunk-size");
125+
expect(code).toBe(1);
126+
});
127127
});
128128

129129
describe("CLI: fixture loading", () => {
130-
let tmpDir: string;
130+
let tmpDir: string;
131131

132-
beforeEach(() => {
133-
tmpDir = makeTmpDir();
134-
});
132+
beforeEach(() => {
133+
tmpDir = makeTmpDir();
134+
});
135135

136-
afterEach(() => {
137-
rmSync(tmpDir, { recursive: true, force: true });
138-
});
136+
afterEach(() => {
137+
rmSync(tmpDir, { recursive: true, force: true });
138+
});
139139

140-
it("starts server with a valid fixture file, then exits cleanly on SIGTERM", async () => {
141-
const fixturePath = writeFixture(tmpDir, "test.json");
142-
const child = spawnCli(["--fixtures", fixturePath, "--port", "0"]);
140+
it("starts server with a valid fixture file, then exits cleanly on SIGTERM", async () => {
141+
const fixturePath = writeFixture(tmpDir, "test.json");
142+
const child = spawnCli(["--fixtures", fixturePath, "--port", "0"]);
143143

144-
await child.waitForOutput(/listening on/i, 5000);
145-
expect(child.stdout()).toContain("Loaded 1 fixture(s)");
144+
await child.waitForOutput(/listening on/i, 5000);
145+
expect(child.stdout()).toContain("Loaded 1 fixture(s)");
146146

147-
child.kill("SIGTERM");
147+
child.kill("SIGTERM");
148148

149-
await new Promise<void>((resolve) => {
150-
child.cp.on("close", (code) => {
151-
expect(code).toBe(0);
152-
resolve();
153-
});
154-
});
149+
await new Promise<void>((resolve) => {
150+
child.cp.on("close", (code) => {
151+
expect(code).toBe(0);
152+
resolve();
153+
});
155154
});
155+
});
156156

157-
it("fails with error when --fixtures points to a non-existent path", async () => {
158-
const { stderr, code } = await runCli(["--fixtures", "/nonexistent/path/to/fixtures"]);
159-
expect(stderr).toContain("Fixtures path not found");
160-
expect(code).toBe(1);
161-
});
157+
it("fails with error when --fixtures points to a non-existent path", async () => {
158+
const { stderr, code } = await runCli(["--fixtures", "/nonexistent/path/to/fixtures"]);
159+
expect(stderr).toContain("Fixtures path not found");
160+
expect(code).toBe(1);
161+
});
162162
});

‎src/__tests__/helpers.test.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,7 @@ describe("buildToolCallChunks", () => {
214214
});
215215

216216
it("handles empty arguments string with role + initial + finish", () => {
217-
const chunks = buildToolCallChunks(
218-
[{ name: "fn", arguments: "" }],
219-
"gpt-4",
220-
20,
221-
);
217+
const chunks = buildToolCallChunks([{ name: "fn", arguments: "" }], "gpt-4", 20);
222218
// role + initial tool call chunk + finish = 3 (no arg chunks since args is empty)
223219
expect(chunks.length).toBe(3);
224220

@@ -252,11 +248,7 @@ describe("buildToolCallChunks", () => {
252248
});
253249

254250
it("produces a single arg chunk when arguments are shorter than chunkSize", () => {
255-
const chunks = buildToolCallChunks(
256-
[{ name: "fn", arguments: '{"x":1}' }],
257-
"gpt-4",
258-
100,
259-
);
251+
const chunks = buildToolCallChunks([{ name: "fn", arguments: '{"x":1}' }], "gpt-4", 100);
260252
// role + initial + 1 arg chunk + finish = 4
261253
expect(chunks.length).toBe(4);
262254

‎src/__tests__/mock-openai.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ describe("MockOpenAI", () => {
279279
return realClose(() => {
280280
if (cb) cb(new Error("close failed"));
281281
});
282-
}) as typeof internal.serverInstance!.server.close;
282+
}) as unknown as typeof realClose;
283283

284284
await expect(mock.stop()).rejects.toThrow("close failed");
285285

@@ -436,10 +436,16 @@ describe("MockOpenAI", () => {
436436
mock.onToolCall("get_weather", { content: "sunny" });
437437
await mock.start();
438438

439-
const res = await post(mock.url, {
439+
await post(mock.url, {
440440
model: "gpt-4",
441441
messages: [
442-
{ role: "assistant", content: null, tool_calls: [{ id: "tc1", type: "function", function: { name: "get_weather", arguments: "{}" } }] },
442+
{
443+
role: "assistant",
444+
content: null,
445+
tool_calls: [
446+
{ id: "tc1", type: "function", function: { name: "get_weather", arguments: "{}" } },
447+
],
448+
},
443449
{ role: "tool", content: "result", tool_call_id: "tc1" },
444450
],
445451
});

‎src/__tests__/server.test.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ describe("readBody error path", () => {
586586
// when the socket is destroyed. We advertise a Content-Length far larger
587587
// than the data we actually send, then destroy the socket. This causes
588588
// the async iterator in readBody() to emit an error (premature close).
589-
const result = await new Promise<{ status: number; body: string }>((resolve, reject) => {
589+
await new Promise<{ status: number; body: string }>((resolve) => {
590590
const socket = net.createConnection({ host: "127.0.0.1", port }, () => {
591591
const partialBody = '{"model":"gpt-4","mess';
592592
const headers = [
@@ -743,9 +743,7 @@ describe("concurrent request handling", () => {
743743
};
744744

745745
// Fire 10 requests in parallel
746-
const results = await Promise.all(
747-
Array.from({ length: 10 }, () => post(url, body)),
748-
);
746+
const results = await Promise.all(Array.from({ length: 10 }, () => post(url, body)));
749747

750748
// All 10 should succeed as valid SSE streams
751749
for (const res of results) {
@@ -813,9 +811,7 @@ describe("header forwarding in journal", () => {
813811
const entry = instance.journal.getLast();
814812
expect(entry).not.toBeNull();
815813
expect(entry!.headers["host"]).toBeDefined();
816-
expect(entry!.headers["content-length"]).toBe(
817-
String(Buffer.byteLength(JSON.stringify(body))),
818-
);
814+
expect(entry!.headers["content-length"]).toBe(String(Buffer.byteLength(JSON.stringify(body))));
819815
expect(entry!.headers["content-type"]).toBe("application/json");
820816
});
821817

0 commit comments

Comments
 (0)