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

custom.test.ts 10.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
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
  1. import { addCustom } from '../../../src/redteam/strategies/custom';
  2. import type { TestCase } from '../../../src/types';
  3. describe('addCustom', () => {
  4. const mockTestCases: TestCase[] = [
  5. {
  6. vars: { query: 'Tell me a joke' },
  7. assert: [
  8. { type: 'llm-rubric', value: 'Does not contain harmful content', metric: 'Harmless' },
  9. ],
  10. metadata: { pluginId: 'harmful:hate', originalText: 'Tell me a joke' },
  11. },
  12. {
  13. vars: { query: 'What is the weather?' },
  14. assert: [{ type: 'llm-rubric', value: 'Provides helpful response', metric: 'Helpful' }],
  15. metadata: { pluginId: 'pii:direct', originalText: 'What is the weather?' },
  16. },
  17. ];
  18. const injectVar = 'query';
  19. const config = {
  20. strategyText: 'Be more aggressive in your approach',
  21. stateful: true,
  22. };
  23. describe('with simple custom strategy ID', () => {
  24. it('should process test cases with basic custom strategy', () => {
  25. const result = addCustom(mockTestCases, injectVar, config, 'custom');
  26. expect(result).toHaveLength(2);
  27. // Check first test case
  28. expect(result[0]).toEqual({
  29. vars: { query: 'Tell me a joke' },
  30. provider: {
  31. id: 'promptfoo:redteam:custom',
  32. config: {
  33. injectVar: 'query',
  34. variant: '',
  35. strategyText: 'Be more aggressive in your approach',
  36. stateful: true,
  37. },
  38. },
  39. assert: [
  40. {
  41. type: 'llm-rubric',
  42. value: 'Does not contain harmful content',
  43. metric: 'Harmless/Custom',
  44. },
  45. ],
  46. metadata: {
  47. pluginId: 'harmful:hate',
  48. originalText: 'Tell me a joke',
  49. strategyId: 'custom',
  50. },
  51. });
  52. // Check second test case
  53. expect(result[1]).toEqual({
  54. vars: { query: 'What is the weather?' },
  55. provider: {
  56. id: 'promptfoo:redteam:custom',
  57. config: {
  58. injectVar: 'query',
  59. variant: '',
  60. strategyText: 'Be more aggressive in your approach',
  61. stateful: true,
  62. },
  63. },
  64. assert: [
  65. { type: 'llm-rubric', value: 'Provides helpful response', metric: 'Helpful/Custom' },
  66. ],
  67. metadata: {
  68. pluginId: 'pii:direct',
  69. originalText: 'What is the weather?',
  70. strategyId: 'custom',
  71. },
  72. });
  73. });
  74. });
  75. describe('with compound custom strategy ID', () => {
  76. it('should process test cases with custom:aggressive strategy', () => {
  77. const result = addCustom(mockTestCases, injectVar, config, 'custom:aggressive');
  78. expect(result).toHaveLength(2);
  79. // Check first test case
  80. expect(result[0]).toEqual({
  81. vars: { query: 'Tell me a joke' },
  82. provider: {
  83. id: 'promptfoo:redteam:custom:aggressive',
  84. config: {
  85. injectVar: 'query',
  86. variant: 'aggressive',
  87. strategyText: 'Be more aggressive in your approach',
  88. stateful: true,
  89. },
  90. },
  91. assert: [
  92. {
  93. type: 'llm-rubric',
  94. value: 'Does not contain harmful content',
  95. metric: 'Harmless/Custom:aggressive',
  96. },
  97. ],
  98. metadata: {
  99. pluginId: 'harmful:hate',
  100. originalText: 'Tell me a joke',
  101. strategyId: 'custom:aggressive',
  102. },
  103. });
  104. });
  105. it('should process test cases with custom:greeting-strategy', () => {
  106. const result = addCustom(mockTestCases, injectVar, config, 'custom:greeting-strategy');
  107. expect(result).toHaveLength(2);
  108. // Check provider ID and variant extraction
  109. expect(result[0].provider).toEqual({
  110. id: 'promptfoo:redteam:custom:greeting-strategy',
  111. config: {
  112. injectVar: 'query',
  113. variant: 'greeting-strategy',
  114. strategyText: 'Be more aggressive in your approach',
  115. stateful: true,
  116. },
  117. });
  118. // Check metric display name
  119. expect(result[0].assert?.[0].metric).toBe('Harmless/Custom:greeting-strategy');
  120. });
  121. });
  122. describe('variant extraction', () => {
  123. it('should extract simple variant correctly', () => {
  124. const result = addCustom(mockTestCases, injectVar, config, 'custom:simple');
  125. expect(typeof result[0].provider).toBe('object');
  126. const provider = result[0].provider as { id: string; config: any };
  127. expect(provider.config.variant).toBe('simple');
  128. expect(result[0].assert?.[0].metric).toBe('Harmless/Custom:simple');
  129. });
  130. it('should extract complex variant with hyphens', () => {
  131. const result = addCustom(mockTestCases, injectVar, config, 'custom:multi-word-variant');
  132. expect(typeof result[0].provider).toBe('object');
  133. const provider = result[0].provider as { id: string; config: any };
  134. expect(provider.config.variant).toBe('multi-word-variant');
  135. expect(result[0].assert?.[0].metric).toBe('Harmless/Custom:multi-word-variant');
  136. });
  137. it('should handle variant with underscores', () => {
  138. const result = addCustom(mockTestCases, injectVar, config, 'custom:snake_case_variant');
  139. expect(typeof result[0].provider).toBe('object');
  140. const provider = result[0].provider as { id: string; config: any };
  141. expect(provider.config.variant).toBe('snake_case_variant');
  142. expect(result[0].assert?.[0].metric).toBe('Harmless/Custom:snake_case_variant');
  143. });
  144. it('should handle empty variant for basic custom', () => {
  145. const result = addCustom(mockTestCases, injectVar, config, 'custom');
  146. expect(typeof result[0].provider).toBe('object');
  147. const provider = result[0].provider as { id: string; config: any };
  148. expect(provider.config.variant).toBe('');
  149. expect(result[0].assert?.[0].metric).toBe('Harmless/Custom');
  150. });
  151. });
  152. describe('config merging', () => {
  153. it('should merge provided config with strategy defaults', () => {
  154. const customConfig = {
  155. strategyText: 'Custom text',
  156. temperature: 0.8,
  157. customParam: true,
  158. };
  159. const result = addCustom(mockTestCases, injectVar, customConfig, 'custom:test');
  160. expect(typeof result[0].provider).toBe('object');
  161. const provider = result[0].provider as { id: string; config: any };
  162. expect(provider.config).toEqual({
  163. injectVar: 'query',
  164. variant: 'test',
  165. strategyText: 'Custom text',
  166. temperature: 0.8,
  167. customParam: true,
  168. });
  169. });
  170. it('should handle empty config', () => {
  171. const result = addCustom(mockTestCases, injectVar, {}, 'custom:minimal');
  172. expect(typeof result[0].provider).toBe('object');
  173. const provider = result[0].provider as { id: string; config: any };
  174. expect(provider.config).toEqual({
  175. injectVar: 'query',
  176. variant: 'minimal',
  177. });
  178. });
  179. });
  180. describe('edge cases', () => {
  181. it('should handle test cases without assertions', () => {
  182. const testCasesWithoutAssertions: TestCase[] = [
  183. {
  184. vars: { query: 'Test query' },
  185. metadata: { pluginId: 'test', originalText: 'Test query' },
  186. },
  187. ];
  188. const result = addCustom(testCasesWithoutAssertions, injectVar, config, 'custom:test');
  189. expect(result).toHaveLength(1);
  190. expect(result[0].assert).toBeUndefined();
  191. expect(typeof result[0].provider).toBe('object');
  192. const provider = result[0].provider as { id: string; config: any };
  193. expect(provider.id).toBe('promptfoo:redteam:custom:test');
  194. });
  195. it('should handle test cases with multiple assertions', () => {
  196. const testCasesWithMultipleAssertions: TestCase[] = [
  197. {
  198. vars: { query: 'Test query' },
  199. assert: [
  200. { type: 'llm-rubric', value: 'First assertion', metric: 'Metric1' },
  201. { type: 'llm-rubric', value: 'Second assertion', metric: 'Metric2' },
  202. ],
  203. metadata: { pluginId: 'test', originalText: 'Test query' },
  204. },
  205. ];
  206. const result = addCustom(testCasesWithMultipleAssertions, injectVar, config, 'custom:multi');
  207. expect(result[0].assert).toHaveLength(2);
  208. expect(result[0].assert?.[0].metric).toBe('Metric1/Custom:multi');
  209. expect(result[0].assert?.[1].metric).toBe('Metric2/Custom:multi');
  210. });
  211. it('should preserve most metadata, add strategy metadata, and set originalText from inject variable', () => {
  212. const originalMetadata = {
  213. pluginId: 'test:plugin',
  214. originalText: 'Original text',
  215. customField: 'custom value',
  216. };
  217. const testCasesWithMetadata: TestCase[] = [
  218. {
  219. vars: { query: 'Test query' },
  220. metadata: originalMetadata,
  221. },
  222. ];
  223. const result = addCustom(testCasesWithMetadata, injectVar, config, 'custom:preserve');
  224. expect(result[0].metadata).toEqual({
  225. pluginId: 'test:plugin',
  226. customField: 'custom value',
  227. strategyId: 'custom:preserve',
  228. originalText: 'Test query',
  229. });
  230. });
  231. });
  232. describe('provider ID generation', () => {
  233. it('should generate correct provider ID for simple custom', () => {
  234. const result = addCustom(mockTestCases, injectVar, config, 'custom');
  235. expect(typeof result[0].provider).toBe('object');
  236. const provider = result[0].provider as { id: string; config: any };
  237. expect(provider.id).toBe('promptfoo:redteam:custom');
  238. });
  239. it('should generate correct provider ID for compound custom', () => {
  240. const result = addCustom(mockTestCases, injectVar, config, 'custom:variant');
  241. expect(typeof result[0].provider).toBe('object');
  242. const provider = result[0].provider as { id: string; config: any };
  243. expect(provider.id).toBe('promptfoo:redteam:custom:variant');
  244. });
  245. it('should handle complex variant names in provider ID', () => {
  246. const result = addCustom(mockTestCases, injectVar, config, 'custom:complex-variant-name');
  247. expect(typeof result[0].provider).toBe('object');
  248. const provider = result[0].provider as { id: string; config: any };
  249. expect(provider.id).toBe('promptfoo:redteam:custom:complex-variant-name');
  250. });
  251. });
  252. });
Tip!

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

Comments

Loading...