File size: 1,405 Bytes
10d1fd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { beforeEach, describe, it, vi } from "vitest";
import {
  mockPubSub,
  mockTextGenerationUtilities,
  testTextGenerationBehavior,
} from "./testUtils";

vi.stubGlobal("fetch", vi.fn());

vi.mock("./pubSub", () => mockPubSub());
vi.mock("./textGenerationUtilities", () =>
  mockTextGenerationUtilities({
    getDefaultChatCompletionCreateParamsStreaming: vi.fn(),
  }),
);
vi.mock("./searchTokenHash", () => ({
  getSearchTokenHash: vi.fn().mockResolvedValue("token"),
}));
vi.mock("./systemPrompt", () => ({
  getSystemPrompt: vi.fn(() => "system"),
}));

import * as mod from "./textGenerationWithInternalApi";

describe("generateTextWithInternalApi", () => {
  beforeEach(() => {
    vi.resetAllMocks();
    // biome-ignore lint/suspicious/noExplicitAny: Necessary for test mocking
    (mod as any).processStreamResponse = vi
      .fn()
      .mockResolvedValue("streamed message");

    const mockResponse = {
      ok: true,
      body: {
        getReader: vi.fn().mockReturnValue({
          read: vi.fn().mockResolvedValue({ done: true }),
        }),
      },
    };
    // biome-ignore lint/suspicious/noExplicitAny: Necessary for test mocking
    (global.fetch as any) = vi.fn().mockResolvedValue(mockResponse);
  });

  it("calls helpers and updates state", async () => {
    await testTextGenerationBehavior(
      () => mod.generateTextWithInternalApi(),
      "",
    );
  });
});