openai-api-mock 0.1.28 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import fetch from 'node-fetch';
2
+ import { mockOpenAIResponse, stopMocking } from '../src/index.js';
3
+ import OpenAI from 'openai';
4
+
5
+ const openai = new OpenAI({ apiKey: "OPENAI_API_KEY" });
6
+
7
+ describe('Mock OpenAI Chat with a delay', () => {
8
+
9
+ beforeEach(() => {
10
+ mockOpenAIResponse(true, { latency: 1000 , logRequests: true });
11
+ });
12
+
13
+ afterEach(() => {
14
+ stopMocking();
15
+ });
16
+
17
+ test('should mock OpenAI Chat with a delay', async () => {
18
+ const response = await openai.chat.completions.create({
19
+ model: "gpt-3.5",
20
+ messages: [
21
+ { role: 'system', content: "You're an expert chef" },
22
+ { role: 'user', content: "Suggest at least 5 recipes" },
23
+ ]
24
+ });
25
+
26
+ expect(response).toBeDefined();
27
+ });
28
+ });
package/dist/index.cjs CHANGED
@@ -2199,8 +2199,7 @@ function mockOpenAIResponse(force = false, options = {}) {
2199
2199
  if (env !== "development" && !force) {
2200
2200
  return { isActive: false, stopMocking };
2201
2201
  }
2202
- const mockWithDelay = latency > 0 ? nock(OPEN_AI_BASE_URL).delay(latency) : nock(OPEN_AI_BASE_URL);
2203
- mockWithDelay.post(CHAT_COMPLETIONS_ENDPOINT).reply(function(uri, requestBody) {
2202
+ nock(OPEN_AI_BASE_URL).post(CHAT_COMPLETIONS_ENDPOINT).delay(latency).reply(function(uri, requestBody) {
2204
2203
  if (logRequests) {
2205
2204
  console.log(`[openai-api-mock] Chat request:`, JSON.stringify(requestBody, null, 2));
2206
2205
  }
@@ -2222,7 +2221,7 @@ function mockOpenAIResponse(force = false, options = {}) {
2222
2221
  return [500, { error: { message: "Internal server error in mock" } }];
2223
2222
  }
2224
2223
  });
2225
- mockWithDelay.post(IMAGE_GENERATIONS_ENDPOINT).reply(function(uri, requestBody) {
2224
+ nock(OPEN_AI_BASE_URL).post(IMAGE_GENERATIONS_ENDPOINT).delay(latency).reply(function(uri, requestBody) {
2226
2225
  if (logRequests) {
2227
2226
  console.log(`[openai-api-mock] Image request:`, JSON.stringify(requestBody, null, 2));
2228
2227
  }