Elffuss BraidHub

kikocis/continue public

main · 16.6 KiBRawPermalink
1import { beforeEach, describe, expect, it, MockedFunction, vi } from "vitest";
2import { contextProviderClassFromName } from "../context/providers";
3import CurrentFileContextProvider from "../context/providers/CurrentFileContextProvider";
4import DiffContextProvider from "../context/providers/DiffContextProvider";
5import FileContextProvider from "../context/providers/FileContextProvider";
6import ProblemsContextProvider from "../context/providers/ProblemsContextProvider";
7import RulesContextProvider from "../context/providers/RulesContextProvider";
8import TerminalContextProvider from "../context/providers/TerminalContextProvider";
9import { loadConfigContextProviders } from "./loadContextProviders";
10
11// Mock the contextProviderClassFromName function
12vi.mock("../context/providers", () => ({
13 contextProviderClassFromName: vi.fn(),
14}));
15
16// Mock all provider classes
17vi.mock("../context/providers/CurrentFileContextProvider", () => ({
18 default: vi.fn().mockImplementation((options) => ({
19 description: { title: "current-file" },
20 options,
21 })),
22}));
23
24vi.mock("../context/providers/DiffContextProvider", () => ({
25 default: vi.fn().mockImplementation((options) => ({
26 description: { title: "diff" },
27 options,
28 })),
29}));
30
31vi.mock("../context/providers/FileContextProvider", () => ({
32 default: vi.fn().mockImplementation((options) => ({
33 description: { title: "file" },
34 options,
35 })),
36}));
37
38vi.mock("../context/providers/ProblemsContextProvider", () => {
39 const MockProblemsContextProvider = vi.fn().mockImplementation((options) => ({
40 description: { title: "problems" },
41 options,
42 }));
43 (MockProblemsContextProvider as any).description = { title: "problems" };
44 return { default: MockProblemsContextProvider };
45});
46
47vi.mock("../context/providers/RulesContextProvider", () => ({
48 default: vi.fn().mockImplementation((options) => ({
49 description: { title: "rules" },
50 options,
51 })),
52}));
53
54vi.mock("../context/providers/TerminalContextProvider", () => {
55 const MockTerminalContextProvider = vi.fn().mockImplementation((options) => ({
56 description: { title: "terminal" },
57 options,
58 }));
59 (MockTerminalContextProvider as any).description = { title: "terminal" };
60 return { default: MockTerminalContextProvider };
61});
62
63const mockedContextProviderClassFromName =
64 contextProviderClassFromName as MockedFunction<
65 typeof contextProviderClassFromName
66 >;
67
68beforeEach(() => {
69 vi.clearAllMocks();
70});
71
72describe("loadConfigContextProviders", () => {
73 describe("with empty or undefined config", () => {
74 it("should return only default providers when config is undefined", () => {
75 const result = loadConfigContextProviders(undefined, false, "vscode");
76
77 expect(result.errors).toEqual([]);
78 expect(result.providers).toHaveLength(6);
79
80 // Verify all default providers are included
81 const providerTitles = result.providers.map((p) => p.description.title);
82 expect(providerTitles).toEqual(
83 expect.arrayContaining([
84 "file",
85 "current-file",
86 "diff",
87 "terminal",
88 "problems",
89 "rules",
90 ]),
91 );
92 });
93
94 it("should return only default providers when config is empty array", () => {
95 const result = loadConfigContextProviders([], false, "vscode");
96
97 expect(result.errors).toEqual([]);
98 expect(result.providers).toHaveLength(6);
99
100 const providerTitles = result.providers.map((p) => p.description.title);
101 expect(providerTitles).toEqual(
102 expect.arrayContaining([
103 "file",
104 "current-file",
105 "diff",
106 "terminal",
107 "problems",
108 "rules",
109 ]),
110 );
111 });
112
113 it("should exclude problems and terminal in jetbrains", () => {
114 const result = loadConfigContextProviders([], false, "jetbrains");
115
116 expect(result.errors).toEqual([]);
117 expect(result.providers).toHaveLength(4);
118
119 const providerTitles = result.providers.map((p) => p.description.title);
120 expect(providerTitles).toEqual(
121 expect.arrayContaining(["file", "current-file", "diff", "rules"]),
122 );
123 });
124 });
125
126 it("should return default + docs when has docs is true", () => {
127 const result = loadConfigContextProviders([], true, "vscode");
128
129 expect(result.errors).toEqual([]);
130 expect(result.providers).toHaveLength(7);
131
132 const providerTitles = result.providers.map((p) => p.description.title);
133 expect(providerTitles).toEqual(
134 expect.arrayContaining([
135 "file",
136 "current-file",
137 "diff",
138 "terminal",
139 "problems",
140 "rules",
141 "docs",
142 ]),
143 );
144 });
145});
146
147describe("with valid config", () => {
148 it("should load providers from config with correct parameters", () => {
149 const mockProvider = vi.fn().mockImplementation((options) => ({
150 description: { title: "custom-provider" },
151 options,
152 })) as any;
153
154 mockedContextProviderClassFromName.mockReturnValue(mockProvider);
155
156 const config = [
157 {
158 provider: "custom-provider",
159 name: "my-custom-provider",
160 params: {
161 apiKey: "test-key",
162 baseUrl: "https://api.example.com",
163 },
164 },
165 ];
166
167 const result = loadConfigContextProviders(config, false, "vscode");
168
169 expect(result.errors).toEqual([]);
170 expect(mockedContextProviderClassFromName).toHaveBeenCalledWith(
171 "custom-provider",
172 );
173 expect(mockProvider).toHaveBeenCalledWith({
174 name: "my-custom-provider",
175 apiKey: "test-key",
176 baseUrl: "https://api.example.com",
177 });
178
179 // Should have custom provider + all defaults
180 expect(result.providers).toHaveLength(7);
181
182 const customProvider = result.providers.find(
183 (p) => p.description.title === "custom-provider",
184 );
185 expect(customProvider).toBeDefined();
186 expect((customProvider as any)?.options).toEqual({
187 name: "my-custom-provider",
188 apiKey: "test-key",
189 baseUrl: "https://api.example.com",
190 });
191 });
192
193 it("should handle multiple providers in config", () => {
194 const mockProvider1 = vi.fn().mockImplementation((options) => ({
195 description: { title: "provider-1" },
196 options,
197 })) as any;
198
199 const mockProvider2 = vi.fn().mockImplementation((options) => ({
200 description: { title: "provider-2" },
201 options,
202 })) as any;
203
204 mockedContextProviderClassFromName
205 .mockReturnValueOnce(mockProvider1)
206 .mockReturnValueOnce(mockProvider2);
207
208 const config = [
209 {
210 provider: "provider-1",
211 name: "first-provider",
212 params: { setting1: "value1" },
213 },
214 {
215 provider: "provider-2",
216 name: "second-provider",
217 params: { setting2: "value2" },
218 },
219 ];
220
221 const result = loadConfigContextProviders(config, false, "vscode");
222
223 expect(result.errors).toEqual([]);
224 expect(result.providers).toHaveLength(8); // 2 custom + 6 defaults
225
226 expect(mockProvider1).toHaveBeenCalledWith({
227 name: "first-provider",
228 setting1: "value1",
229 });
230
231 expect(mockProvider2).toHaveBeenCalledWith({
232 name: "second-provider",
233 setting2: "value2",
234 });
235 });
236
237 it("should handle config with empty params", () => {
238 const mockProvider = vi.fn().mockImplementation((options) => ({
239 description: { title: "no-params-provider" },
240 options,
241 })) as any;
242
243 mockedContextProviderClassFromName.mockReturnValue(mockProvider);
244
245 const config = [
246 {
247 provider: "no-params-provider",
248 name: "simple-provider",
249 params: {},
250 },
251 ];
252
253 const result = loadConfigContextProviders(config, false, "vscode");
254
255 expect(result.errors).toEqual([]);
256 expect(mockProvider).toHaveBeenCalledWith({
257 name: "simple-provider",
258 });
259 });
260
261 it("should handle config without params property", () => {
262 const mockProvider = vi.fn().mockImplementation((options) => ({
263 description: { title: "provider-without-params" },
264 options,
265 })) as any;
266
267 mockedContextProviderClassFromName.mockReturnValue(mockProvider);
268
269 const config = [
270 {
271 provider: "provider-without-params",
272 name: "minimal-provider",
273 } as any, // Cast to bypass type checking for test
274 ];
275
276 const result = loadConfigContextProviders(config, false, "vscode");
277
278 expect(result.errors).toEqual([]);
279 expect(mockProvider).toHaveBeenCalledWith({
280 name: "minimal-provider",
281 });
282 });
283});
284
285describe("error handling", () => {
286 it("should add error for unknown provider", () => {
287 mockedContextProviderClassFromName.mockReturnValue(undefined);
288
289 const config = [
290 {
291 provider: "unknown-provider",
292 name: "test-provider",
293 params: {},
294 },
295 ];
296
297 const result = loadConfigContextProviders(config, false, "vscode");
298
299 expect(result.errors).toEqual([
300 {
301 fatal: false,
302 message: "Unknown context provider unknown-provider",
303 },
304 ]);
305
306 // Should still have default providers
307 expect(result.providers).toHaveLength(6);
308 expect(mockedContextProviderClassFromName).toHaveBeenCalledWith(
309 "unknown-provider",
310 );
311 });
312
313 it("should handle multiple unknown providers", () => {
314 mockedContextProviderClassFromName.mockReturnValue(undefined);
315
316 const config = [
317 {
318 provider: "unknown-1",
319 name: "test-1",
320 params: {},
321 },
322 {
323 provider: "unknown-2",
324 name: "test-2",
325 params: {},
326 },
327 ];
328
329 const result = loadConfigContextProviders(config, false, "vscode");
330
331 expect(result.errors).toEqual([
332 {
333 fatal: false,
334 message: "Unknown context provider unknown-1",
335 },
336 {
337 fatal: false,
338 message: "Unknown context provider unknown-2",
339 },
340 ]);
341
342 expect(result.providers).toHaveLength(6); // Only defaults
343 });
344
345 it("should handle mix of valid and invalid providers", () => {
346 const mockValidProvider = vi.fn().mockImplementation((options) => ({
347 description: { title: "valid-provider" },
348 options,
349 })) as any;
350
351 mockedContextProviderClassFromName
352 .mockReturnValueOnce(mockValidProvider)
353 .mockReturnValueOnce(undefined);
354
355 const config = [
356 {
357 provider: "valid-provider",
358 name: "valid",
359 params: { key: "value" },
360 },
361 {
362 provider: "invalid-provider",
363 name: "invalid",
364 params: {},
365 },
366 ];
367
368 const result = loadConfigContextProviders(config, false, "vscode");
369
370 expect(result.errors).toEqual([
371 {
372 fatal: false,
373 message: "Unknown context provider invalid-provider",
374 },
375 ]);
376
377 // Should have valid provider + defaults
378 expect(result.providers).toHaveLength(7);
379 expect(mockValidProvider).toHaveBeenCalledWith({
380 name: "valid",
381 key: "value",
382 });
383 });
384});
385
386describe("default provider merging", () => {
387 it("should not duplicate providers when config matches defaults", () => {
388 // Mock FileContextProvider to be returned by contextProviderClassFromName
389 mockedContextProviderClassFromName.mockReturnValue(FileContextProvider);
390
391 const config = [
392 {
393 provider: "file",
394 name: "custom-file-provider",
395 params: { maxFiles: 100 },
396 },
397 ];
398
399 const result = loadConfigContextProviders(config, false, "vscode");
400
401 expect(result.errors).toEqual([]);
402
403 // Should have configured file provider + other defaults (not duplicate file)
404 expect(result.providers).toHaveLength(6);
405
406 const fileProviders = result.providers.filter(
407 (p) => p.description.title === "file",
408 );
409 expect(fileProviders).toHaveLength(1);
410
411 // The configured one should be used (with custom params)
412 expect(FileContextProvider).toHaveBeenCalledWith({
413 name: "custom-file-provider",
414 maxFiles: 100,
415 });
416 });
417
418 it("should add default providers not present in config", () => {
419 const mockCustomProvider = vi.fn().mockImplementation((options) => ({
420 description: { title: "custom-only" },
421 options,
422 })) as any;
423
424 mockedContextProviderClassFromName.mockReturnValue(mockCustomProvider);
425
426 const config = [
427 {
428 provider: "custom-only",
429 name: "custom",
430 params: {},
431 },
432 ];
433
434 const result = loadConfigContextProviders(config, false, "vscode");
435
436 expect(result.errors).toEqual([]);
437 expect(result.providers).toHaveLength(7); // 1 custom + 6 defaults
438
439 // All defaults should be present
440 const providerTitles = result.providers.map((p) => p.description.title);
441 expect(providerTitles).toEqual(
442 expect.arrayContaining([
443 "custom-only",
444 "file",
445 "current-file",
446 "diff",
447 "terminal",
448 "problems",
449 "rules",
450 ]),
451 );
452 });
453
454 it("should preserve order with configured providers first", () => {
455 const mockProvider = vi.fn().mockImplementation((options) => ({
456 description: { title: "custom-provider" },
457 options,
458 })) as any;
459
460 mockedContextProviderClassFromName.mockReturnValue(mockProvider);
461
462 const config = [
463 {
464 provider: "custom-provider",
465 name: "custom",
466 params: {},
467 },
468 ];
469
470 const result = loadConfigContextProviders(config, false, "vscode");
471
472 expect(result.providers[0].description.title).toBe("custom-provider");
473
474 // Rest should be defaults in their original order
475 const defaultTitles = result.providers
476 .slice(1)
477 .map((p) => p.description.title);
478 expect(defaultTitles).toEqual([
479 "file",
480 "current-file",
481 "diff",
482 "terminal",
483 "problems",
484 "rules",
485 ]);
486 });
487});
488
489describe("edge cases", () => {
490 it("should handle provider constructor throwing error", () => {
491 const mockProvider = vi.fn().mockImplementation(() => {
492 throw new Error("Provider construction failed");
493 }) as any;
494
495 mockedContextProviderClassFromName.mockReturnValue(mockProvider);
496
497 const config = [
498 {
499 provider: "failing-provider",
500 name: "failing",
501 params: {},
502 },
503 ];
504
505 expect(() => loadConfigContextProviders(config, false, "vscode")).toThrow(
506 "Provider construction failed",
507 );
508 });
509
510 it("should handle null provider class", () => {
511 mockedContextProviderClassFromName.mockReturnValue(undefined);
512
513 const config = [
514 {
515 provider: "null-provider",
516 name: "null",
517 params: {},
518 },
519 ];
520
521 const result = loadConfigContextProviders(config, false, "vscode");
522
523 expect(result.errors).toEqual([
524 {
525 fatal: false,
526 message: "Unknown context provider null-provider",
527 },
528 ]);
529 });
530
531 it("should handle provider with same title as default but different implementation", () => {
532 // Mock a provider with same title as a default
533 const mockFileProvider = vi.fn().mockImplementation((options) => ({
534 description: { title: "file" }, // Same title as FileContextProvider
535 options,
536 customProperty: "custom-implementation",
537 })) as any;
538
539 mockedContextProviderClassFromName.mockReturnValue(mockFileProvider);
540
541 const config = [
542 {
543 provider: "custom-file",
544 name: "my-file-provider",
545 params: { customParam: "value" },
546 },
547 ];
548
549 const result = loadConfigContextProviders(config, false, "vscode");
550
551 expect(result.errors).toEqual([]);
552
553 // Should not duplicate - custom implementation should be used
554 const fileProviders = result.providers.filter(
555 (p) => p.description.title === "file",
556 );
557 expect(fileProviders).toHaveLength(1);
558
559 const fileProvider = fileProviders[0] as any;
560 expect(fileProvider.customProperty).toBe("custom-implementation");
561 expect(fileProvider.options).toEqual({
562 name: "my-file-provider",
563 customParam: "value",
564 });
565 });
566});
567
568describe("provider instantiation", () => {
569 it("should call default provider constructors with empty options", () => {
570 loadConfigContextProviders([], false, "vscode");
571
572 expect(FileContextProvider).toHaveBeenCalledWith({});
573 expect(CurrentFileContextProvider).toHaveBeenCalledWith({});
574 expect(DiffContextProvider).toHaveBeenCalledWith({});
575 expect(TerminalContextProvider).toHaveBeenCalledWith({});
576 expect(ProblemsContextProvider).toHaveBeenCalledWith({});
577 expect(RulesContextProvider).toHaveBeenCalledWith({});
578 });
579
580 it("should call provider constructors exactly once per provider", () => {
581 loadConfigContextProviders([], false, "vscode");
582
583 expect(FileContextProvider).toHaveBeenCalledTimes(1);
584 expect(CurrentFileContextProvider).toHaveBeenCalledTimes(1);
585 expect(DiffContextProvider).toHaveBeenCalledTimes(1);
586 expect(TerminalContextProvider).toHaveBeenCalledTimes(1);
587 expect(ProblemsContextProvider).toHaveBeenCalledTimes(1);
588 expect(RulesContextProvider).toHaveBeenCalledTimes(1);
589 });
590});