Elffuss BraidHub

kikocis/continue public

main · 12.3 KiBRawPermalink
1import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
2import { describe, expect, beforeEach, vi, test } from "vitest";
3
4// Mock dependencies before imports
5vi.mock("../config.js");
6vi.mock("../auth/workos.js");
7
8import { AuthConfig } from "../auth/workos.js";
9import * as workos from "../auth/workos.js";
10import * as config from "../config.js";
11
12import { ModelService } from "./ModelService.js";
13
14describe("ModelService", () => {
15 let service: ModelService;
16 let mockAssistant: AssistantUnrolled;
17 let mockAuthConfig: AuthConfig;
18 const mockLlmApi = { complete: vi.fn(), stream: vi.fn() };
19
20 beforeEach(() => {
21 vi.clearAllMocks();
22 service = new ModelService();
23
24 mockAssistant = {
25 name: "test-assistant",
26 version: "1.0.0",
27 models: [
28 {
29 provider: "openai",
30 model: "gpt-4",
31 name: "GPT-4",
32 apiKey: "test-key",
33 roles: ["chat"],
34 } as ModelConfig,
35 {
36 provider: "anthropic",
37 model: "claude-3",
38 name: "Claude 3",
39 apiKey: "test-key",
40 roles: ["chat"],
41 } as ModelConfig,
42 {
43 provider: "openai",
44 model: "gpt-3.5",
45 name: "GPT-3.5",
46 apiKey: "test-key",
47 roles: ["embed"],
48 } as ModelConfig,
49 ],
50 } as AssistantUnrolled;
51
52 mockAuthConfig = {
53 accessToken: "test-token",
54 refreshToken: "test-refresh",
55 userEmail: "test@example.com",
56 userId: "test-user",
57 organizationId: "test-org",
58 expiresAt: Date.now() + 3600000, // 1 hour from now
59 };
60 });
61
62 describe("State Management", () => {
63 test("should initialize with default model", async () => {
64 vi.mocked(config.getLlmApi).mockReturnValue([
65 mockLlmApi as any,
66 mockAssistant.models![0] as ModelConfig,
67 ]);
68 vi.mocked(workos.getModelName).mockReturnValue(null);
69
70 const state = await service.initialize(mockAssistant, mockAuthConfig);
71
72 expect(state).toEqual({
73 llmApi: mockLlmApi,
74 model: mockAssistant.models![0],
75 assistant: mockAssistant,
76 authConfig: mockAuthConfig,
77 });
78 });
79
80 test("should initialize with persisted model if valid", async () => {
81 vi.mocked(workos.getModelName).mockReturnValue("Claude 3");
82 vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
83
84 const state = await service.initialize(mockAssistant, mockAuthConfig);
85
86 expect(state.model?.name).toBe("Claude 3");
87 expect(state.llmApi).toStrictEqual(mockLlmApi);
88 });
89
90 test("should fall back to default if persisted model not found", async () => {
91 vi.mocked(workos.getModelName).mockReturnValue("Non-existent Model");
92 vi.mocked(config.getLlmApi).mockReturnValue([
93 mockLlmApi as any,
94 mockAssistant.models![0] as ModelConfig,
95 ]);
96
97 const state = await service.initialize(mockAssistant, mockAuthConfig);
98
99 expect(state.model).toBe(mockAssistant.models![0]);
100 });
101 });
102
103 describe("update()", () => {
104 test("should update model with new assistant config", async () => {
105 // Initialize first
106 vi.mocked(config.getLlmApi).mockReturnValue([
107 mockLlmApi as any,
108 mockAssistant.models![0] as ModelConfig,
109 ]);
110 await service.initialize(mockAssistant, mockAuthConfig);
111
112 // Update with new assistant
113 const newAssistant = {
114 ...mockAssistant,
115 models: [
116 {
117 provider: "openai",
118 model: "gpt-4-turbo",
119 name: "GPT-4 Turbo",
120 apiKey: "new-key",
121 roles: ["chat"],
122 } as ModelConfig,
123 ],
124 };
125 const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
126 vi.mocked(config.getLlmApi).mockReturnValue([
127 newLlmApi as any,
128 newAssistant.models![0] as ModelConfig,
129 ]);
130
131 const state = await service.update(newAssistant, mockAuthConfig);
132
133 expect(state).toEqual({
134 llmApi: newLlmApi,
135 model: newAssistant.models![0],
136 assistant: newAssistant,
137 authConfig: mockAuthConfig,
138 });
139 });
140 });
141
142 describe("switchModel()", () => {
143 test("should switch to a different model by index", async () => {
144 vi.mocked(config.getLlmApi).mockReturnValue([
145 mockLlmApi as any,
146 mockAssistant.models![0] as ModelConfig,
147 ]);
148 await service.initialize(mockAssistant, mockAuthConfig);
149
150 const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
151 vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
152
153 const state = await service.switchModel(1);
154
155 expect(state).toEqual({
156 llmApi: newLlmApi,
157 model: mockAssistant.models![1],
158 assistant: mockAssistant,
159 authConfig: mockAuthConfig,
160 });
161 expect(service.getState()).toEqual(state);
162 });
163
164 test("should throw error for invalid model index", async () => {
165 vi.mocked(config.getLlmApi).mockReturnValue([
166 mockLlmApi as any,
167 mockAssistant.models![0] as ModelConfig,
168 ]);
169 await service.initialize(mockAssistant, mockAuthConfig);
170
171 await expect(service.switchModel(5)).rejects.toThrow(
172 "Invalid model index: 5. Available models: 0-1",
173 );
174 await expect(service.switchModel(-1)).rejects.toThrow(
175 "Invalid model index: -1. Available models: 0-1",
176 );
177 });
178
179 test("should throw error when service not initialized", async () => {
180 await expect(service.switchModel(0)).rejects.toThrow(
181 "ModelService not initialized",
182 );
183 });
184
185 test("should handle continue-proxy provider specially", async () => {
186 const proxyModel = {
187 provider: "continue-proxy",
188 name: "Proxy Model",
189 apiBase: "https://proxy.continue.dev",
190 apiKeyLocation: "env.PROXY_KEY",
191 roles: ["chat"],
192 } as ModelConfig;
193
194 mockAssistant.models = [proxyModel];
195 vi.mocked(config.getLlmApi).mockReturnValue([
196 mockLlmApi as any,
197 proxyModel,
198 ]);
199 await service.initialize(mockAssistant, mockAuthConfig);
200
201 vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
202
203 await service.switchModel(0);
204
205 expect(vi.mocked(config.createLlmApi)).toHaveBeenCalledWith(
206 proxyModel,
207 mockAuthConfig,
208 );
209 });
210 });
211
212 describe("getAvailableChatModels()", () => {
213 test("should return only models with chat role", async () => {
214 vi.mocked(config.getLlmApi).mockReturnValue([
215 mockLlmApi as any,
216 mockAssistant.models![0] as ModelConfig,
217 ]);
218 await service.initialize(mockAssistant, mockAuthConfig);
219
220 const models = service.getAvailableChatModels();
221
222 expect(models).toHaveLength(2);
223 expect(models[0]).toEqual({
224 provider: "openai",
225 name: "GPT-4",
226 index: 0,
227 });
228 expect(models[1]).toEqual({
229 provider: "anthropic",
230 name: "Claude 3",
231 index: 1,
232 });
233 });
234
235 test("should include models with undefined roles", async () => {
236 // Add a model with undefined roles to the assistant
237 const assistantWithUndefinedRoles = {
238 ...mockAssistant,
239 models: [
240 ...mockAssistant.models!,
241 {
242 provider: "local",
243 model: "llama-2",
244 name: "Llama 2",
245 apiKey: "test-key",
246 // roles is undefined
247 } as ModelConfig,
248 ],
249 };
250
251 vi.mocked(config.getLlmApi).mockReturnValue([
252 mockLlmApi as any,
253 assistantWithUndefinedRoles.models![0] as ModelConfig,
254 ]);
255 await service.initialize(assistantWithUndefinedRoles, mockAuthConfig);
256
257 const models = service.getAvailableChatModels();
258
259 // Should include the two chat models and the model with undefined roles
260 expect(models).toHaveLength(3);
261 expect(models[2]).toEqual({
262 provider: "local",
263 name: "Llama 2",
264 index: 2,
265 });
266 });
267 });
268
269 describe("getCurrentModelIndex()", () => {
270 test("should return correct index of current model", async () => {
271 vi.mocked(config.getLlmApi).mockReturnValue([
272 mockLlmApi as any,
273 mockAssistant.models![0] as ModelConfig,
274 ]);
275 await service.initialize(mockAssistant, mockAuthConfig);
276
277 expect(service.getCurrentModelIndex()).toBe(0);
278
279 vi.mocked(config.createLlmApi).mockReturnValue(mockLlmApi as any);
280 await service.switchModel(1);
281 expect(service.getCurrentModelIndex()).toBe(1);
282 });
283
284 test("should return -1 when no model is set", () => {
285 expect(service.getCurrentModelIndex()).toBe(-1);
286 });
287 });
288
289 describe("getModelIndexByName()", () => {
290 test("should find model by name", async () => {
291 vi.mocked(config.getLlmApi).mockReturnValue([
292 mockLlmApi as any,
293 mockAssistant.models![0] as ModelConfig,
294 ]);
295 await service.initialize(mockAssistant, mockAuthConfig);
296
297 expect(service.getModelIndexByName("GPT-4")).toBe(0);
298 expect(service.getModelIndexByName("Claude 3")).toBe(1);
299 expect(service.getModelIndexByName("Non-existent")).toBe(-1);
300 });
301
302 test("should find model by name and provider", async () => {
303 vi.mocked(config.getLlmApi).mockReturnValue([
304 mockLlmApi as any,
305 mockAssistant.models![0] as ModelConfig,
306 ]);
307 await service.initialize(mockAssistant, mockAuthConfig);
308
309 expect(service.getModelIndexByName("GPT-4", "openai")).toBe(0);
310 expect(service.getModelIndexByName("GPT-4", "anthropic")).toBe(-1);
311 });
312 });
313
314 describe("getModelInfo()", () => {
315 test("should return current model info", async () => {
316 vi.mocked(config.getLlmApi).mockReturnValue([
317 mockLlmApi as any,
318 mockAssistant.models![0] as ModelConfig,
319 ]);
320 await service.initialize(mockAssistant, mockAuthConfig);
321
322 expect(service.getModelInfo()).toEqual({
323 provider: "openai",
324 name: "GPT-4",
325 });
326 });
327
328 test("should return null when no model set", () => {
329 expect(service.getModelInfo()).toBeNull();
330 });
331 });
332
333 describe("isReady()", () => {
334 test("should return false when not initialized", () => {
335 expect(service.isReady()).toBe(false);
336 });
337
338 test("should return true when initialized with model and llmApi", async () => {
339 vi.mocked(config.getLlmApi).mockReturnValue([
340 mockLlmApi as any,
341 mockAssistant.models![0] as ModelConfig,
342 ]);
343 await service.initialize(mockAssistant, mockAuthConfig);
344
345 expect(service.isReady()).toBe(true);
346 });
347
348 test("should return false if llmApi is null", async () => {
349 vi.mocked(config.getLlmApi).mockReturnValue([
350 null as any,
351 mockAssistant.models![0] as ModelConfig,
352 ]);
353 await service.initialize(mockAssistant, mockAuthConfig);
354
355 expect(service.isReady()).toBe(false);
356 });
357 });
358
359 describe("getDependencies()", () => {
360 test("should declare auth and config dependencies", () => {
361 expect(service.getDependencies()).toEqual(["auth", "config"]);
362 });
363 });
364
365 describe("Event Emission", () => {
366 test("should emit stateChanged on model switch", async () => {
367 vi.mocked(config.getLlmApi).mockReturnValue([
368 mockLlmApi as any,
369 mockAssistant.models![0] as ModelConfig,
370 ]);
371 await service.initialize(mockAssistant, mockAuthConfig);
372
373 const listener = vi.fn();
374 service.on("stateChanged", listener);
375
376 const newLlmApi = { complete: vi.fn(), stream: vi.fn() };
377 vi.mocked(config.createLlmApi).mockReturnValue(newLlmApi as any);
378 await service.switchModel(1);
379
380 expect(listener).toHaveBeenCalledWith(
381 expect.objectContaining({
382 model: mockAssistant.models![1],
383 llmApi: newLlmApi,
384 }),
385 expect.objectContaining({
386 model: mockAssistant.models![0],
387 llmApi: mockLlmApi,
388 }),
389 );
390 });
391
392 test("should emit error on switch failure", async () => {
393 vi.mocked(config.getLlmApi).mockReturnValue([
394 mockLlmApi as any,
395 mockAssistant.models![0] as ModelConfig,
396 ]);
397 await service.initialize(mockAssistant, mockAuthConfig);
398
399 const errorListener = vi.fn();
400 service.on("error", errorListener);
401
402 vi.mocked(config.createLlmApi).mockReturnValue(null as any);
403
404 await expect(service.switchModel(1)).rejects.toThrow(
405 "Failed to initialize LLM",
406 );
407 expect(errorListener).toHaveBeenCalledWith(expect.any(Error));
408 });
409 });
410});