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

poison.test.ts 8.7 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
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { getUserEmail } from '../../../src/globalConfig/accounts';
  4. import {
  5. doPoisonDocuments,
  6. generatePoisonedDocument,
  7. getAllFiles,
  8. poisonDocument,
  9. } from '../../../src/redteam/commands/poison';
  10. import { getRemoteGenerationUrl } from '../../../src/redteam/remoteGeneration';
  11. jest.mock('fs');
  12. jest.mock('path', () => ({
  13. ...jest.requireActual('path'),
  14. resolve: jest.fn(),
  15. join: jest.fn(),
  16. relative: jest.fn(),
  17. dirname: jest.fn().mockReturnValue('mock-dir'),
  18. }));
  19. jest.mock('node-fetch');
  20. describe('poison command', () => {
  21. beforeEach(() => {
  22. jest.resetAllMocks();
  23. jest.mocked(fs.mkdirSync).mockReturnValue('/mock/dir');
  24. jest.mocked(fs.writeFileSync).mockReturnValue();
  25. });
  26. describe('getAllFiles', () => {
  27. it('should get all files recursively', () => {
  28. const mockFiles = ['file1.txt', 'file2.txt'];
  29. const mockDirs = ['dir1'];
  30. jest.mocked(fs.readdirSync).mockReturnValueOnce([...mockFiles, ...mockDirs] as any);
  31. jest.mocked(fs.readdirSync).mockReturnValueOnce([] as any);
  32. jest.mocked(fs.statSync).mockImplementation(
  33. (filePath) =>
  34. ({
  35. isDirectory: () => filePath.toString().includes('dir1'),
  36. }) as fs.Stats,
  37. );
  38. jest.mocked(path.join).mockImplementation((...parts) => parts.join('/'));
  39. const files = getAllFiles('testDir');
  40. expect(files).toEqual(['testDir/file1.txt', 'testDir/file2.txt']);
  41. });
  42. });
  43. describe('generatePoisonedDocument', () => {
  44. it('should call remote API and return response', async () => {
  45. const mockResponse = {
  46. ok: true,
  47. json: () =>
  48. Promise.resolve({
  49. poisonedDocument: 'poisoned content',
  50. intendedResult: 'result',
  51. task: 'poison-document',
  52. }),
  53. headers: new Headers(),
  54. redirected: false,
  55. status: 200,
  56. statusText: 'OK',
  57. type: 'basic',
  58. url: 'test-url',
  59. } as Response;
  60. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  61. const result = await generatePoisonedDocument('test doc', 'test goal');
  62. expect(fetch).toHaveBeenCalledWith(getRemoteGenerationUrl(), {
  63. method: 'POST',
  64. headers: {
  65. 'Content-Type': 'application/json',
  66. },
  67. body: JSON.stringify({
  68. task: 'poison-document',
  69. document: 'test doc',
  70. goal: 'test goal',
  71. email: getUserEmail(),
  72. }),
  73. });
  74. expect(result).toEqual({
  75. poisonedDocument: 'poisoned content',
  76. intendedResult: 'result',
  77. task: 'poison-document',
  78. });
  79. });
  80. it('should throw error on API failure', async () => {
  81. const mockResponse = {
  82. ok: false,
  83. status: 500,
  84. statusText: 'Internal Server Error',
  85. text: () => Promise.resolve('API Error'),
  86. headers: new Headers(),
  87. redirected: false,
  88. type: 'basic',
  89. url: 'test-url',
  90. } as Response;
  91. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  92. await expect(generatePoisonedDocument('test doc')).rejects.toThrow(
  93. 'Failed to generate poisoned document',
  94. );
  95. });
  96. });
  97. describe('poisonDocument', () => {
  98. it('should poison file document', async () => {
  99. const mockDoc = {
  100. docLike: 'test.txt',
  101. isFile: true,
  102. dir: null,
  103. };
  104. jest.mocked(fs.readFileSync).mockReturnValue('test content');
  105. jest.mocked(path.relative).mockImplementation((from, to) => {
  106. return 'test.txt';
  107. });
  108. jest.mocked(path.dirname).mockReturnValue('output-dir');
  109. jest.mocked(path.join).mockImplementation((...args) => args.join('/'));
  110. const mockPoisonResponse = {
  111. poisonedDocument: 'poisoned content',
  112. intendedResult: 'result',
  113. task: 'poison-document',
  114. };
  115. const mockResponse = {
  116. ok: true,
  117. json: () => Promise.resolve(mockPoisonResponse),
  118. headers: new Headers(),
  119. redirected: false,
  120. status: 200,
  121. statusText: 'OK',
  122. type: 'basic',
  123. url: 'test-url',
  124. } as Response;
  125. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  126. const result = await poisonDocument(mockDoc, 'output-dir');
  127. expect(result).toEqual({
  128. originalPath: 'test.txt',
  129. poisonedDocument: 'poisoned content',
  130. intendedResult: 'result',
  131. });
  132. });
  133. it('should poison content document', async () => {
  134. const mockDoc = {
  135. docLike: 'test content',
  136. isFile: false,
  137. dir: null,
  138. };
  139. const mockPoisonResponse = {
  140. poisonedDocument: 'poisoned content',
  141. intendedResult: 'result',
  142. task: 'poison-document',
  143. };
  144. const mockResponse = {
  145. ok: true,
  146. json: () => Promise.resolve(mockPoisonResponse),
  147. headers: new Headers(),
  148. redirected: false,
  149. status: 200,
  150. statusText: 'OK',
  151. type: 'basic',
  152. url: 'test-url',
  153. } as Response;
  154. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  155. const result = await poisonDocument(mockDoc, 'output-dir');
  156. expect(result).toEqual({
  157. poisonedDocument: 'poisoned content',
  158. intendedResult: 'result',
  159. });
  160. });
  161. it('should throw error when poisoning fails', async () => {
  162. const mockDoc = {
  163. docLike: 'test.txt',
  164. isFile: true,
  165. dir: null,
  166. };
  167. jest.mocked(fs.readFileSync).mockImplementation(() => {
  168. throw new Error('File read error');
  169. });
  170. await expect(poisonDocument(mockDoc, 'output-dir')).rejects.toThrow(
  171. 'Failed to poison test.txt: Error: File read error',
  172. );
  173. });
  174. });
  175. describe('doPoisonDocuments', () => {
  176. it('should process multiple documents', async () => {
  177. const options = {
  178. documents: ['test.txt', 'test content'],
  179. goal: 'test goal',
  180. output: 'output.yaml',
  181. outputDir: 'output-dir',
  182. };
  183. jest.mocked(fs.existsSync).mockImplementation((path) => path === 'test.txt');
  184. jest.mocked(fs.statSync).mockReturnValue({
  185. isDirectory: () => false,
  186. } as fs.Stats);
  187. jest.mocked(path.relative).mockImplementation((from, to) => 'test.txt');
  188. jest.mocked(path.dirname).mockReturnValue('output-dir');
  189. jest.mocked(path.join).mockImplementation((...args) => args.join('/'));
  190. const mockPoisonResponse = {
  191. poisonedDocument: 'poisoned content',
  192. intendedResult: 'result',
  193. task: 'poison-document',
  194. };
  195. const mockResponse = {
  196. ok: true,
  197. json: () => Promise.resolve(mockPoisonResponse),
  198. headers: new Headers(),
  199. redirected: false,
  200. status: 200,
  201. statusText: 'OK',
  202. type: 'basic',
  203. url: 'test-url',
  204. } as Response;
  205. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  206. await doPoisonDocuments(options);
  207. expect(fs.mkdirSync).toHaveBeenCalledWith('output-dir', { recursive: true });
  208. expect(fs.writeFileSync).toHaveBeenCalledWith('output.yaml', expect.any(String));
  209. });
  210. it('should handle directory input', async () => {
  211. const options = {
  212. documents: ['test-dir'],
  213. goal: 'test goal',
  214. output: 'output.yaml',
  215. outputDir: 'output-dir',
  216. };
  217. jest.mocked(fs.existsSync).mockReturnValue(true);
  218. jest
  219. .mocked(fs.statSync)
  220. .mockReturnValueOnce({
  221. isDirectory: () => true,
  222. } as fs.Stats)
  223. .mockReturnValue({
  224. isDirectory: () => false,
  225. } as fs.Stats);
  226. jest.mocked(fs.readdirSync).mockReturnValueOnce(['file1.txt'] as any);
  227. jest.mocked(path.join).mockImplementation((...parts) => parts.join('/'));
  228. jest.mocked(path.relative).mockImplementation((from, to) => 'file1.txt');
  229. jest.mocked(path.dirname).mockReturnValue('output-dir');
  230. const mockPoisonResponse = {
  231. poisonedDocument: 'poisoned content',
  232. intendedResult: 'result',
  233. task: 'poison-document',
  234. };
  235. const mockResponse = {
  236. ok: true,
  237. json: () => Promise.resolve(mockPoisonResponse),
  238. headers: new Headers(),
  239. redirected: false,
  240. status: 200,
  241. statusText: 'OK',
  242. type: 'basic',
  243. url: 'test-url',
  244. } as Response;
  245. jest.spyOn(global, 'fetch').mockImplementation().mockResolvedValue(mockResponse);
  246. await doPoisonDocuments(options);
  247. expect(fs.mkdirSync).toHaveBeenCalledWith('output-dir', { recursive: true });
  248. expect(fs.writeFileSync).toHaveBeenCalledWith('output.yaml', expect.any(String));
  249. });
  250. });
  251. });
Tip!

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

Comments

Loading...