| 1 | import nock from "nock"; |
| 2 | import { ChatCompletionMessageParam } from "openai/resources"; |
| 3 | |
| 4 | export interface MockAPIOptions { |
| 5 | baseURL?: string; |
| 6 | delay?: number; |
| 7 | error?: boolean; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Sets up mock for OpenAI API calls |
| 12 | */ |
| 13 | export function mockOpenAIChat( |
| 14 | response: string, |
| 15 | options: MockAPIOptions = {}, |
| 16 | ): nock.Scope { |
| 17 | const { |
| 18 | baseURL = "https://api.openai.com", |
| 19 | delay = 0, |
| 20 | error = false, |
| 21 | } = options; |
| 22 | |
| 23 | const scope = nock(baseURL).post("/v1/chat/completions").delay(delay); |
| 24 | |
| 25 | if (error) { |
| 26 | return scope.reply(500, { error: { message: "Internal server error" } }); |
| 27 | } |
| 28 | |
| 29 | return scope.reply(200, { |
| 30 | id: "chatcmpl-test", |
| 31 | object: "chat.completion", |
| 32 | created: Date.now(), |
| 33 | model: "gpt-4", |
| 34 | choices: [ |
| 35 | { |
| 36 | index: 0, |
| 37 | message: { |
| 38 | role: "assistant", |
| 39 | content: response, |
| 40 | }, |
| 41 | finish_reason: "stop", |
| 42 | }, |
| 43 | ], |
| 44 | usage: { |
| 45 | prompt_tokens: 10, |
| 46 | completion_tokens: 20, |
| 47 | total_tokens: 30, |
| 48 | }, |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Sets up mock for streaming OpenAI API calls |
| 54 | */ |
| 55 | export function mockOpenAIStream( |
| 56 | chunks: string[], |
| 57 | options: MockAPIOptions = {}, |
| 58 | ): nock.Scope { |
| 59 | const { |
| 60 | baseURL = "https://api.openai.com", |
| 61 | delay = 0, |
| 62 | error = false, |
| 63 | } = options; |
| 64 | |
| 65 | const scope = nock(baseURL).post("/v1/chat/completions").delay(delay); |
| 66 | |
| 67 | if (error) { |
| 68 | return scope.reply(500, { error: { message: "Internal server error" } }); |
| 69 | } |
| 70 | |
| 71 | return scope.reply( |
| 72 | 200, |
| 73 | () => { |
| 74 | // Return a stream of Server-Sent Events |
| 75 | const events = chunks.map((chunk, index) => { |
| 76 | const data = { |
| 77 | id: `chatcmpl-test`, |
| 78 | object: "chat.completion.chunk", |
| 79 | created: Date.now(), |
| 80 | model: "gpt-4", |
| 81 | choices: [ |
| 82 | { |
| 83 | index: 0, |
| 84 | delta: { |
| 85 | content: chunk, |
| 86 | }, |
| 87 | finish_reason: index === chunks.length - 1 ? "stop" : null, |
| 88 | }, |
| 89 | ], |
| 90 | }; |
| 91 | return `data: ${JSON.stringify(data)}\n\n`; |
| 92 | }); |
| 93 | |
| 94 | events.push("data: [DONE]\n\n"); |
| 95 | return events.join(""); |
| 96 | }, |
| 97 | { |
| 98 | "Content-Type": "text/event-stream", |
| 99 | "Cache-Control": "no-cache", |
| 100 | Connection: "keep-alive", |
| 101 | }, |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Mocks WorkOS authentication endpoints |
| 107 | */ |
| 108 | export function mockWorkOSAuth(options: { |
| 109 | accessToken?: string; |
| 110 | userInfo?: any; |
| 111 | organizations?: any[]; |
| 112 | error?: boolean; |
| 113 | }): void { |
| 114 | const { |
| 115 | accessToken = "test-access-token", |
| 116 | userInfo = { id: "user-123", email: "test@example.com" }, |
| 117 | organizations = [{ id: "org-123", name: "Test Org" }], |
| 118 | error = false, |
| 119 | } = options; |
| 120 | |
| 121 | // Mock token exchange |
| 122 | nock("https://api.workos.com") |
| 123 | .post("/user_management/authenticate") |
| 124 | .reply( |
| 125 | error ? 401 : 200, |
| 126 | error |
| 127 | ? { error: "Invalid credentials" } |
| 128 | : { |
| 129 | access_token: accessToken, |
| 130 | user: userInfo, |
| 131 | }, |
| 132 | ); |
| 133 | |
| 134 | // Mock get user info |
| 135 | nock("https://api.workos.com") |
| 136 | .get("/user_management/users/me") |
| 137 | .matchHeader("authorization", `Bearer ${accessToken}`) |
| 138 | .reply(error ? 401 : 200, error ? { error: "Unauthorized" } : userInfo); |
| 139 | |
| 140 | // Mock list organizations |
| 141 | nock("https://api.workos.com") |
| 142 | .get("/user_management/organizations") |
| 143 | .matchHeader("authorization", `Bearer ${accessToken}`) |
| 144 | .reply( |
| 145 | error ? 401 : 200, |
| 146 | error |
| 147 | ? { error: "Unauthorized" } |
| 148 | : { |
| 149 | data: organizations, |
| 150 | }, |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Creates a mock configuration for testing |
| 156 | */ |
| 157 | export function createMockConfig(overrides: any = {}): any { |
| 158 | return { |
| 159 | name: "Test Assistant", |
| 160 | model: "gpt-4", |
| 161 | provider: "openai", |
| 162 | apiKey: "test-api-key", |
| 163 | systemMessage: "You are a helpful test assistant.", |
| 164 | ...overrides, |
| 165 | }; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Creates mock chat messages |
| 170 | */ |
| 171 | export function createMockMessages( |
| 172 | count: number = 2, |
| 173 | ): ChatCompletionMessageParam[] { |
| 174 | const messages: ChatCompletionMessageParam[] = [ |
| 175 | { role: "system", content: "You are a helpful assistant." }, |
| 176 | ]; |
| 177 | |
| 178 | for (let i = 0; i < count; i++) { |
| 179 | messages.push( |
| 180 | { role: "user", content: `Test message ${i + 1}` }, |
| 181 | { role: "assistant", content: `Test response ${i + 1}` }, |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | return messages; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Cleans up all nock interceptors |
| 190 | */ |
| 191 | export function cleanupMocks(): void { |
| 192 | nock.cleanAll(); |
| 193 | } |