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

cloud.test.ts 9.9 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
  1. import { fetchWithProxy } from '../../src/fetch';
  2. import { cloudConfig } from '../../src/globalConfig/cloud';
  3. import { makeRequest, getProviderFromCloud, getConfigFromCloud } from '../../src/util/cloud';
  4. jest.mock('../../src/fetch');
  5. jest.mock('../../src/globalConfig/cloud');
  6. jest.mock('../../src/util/cloud', () => ({
  7. ...jest.requireActual('../../src/util/cloud'),
  8. cloudCanBuildFormattedConfig: jest.fn().mockResolvedValue(true),
  9. }));
  10. describe('cloud utils', () => {
  11. const mockFetchWithProxy = jest.mocked(fetchWithProxy);
  12. const mockCloudConfig = cloudConfig as jest.Mocked<typeof cloudConfig>;
  13. beforeEach(() => {
  14. jest.resetAllMocks();
  15. mockCloudConfig.getApiHost.mockReturnValue('https://api.example.com');
  16. mockCloudConfig.getApiKey.mockReturnValue('test-api-key');
  17. });
  18. describe('makeRequest', () => {
  19. it('should make request with correct URL and headers', async () => {
  20. const path = 'test/path';
  21. const method = 'POST';
  22. const body = { data: 'test' };
  23. await makeRequest(path, method, body);
  24. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  25. method: 'POST',
  26. body: JSON.stringify(body),
  27. headers: { Authorization: 'Bearer test-api-key' },
  28. });
  29. });
  30. it('should make GET request without body', async () => {
  31. const path = 'test/path';
  32. const method = 'GET';
  33. await makeRequest(path, method);
  34. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  35. method: 'GET',
  36. body: undefined,
  37. headers: { Authorization: 'Bearer test-api-key' },
  38. });
  39. });
  40. it('should handle undefined API key', async () => {
  41. mockCloudConfig.getApiKey.mockReturnValue(undefined);
  42. const path = 'test/path';
  43. const method = 'GET';
  44. await makeRequest(path, method);
  45. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  46. method: 'GET',
  47. body: undefined,
  48. headers: { Authorization: 'Bearer undefined' },
  49. });
  50. });
  51. it('should handle empty path', async () => {
  52. const path = '';
  53. const method = 'GET';
  54. await makeRequest(path, method);
  55. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/', {
  56. method: 'GET',
  57. body: undefined,
  58. headers: { Authorization: 'Bearer test-api-key' },
  59. });
  60. });
  61. it('should handle API host without trailing slash', async () => {
  62. mockCloudConfig.getApiHost.mockReturnValue('https://api.example.com');
  63. const path = 'test/path';
  64. const method = 'GET';
  65. await makeRequest(path, method);
  66. expect(mockFetchWithProxy).toHaveBeenCalledWith(
  67. 'https://api.example.com/test/path',
  68. expect.any(Object),
  69. );
  70. });
  71. it('should handle API host with trailing slash', async () => {
  72. mockCloudConfig.getApiHost.mockReturnValue('https://api.example.com/');
  73. const path = 'test/path';
  74. const method = 'GET';
  75. await makeRequest(path, method);
  76. expect(mockFetchWithProxy).toHaveBeenCalledWith(
  77. 'https://api.example.com//test/path',
  78. expect.any(Object),
  79. );
  80. });
  81. it('should handle path with leading slash', async () => {
  82. const path = '/test/path';
  83. const method = 'GET';
  84. await makeRequest(path, method);
  85. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com//test/path', {
  86. method: 'GET',
  87. body: undefined,
  88. headers: { Authorization: 'Bearer test-api-key' },
  89. });
  90. });
  91. it('should handle complex request body', async () => {
  92. const path = 'test/path';
  93. const method = 'POST';
  94. const body = {
  95. string: 'test',
  96. number: 123,
  97. boolean: true,
  98. array: [1, 2, 3],
  99. nested: {
  100. field: 'value',
  101. },
  102. };
  103. await makeRequest(path, method, body);
  104. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  105. method: 'POST',
  106. body: JSON.stringify(body),
  107. headers: { Authorization: 'Bearer test-api-key' },
  108. });
  109. });
  110. it('should handle non-JSON body', async () => {
  111. const path = 'test/path';
  112. const method = 'POST';
  113. const body = 'plain text body';
  114. await makeRequest(path, method, body);
  115. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  116. method: 'POST',
  117. body: JSON.stringify(body),
  118. headers: { Authorization: 'Bearer test-api-key' },
  119. });
  120. });
  121. it('should handle null/undefined body', async () => {
  122. const path = 'test/path';
  123. const method = 'POST';
  124. await makeRequest(path, method, null);
  125. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  126. method: 'POST',
  127. body: 'null',
  128. headers: { Authorization: 'Bearer test-api-key' },
  129. });
  130. await makeRequest(path, method, undefined);
  131. expect(mockFetchWithProxy).toHaveBeenCalledWith('https://api.example.com/test/path', {
  132. method: 'POST',
  133. body: undefined,
  134. headers: { Authorization: 'Bearer test-api-key' },
  135. });
  136. });
  137. });
  138. describe('getProviderFromCloud', () => {
  139. beforeEach(() => {
  140. mockCloudConfig.isEnabled.mockReturnValue(true);
  141. });
  142. it('should fetch and parse provider successfully', async () => {
  143. const mockProvider = {
  144. config: {
  145. id: 'test-provider',
  146. label: 'Test Provider',
  147. },
  148. };
  149. mockFetchWithProxy.mockResolvedValueOnce({
  150. json: () => Promise.resolve(mockProvider),
  151. ok: true,
  152. } as Response);
  153. const result = await getProviderFromCloud('test-provider');
  154. expect(result).toEqual({ ...mockProvider.config });
  155. expect(mockFetchWithProxy).toHaveBeenCalledWith(
  156. 'https://api.example.com/api/providers/test-provider',
  157. {
  158. method: 'GET',
  159. headers: { Authorization: 'Bearer test-api-key' },
  160. },
  161. );
  162. });
  163. it('should throw error when cloud config is not enabled', async () => {
  164. mockCloudConfig.isEnabled.mockReturnValue(false);
  165. await expect(getProviderFromCloud('test-provider')).rejects.toThrow(
  166. 'Could not fetch Provider test-provider from cloud. Cloud config is not enabled.',
  167. );
  168. });
  169. it('should throw error when provider fetch fails', async () => {
  170. mockFetchWithProxy.mockRejectedValueOnce(new Error('Network error'));
  171. await expect(getProviderFromCloud('test-provider')).rejects.toThrow(
  172. 'Failed to fetch provider from cloud: test-provider.',
  173. );
  174. });
  175. it('should throw error when provider has no id', async () => {
  176. const mockProvider = {
  177. config: {
  178. label: 'Test Provider',
  179. // Missing id field
  180. },
  181. };
  182. mockFetchWithProxy.mockResolvedValueOnce({
  183. json: () => Promise.resolve({ buildDate: '2025-03-011' }),
  184. } as Response);
  185. mockFetchWithProxy.mockResolvedValueOnce({
  186. json: () => Promise.resolve(mockProvider),
  187. } as Response);
  188. await expect(getProviderFromCloud('test-provider')).rejects.toThrow(
  189. 'Failed to fetch provider from cloud: test-provider.',
  190. );
  191. });
  192. });
  193. describe('getConfigFromCloud', () => {
  194. beforeEach(() => {
  195. mockCloudConfig.isEnabled.mockReturnValue(true);
  196. });
  197. it('should fetch unified config when formatted config is supported', async () => {
  198. const mockUnifiedConfig = {
  199. description: 'Test Config',
  200. providers: ['test-provider'],
  201. prompts: ['test prompt'],
  202. tests: [{ vars: { input: 'test' } }],
  203. };
  204. mockFetchWithProxy.mockResolvedValueOnce({
  205. json: () => Promise.resolve(mockUnifiedConfig),
  206. ok: true,
  207. } as Response);
  208. const result = await getConfigFromCloud('test-config');
  209. expect(result).toEqual(mockUnifiedConfig);
  210. expect(mockFetchWithProxy).toHaveBeenCalledWith(
  211. 'https://api.example.com/api/redteam/configs/test-config/unified',
  212. {
  213. method: 'GET',
  214. headers: { Authorization: 'Bearer test-api-key' },
  215. },
  216. );
  217. });
  218. it('should fetch unified config with target', async () => {
  219. const mockUnifiedConfig = {
  220. description: 'Test Config',
  221. providers: ['test-provider'],
  222. prompts: ['test prompt'],
  223. tests: [{ vars: { input: 'test' } }],
  224. };
  225. mockFetchWithProxy.mockResolvedValueOnce({
  226. json: () => Promise.resolve(mockUnifiedConfig),
  227. ok: true,
  228. } as Response);
  229. const result = await getConfigFromCloud('test-config', 'test-provider');
  230. expect(result).toEqual(mockUnifiedConfig);
  231. expect(mockFetchWithProxy).toHaveBeenCalledWith(
  232. 'https://api.example.com/api/redteam/configs/test-config/unified?providerId=test-provider',
  233. {
  234. method: 'GET',
  235. headers: { Authorization: 'Bearer test-api-key' },
  236. },
  237. );
  238. });
  239. it('should throw error when cloud config is not enabled', async () => {
  240. mockCloudConfig.isEnabled.mockReturnValue(false);
  241. await expect(getConfigFromCloud('test-config')).rejects.toThrow(
  242. 'Could not fetch Config test-config from cloud. Cloud config is not enabled.',
  243. );
  244. });
  245. it('should throw error when config fetch fails', async () => {
  246. mockFetchWithProxy.mockRejectedValueOnce(new Error('Network error'));
  247. await expect(getConfigFromCloud('test-config')).rejects.toThrow(
  248. 'Failed to fetch config from cloud: test-config.',
  249. );
  250. });
  251. it('should throw error when response is not ok', async () => {
  252. mockFetchWithProxy.mockResolvedValueOnce({
  253. ok: false,
  254. statusText: 'Not Found',
  255. json: () => Promise.resolve({}),
  256. } as Response);
  257. await expect(getConfigFromCloud('test-config')).rejects.toThrow(
  258. 'Failed to fetch config from cloud: test-config.',
  259. );
  260. });
  261. });
  262. });
Tip!

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

Comments

Loading...