Skip to content

Commit fa745bd

Browse files
committed
test: fix spy leaks, misleading assertions, and test cleanup
- recorder.test.ts: fix misleading "toolCalls should win" comments and assertions to reflect actual content+toolCalls coexistence; move prototype-level setTimeout spy to afterEach for leak safety - fixture-loader.test.ts, sse-writer.test.ts, mcp-mock.test.ts, metrics.test.ts, chaos.test.ts: add vi.restoreAllMocks() to afterEach for unrestored spy cleanup - ws-framing.test.ts: add closeAllConnections before server.close - 17 test files: update "Malformed JSON" assertions to match new error detail format
1 parent 5dd430c commit fa745bd

24 files changed

Lines changed: 59 additions & 29 deletions

src/__tests__/api-conformance.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ describe("OpenAI Embeddings API conformance", () => {
10371037
);
10381038
expect(res.status).toBe(400);
10391039
const json = JSON.parse(res.body);
1040-
expect(json.error.message).toBe("Malformed JSON");
1040+
expect(json.error.message).toMatch(/^Malformed JSON body: /);
10411041
});
10421042

10431043
it("Content-Type is application/json", async () => {

src/__tests__/bedrock-stream.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ describe("POST /model/{modelId}/converse (malformed JSON)", () => {
14421442

14431443
expect(res.status).toBe(400);
14441444
const body = JSON.parse(res.body);
1445-
expect(body.error.message).toBe("Malformed JSON");
1445+
expect(body.error.message).toMatch(/^Malformed JSON: /);
14461446
});
14471447
});
14481448

@@ -1569,7 +1569,7 @@ describe("POST /model/{modelId}/converse-stream (malformed JSON)", () => {
15691569

15701570
expect(res.status).toBe(400);
15711571
const body = JSON.parse(res.body);
1572-
expect(body.error.message).toBe("Malformed JSON");
1572+
expect(body.error.message).toMatch(/^Malformed JSON: /);
15731573
});
15741574
});
15751575

src/__tests__/bedrock.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ describe("POST /model/{modelId}/invoke (error handling)", () => {
243243

244244
expect(res.status).toBe(400);
245245
const body = JSON.parse(res.body);
246-
expect(body.error.message).toBe("Malformed JSON");
246+
expect(body.error.message).toMatch(/^Malformed JSON: /);
247247
});
248248
});
249249

src/__tests__/chaos.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ describe("fixture-level chaos on non-OpenAI provider", () => {
590590
// ---------------------------------------------------------------------------
591591

592592
describe("chaos with logLevel silent: invalid header is ignored gracefully", () => {
593+
afterEach(() => {
594+
vi.restoreAllMocks();
595+
});
596+
593597
it("proceeds normally and does not throw when x-aimock-chaos-drop is not a number", async () => {
594598
const fixtures: Fixture[] = [
595599
{ match: { userMessage: "hello" }, response: { content: "Hi there" } },

src/__tests__/cohere.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ describe("POST /v2/chat (validation)", () => {
575575

576576
expect(res.status).toBe(400);
577577
const body = JSON.parse(res.body);
578-
expect(body.error.message).toBe("Malformed JSON");
578+
expect(body.error.message).toMatch(/^Malformed JSON body: /);
579579
});
580580

581581
it("returns 404 when no fixture matches", async () => {

src/__tests__/embeddings.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ describe("POST /v1/embeddings (error handling)", () => {
425425

426426
expect(res.status).toBe(400);
427427
const body = JSON.parse(res.body);
428-
expect(body.error.message).toBe("Malformed JSON");
428+
expect(body.error.message).toMatch(/^Malformed JSON body: /);
429429
expect(body.error.code).toBe("invalid_json");
430430
});
431431
});

src/__tests__/fixture-loader.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ describe("loadFixtureFile", () => {
4949
});
5050

5151
afterEach(() => {
52+
// Restore console spies that individual tests create via vi.spyOn(console, "warn").
53+
// We cannot use vi.restoreAllMocks() here because the top-level vi.mock("node:fs")
54+
// overrides would also be wiped, breaking subsequent tests.
55+
if ("mockRestore" in console.warn) {
56+
(console.warn as ReturnType<typeof vi.spyOn>).mockRestore();
57+
}
5258
rmSync(tmpDir, { recursive: true, force: true });
5359
});
5460

@@ -360,6 +366,12 @@ describe("loadFixturesFromDir", () => {
360366
});
361367

362368
afterEach(() => {
369+
// Restore console spies that individual tests create via vi.spyOn(console, "warn").
370+
// We cannot use vi.restoreAllMocks() here because the top-level vi.mock("node:fs")
371+
// overrides would also be wiped, breaking subsequent tests.
372+
if ("mockRestore" in console.warn) {
373+
(console.warn as ReturnType<typeof vi.spyOn>).mockRestore();
374+
}
363375
rmSync(tmpDir, { recursive: true, force: true });
364376
});
365377

src/__tests__/gemini.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ describe("Gemini error handling", () => {
610610

611611
expect(res.status).toBe(400);
612612
const body = JSON.parse(res.body);
613-
expect(body.error.message).toBe("Malformed JSON");
613+
expect(body.error.message).toMatch(/^Malformed JSON body: /);
614614
});
615615

616616
it("returns 500 for unknown response type", async () => {

src/__tests__/mcp-mock.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,10 @@ describe("MCPMock", () => {
874874
// ---- Lifecycle edge cases ----
875875

876876
describe("lifecycle", () => {
877+
afterEach(() => {
878+
vi.restoreAllMocks();
879+
});
880+
877881
it("start() when already started throws", async () => {
878882
mcp = new MCPMock();
879883
await mcp.start();

src/__tests__/messages.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ describe("POST /v1/messages (error handling)", () => {
703703

704704
expect(res.status).toBe(400);
705705
const body = JSON.parse(res.body);
706-
expect(body.error.message).toBe("Malformed JSON");
706+
expect(body.error.message).toMatch(/^Malformed JSON: /);
707707
});
708708

709709
it("returns 500 for unknown response type", async () => {

0 commit comments

Comments
 (0)