Elffuss BraidHub

kikocis/continue public

main · 2.7 KiBRawPermalink
1import { vi } from "vitest";
2
3// Mock BaseLlmApi class to avoid inline class definitions
4class MockBaseLlmApi {}
5
6/**
7 * Mock the constructLlmApi function to return a mock LLM API
8 */
9export function mockLLMResponse(response: string) {
10 const mockApi = {
11 chatCompletionStream: async function* () {
12 // Simulate streaming response
13 const words = response.split(" ");
14 for (const word of words) {
15 yield {
16 choices: [
17 {
18 delta: {
19 content: word + " ",
20 },
21 },
22 ],
23 };
24 }
25 },
26
27 chatCompletion: async () => ({
28 choices: [
29 {
30 message: {
31 role: "assistant",
32 content: response,
33 },
34 },
35 ],
36 }),
37
38 // Add any other methods that might be used
39 completionStream: async function* () {
40 yield response;
41 },
42
43 completion: async () => response,
44 };
45
46 vi.doMock("@continuedev/openai-adapters", () => ({
47 constructLlmApi: () => mockApi,
48 BaseLlmApi: MockBaseLlmApi,
49 LLMConfig: {},
50 }));
51
52 return mockApi;
53}
54
55/**
56 * Mock streaming responses with chunks
57 */
58export function mockLLMStreamResponse(chunks: string[]) {
59 const mockApi = {
60 chatCompletionStream: async function* () {
61 for (const chunk of chunks) {
62 yield {
63 choices: [
64 {
65 delta: {
66 content: chunk,
67 },
68 },
69 ],
70 };
71 }
72 },
73
74 chatCompletion: async () => ({
75 choices: [
76 {
77 message: {
78 role: "assistant",
79 content: chunks.join(""),
80 },
81 },
82 ],
83 }),
84
85 completionStream: async function* () {
86 for (const chunk of chunks) {
87 yield chunk;
88 }
89 },
90
91 completion: async () => chunks.join(""),
92 };
93
94 vi.doMock("@continuedev/openai-adapters", () => ({
95 constructLlmApi: () => mockApi,
96 BaseLlmApi: MockBaseLlmApi,
97 LLMConfig: {},
98 }));
99
100 return mockApi;
101}
102
103/**
104 * Mock an error response from the LLM
105 */
106export function mockLLMError(errorMessage: string) {
107 const mockApi = {
108 chatCompletionStream: async function* () {
109 throw new Error(errorMessage);
110 },
111
112 chatCompletion: async () => {
113 throw new Error(errorMessage);
114 },
115
116 completionStream: async function* () {
117 throw new Error(errorMessage);
118 },
119
120 completion: async () => {
121 throw new Error(errorMessage);
122 },
123 };
124
125 vi.doMock("@continuedev/openai-adapters", () => ({
126 constructLlmApi: () => mockApi,
127 BaseLlmApi: MockBaseLlmApi,
128 LLMConfig: {},
129 }));
130
131 return mockApi;
132}
133
134/**
135 * Clear all LLM mocks
136 */
137export function clearLLMMocks() {
138 vi.unmock("@continuedev/openai-adapters");
139}