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 7.0 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
  1. import * as cache from '../src/cache';
  2. import { evaluate as doEvaluate } from '../src/evaluator';
  3. import * as index from '../src/index';
  4. import { evaluate } from '../src/index';
  5. import Eval from '../src/models/eval';
  6. import { readProviderPromptMap } from '../src/prompts';
  7. import { writeOutput, writeMultipleOutputs } from '../src/util';
  8. jest.mock('../src/cache');
  9. jest.mock('../src/database', () => ({
  10. getDb: jest
  11. .fn()
  12. .mockReturnValue({ select: jest.fn(), insert: jest.fn(), transaction: jest.fn() }),
  13. }));
  14. jest.mock('../src/evaluator', () => {
  15. const originalModule = jest.requireActual('../src/evaluator');
  16. return {
  17. ...originalModule,
  18. evaluate: jest.fn().mockResolvedValue({ results: [] }),
  19. };
  20. });
  21. jest.mock('../src/migrate');
  22. jest.mock('../src/prompts', () => {
  23. const originalModule = jest.requireActual('../src/prompts');
  24. return {
  25. ...originalModule,
  26. readProviderPromptMap: jest.fn().mockReturnValue({}),
  27. };
  28. });
  29. jest.mock('../src/telemetry');
  30. jest.mock('../src/util');
  31. jest.mock('../src/logger');
  32. describe('index.ts exports', () => {
  33. const expectedNamedExports = [
  34. 'assertions',
  35. 'cache',
  36. 'evaluate',
  37. 'generateTable',
  38. 'isApiProvider',
  39. 'isGradingResult',
  40. 'isProviderOptions',
  41. 'providers',
  42. 'redteam',
  43. ];
  44. const expectedSchemaExports = [
  45. 'AssertionSchema',
  46. 'AssertionTypeSchema',
  47. 'AtomicTestCaseSchema',
  48. 'BaseAssertionTypesSchema',
  49. 'CommandLineOptionsSchema',
  50. 'CompletedPromptSchema',
  51. 'DerivedMetricSchema',
  52. 'NotPrefixedAssertionTypesSchema',
  53. 'OutputConfigSchema',
  54. 'OutputFileExtension',
  55. 'ScenarioSchema',
  56. 'SpecialAssertionTypesSchema',
  57. 'TestCaseSchema',
  58. 'TestCasesWithMetadataPromptSchema',
  59. 'TestCasesWithMetadataSchema',
  60. 'TestCaseWithVarsFileSchema',
  61. 'TestSuiteConfigSchema',
  62. 'TestSuiteSchema',
  63. 'UnifiedConfigSchema',
  64. 'VarsSchema',
  65. ];
  66. it('should export all expected named modules', () => {
  67. expectedNamedExports.forEach((exportName) => {
  68. expect(index).toHaveProperty(exportName);
  69. });
  70. });
  71. it('should export all expected schemas', () => {
  72. expectedSchemaExports.forEach((exportName) => {
  73. expect(index).toHaveProperty(exportName);
  74. });
  75. });
  76. it('should not have unexpected exports', () => {
  77. const actualExports = Object.keys(index)
  78. .filter((key) => key !== 'default')
  79. .sort();
  80. const expectedExports = [...expectedNamedExports, ...expectedSchemaExports].sort();
  81. expect(actualExports).toHaveLength(expectedExports.length);
  82. expect(actualExports).toEqual(expectedExports);
  83. });
  84. it('redteam should have expected properties', () => {
  85. expect(index.redteam).toEqual({
  86. Base: {
  87. Grader: expect.any(Function),
  88. Plugin: expect.any(Function),
  89. },
  90. Extractors: {
  91. extractEntities: expect.any(Function),
  92. extractSystemPurpose: expect.any(Function),
  93. },
  94. Graders: expect.any(Object),
  95. Plugins: expect.any(Object),
  96. Strategies: expect.any(Object),
  97. });
  98. });
  99. it('default export should match named exports', () => {
  100. expect(index.default).toEqual({
  101. assertions: index.assertions,
  102. cache: index.cache,
  103. evaluate: index.evaluate,
  104. providers: index.providers,
  105. redteam: index.redteam,
  106. });
  107. });
  108. it('should export cache with correct methods', () => {
  109. expect(cache).toHaveProperty('getCache');
  110. expect(cache).toHaveProperty('fetchWithCache');
  111. expect(cache).toHaveProperty('enableCache');
  112. expect(cache).toHaveProperty('disableCache');
  113. expect(cache).toHaveProperty('clearCache');
  114. expect(cache).toHaveProperty('isCacheEnabled');
  115. });
  116. });
  117. describe('evaluate function', () => {
  118. it('should handle function prompts correctly', async () => {
  119. const mockPromptFunction = function testPrompt() {
  120. return 'Test prompt';
  121. };
  122. const testSuite = {
  123. prompts: [mockPromptFunction],
  124. providers: [],
  125. tests: [],
  126. };
  127. await index.evaluate(testSuite);
  128. expect(readProviderPromptMap).toHaveBeenCalledWith(testSuite, [
  129. {
  130. raw: mockPromptFunction.toString(),
  131. label: 'testPrompt',
  132. function: mockPromptFunction,
  133. },
  134. ]);
  135. expect(doEvaluate).toHaveBeenCalledWith(
  136. expect.objectContaining({
  137. prompts: [
  138. {
  139. raw: mockPromptFunction.toString(),
  140. label: 'testPrompt',
  141. function: mockPromptFunction,
  142. },
  143. ],
  144. providerPromptMap: {},
  145. }),
  146. expect.anything(),
  147. expect.objectContaining({
  148. eventSource: 'library',
  149. }),
  150. );
  151. });
  152. it('should process different types of prompts correctly', async () => {
  153. const testSuite = {
  154. prompts: [
  155. 'string prompt',
  156. { raw: 'object prompt' },
  157. function functionPrompt() {
  158. return 'function prompt';
  159. },
  160. ],
  161. providers: [],
  162. };
  163. await evaluate(testSuite);
  164. expect(doEvaluate).toHaveBeenCalledWith(
  165. expect.objectContaining({
  166. prompts: expect.arrayContaining([
  167. expect.any(Object),
  168. expect.any(Object),
  169. expect.any(Object),
  170. ]),
  171. }),
  172. expect.anything(),
  173. expect.any(Object),
  174. );
  175. });
  176. it('should resolve nested providers', async () => {
  177. const testSuite = {
  178. prompts: ['test prompt'],
  179. providers: [],
  180. tests: [{ options: { provider: 'test-provider' } }],
  181. };
  182. await evaluate(testSuite);
  183. expect(doEvaluate).toHaveBeenCalledWith(
  184. expect.objectContaining({
  185. tests: expect.arrayContaining([
  186. expect.objectContaining({
  187. options: expect.objectContaining({
  188. provider: expect.anything(),
  189. }),
  190. }),
  191. ]),
  192. }),
  193. expect.anything(),
  194. expect.anything(),
  195. );
  196. });
  197. it('should disable cache when specified', async () => {
  198. await evaluate({ prompts: ['test'], providers: [] }, { cache: false });
  199. expect(cache.disableCache).toHaveBeenCalledWith();
  200. });
  201. it('should write results to database when writeLatestResults is true', async () => {
  202. const createEvalSpy = jest.spyOn(Eval, 'create');
  203. const testSuite = {
  204. prompts: ['test'],
  205. providers: [],
  206. writeLatestResults: true,
  207. };
  208. await evaluate(testSuite);
  209. expect(createEvalSpy).toHaveBeenCalledWith(expect.anything(), expect.anything());
  210. createEvalSpy.mockRestore();
  211. });
  212. it('should write output to file when outputPath is set', async () => {
  213. const testSuite = {
  214. prompts: ['test'],
  215. providers: [],
  216. outputPath: 'test.json',
  217. };
  218. await evaluate(testSuite);
  219. expect(writeOutput).toHaveBeenCalledWith('test.json', expect.any(Eval), null);
  220. });
  221. it('should write multiple outputs when outputPath is an array', async () => {
  222. const testSuite = {
  223. prompts: ['test'],
  224. providers: [],
  225. outputPath: ['test1.json', 'test2.json'],
  226. };
  227. await evaluate(testSuite);
  228. expect(writeMultipleOutputs).toHaveBeenCalledWith(
  229. ['test1.json', 'test2.json'],
  230. expect.any(Eval),
  231. null,
  232. );
  233. });
  234. });
Tip!

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

Comments

Loading...