Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 11.3 KiBRawPermalink
1import { beforeEach, describe, expect, test, vi } from "vitest";
2
3// Mock modules
4vi.mock("../auth/workos.js");
5vi.mock("../configLoader.js");
6vi.mock("../args.js");
7vi.mock("../configEnhancer.js");
8vi.mock("./ServiceContainer.js");
9
10import * as workos from "../auth/workos.js";
11import { configEnhancer } from "../configEnhancer.js";
12import * as configLoader from "../configLoader.js";
13
14import { ConfigService } from "./ConfigService.js";
15import { serviceContainer } from "./ServiceContainer.js";
16import { SERVICE_NAMES } from "./types.js";
17
18describe("ConfigService", () => {
19 let service: ConfigService;
20 const mockConfig = {
21 name: "test-assistant",
22 version: "1.0.0",
23 models: [],
24 systemMessage: "Test system message",
25 } as any;
26 const mockApiClient = { get: vi.fn(), post: vi.fn() };
27
28 beforeEach(() => {
29 vi.clearAllMocks();
30 service = new ConfigService();
31 });
32
33 describe("State Management", () => {
34 test("should initialize with config from path", async () => {
35 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
36 config: mockConfig as any,
37 source: { type: "cli-flag", path: "/path/to/config.yaml" } as any,
38 });
39
40 const state = await service.initialize(
41 { accessToken: "token" } as any,
42 "/path/to/config.yaml",
43 "org-123",
44 mockApiClient as any,
45 );
46
47 expect(state).toEqual({
48 config: mockConfig as any,
49 configPath: "/path/to/config.yaml",
50 });
51 });
52
53 test("should initialize with undefined config path", async () => {
54 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
55 config: mockConfig as any,
56 source: { type: "default-agent" } as any,
57 });
58
59 const state = await service.initialize(
60 { accessToken: "token" } as any,
61 undefined,
62 "org-123",
63 mockApiClient as any,
64 );
65
66 expect(state).toEqual({
67 config: mockConfig as any,
68 configPath: undefined,
69 });
70 });
71
72 test("should inject rules into config", async () => {
73 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
74 config: mockConfig as any,
75 source: { type: "cli-flag", path: "/config.yaml" } as any,
76 });
77
78 const expectedConfig = {
79 ...mockConfig,
80 systemMessage:
81 "Test system message\n\nProcessed: rule1\n\nProcessed: rule2",
82 };
83
84 vi.mocked(configEnhancer.enhanceConfig).mockResolvedValue(expectedConfig);
85
86 const state = await service.initialize(
87 { accessToken: "token" } as any,
88 "/config.yaml",
89 "org-123",
90 mockApiClient as any,
91 { rule: ["rule1", "rule2"] },
92 );
93
94 // Verify configEnhancer was called with the right parameters
95 expect(vi.mocked(configEnhancer.enhanceConfig)).toHaveBeenCalledWith(
96 mockConfig,
97 { rule: ["rule1", "rule2"] },
98 );
99
100 expect(state.config).toEqual(expectedConfig);
101 });
102 });
103
104 describe("switchConfig()", () => {
105 test("should switch to new configuration", async () => {
106 // Initialize first
107 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
108 config: mockConfig as any,
109 source: { type: "cli-flag", path: "/old.yaml" } as any,
110 });
111 await service.initialize(
112 { accessToken: "token" } as any,
113 "/old.yaml",
114 "org-123",
115 mockApiClient as any,
116 );
117
118 // Switch to new config
119 const newConfig = { ...mockConfig, name: "new-assistant" } as any;
120 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
121 config: newConfig,
122 source: { type: "cli-flag", path: "/new.yaml" } as any,
123 });
124
125 const state = await service.switchConfig(
126 "/new.yaml",
127 { accessToken: "token" } as any,
128 "org-123",
129 mockApiClient as any,
130 );
131
132 expect(state).toEqual({
133 config: newConfig,
134 configPath: "/new.yaml",
135 });
136 expect(service.getState()).toEqual(state);
137 });
138
139 test("should handle switch config errors", async () => {
140 await service.initialize(
141 { accessToken: "token" } as any,
142 "/old.yaml",
143 "org-123",
144 mockApiClient as any,
145 );
146
147 vi.mocked(configLoader.loadConfiguration).mockRejectedValue(
148 new Error("Config not found"),
149 );
150
151 await expect(
152 service.switchConfig(
153 "/bad.yaml",
154 { accessToken: "token" } as any,
155 "org-123",
156 mockApiClient as any,
157 ),
158 ).rejects.toThrow("Config not found");
159 });
160 });
161
162 describe("reload()", () => {
163 test("should reload current configuration", async () => {
164 // Initialize with a config
165 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
166 config: mockConfig as any,
167 source: { type: "cli-flag", path: "/config.yaml" } as any,
168 });
169 await service.initialize(
170 { accessToken: "token" } as any,
171 "/config.yaml",
172 "org-123",
173 mockApiClient as any,
174 );
175
176 // Modify mock to return updated config
177 const updatedConfig = { ...mockConfig, name: "updated-assistant" } as any;
178 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
179 config: updatedConfig,
180 source: { type: "cli-flag", path: "/config.yaml" } as any,
181 });
182
183 const state = await service.reload(
184 { accessToken: "token" } as any,
185 "org-123",
186 mockApiClient as any,
187 );
188
189 expect(state).toEqual({
190 config: updatedConfig,
191 configPath: "/config.yaml",
192 });
193 });
194
195 test("should throw error if no config path available", async () => {
196 // Initialize without config path
197 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
198 config: mockConfig as any,
199 source: { type: "default-agent" } as any,
200 });
201 await service.initialize(
202 { accessToken: "token" } as any,
203 undefined,
204 "org-123",
205 mockApiClient as any,
206 );
207
208 await expect(
209 service.reload(
210 { accessToken: "token" } as any,
211 "org-123",
212 mockApiClient as any,
213 ),
214 ).rejects.toThrow("No configuration path available for reload");
215 });
216 });
217
218 describe("updateConfigPath()", () => {
219 test("should update config path and reload dependent services", async () => {
220 // Initialize service first
221 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
222 config: mockConfig as any,
223 source: { type: "cli-flag", path: "/old.yaml" } as any,
224 });
225 await service.initialize(
226 { accessToken: "token" } as any,
227 "/old.yaml",
228 "org-123",
229 mockApiClient as any,
230 );
231
232 // Mock service container
233 vi.mocked(workos.loadAuthConfig).mockReturnValue({
234 accessToken: "token",
235 organizationId: "org-123",
236 } as any);
237 vi.mocked(serviceContainer.get).mockResolvedValue({
238 apiClient: mockApiClient,
239 });
240
241 // Mock new config load
242 const newConfig = { ...mockConfig, name: "new-assistant" } as any;
243 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
244 config: newConfig,
245 source: { type: "cli-flag", path: "/new.yaml" } as any,
246 });
247
248 await service.updateConfigPath("/new.yaml");
249
250 expect(service.getState()).toEqual({
251 config: newConfig,
252 configPath: "/new.yaml",
253 });
254 expect(vi.mocked(serviceContainer.set)).toHaveBeenCalledWith(
255 SERVICE_NAMES.CONFIG,
256 expect.objectContaining({ config: newConfig }),
257 );
258 expect(vi.mocked(serviceContainer.reload)).toHaveBeenCalledWith(
259 SERVICE_NAMES.MODEL,
260 );
261 expect(vi.mocked(serviceContainer.reload)).toHaveBeenCalledWith(
262 SERVICE_NAMES.MCP,
263 );
264 });
265
266 test("should handle missing API client", async () => {
267 await service.initialize(
268 { accessToken: "token" } as any,
269 "/old.yaml",
270 "org-123",
271 mockApiClient as any,
272 );
273
274 vi.mocked(workos.loadAuthConfig).mockReturnValue({
275 accessToken: "token",
276 organizationId: "org-123",
277 } as any);
278 vi.mocked(serviceContainer.get).mockResolvedValue({
279 apiClient: null,
280 });
281
282 await expect(service.updateConfigPath("/new.yaml")).rejects.toThrow(
283 "API client not available",
284 );
285 });
286 });
287
288 describe("getDependencies()", () => {
289 test("should declare auth and apiClient dependencies", () => {
290 expect(service.getDependencies()).toEqual(["auth", "apiClient"]);
291 });
292 });
293
294 describe("Event Emission", () => {
295 test("should emit stateChanged when switching config", async () => {
296 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
297 config: mockConfig as any,
298 source: { type: "cli-flag", path: "/old.yaml" } as any,
299 });
300 await service.initialize(
301 { accessToken: "token" } as any,
302 "/old.yaml",
303 "org-123",
304 mockApiClient as any,
305 );
306
307 const listener = vi.fn();
308 service.on("stateChanged", listener);
309
310 const newConfig = { ...mockConfig, name: "new-assistant" } as any;
311 vi.mocked(configLoader.loadConfiguration).mockResolvedValue({
312 config: newConfig,
313 source: { type: "cli-flag", path: "/new.yaml" } as any,
314 });
315
316 await service.switchConfig(
317 "/new.yaml",
318 { accessToken: "token" } as any,
319 "org-123",
320 mockApiClient as any,
321 );
322
323 expect(listener).toHaveBeenCalledWith(
324 expect.objectContaining({ config: newConfig, configPath: "/new.yaml" }),
325 expect.objectContaining({
326 config: mockConfig,
327 configPath: "/old.yaml",
328 }),
329 );
330 });
331
332 test("should emit error on switch failure", async () => {
333 await service.initialize(
334 { accessToken: "token" } as any,
335 "/old.yaml",
336 "org-123",
337 mockApiClient as any,
338 );
339
340 const errorListener = vi.fn();
341 service.on("error", errorListener);
342
343 const error = new Error("Config load failed");
344 vi.mocked(configLoader.loadConfiguration).mockRejectedValue(error);
345
346 await expect(
347 service.switchConfig(
348 "/bad.yaml",
349 { accessToken: "token" } as any,
350 "org-123",
351 mockApiClient as any,
352 ),
353 ).rejects.toThrow();
354 expect(errorListener).toHaveBeenCalledWith(error);
355 });
356 });
357
358 describe("Legacy Compatibility", () => {
359 test("updateConfigPath method exists and has correct signature", () => {
360 // Test that the updateConfigPath method exists and is a function
361 expect(typeof service.updateConfigPath).toBe("function");
362
363 // Test that it accepts the correct parameter types
364 const method = service.updateConfigPath;
365 expect(method).toBeInstanceOf(Function);
366 expect(method.length).toBe(1); // Should accept 1 parameter
367 });
368
369 test("ConfigService has reactive pattern implementation", () => {
370 // Verify that ConfigService has the updateConfigPath method
371 // which is the key method for reactive config switching
372 expect(service).toHaveProperty("updateConfigPath");
373 expect(typeof service.updateConfigPath).toBe("function");
374
375 // The method signature should accept string | undefined
376 // This confirms the reactive pattern is implemented
377 expect(service.updateConfigPath.length).toBe(1);
378 });
379 });
380});