(
response:
| string
| ((request: {
authorization?: string;
body: Record<string, any> | undefined;
requestIndex: number;
}) => {
status?: number;
body: Record<string, any>;
headers?: Record<string, string>;
})
)
| 358 | }; |
| 359 | |
| 360 | export const startMockOpenAiServer = async ( |
| 361 | response: |
| 362 | | string |
| 363 | | ((request: { |
| 364 | authorization?: string; |
| 365 | body: Record<string, any> | undefined; |
| 366 | requestIndex: number; |
| 367 | }) => { |
| 368 | status?: number; |
| 369 | body: Record<string, any>; |
| 370 | headers?: Record<string, string>; |
| 371 | }) |
| 372 | ): Promise<{ |
| 373 | authHeaders: string[]; |
| 374 | requestBodies: Array<Record<string, any>>; |
| 375 | baseUrl: string; |
| 376 | cleanup: () => Promise<void>; |
| 377 | }> => { |
| 378 | const authHeaders: string[] = []; |
| 379 | const requestBodies: Array<Record<string, any>> = []; |
| 380 | |
| 381 | const server = http.createServer((req, res) => { |
| 382 | const authorization = req.headers.authorization; |
| 383 | if (authorization) { |
| 384 | authHeaders.push( |
| 385 | Array.isArray(authorization) ? authorization[0] : authorization |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | const chunks: Buffer[] = []; |
| 390 | req.on('data', (chunk) => { |
| 391 | chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); |
| 392 | }); |
| 393 | req.on('end', () => { |
| 394 | const rawBody = Buffer.concat(chunks).toString('utf8'); |
| 395 | let parsedBody: Record<string, any> | undefined; |
| 396 | if (rawBody) { |
| 397 | try { |
| 398 | parsedBody = JSON.parse(rawBody); |
| 399 | requestBodies.push(parsedBody); |
| 400 | } catch { |
| 401 | requestBodies.push({ rawBody }); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (req.method === 'POST' && req.url?.includes('/chat/completions')) { |
| 406 | const payload = |
| 407 | typeof response === 'string' |
| 408 | ? { |
| 409 | status: 200, |
| 410 | body: { |
| 411 | choices: [ |
| 412 | { |
| 413 | message: { |
| 414 | content: response |
| 415 | } |
| 416 | } |
| 417 | ] |
no test coverage detected
searching dependent graphs…