MCPcopy
hub / github.com/di-sukharev/opencommit / startMockOpenAiServer

Function startMockOpenAiServer

test/e2e/utils.ts:360–462  ·  view source on GitHub ↗
(
  response:
    | string
    | ((request: {
        authorization?: string;
        body: Record<string, any> | undefined;
        requestIndex: number;
      }) => {
        status?: number;
        body: Record<string, any>;
        headers?: Record<string, string>;
      })
)

Source from the content-addressed store, hash-verified

358};
359
360export 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 ]

Callers 3

gitPush.test.tsFile · 0.90
oneFile.test.tsFile · 0.90

Calls 10

rejectFunction · 0.85
concatMethod · 0.80
includesMethod · 0.80
endMethod · 0.80
pushMethod · 0.45
onMethod · 0.45
fromMethod · 0.45
toStringMethod · 0.45
parseMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…