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

util.test.ts 9.4 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
  1. import { fetchWithCache } from '../../src/cache';
  2. import {
  3. extractGoalFromPrompt,
  4. getShortPluginId,
  5. isBasicRefusal,
  6. isEmptyResponse,
  7. normalizeApostrophes,
  8. removePrefix,
  9. } from '../../src/redteam/util';
  10. jest.mock('../../src/cache');
  11. describe('removePrefix', () => {
  12. it('should remove a simple prefix', () => {
  13. expect(removePrefix('Prompt: Hello world', 'Prompt')).toBe('Hello world');
  14. });
  15. it('should be case insensitive', () => {
  16. expect(removePrefix('PROMPT: Hello world', 'prompt')).toBe('Hello world');
  17. });
  18. it('should remove asterisks from the prefix', () => {
  19. expect(removePrefix('**Prompt:** Hello world', 'Prompt')).toBe('Hello world');
  20. });
  21. it('should handle multiple asterisks', () => {
  22. expect(removePrefix('***Prompt:*** Hello world', 'Prompt')).toBe('Hello world');
  23. });
  24. it('should return the same string if prefix is not found', () => {
  25. expect(removePrefix('Hello world', 'Prefix')).toBe('Hello world');
  26. });
  27. it('should handle empty strings', () => {
  28. expect(removePrefix('', 'Prefix')).toBe('');
  29. });
  30. it('should handle prefix that is the entire string', () => {
  31. expect(removePrefix('Prompt:', 'Prompt')).toBe('');
  32. });
  33. });
  34. describe('normalizeApostrophes', () => {
  35. it('should normalize different types of apostrophes', () => {
  36. expect(normalizeApostrophes("I'm")).toBe("I'm");
  37. expect(normalizeApostrophes('I′m')).toBe("I'm");
  38. expect(normalizeApostrophes("I'm")).toBe("I'm");
  39. expect(normalizeApostrophes("I'm")).toBe("I'm");
  40. });
  41. it('should handle strings without apostrophes', () => {
  42. expect(normalizeApostrophes('Hello world')).toBe('Hello world');
  43. });
  44. });
  45. describe('isEmptyResponse', () => {
  46. it('should return true for empty responses', () => {
  47. expect(isEmptyResponse('')).toBe(true);
  48. expect(isEmptyResponse(' ')).toBe(true);
  49. expect(isEmptyResponse('{}')).toBe(true);
  50. expect(isEmptyResponse(' {} ')).toBe(true);
  51. });
  52. it('should return false for non-empty responses', () => {
  53. expect(isEmptyResponse('Hello')).toBe(false);
  54. expect(isEmptyResponse('{"key": "value"}')).toBe(false);
  55. });
  56. });
  57. describe('isBasicRefusal', () => {
  58. it('should detect refusal prefixes', () => {
  59. expect(isBasicRefusal('I am sorry, I cannot help with that')).toBe(true);
  60. expect(isBasicRefusal("I'm not able to assist with this request")).toBe(true);
  61. expect(isBasicRefusal('I apologize, but I cannot do that')).toBe(true);
  62. });
  63. it('should detect refusal substrings', () => {
  64. expect(isBasicRefusal('As an AI language model, I cannot help with that')).toBe(true);
  65. expect(isBasicRefusal('I cannot assist with that request')).toBe(true);
  66. expect(isBasicRefusal('That would not be appropriate')).toBe(true);
  67. });
  68. it('should normalize apostrophes in responses', () => {
  69. expect(isBasicRefusal("I′m sorry, I can't help")).toBe(true);
  70. expect(isBasicRefusal("I'm unable to assist")).toBe(true);
  71. });
  72. it('should handle case insensitivity', () => {
  73. expect(isBasicRefusal('I AM SORRY, I CANNOT HELP')).toBe(true);
  74. expect(isBasicRefusal('as an ai language model')).toBe(true);
  75. });
  76. it('should return false for non-refusal responses', () => {
  77. expect(isBasicRefusal('I will help you with that')).toBe(false);
  78. expect(isBasicRefusal('Here is the information you requested')).toBe(false);
  79. expect(isBasicRefusal('The answer is 42')).toBe(false);
  80. });
  81. });
  82. describe('getShortPluginId', () => {
  83. it('should remove promptfoo:redteam: prefix', () => {
  84. expect(getShortPluginId('promptfoo:redteam:test')).toBe('test');
  85. });
  86. it('should return original if no prefix', () => {
  87. expect(getShortPluginId('test')).toBe('test');
  88. });
  89. });
  90. describe('extractGoalFromPrompt', () => {
  91. beforeEach(() => {
  92. jest.resetAllMocks();
  93. });
  94. it('should successfully extract goal', async () => {
  95. jest.mocked(fetchWithCache).mockResolvedValue({
  96. cached: false,
  97. status: 200,
  98. statusText: 'OK',
  99. headers: {},
  100. data: { intent: 'test goal' },
  101. deleteFromCache: async () => {},
  102. });
  103. const result = await extractGoalFromPrompt('test prompt', 'test purpose');
  104. expect(result).toBe('test goal');
  105. });
  106. it('should return null on HTTP error', async () => {
  107. jest.mocked(fetchWithCache).mockResolvedValue({
  108. cached: false,
  109. status: 500,
  110. statusText: 'Internal Server Error',
  111. headers: {},
  112. data: {},
  113. deleteFromCache: async () => {},
  114. });
  115. const result = await extractGoalFromPrompt('test prompt', 'test purpose');
  116. expect(result).toBeNull();
  117. });
  118. it('should return null when no intent returned', async () => {
  119. jest.mocked(fetchWithCache).mockResolvedValue({
  120. cached: false,
  121. status: 200,
  122. statusText: 'OK',
  123. headers: {},
  124. data: {},
  125. deleteFromCache: async () => {},
  126. });
  127. const result = await extractGoalFromPrompt('test prompt', 'test purpose');
  128. expect(result).toBeNull();
  129. });
  130. it('should return null when API throws error', async () => {
  131. jest.mocked(fetchWithCache).mockRejectedValue(new Error('API error'));
  132. const result = await extractGoalFromPrompt('test prompt', 'test purpose');
  133. expect(result).toBeNull();
  134. });
  135. it('should handle empty prompt and purpose', async () => {
  136. jest.mocked(fetchWithCache).mockResolvedValue({
  137. cached: false,
  138. status: 200,
  139. statusText: 'OK',
  140. headers: {},
  141. data: { intent: 'empty goal' },
  142. deleteFromCache: async () => {},
  143. });
  144. const result = await extractGoalFromPrompt('', '');
  145. expect(result).toBe('empty goal');
  146. });
  147. it('should include plugin context when pluginId is provided', async () => {
  148. jest.mocked(fetchWithCache).mockResolvedValue({
  149. cached: false,
  150. status: 200,
  151. statusText: 'OK',
  152. headers: {},
  153. data: { intent: 'plugin-specific goal' },
  154. deleteFromCache: async () => {},
  155. });
  156. const result = await extractGoalFromPrompt(
  157. 'innocent prompt',
  158. 'test purpose',
  159. 'indirect-prompt-injection',
  160. );
  161. expect(result).toBe('plugin-specific goal');
  162. // Verify that the API was called with plugin context
  163. expect(fetchWithCache).toHaveBeenCalledWith(
  164. expect.any(String),
  165. expect.objectContaining({
  166. body: expect.stringContaining('pluginContext'),
  167. }),
  168. expect.any(Number),
  169. );
  170. });
  171. it('should skip remote call when remote generation is disabled', async () => {
  172. // Preserve original environment setting
  173. const originalValue = process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION;
  174. process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'true';
  175. const result = await extractGoalFromPrompt('test prompt', 'test purpose');
  176. expect(result).toBeNull();
  177. expect(fetchWithCache).not.toHaveBeenCalled();
  178. // Cleanup: restore or delete the env var to avoid leaking into other tests
  179. if (originalValue === undefined) {
  180. delete process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION;
  181. } else {
  182. process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = originalValue;
  183. }
  184. });
  185. it('should skip goal extraction for dataset plugins with short plugin ID', async () => {
  186. const result = await extractGoalFromPrompt('test prompt', 'test purpose', 'beavertails');
  187. expect(result).toBeNull();
  188. expect(fetchWithCache).not.toHaveBeenCalled();
  189. });
  190. it('should skip goal extraction for dataset plugins with full plugin ID', async () => {
  191. const result = await extractGoalFromPrompt(
  192. 'test prompt',
  193. 'test purpose',
  194. 'promptfoo:redteam:cyberseceval',
  195. );
  196. expect(result).toBeNull();
  197. expect(fetchWithCache).not.toHaveBeenCalled();
  198. });
  199. it('should skip goal extraction for all dataset plugins', async () => {
  200. const datasetPlugins = [
  201. 'beavertails',
  202. 'cyberseceval',
  203. 'donotanswer',
  204. 'harmbench',
  205. 'toxic-chat',
  206. 'aegis',
  207. 'pliny',
  208. 'unsafebench',
  209. 'xstest',
  210. ];
  211. for (const pluginId of datasetPlugins) {
  212. const result = await extractGoalFromPrompt('test prompt', 'test purpose', pluginId);
  213. expect(result).toBeNull();
  214. // Also test with full plugin ID format
  215. const fullPluginId = `promptfoo:redteam:${pluginId}`;
  216. const resultFull = await extractGoalFromPrompt('test prompt', 'test purpose', fullPluginId);
  217. expect(resultFull).toBeNull();
  218. }
  219. expect(fetchWithCache).not.toHaveBeenCalled();
  220. });
  221. it('should proceed with API call for non-dataset plugins', async () => {
  222. jest.mocked(fetchWithCache).mockResolvedValue({
  223. cached: false,
  224. status: 200,
  225. statusText: 'OK',
  226. headers: {},
  227. data: { intent: 'extracted goal' },
  228. deleteFromCache: async () => {},
  229. });
  230. // Test with a non-dataset plugin
  231. const result = await extractGoalFromPrompt('test prompt', 'test purpose', 'prompt-extraction');
  232. expect(result).toBe('extracted goal');
  233. expect(fetchWithCache).toHaveBeenCalledTimes(1);
  234. });
  235. it('should proceed with API call for non-dataset plugins with full plugin ID', async () => {
  236. jest.mocked(fetchWithCache).mockResolvedValue({
  237. cached: false,
  238. status: 200,
  239. statusText: 'OK',
  240. headers: {},
  241. data: { intent: 'extracted goal' },
  242. deleteFromCache: async () => {},
  243. });
  244. // Test with a full non-dataset plugin ID
  245. const result = await extractGoalFromPrompt(
  246. 'test prompt',
  247. 'test purpose',
  248. 'promptfoo:redteam:sql-injection',
  249. );
  250. expect(result).toBe('extracted goal');
  251. expect(fetchWithCache).toHaveBeenCalledTimes(1);
  252. });
  253. });
Tip!

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

Comments

Loading...