Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

providers.test.ts 25 KB

You have to be logged in to leave a comment. Sign In
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  1. import fs from 'fs';
  2. import path from 'path';
  3. import yaml from 'js-yaml';
  4. import cliState from '../src/cliState';
  5. import { CLOUD_PROVIDER_PREFIX } from '../src/constants';
  6. import { loadApiProvider, loadApiProviders } from '../src/providers';
  7. import { HttpProvider } from '../src/providers/http';
  8. import { OpenAiChatCompletionProvider } from '../src/providers/openai/chat';
  9. import { OpenAiEmbeddingProvider } from '../src/providers/openai/embedding';
  10. import { PythonProvider } from '../src/providers/pythonCompletion';
  11. import { ScriptCompletionProvider } from '../src/providers/scriptCompletion';
  12. import { WebSocketProvider } from '../src/providers/websocket';
  13. import { getCloudDatabaseId, getProviderFromCloud, isCloudProvider } from '../src/util/cloud';
  14. import type { ProviderOptions } from '../src/types';
  15. jest.mock('fs');
  16. jest.mock('js-yaml');
  17. jest.mock('../src/fetch');
  18. jest.mock('../src/providers/http');
  19. jest.mock('../src/providers/openai/chat');
  20. jest.mock('../src/providers/openai/embedding');
  21. jest.mock('../src/providers/pythonCompletion');
  22. jest.mock('../src/providers/scriptCompletion');
  23. jest.mock('../src/providers/websocket');
  24. jest.mock('../src/util/cloud');
  25. describe('loadApiProvider', () => {
  26. beforeEach(() => {
  27. jest.resetAllMocks();
  28. jest.spyOn(process, 'exit').mockImplementation((() => {}) as any);
  29. // Mock the cloud utility functions
  30. jest
  31. .mocked(isCloudProvider)
  32. .mockImplementation((path: string) => path.startsWith('promptfoo://provider/'));
  33. jest
  34. .mocked(getCloudDatabaseId)
  35. .mockImplementation((path: string) => path.slice('promptfoo://provider/'.length));
  36. });
  37. it('should load echo provider', async () => {
  38. const provider = await loadApiProvider('echo');
  39. expect(provider.id()).toBe('echo');
  40. await expect(provider.callApi('test')).resolves.toEqual({
  41. output: 'test',
  42. raw: 'test',
  43. cost: 0,
  44. cached: false,
  45. isRefusal: false,
  46. tokenUsage: {
  47. total: 0,
  48. prompt: 0,
  49. completion: 0,
  50. },
  51. metadata: {},
  52. });
  53. });
  54. it('should load file provider from yaml', async () => {
  55. const yamlContent: ProviderOptions = {
  56. id: 'openai:chat:gpt-4',
  57. config: {
  58. apiKey: 'test-key',
  59. temperature: 0.7,
  60. },
  61. };
  62. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  63. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  64. const provider = await loadApiProvider('file://test.yaml', {
  65. basePath: '/test',
  66. });
  67. expect(fs.readFileSync).toHaveBeenCalledWith(path.join('/test', 'test.yaml'), 'utf8');
  68. expect(yaml.load).toHaveBeenCalledWith('yaml content');
  69. expect(provider).toBeDefined();
  70. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
  71. });
  72. it('should load file provider from json', async () => {
  73. const jsonContent: ProviderOptions = {
  74. id: 'openai:chat:gpt-4',
  75. config: {
  76. apiKey: 'test-key',
  77. },
  78. };
  79. jest.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(jsonContent));
  80. jest.mocked(yaml.load).mockReturnValue(jsonContent);
  81. const provider = await loadApiProvider('file://test.json', {
  82. basePath: '/test',
  83. });
  84. expect(fs.readFileSync).toHaveBeenCalledWith(path.join('/test', 'test.json'), 'utf8');
  85. expect(yaml.load).toHaveBeenCalledWith(JSON.stringify(jsonContent));
  86. expect(provider).toBeDefined();
  87. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
  88. });
  89. it('should load Provider from cloud', async () => {
  90. jest.mocked(getProviderFromCloud).mockResolvedValue({
  91. id: 'openai:chat:gpt-4',
  92. config: {
  93. apiKey: 'test-key',
  94. temperature: 0.7,
  95. },
  96. });
  97. const provider = await loadApiProvider(`${CLOUD_PROVIDER_PREFIX}123`);
  98. expect(provider).toBeDefined();
  99. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', {
  100. config: expect.objectContaining({
  101. apiKey: 'test-key',
  102. temperature: 0.7,
  103. }),
  104. id: 'openai:chat:gpt-4',
  105. });
  106. });
  107. it('should load OpenAI chat provider', async () => {
  108. const provider = await loadApiProvider('openai:chat:gpt-4.1');
  109. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4.1', expect.any(Object));
  110. expect(provider).toBeDefined();
  111. });
  112. it('should load OpenAI GPT-5 chat provider', async () => {
  113. const provider = await loadApiProvider('openai:chat:gpt-5');
  114. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-5', expect.any(Object));
  115. expect(provider).toBeDefined();
  116. });
  117. it('should load OpenAI GPT-5 chat latest provider', async () => {
  118. const provider = await loadApiProvider('openai:chat:gpt-5-chat-latest');
  119. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  120. 'gpt-5-chat-latest',
  121. expect.any(Object),
  122. );
  123. expect(provider).toBeDefined();
  124. });
  125. it('should load OpenAI GPT-5 nano chat provider', async () => {
  126. const provider = await loadApiProvider('openai:chat:gpt-5-nano');
  127. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-5-nano', expect.any(Object));
  128. expect(provider).toBeDefined();
  129. });
  130. it('should load OpenAI GPT-5 mini chat provider', async () => {
  131. const provider = await loadApiProvider('openai:chat:gpt-5-mini');
  132. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-5-mini', expect.any(Object));
  133. expect(provider).toBeDefined();
  134. });
  135. it('should load OpenAI chat provider with default model', async () => {
  136. const provider = await loadApiProvider('openai:chat');
  137. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  138. 'gpt-4.1-2025-04-14',
  139. expect.any(Object),
  140. );
  141. expect(provider).toBeDefined();
  142. });
  143. it('should load OpenAI embedding provider', async () => {
  144. const provider = await loadApiProvider('openai:embedding');
  145. expect(OpenAiEmbeddingProvider).toHaveBeenCalledWith(
  146. 'text-embedding-3-large',
  147. expect.any(Object),
  148. );
  149. expect(provider).toBeDefined();
  150. });
  151. it('should load DeepSeek provider with default model', async () => {
  152. const provider = await loadApiProvider('deepseek:');
  153. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('deepseek-chat', {
  154. config: expect.objectContaining({
  155. apiBaseUrl: 'https://api.deepseek.com/v1',
  156. apiKeyEnvar: 'DEEPSEEK_API_KEY',
  157. }),
  158. });
  159. expect(provider).toBeDefined();
  160. });
  161. it('should load DeepSeek provider with specific model', async () => {
  162. const provider = await loadApiProvider('deepseek:deepseek-coder');
  163. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('deepseek-coder', {
  164. config: expect.objectContaining({
  165. apiBaseUrl: 'https://api.deepseek.com/v1',
  166. apiKeyEnvar: 'DEEPSEEK_API_KEY',
  167. }),
  168. });
  169. expect(provider).toBeDefined();
  170. });
  171. it('should load Hyperbolic provider with specific model', async () => {
  172. const provider = await loadApiProvider('hyperbolic:meta-llama/Meta-Llama-3-8B-Instruct-Turbo');
  173. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  174. 'meta-llama/Meta-Llama-3-8B-Instruct-Turbo',
  175. {
  176. config: expect.objectContaining({
  177. apiBaseUrl: 'https://api.hyperbolic.xyz/v1',
  178. apiKeyEnvar: 'HYPERBOLIC_API_KEY',
  179. }),
  180. },
  181. );
  182. expect(provider).toBeDefined();
  183. });
  184. it('should load GitHub provider with default model', async () => {
  185. const provider = await loadApiProvider('github:');
  186. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('openai/gpt-4.1', {
  187. config: expect.objectContaining({
  188. apiBaseUrl: 'https://models.github.ai',
  189. apiKeyEnvar: 'GITHUB_TOKEN',
  190. }),
  191. });
  192. expect(provider).toBeDefined();
  193. });
  194. it('should load GitHub provider with specific model', async () => {
  195. const provider = await loadApiProvider('github:openai/gpt-4.1-mini');
  196. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('openai/gpt-4.1-mini', {
  197. config: expect.objectContaining({
  198. apiBaseUrl: 'https://models.github.ai',
  199. apiKeyEnvar: 'GITHUB_TOKEN',
  200. }),
  201. });
  202. expect(provider).toBeDefined();
  203. });
  204. it('should load HTTP provider', async () => {
  205. const provider = await loadApiProvider('http://test.com');
  206. expect(HttpProvider).toHaveBeenCalledWith('http://test.com', expect.any(Object));
  207. expect(provider).toBeDefined();
  208. });
  209. it('should load HTTPS provider', async () => {
  210. const provider = await loadApiProvider('https://test.com');
  211. expect(HttpProvider).toHaveBeenCalledWith('https://test.com', expect.any(Object));
  212. expect(provider).toBeDefined();
  213. });
  214. it('should load WebSocket provider', async () => {
  215. const provider = await loadApiProvider('ws://test.com');
  216. expect(WebSocketProvider).toHaveBeenCalledWith('ws://test.com', expect.any(Object));
  217. expect(provider).toBeDefined();
  218. });
  219. it('should load script provider', async () => {
  220. const provider = await loadApiProvider('exec:test.sh');
  221. expect(ScriptCompletionProvider).toHaveBeenCalledWith(
  222. expect.stringMatching(/test\.sh$/),
  223. expect.any(Object),
  224. );
  225. expect(provider).toBeDefined();
  226. });
  227. it('should load Python provider', async () => {
  228. const provider = await loadApiProvider('python:test.py');
  229. expect(PythonProvider).toHaveBeenCalledWith(
  230. expect.stringMatching(/test\.py$/),
  231. expect.any(Object),
  232. );
  233. expect(provider).toBeDefined();
  234. });
  235. it('should load Python provider from file path', async () => {
  236. const provider = await loadApiProvider('file://test.py');
  237. expect(PythonProvider).toHaveBeenCalledWith(
  238. expect.stringMatching(/test\.py$/),
  239. expect.any(Object),
  240. );
  241. expect(provider).toBeDefined();
  242. });
  243. it('should handle unidentified provider', async () => {
  244. await expect(loadApiProvider('unknown:provider')).rejects.toThrow(
  245. 'Could not identify provider',
  246. );
  247. });
  248. it('should load JFrog ML provider', async () => {
  249. const provider = await loadApiProvider('jfrog:test-model');
  250. expect(provider).toBeDefined();
  251. });
  252. it('should handle invalid file path for yaml/json config', async () => {
  253. jest.mocked(fs.readFileSync).mockImplementation(() => {
  254. throw new Error('File not found');
  255. });
  256. await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('File not found');
  257. });
  258. it('should handle invalid yaml content', async () => {
  259. jest.mocked(fs.readFileSync).mockReturnValue('invalid: yaml: content:');
  260. jest.mocked(yaml.load).mockReturnValue(null);
  261. await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('Provider config');
  262. });
  263. it('should handle yaml config without id', async () => {
  264. jest.mocked(fs.readFileSync).mockReturnValue('config:\n key: value');
  265. jest.mocked(yaml.load).mockReturnValue({ config: { key: 'value' } });
  266. await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('must have an id');
  267. });
  268. it('should handle provider with custom base path', async () => {
  269. const mockProvider = {
  270. id: () => 'python:script.py',
  271. config: {
  272. basePath: '/custom/path',
  273. },
  274. callApi: async (input: string) => ({ output: input }),
  275. };
  276. jest.mocked(PythonProvider).mockImplementation(() => mockProvider as any);
  277. const provider = await loadApiProvider('python:script.py', {
  278. basePath: '/custom/path',
  279. options: {
  280. config: {},
  281. },
  282. });
  283. expect(provider.config.basePath).toBe('/custom/path');
  284. });
  285. it('should handle provider with delay', async () => {
  286. const provider = await loadApiProvider('echo', {
  287. options: {
  288. delay: 1000,
  289. },
  290. });
  291. expect(provider.delay).toBe(1000);
  292. });
  293. it('should handle provider with custom label template', async () => {
  294. process.env.CUSTOM_LABEL = 'my-label';
  295. const provider = await loadApiProvider('echo', {
  296. options: {
  297. label: '{{ env.CUSTOM_LABEL }}',
  298. },
  299. });
  300. expect(provider.label).toBe('my-label');
  301. delete process.env.CUSTOM_LABEL;
  302. });
  303. it('should throw error when file provider array is loaded with loadApiProvider', async () => {
  304. const yamlContent = [
  305. {
  306. id: 'openai:chat:gpt-4',
  307. config: { apiKey: 'test-key1' },
  308. },
  309. {
  310. id: 'anthropic:claude-2',
  311. config: { apiKey: 'test-key2' },
  312. },
  313. ];
  314. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  315. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  316. await expect(loadApiProvider('file://test.yaml')).rejects.toThrow(
  317. 'Multiple providers found in test.yaml. Use loadApiProviders instead of loadApiProvider.',
  318. );
  319. });
  320. it('should handle file provider with environment variables', async () => {
  321. process.env.OPENAI_API_KEY = 'test-key-from-env';
  322. const yamlContent: ProviderOptions = {
  323. id: 'openai:chat:gpt-4',
  324. config: {
  325. apiKey: '{{ env.OPENAI_API_KEY }}',
  326. },
  327. env: {
  328. OPENAI_API_KEY: 'override-key',
  329. },
  330. };
  331. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  332. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  333. const provider = await loadApiProvider('file://test.yaml', {
  334. basePath: '/test',
  335. env: { OPENAI_API_KEY: 'final-override-key' },
  336. });
  337. expect(provider).toBeDefined();
  338. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  339. 'gpt-4',
  340. expect.objectContaining({
  341. config: expect.objectContaining({
  342. apiKey: expect.any(String),
  343. }),
  344. env: expect.objectContaining({
  345. OPENAI_API_KEY: 'final-override-key',
  346. }),
  347. }),
  348. );
  349. delete process.env.OPENAI_API_KEY;
  350. });
  351. it('should load multiple providers from yaml file using loadApiProviders', async () => {
  352. const yamlContent = [
  353. {
  354. id: 'openai:chat:gpt-4o-mini',
  355. config: { apiKey: 'test-key1' },
  356. },
  357. {
  358. id: 'anthropic:claude-3-7-sonnet-20250219',
  359. config: { apiKey: 'test-key2' },
  360. },
  361. ];
  362. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  363. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  364. const providers = await loadApiProviders('file://test.yaml');
  365. expect(providers).toHaveLength(2);
  366. expect(providers[0]).toBeDefined();
  367. expect(providers[1]).toBeDefined();
  368. expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('test.yaml'), 'utf8');
  369. expect(yaml.load).toHaveBeenCalledWith('yaml content');
  370. });
  371. it('should handle absolute file paths', async () => {
  372. const yamlContent: ProviderOptions = {
  373. id: 'openai:chat:gpt-4',
  374. config: { apiKey: 'test-key' },
  375. };
  376. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  377. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  378. const absolutePath = path.resolve('/absolute/path/to/providers.yaml');
  379. const provider = await loadApiProvider(`file://${absolutePath}`);
  380. expect(provider).toBeDefined();
  381. expect(fs.readFileSync).toHaveBeenCalledWith(absolutePath, 'utf8');
  382. });
  383. it('should handle provider with null or undefined config values', async () => {
  384. const yamlContent: ProviderOptions = {
  385. id: 'openai:chat:gpt-4',
  386. config: {
  387. apiKey: 'test-key',
  388. nullValue: null,
  389. undefinedValue: undefined,
  390. },
  391. };
  392. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  393. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  394. const provider = await loadApiProvider('file://test.yaml');
  395. expect(provider).toBeDefined();
  396. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  397. 'gpt-4',
  398. expect.objectContaining({
  399. config: expect.objectContaining({
  400. apiKey: 'test-key',
  401. }),
  402. }),
  403. );
  404. });
  405. it('should handle provider with undefined options', async () => {
  406. const provider = await loadApiProvider('echo', {
  407. options: undefined,
  408. });
  409. expect(provider).toBeDefined();
  410. });
  411. it('should throw error for invalid providerPaths type', async () => {
  412. // Test with a number, which is an invalid type
  413. await expect(loadApiProviders(42 as any)).rejects.toThrow('Invalid providers list');
  414. // Test with an object that doesn't match any valid format
  415. await expect(loadApiProviders({ foo: 'bar' } as any)).rejects.toThrow('Invalid providers list');
  416. });
  417. it('should handle non-yaml/json file paths', async () => {
  418. // Test with a text file path
  419. await expect(loadApiProviders('file://test.txt')).rejects.toThrow(
  420. /Could not identify provider/,
  421. );
  422. });
  423. });
  424. describe('loadApiProviders', () => {
  425. beforeEach(() => {
  426. jest.resetAllMocks();
  427. cliState.config = undefined;
  428. });
  429. it('should load single provider from string', async () => {
  430. const providers = await loadApiProviders('echo');
  431. expect(providers).toHaveLength(1);
  432. expect(providers[0].id()).toBe('echo');
  433. });
  434. it('should load multiple providers from array of strings', async () => {
  435. const providers = await loadApiProviders(['echo', 'openai:chat:gpt-4']);
  436. expect(providers).toHaveLength(2);
  437. expect(providers[0].id()).toBe('echo');
  438. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
  439. });
  440. it('should load provider from function', async () => {
  441. const customFunction = async (prompt: string) => ({ output: prompt });
  442. const providers = await loadApiProviders(customFunction);
  443. expect(providers).toHaveLength(1);
  444. expect(providers[0].id()).toBe('custom-function');
  445. await expect(providers[0].callApi('test')).resolves.toEqual({ output: 'test' });
  446. });
  447. it('should load provider from function with label', async () => {
  448. const customFunction = async (prompt: string) => ({ output: prompt });
  449. customFunction.label = 'custom-label';
  450. const providers = await loadApiProviders([customFunction]);
  451. expect(providers).toHaveLength(1);
  452. expect(providers[0].id()).toBe('custom-label');
  453. });
  454. it('should load provider from options object', async () => {
  455. const options: ProviderOptions = {
  456. id: 'openai:chat:gpt-4',
  457. config: {
  458. apiKey: 'test-key',
  459. },
  460. };
  461. const providers = await loadApiProviders([options]);
  462. expect(providers).toHaveLength(1);
  463. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
  464. });
  465. it('should load provider from options map', async () => {
  466. const providers = await loadApiProviders([
  467. {
  468. 'openai:chat:gpt-4': {
  469. config: {
  470. apiKey: 'test-key',
  471. },
  472. },
  473. },
  474. ]);
  475. expect(providers).toHaveLength(1);
  476. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
  477. });
  478. it('should throw error for invalid providers list', async () => {
  479. await expect(loadApiProviders({} as any)).rejects.toThrow('Invalid providers list');
  480. });
  481. it('should handle loadApiProviders with empty array', async () => {
  482. const providers = await loadApiProviders([]);
  483. expect(providers).toHaveLength(0);
  484. });
  485. it('should handle loadApiProviders with mixed provider types', async () => {
  486. const customFunction = async (prompt: string) => ({ output: prompt });
  487. const providers = await loadApiProviders([
  488. 'echo',
  489. customFunction,
  490. { id: 'openai:chat', config: {} },
  491. { 'openai:completion': { config: {} } },
  492. ]);
  493. expect(providers).toHaveLength(4);
  494. });
  495. it('should handle provider with null config', async () => {
  496. const provider = await loadApiProvider('echo', {
  497. options: {
  498. config: null,
  499. },
  500. });
  501. expect(provider).toBeDefined();
  502. });
  503. it('should handle provider with undefined options', async () => {
  504. const provider = await loadApiProvider('echo', {
  505. options: undefined,
  506. });
  507. expect(provider).toBeDefined();
  508. });
  509. it('should handle relative file paths', async () => {
  510. const yamlContent: ProviderOptions = {
  511. id: 'openai:chat:gpt-4',
  512. config: { apiKey: 'test-key' },
  513. };
  514. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  515. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  516. const relativePath = 'relative/path/to/providers.yaml';
  517. const providers = await loadApiProviders(`file://${relativePath}`, {
  518. basePath: '/test/base/path',
  519. });
  520. expect(providers).toHaveLength(1);
  521. expect(fs.readFileSync).toHaveBeenCalledWith(
  522. path.join('/test/base/path', relativePath),
  523. 'utf8',
  524. );
  525. });
  526. it('should handle absolute file paths in loadApiProviders', async () => {
  527. const yamlContent: ProviderOptions = {
  528. id: 'openai:chat:gpt-4',
  529. config: { apiKey: 'test-key' },
  530. };
  531. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  532. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  533. const absolutePath = path.resolve('/absolute/path/to/providers.yaml');
  534. const providers = await loadApiProviders(`file://${absolutePath}`);
  535. expect(providers).toHaveLength(1);
  536. expect(fs.readFileSync).toHaveBeenCalledWith(absolutePath, 'utf8');
  537. expect(yaml.load).toHaveBeenCalledWith('yaml content');
  538. });
  539. it('should load multiple providers from a file specified in a providers array', async () => {
  540. // Setup mock file with multiple providers
  541. const yamlContent = [
  542. {
  543. id: 'echo',
  544. config: { prefix: 'Echo Provider: ' },
  545. },
  546. {
  547. id: 'openai:gpt-4o-mini',
  548. config: { temperature: 0.1 },
  549. },
  550. ];
  551. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  552. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  553. // Create provider array with a mix of direct provider and file reference
  554. const providerArray = [
  555. 'anthropic:claude-3-5-sonnet-20241022',
  556. 'file://./providers.yaml', // This should expand to the two providers above
  557. ];
  558. const providers = await loadApiProviders(providerArray);
  559. // We should get 3 providers: 1 direct + 2 from file
  560. expect(providers).toHaveLength(3);
  561. // Just verify that all providers are defined
  562. providers.forEach((provider) => {
  563. expect(provider).toBeDefined();
  564. });
  565. // Verify file was read correctly
  566. expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('providers.yaml'), 'utf8');
  567. expect(yaml.load).toHaveBeenCalledWith('yaml content');
  568. });
  569. it('should handle nested arrays of providers from multiple file references', async () => {
  570. // First file
  571. const firstFileContent = [
  572. {
  573. id: 'echo',
  574. config: { prefix: 'First file: ' },
  575. },
  576. ];
  577. // Second file
  578. const secondFileContent = [
  579. {
  580. id: 'openai:gpt-4o-mini',
  581. config: { temperature: 0.1 },
  582. },
  583. {
  584. id: 'anthropic:claude-3-5-sonnet-20241022',
  585. config: { temperature: 0.7 },
  586. },
  587. ];
  588. // Mock the file system read for different paths
  589. jest.mocked(fs.readFileSync).mockImplementation((filePath) => {
  590. if (filePath.toString().includes('first.yaml')) {
  591. return 'first file content';
  592. } else if (filePath.toString().includes('second.yaml')) {
  593. return 'second file content';
  594. }
  595. return '';
  596. });
  597. // Mock yaml loading based on different file contents
  598. jest.mocked(yaml.load).mockImplementation((content) => {
  599. if (content === 'first file content') {
  600. return firstFileContent;
  601. } else if (content === 'second file content') {
  602. return secondFileContent;
  603. }
  604. return null;
  605. });
  606. // Provider array with multiple file references
  607. const providerArray = ['file://./first.yaml', 'file://./second.yaml'];
  608. const providers = await loadApiProviders(providerArray);
  609. // We should get 3 providers total: 1 from first file + 2 from second file
  610. expect(providers).toHaveLength(3);
  611. // Just verify all providers are defined
  612. providers.forEach((provider) => {
  613. expect(provider).toBeDefined();
  614. });
  615. // Verify both files were read
  616. expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('first.yaml'), 'utf8');
  617. expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('second.yaml'), 'utf8');
  618. });
  619. it('should use env values from cliState.config', async () => {
  620. // Set up dummy config with env block
  621. cliState.config = {
  622. env: {
  623. TEST_API_KEY: 'test-key-from-cli-state',
  624. OTHER_VAR: 'other-value',
  625. },
  626. };
  627. const yamlContent: ProviderOptions = {
  628. id: 'openai:chat:gpt-4',
  629. config: {
  630. apiKey: '{{ env.TEST_API_KEY }}',
  631. },
  632. };
  633. jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
  634. jest.mocked(yaml.load).mockReturnValue(yamlContent);
  635. const providers = await loadApiProviders('file://test.yaml');
  636. expect(providers).toHaveLength(1);
  637. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
  638. 'gpt-4',
  639. expect.objectContaining({
  640. config: expect.objectContaining({
  641. apiKey: expect.any(String),
  642. }),
  643. env: expect.objectContaining({
  644. TEST_API_KEY: 'test-key-from-cli-state',
  645. OTHER_VAR: 'other-value',
  646. }),
  647. }),
  648. );
  649. });
  650. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...