All files / kernel-language-model-service/src/test-utils mock-fetch.ts

0% Statements 0/10
0% Branches 0/3
0% Functions 0/4
0% Lines 0/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                             
/**
 * Returns a fetch implementation that responds to Open /v1 chat completion requests
 * with a sequence of non-streaming JSON responses (one content string per request).
 *
 * @param responses - Content strings to return, in order, for each request.
 * @param model - Model name to include in the response (default `'test-model'`).
 * @returns A fetch function suitable for use as an endowment.
 */
export const makeMockOpenV1Fetch = (
  responses: string[],
  model = 'test-model',
): typeof globalThis.fetch => {
  let idx = 0;
  return async (_url, _init) => {
    const content = responses[idx] ?? '';
    idx += 1;
    const result = {
      id: `chat-${idx}`,
      model,
      choices: [
        {
          message: { role: 'assistant', content },
          index: 0,
          finish_reason: 'stop',
        },
      ],
      usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
    };
    const bodyText = JSON.stringify(result);
    return {
      ok: true,
      status: 200,
      statusText: 'OK',
      text: async () => bodyText,
      json: async () => JSON.parse(bodyText),
    } as unknown as globalThis.Response;
  };
};