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

index.test.ts 21 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
  1. import dedent from 'dedent';
  2. import * as fs from 'fs';
  3. import { globSync } from 'glob';
  4. import { importModule } from '../../src/esm';
  5. import { readPrompts, readProviderPromptMap } from '../../src/prompts';
  6. import { maybeFilePath } from '../../src/prompts/utils';
  7. import type { ApiProvider, Prompt, ProviderResponse, UnifiedConfig } from '../../src/types';
  8. jest.mock('proxy-agent', () => ({
  9. ProxyAgent: jest.fn().mockImplementation(() => ({})),
  10. }));
  11. jest.mock('glob', () => {
  12. const actual = jest.requireActual('glob');
  13. return {
  14. ...actual,
  15. globSync: jest.fn(actual.globSync),
  16. };
  17. });
  18. jest.mock('fs', () => {
  19. const actual = jest.requireActual('fs');
  20. return {
  21. ...actual,
  22. existsSync: jest.fn(actual.existsSync),
  23. mkdirSync: jest.fn(),
  24. readFileSync: jest.fn(),
  25. statSync: jest.fn(actual.statSync),
  26. writeFileSync: jest.fn(),
  27. };
  28. });
  29. jest.mock('python-shell');
  30. jest.mock('../../src/esm', () => {
  31. const actual = jest.requireActual('../../src/esm');
  32. return {
  33. ...actual,
  34. importModule: jest.fn(actual.importModule),
  35. };
  36. });
  37. jest.mock('../../src/logger');
  38. jest.mock('../../src/python/wrapper');
  39. jest.mock('../../src/prompts/utils', () => {
  40. const actual = jest.requireActual('../../src/prompts/utils');
  41. return {
  42. ...actual,
  43. maybeFilePath: jest.fn(actual.maybeFilePath),
  44. };
  45. });
  46. describe('readPrompts', () => {
  47. afterEach(() => {
  48. delete process.env.PROMPTFOO_STRICT_FILES;
  49. jest.mocked(fs.readFileSync).mockReset();
  50. jest.mocked(fs.statSync).mockReset();
  51. jest.mocked(globSync).mockReset();
  52. jest.mocked(maybeFilePath).mockClear();
  53. });
  54. it('should throw an error for invalid inputs', async () => {
  55. await expect(readPrompts(null as any)).rejects.toThrow('Invalid input prompt: null');
  56. await expect(readPrompts(undefined as any)).rejects.toThrow('Invalid input prompt: undefined');
  57. await expect(readPrompts(1 as any)).rejects.toThrow('Invalid input prompt: 1');
  58. await expect(readPrompts(true as any)).rejects.toThrow('Invalid input prompt: true');
  59. await expect(readPrompts(false as any)).rejects.toThrow('Invalid input prompt: false');
  60. });
  61. it('should throw an error for empty inputs', async () => {
  62. await expect(readPrompts([])).rejects.toThrow('Invalid input prompt: []');
  63. await expect(readPrompts({} as any)).rejects.toThrow('Invalid input prompt: {}');
  64. await expect(readPrompts('')).rejects.toThrow('Invalid input prompt: ""');
  65. });
  66. it('should throw an error when PROMPTFOO_STRICT_FILES is true and the file does not exist', async () => {
  67. process.env.PROMPTFOO_STRICT_FILES = 'true';
  68. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  69. jest.mocked(fs.readFileSync).mockImplementationOnce(() => {
  70. throw new Error("ENOENT: no such file or directory, stat 'non-existent-file.txt'");
  71. });
  72. await expect(readPrompts('non-existent-file.txt')).rejects.toThrow(
  73. expect.objectContaining({
  74. message: expect.stringMatching(
  75. /ENOENT: no such file or directory, stat '.*non-existent-file.txt'/,
  76. ),
  77. }),
  78. );
  79. });
  80. it('should throw an error for a .txt file with no prompts', async () => {
  81. jest.mocked(fs.readFileSync).mockReturnValueOnce('');
  82. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  83. await expect(readPrompts(['prompts.txt'])).rejects.toThrow(
  84. 'There are no prompts in "prompts.txt"',
  85. );
  86. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  87. });
  88. it('should throw an error for an unsupported file format', async () => {
  89. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  90. await expect(readPrompts(['unsupported.for.mat'])).rejects.toThrow(
  91. 'There are no prompts in "unsupported.for.mat"',
  92. );
  93. expect(fs.readFileSync).toHaveBeenCalledTimes(0);
  94. });
  95. it('should read a single prompt', async () => {
  96. const prompt = 'This is a test prompt';
  97. await expect(readPrompts(prompt)).resolves.toEqual([
  98. {
  99. raw: prompt,
  100. label: prompt,
  101. },
  102. ]);
  103. });
  104. it('should read a list of prompts', async () => {
  105. const prompts = ['Sample prompt A', 'Sample prompt B'];
  106. await expect(readPrompts(prompts)).resolves.toEqual([
  107. {
  108. raw: 'Sample prompt A',
  109. label: 'Sample prompt A',
  110. },
  111. {
  112. raw: 'Sample prompt B',
  113. label: 'Sample prompt B',
  114. },
  115. ]);
  116. });
  117. it('should read a .txt file with a single prompt', async () => {
  118. jest.mocked(fs.readFileSync).mockReturnValueOnce('Sample Prompt');
  119. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  120. await expect(readPrompts('prompts.txt')).resolves.toEqual([
  121. {
  122. label: 'prompts.txt: Sample Prompt',
  123. raw: 'Sample Prompt',
  124. },
  125. ]);
  126. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  127. });
  128. it.each([['prompts.txt'], 'prompts.txt'])(
  129. `should read a single prompt file with input:%p`,
  130. async (promptPath) => {
  131. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  132. jest.mocked(fs.readFileSync).mockReturnValue('Test prompt 1\n---\nTest prompt 2');
  133. jest.mocked(globSync).mockImplementation((pathOrGlob) => [pathOrGlob.toString()]);
  134. await expect(readPrompts(promptPath)).resolves.toEqual([
  135. {
  136. label: 'prompts.txt: Test prompt 1',
  137. raw: 'Test prompt 1',
  138. },
  139. {
  140. label: 'prompts.txt: Test prompt 2',
  141. raw: 'Test prompt 2',
  142. },
  143. ]);
  144. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  145. },
  146. );
  147. it('should read multiple prompt files', async () => {
  148. jest.mocked(fs.readFileSync).mockImplementation((filePath) => {
  149. if (filePath.toString().endsWith('prompt1.txt')) {
  150. return 'Test prompt 1\n---\nTest prompt 2';
  151. } else if (filePath.toString().endsWith('prompt2.txt')) {
  152. return 'Test prompt 3\n---\nTest prompt 4\n---\nTest prompt 5';
  153. }
  154. throw new Error(`Unexpected file path in test: ${filePath}`);
  155. });
  156. jest.mocked(fs.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats);
  157. jest.mocked(globSync).mockImplementation((pathOrGlob) => [pathOrGlob.toString()]);
  158. await expect(readPrompts(['prompt1.txt', 'prompt2.txt'])).resolves.toEqual([
  159. {
  160. label: 'prompt1.txt: Test prompt 1',
  161. raw: 'Test prompt 1',
  162. },
  163. {
  164. label: 'prompt1.txt: Test prompt 2',
  165. raw: 'Test prompt 2',
  166. },
  167. {
  168. label: 'prompt2.txt: Test prompt 3',
  169. raw: 'Test prompt 3',
  170. },
  171. {
  172. label: 'prompt2.txt: Test prompt 4',
  173. raw: 'Test prompt 4',
  174. },
  175. {
  176. label: 'prompt2.txt: Test prompt 5',
  177. raw: 'Test prompt 5',
  178. },
  179. ]);
  180. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  181. });
  182. it('should read with map input', async () => {
  183. jest.mocked(fs.readFileSync).mockReturnValue('some raw text');
  184. jest.mocked(fs.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats);
  185. await expect(
  186. readPrompts({
  187. 'prompts.txt': 'foo1',
  188. 'prompts2.txt': 'foo2',
  189. }),
  190. ).resolves.toEqual([
  191. { raw: 'some raw text', label: 'foo1: prompts.txt: some raw text' },
  192. { raw: 'some raw text', label: 'foo2: prompts2.txt: some raw text' },
  193. ]);
  194. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  195. });
  196. it('should read a .json file', async () => {
  197. const mockJsonContent = JSON.stringify([
  198. { name: 'You are a helpful assistant', role: 'system' },
  199. { name: 'How do I get to the moon?', role: 'user' },
  200. ]);
  201. jest.mocked(fs.readFileSync).mockReturnValueOnce(mockJsonContent);
  202. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  203. const filePath = 'file://path/to/mock.json';
  204. await expect(readPrompts([filePath])).resolves.toEqual([
  205. {
  206. raw: mockJsonContent,
  207. label: expect.stringContaining(`mock.json: ${mockJsonContent}`),
  208. },
  209. ]);
  210. expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('mock.json'), 'utf8');
  211. expect(fs.statSync).toHaveBeenCalledTimes(1);
  212. });
  213. it('should read a .jsonl file', async () => {
  214. const data = [
  215. [
  216. { role: 'system', content: 'You are a helpful assistant.' },
  217. { role: 'user', content: 'Who won the world series in {{ year }}?' },
  218. ],
  219. [
  220. { role: 'system', content: 'You are a helpful assistant.' },
  221. { role: 'user', content: 'Who won the superbowl in {{ year }}?' },
  222. ],
  223. ];
  224. jest.mocked(fs.readFileSync).mockReturnValueOnce(data.map((o) => JSON.stringify(o)).join('\n'));
  225. jest.mocked(fs.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats);
  226. await expect(readPrompts(['prompts.jsonl'])).resolves.toEqual([
  227. {
  228. raw: JSON.stringify(data[0]),
  229. label: `prompts.jsonl: ${JSON.stringify(data[0])}`,
  230. },
  231. {
  232. raw: JSON.stringify(data[1]),
  233. label: `prompts.jsonl: ${JSON.stringify(data[1])}`,
  234. },
  235. ]);
  236. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  237. });
  238. const yamlContent = dedent`
  239. - role: user
  240. content:
  241. - type: text
  242. text: "What's in this image?"
  243. - type: image_url
  244. image_url:
  245. url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"`;
  246. it('should read a .yaml file', async () => {
  247. const expectedJson = JSON.stringify([
  248. {
  249. role: 'user',
  250. content: [
  251. { type: 'text', text: "What's in this image?" },
  252. {
  253. type: 'image_url',
  254. image_url: {
  255. url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
  256. },
  257. },
  258. ],
  259. },
  260. ]);
  261. jest.mocked(fs.readFileSync).mockReturnValueOnce(yamlContent);
  262. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  263. await expect(readPrompts('prompts.yaml')).resolves.toEqual([
  264. {
  265. raw: expectedJson,
  266. label: expect.stringContaining(
  267. 'prompts.yaml: [{"role":"user","content":[{"type":"text","text":"What\'s in this image?"}',
  268. ),
  269. config: undefined,
  270. },
  271. ]);
  272. });
  273. it('should read a .yml file', async () => {
  274. const expectedJson = JSON.stringify([
  275. {
  276. role: 'user',
  277. content: [
  278. { type: 'text', text: "What's in this image?" },
  279. {
  280. type: 'image_url',
  281. image_url: {
  282. url: 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
  283. },
  284. },
  285. ],
  286. },
  287. ]);
  288. jest.mocked(fs.readFileSync).mockReturnValueOnce(yamlContent);
  289. jest.mocked(fs.statSync).mockReturnValueOnce({ isDirectory: () => false } as fs.Stats);
  290. await expect(readPrompts('image-summary.yml')).resolves.toEqual([
  291. {
  292. raw: expectedJson,
  293. label: expect.stringContaining(
  294. 'image-summary.yml: [{"role":"user","content":[{"type":"text","text":"What\'s in this image?"}',
  295. ),
  296. config: undefined,
  297. },
  298. ]);
  299. });
  300. it('should read a .py prompt object array', async () => {
  301. const prompts = [
  302. { id: 'prompts.py:prompt1', label: 'First prompt' },
  303. { id: 'prompts.py:prompt2', label: 'Second prompt' },
  304. ];
  305. const code = dedent`
  306. def prompt1:
  307. return 'First prompt'
  308. def prompt2:
  309. return 'Second prompt'
  310. `;
  311. jest.mocked(fs.readFileSync).mockReturnValue(code);
  312. await expect(readPrompts(prompts)).resolves.toEqual([
  313. {
  314. raw: code,
  315. label: 'First prompt',
  316. function: expect.any(Function),
  317. },
  318. {
  319. raw: code,
  320. label: 'Second prompt',
  321. function: expect.any(Function),
  322. },
  323. ]);
  324. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  325. });
  326. it('should read a .py file', async () => {
  327. const code = `print('dummy prompt')`;
  328. jest.mocked(fs.readFileSync).mockReturnValue(code);
  329. await expect(readPrompts('prompt.py')).resolves.toEqual([
  330. {
  331. function: expect.any(Function),
  332. label: `prompt.py: ${code}`,
  333. raw: code,
  334. },
  335. ]);
  336. expect(fs.readFileSync).toHaveBeenCalledTimes(1);
  337. });
  338. it('should read a .js file without named function', async () => {
  339. const promptPath = 'prompt.js';
  340. const mockFunction = () => console.log('dummy prompt');
  341. jest.mocked(importModule).mockResolvedValueOnce(mockFunction);
  342. jest.mocked(fs.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats);
  343. await expect(readPrompts(promptPath)).resolves.toEqual([
  344. {
  345. raw: "()=>console.log('dummy prompt')",
  346. label: 'prompt.js',
  347. function: expect.any(Function),
  348. config: {},
  349. },
  350. ]);
  351. expect(importModule).toHaveBeenCalledWith(promptPath, undefined);
  352. expect(fs.statSync).toHaveBeenCalledTimes(1);
  353. });
  354. it('should read a .js file with named function', async () => {
  355. const promptPath = 'prompt.js:functionName';
  356. const mockFunction = (context: {
  357. vars: Record<string, string | object>;
  358. provider?: ApiProvider;
  359. }) => 'dummy prompt result';
  360. jest.mocked(importModule).mockResolvedValueOnce(mockFunction);
  361. jest.mocked(fs.statSync).mockReturnValue({ isDirectory: () => false } as fs.Stats);
  362. const result = await readPrompts(promptPath);
  363. expect(result).toEqual([
  364. {
  365. raw: String(mockFunction),
  366. label: 'prompt.js:functionName',
  367. function: expect.any(Function),
  368. config: {}, // Add this line
  369. },
  370. ]);
  371. const promptFunction = result[0].function as unknown as (context: {
  372. vars: Record<string, string | object>;
  373. provider?: ApiProvider;
  374. }) => string;
  375. expect(promptFunction({ vars: {}, provider: { id: () => 'foo' } as ApiProvider })).toBe(
  376. 'dummy prompt result',
  377. );
  378. expect(importModule).toHaveBeenCalledWith('prompt.js', 'functionName');
  379. expect(fs.statSync).toHaveBeenCalledTimes(1);
  380. });
  381. it('should read a directory', async () => {
  382. jest.mocked(fs.statSync).mockImplementation((filePath) => {
  383. if (filePath.toString().endsWith('prompt1.txt')) {
  384. return { isDirectory: () => false } as fs.Stats;
  385. } else if (filePath.toString().endsWith('prompts')) {
  386. return { isDirectory: () => true } as fs.Stats;
  387. }
  388. throw new Error(`Unexpected file path in test: ${filePath}`);
  389. });
  390. jest.mocked(globSync).mockImplementation(() => ['prompt1.txt', 'prompt2.txt']);
  391. // The mocked paths here are an artifact of our globSync mock. In a real
  392. // world setting we would get back `prompts/prompt1.txt` instead of `prompts/*/prompt1.txt`
  393. // but for the sake of this test we are just going to pretend that the globSync
  394. // mock is doing the right thing and giving us back the right paths.
  395. jest.mocked(fs.readFileSync).mockImplementation((filePath) => {
  396. if (
  397. filePath.toString().endsWith('prompt1.txt') ||
  398. filePath.toString().endsWith('*/prompt1.txt')
  399. ) {
  400. return 'Test prompt 1\n---\nTest prompt 2';
  401. } else if (
  402. filePath.toString().endsWith('prompt2.txt') ||
  403. filePath.toString().endsWith('*/prompt2.txt')
  404. ) {
  405. return 'Test prompt 3\n---\nTest prompt 4\n---\nTest prompt 5';
  406. }
  407. throw new Error(`Unexpected file path in test: ${filePath}`);
  408. });
  409. await expect(readPrompts(['prompts/*'])).resolves.toEqual([
  410. {
  411. label: expect.stringMatching('prompt1.txt: Test prompt 1'),
  412. raw: 'Test prompt 1',
  413. },
  414. {
  415. label: expect.stringMatching('prompt1.txt: Test prompt 2'),
  416. raw: 'Test prompt 2',
  417. },
  418. {
  419. label: expect.stringMatching('prompt2.txt: Test prompt 3'),
  420. raw: 'Test prompt 3',
  421. },
  422. {
  423. label: expect.stringMatching('prompt2.txt: Test prompt 4'),
  424. raw: 'Test prompt 4',
  425. },
  426. {
  427. label: expect.stringMatching('prompt2.txt: Test prompt 5'),
  428. raw: 'Test prompt 5',
  429. },
  430. ]);
  431. expect(fs.readFileSync).toHaveBeenCalledTimes(2);
  432. expect(fs.statSync).toHaveBeenCalledTimes(3);
  433. });
  434. it('should fall back to a string if maybeFilePath is true but a file does not exist', async () => {
  435. jest.mocked(globSync).mockReturnValueOnce([]);
  436. jest.mocked(maybeFilePath).mockReturnValueOnce(true);
  437. await expect(readPrompts('non-existent-file.txt*')).resolves.toEqual([
  438. { raw: 'non-existent-file.txt*', label: 'non-existent-file.txt*' },
  439. ]);
  440. });
  441. it('should handle a prompt with a function', async () => {
  442. const promptWithFunction: Partial<Prompt> = {
  443. raw: 'dummy raw text',
  444. label: 'Function Prompt',
  445. function: jest.fn().mockResolvedValue('Hello, world!'),
  446. };
  447. await expect(readPrompts([promptWithFunction])).resolves.toEqual([
  448. {
  449. raw: 'dummy raw text',
  450. label: 'Function Prompt',
  451. function: expect.any(Function),
  452. },
  453. ]);
  454. expect(promptWithFunction.function).not.toHaveBeenCalled();
  455. });
  456. });
  457. describe('readProviderPromptMap', () => {
  458. let config: Partial<UnifiedConfig>;
  459. let parsedPrompts: Prompt[];
  460. beforeEach(() => {
  461. parsedPrompts = [
  462. { label: 'prompt1', raw: 'prompt1' },
  463. { label: 'prompt2', raw: 'prompt2' },
  464. ];
  465. });
  466. it('should return an empty object if config.providers is undefined', () => {
  467. config = {};
  468. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({});
  469. });
  470. it('should return a map with all prompts if config.providers is a string', () => {
  471. config = { providers: 'provider1' };
  472. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  473. provider1: ['prompt1', 'prompt2'],
  474. });
  475. });
  476. it('should return a map with all prompts if config.providers is a function', () => {
  477. config = {
  478. providers: () =>
  479. Promise.resolve({
  480. providerName: 'Custom function',
  481. prompts: ['prompt1', 'prompt2'],
  482. }) as Promise<ProviderResponse>,
  483. };
  484. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  485. 'Custom function': ['prompt1', 'prompt2'],
  486. });
  487. });
  488. it('should handle provider objects with id and prompts', () => {
  489. config = {
  490. providers: [{ id: 'provider1', prompts: ['customPrompt1'] }],
  491. };
  492. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({ provider1: ['customPrompt1'] });
  493. });
  494. it('should handle provider objects with id, label, and prompts', () => {
  495. config = {
  496. providers: [{ id: 'provider1', label: 'providerLabel', prompts: ['customPrompt1'] }],
  497. };
  498. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  499. provider1: ['customPrompt1'],
  500. providerLabel: ['customPrompt1'],
  501. });
  502. });
  503. it('should handle provider options map with id and prompts', () => {
  504. config = {
  505. providers: [
  506. {
  507. originalProvider: {
  508. id: 'provider1',
  509. prompts: ['customPrompt1'],
  510. },
  511. },
  512. ],
  513. };
  514. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({ provider1: ['customPrompt1'] });
  515. });
  516. it('should handle provider options map without id and use original id', () => {
  517. config = {
  518. providers: [
  519. {
  520. originalProvider: {
  521. prompts: ['customPrompt1'],
  522. },
  523. },
  524. ],
  525. };
  526. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  527. originalProvider: ['customPrompt1'],
  528. });
  529. });
  530. it('should use rawProvider.prompts if provided for provider objects with id', () => {
  531. config = {
  532. providers: [{ id: 'provider1', prompts: ['customPrompt1'] }],
  533. };
  534. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({ provider1: ['customPrompt1'] });
  535. });
  536. it('should fall back to allPrompts if no prompts provided for provider objects with id', () => {
  537. config = {
  538. providers: [{ id: 'provider1' }],
  539. };
  540. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  541. provider1: ['prompt1', 'prompt2'],
  542. });
  543. });
  544. it('should use rawProvider.prompts for both id and label if provided', () => {
  545. config = {
  546. providers: [{ id: 'provider1', label: 'providerLabel', prompts: ['customPrompt1'] }],
  547. };
  548. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  549. provider1: ['customPrompt1'],
  550. providerLabel: ['customPrompt1'],
  551. });
  552. });
  553. it('should fall back to allPrompts for both id and label if no prompts provided', () => {
  554. config = {
  555. providers: [{ id: 'provider1', label: 'providerLabel' }],
  556. };
  557. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  558. provider1: ['prompt1', 'prompt2'],
  559. providerLabel: ['prompt1', 'prompt2'],
  560. });
  561. });
  562. it('should use providerObject.id from ProviderOptionsMap when provided', () => {
  563. config = {
  564. providers: [
  565. {
  566. originalProvider: {
  567. id: 'explicitId',
  568. prompts: ['customPrompt1'],
  569. },
  570. },
  571. ],
  572. };
  573. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({ explicitId: ['customPrompt1'] });
  574. });
  575. it('should fallback to originalId when providerObject.id is not specified in ProviderOptionsMap', () => {
  576. config = {
  577. providers: [
  578. {
  579. originalProvider: {
  580. // 'originalProvider' is treated as originalId
  581. prompts: ['customPrompt1'],
  582. },
  583. },
  584. ],
  585. };
  586. expect(readProviderPromptMap(config, parsedPrompts)).toEqual({
  587. originalProvider: ['customPrompt1'],
  588. });
  589. });
  590. });
Tip!

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

Comments

Loading...