This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathopenai-timeout.spec.ts
More file actions
154 lines (127 loc) · 3.77 KB
/
Copy pathopenai-timeout.spec.ts
File metadata and controls
154 lines (127 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// npx vitest run api/providers/__tests__/openai-timeout.spec.ts
import { OpenAiHandler } from "../openai"
import { ApiHandlerOptions } from "../../../shared/api"
// Mock the timeout config utility
vitest.mock("../utils/timeout-config", () => ({
getApiRequestTimeout: vitest.fn(),
}))
// Mock the undici-fetch utility
const mockFetchFn = vitest.fn()
vitest.mock("../utils/undici-fetch", () => ({
createFetchWithUndiciTimeout: vitest.fn(() => mockFetchFn),
}))
import { getApiRequestTimeout } from "../utils/timeout-config"
import { createFetchWithUndiciTimeout } from "../utils/undici-fetch"
// Mock OpenAI and AzureOpenAI
const mockOpenAIConstructor = vitest.fn()
const mockAzureOpenAIConstructor = vitest.fn()
vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
AzureOpenAI: vitest.fn().mockImplementation((config) => {
mockAzureOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})
describe("OpenAiHandler timeout configuration", () => {
beforeEach(() => {
vitest.clearAllMocks()
})
it("should use default timeout for standard OpenAI", () => {
;(getApiRequestTimeout as any).mockReturnValue(600000)
const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiApiKey: "test-key",
}
new OpenAiHandler(options)
expect(getApiRequestTimeout).toHaveBeenCalled()
expect(createFetchWithUndiciTimeout).toHaveBeenCalled()
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://api.openai.com/v1",
apiKey: "test-key",
timeout: 600000, // 600 seconds in milliseconds
fetch: mockFetchFn,
}),
)
})
it("should use custom timeout for OpenAI-compatible providers", () => {
;(getApiRequestTimeout as any).mockReturnValue(1800000) // 30 minutes
const options: ApiHandlerOptions = {
apiModelId: "custom-model",
openAiModelId: "custom-model",
openAiBaseUrl: "http://localhost:8080/v1",
openAiApiKey: "test-key",
}
new OpenAiHandler(options)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:8080/v1",
timeout: 1800000, // 1800 seconds in milliseconds
}),
)
})
it("should use timeout for Azure OpenAI", () => {
;(getApiRequestTimeout as any).mockReturnValue(900000) // 15 minutes
const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiBaseUrl: "https://myinstance.openai.azure.com",
openAiApiKey: "test-key",
openAiUseAzure: true,
}
new OpenAiHandler(options)
expect(mockAzureOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 900000, // 900 seconds in milliseconds
fetch: mockFetchFn,
}),
)
})
it("should use timeout for Azure AI Inference", () => {
;(getApiRequestTimeout as any).mockReturnValue(1200000) // 20 minutes
const options: ApiHandlerOptions = {
apiModelId: "deepseek",
openAiModelId: "deepseek",
openAiBaseUrl: "https://myinstance.services.ai.azure.com",
openAiApiKey: "test-key",
}
new OpenAiHandler(options)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 1200000, // 1200 seconds in milliseconds
}),
)
})
it("should handle zero timeout (no timeout)", () => {
;(getApiRequestTimeout as any).mockReturnValue(0)
const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
}
new OpenAiHandler(options)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})
})