|
1 | | -import { expect, test } from "vitest"; |
| 1 | +import { MockAgent, setGlobalDispatcher } from "undici"; |
| 2 | +import { afterAll, beforeAll, describe, expect, test } from "vitest"; |
2 | 3 | import { CreateModerationCommand } from "./fixtures/openai/commands.ts"; |
3 | 4 | import { OpenAiApiRestClient } from "./fixtures/openai/main.ts"; |
4 | 5 |
|
5 | | -test("OpenAI CreateModeration", async () => { |
6 | | - const openAiClient = new OpenAiApiRestClient(new URL("http://invalid")); |
| 6 | +const mockAgent = new MockAgent(); |
| 7 | +setGlobalDispatcher(mockAgent); |
7 | 8 |
|
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"; |
11 | 19 |
|
12 | | - const result = await openAiClient.json(command).catch((err) => err); |
| 20 | +describe("OpenAI", () => { |
| 21 | + const pool = mockAgent.get(apiUrl); |
13 | 22 |
|
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 | + }); |
15 | 48 | }); |
0 commit comments