Skip to content

Commit 918ac72

Browse files
maxholmanclaude
andcommitted
test: use mock agent for openai test to prevent network calls
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f22eb9a commit 918ac72

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

__tests__/openai.test.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
1-
import { expect, test } from "vitest";
1+
import { MockAgent, setGlobalDispatcher } from "undici";
2+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
23
import { CreateModerationCommand } from "./fixtures/openai/commands.ts";
34
import { OpenAiApiRestClient } from "./fixtures/openai/main.ts";
45

5-
test("OpenAI CreateModeration", async () => {
6-
const openAiClient = new OpenAiApiRestClient(new URL("http://invalid"));
6+
const mockAgent = new MockAgent();
7+
setGlobalDispatcher(mockAgent);
78

8-
const command = new CreateModerationCommand({
9-
input: "This is a test",
10-
});
9+
beforeAll(() => {
10+
mockAgent.activate();
11+
mockAgent.disableNetConnect();
12+
});
13+
14+
afterAll(() => {
15+
mockAgent.assertNoPendingInterceptors();
16+
});
17+
18+
const apiUrl = "http://192.2.0.1";
1119

12-
const result = await openAiClient.json(command).catch((err) => err);
20+
describe("OpenAI", () => {
21+
const pool = mockAgent.get(apiUrl);
1322

14-
expect(result).toBeTruthy();
23+
pool
24+
.intercept({
25+
path: "/moderations",
26+
method: "POST",
27+
})
28+
.reply(200, {
29+
id: "modr-123",
30+
model: "text-moderation-latest",
31+
results: [],
32+
})
33+
.times(1);
34+
35+
test("CreateModeration", async () => {
36+
const openAiClient = new OpenAiApiRestClient(apiUrl, {
37+
logger: console.debug,
38+
});
39+
40+
const command = new CreateModerationCommand({
41+
input: "This is a test",
42+
});
43+
44+
const result = await openAiClient.json(command);
45+
46+
expect(result).toBeTruthy();
47+
});
1548
});

0 commit comments

Comments
 (0)