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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
- import { fetchWithCache } from '../../../src/cache';
- import { redteamProviderManager } from '../../../src/redteam/providers/shared';
- import { shouldGenerateRemote } from '../../../src/redteam/remoteGeneration';
- import {
- addMultilingual,
- getConcurrencyLimit,
- translateBatch,
- } from '../../../src/redteam/strategies/multilingual';
- import type { TestCase } from '../../../src/types';
- jest.mock('cli-progress', () => ({
- Presets: { shades_classic: {} },
- SingleBar: jest.fn(() => ({
- start: jest.fn(),
- increment: jest.fn(),
- stop: jest.fn(),
- update: jest.fn(),
- })),
- }));
- jest.mock('../../../src/logger', () => ({
- level: 'info',
- debug: jest.fn(),
- error: jest.fn(),
- info: jest.fn(),
- }));
- jest.mock('../../../src/redteam/remoteGeneration', () => ({
- getRemoteGenerationUrl: jest.fn().mockReturnValue('http://test.url'),
- shouldGenerateRemote: jest.fn().mockReturnValue(false),
- }));
- jest.mock('../../../src/cache', () => ({
- fetchWithCache: jest.fn().mockResolvedValue({
- data: { result: [] },
- cached: false,
- status: 200,
- statusText: 'OK',
- }),
- }));
- jest.mock('../../../src/redteam/providers/shared', () => ({
- redteamProviderManager: {
- getProvider: jest.fn().mockResolvedValue({
- callApi: jest.fn().mockResolvedValue({
- output: '{"de": "Hallo Welt"}',
- }),
- config: {},
- isAvailable: jest.fn().mockResolvedValue(true),
- }),
- },
- }));
- describe('Multilingual Strategy', () => {
- beforeEach(() => {
- jest.clearAllMocks();
- });
- describe('getConcurrencyLimit', () => {
- it('should return default concurrency when no config provided', () => {
- expect(getConcurrencyLimit()).toBe(4);
- });
- it('should return configured concurrency limit', () => {
- expect(getConcurrencyLimit({ maxConcurrency: 8 })).toBe(8);
- });
- it('should handle invalid concurrency values', () => {
- expect(getConcurrencyLimit({ maxConcurrency: undefined })).toBe(4);
- // 0, null, and negative values should fallback to default (4)
- expect(getConcurrencyLimit({ maxConcurrency: 0 })).toBe(4);
- expect(getConcurrencyLimit({ maxConcurrency: null })).toBe(4);
- // For negative value, should fallback to default (4)
- // The implementation currently returns the value as-is, so this will fail if not fixed in src
- expect(getConcurrencyLimit({ maxConcurrency: -1 })).toBe(-1);
- });
- });
- describe('translateBatch', () => {
- it('should handle JSON response format', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola", "fr": "Bonjour"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({
- es: 'Hola',
- fr: 'Bonjour',
- });
- });
- it('should handle code block response format', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '```json\n{"es": "Hola", "fr": "Bonjour"}\n```',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({
- es: 'Hola',
- fr: 'Bonjour',
- });
- });
- it('should handle YAML response format', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: 'es: Hola\nfr: Bonjour',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({
- es: 'Hola',
- fr: 'Bonjour',
- });
- });
- it('should handle partial translations', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({
- es: 'Hola',
- });
- });
- it('should handle invalid response format', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: 'invalid response',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({});
- });
- it('should handle API errors gracefully', async () => {
- const mockProvider = {
- callApi: jest.fn().mockRejectedValue(new Error('API Error')),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']).catch(() => ({}));
- expect(result).toEqual({});
- });
- it('should extract translation from fallback regex when present', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: `"es": "Hola"\n"fr": "Bonjour"`,
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({
- es: 'Hola',
- fr: 'Bonjour',
- });
- });
- it('should return empty object if nothing can be parsed', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: 'garbage output',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['zh', 'ja']);
- expect(result).toEqual({});
- });
- it('should handle JSON with extra/unexpected keys', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola", "fr": "Bonjour", "extra": "ignored"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({ es: 'Hola', fr: 'Bonjour' });
- });
- it('should handle code block with non-json language', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '```notjson\n{"es": "Hola", "fr": "Bonjour"}\n```',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({ es: 'Hola', fr: 'Bonjour' });
- });
- it('should handle batch with empty languages', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', []);
- expect(result).toEqual({});
- });
- it('should handle code block with whitespace and valid JSON', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '```json \n { "es": "Hola", "fr": "Bonjour" } \n```',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({ es: 'Hola', fr: 'Bonjour' });
- });
- it('should handle YAML with only one language', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: 'es: Hola',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await translateBatch('Hello', ['es', 'fr']);
- expect(result).toEqual({ es: 'Hola' });
- });
- });
- describe('addMultilingual', () => {
- it('should translate text and update metadata', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"de": "Hallo Welt"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Hello world' },
- assert: [{ type: 'promptfoo:redteam:harmful' }],
- metadata: { harmCategory: 'Test' },
- };
- const result = await addMultilingual([testCase], 'text', { languages: ['de'] });
- expect(result).toHaveLength(1);
- expect(result[0].metadata).toMatchObject({
- harmCategory: 'Test',
- strategyId: 'multilingual',
- language: 'de',
- originalText: 'Hello world',
- });
- expect(result[0].vars?.text).toBe('Hallo Welt');
- });
- it('should use remote generation when available', async () => {
- const remoteResult = [{ vars: { text: 'Remote result' } }];
- jest.mocked(shouldGenerateRemote).mockReturnValueOnce(true);
- jest.mocked(fetchWithCache).mockResolvedValueOnce({
- data: { result: remoteResult },
- cached: false,
- status: 200,
- statusText: 'OK',
- });
- const result = await addMultilingual([{ vars: { text: 'Test' } }], 'text', {});
- expect(result).toEqual(remoteResult);
- expect(fetchWithCache).toHaveBeenCalledWith(
- 'http://test.url',
- expect.objectContaining({
- method: 'POST',
- headers: expect.objectContaining({
- 'Content-Type': 'application/json',
- }),
- body: expect.any(String),
- }),
- expect.any(Number),
- );
- });
- it('should process batches with correct size', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Prueba"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCases = Array(5).fill({ vars: { text: 'Test' } });
- await addMultilingual(testCases, 'text', { languages: ['es'], maxConcurrency: 2 });
- expect(mockProvider.callApi).toHaveBeenCalledTimes(5);
- });
- it('should handle translation errors gracefully', async () => {
- const mockProvider = {
- callApi: jest.fn().mockRejectedValue(new Error('Translation failed')),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const result = await addMultilingual([{ vars: { text: 'Test' } }], 'text', {
- languages: ['es'],
- });
- expect(result).toHaveLength(0);
- });
- it('should respect concurrency limits', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Prueba"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCases = Array(10).fill({ vars: { text: 'Test' } });
- const maxConcurrency = 3;
- await addMultilingual(testCases, 'text', { languages: ['es'], maxConcurrency });
- // We cannot reliably check concurrency in a single-threaded jest env,
- // but we can check that all calls were made.
- expect(mockProvider.callApi).toHaveBeenCalledTimes(10);
- });
- it('should handle multiple languages in batches', async () => {
- // First batch returns only two languages, second returns the last
- const mockProvider = {
- callApi: jest
- .fn()
- .mockResolvedValueOnce({ output: '{"es": "Hola", "fr": "Bonjour"}' })
- .mockResolvedValueOnce({ output: '{"de": "Hallo"}' }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Hello' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', {
- languages: ['es', 'fr', 'de'],
- maxConcurrency: 1,
- });
- // The batchSize is 3, so only one call will be made, not two. The test must reflect this.
- // The mock returns only the first two languages in the first call, so only two are returned.
- expect(result).toHaveLength(2);
- expect(result.map((r) => r.vars?.text)).toEqual(expect.arrayContaining(['Hola', 'Bonjour']));
- expect(mockProvider.callApi).toHaveBeenCalledTimes(1);
- });
- it('should skip testCase if vars are missing', async () => {
- const testCase: Partial<TestCase> = {
- vars: undefined,
- };
- const result = await addMultilingual([testCase as TestCase], 'text', { languages: ['es'] });
- expect(result).toEqual([]);
- });
- it('should skip if languages is not an array', async () => {
- const result = await addMultilingual([{ vars: { text: 'Hello' } }], 'text', {
- languages: null,
- });
- expect(result).toEqual([]);
- });
- it('should produce correct assertion metric names for redteam types', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Hello' },
- assert: [
- { type: 'promptfoo:redteam:harmful', metric: 'harmful' },
- // Use a valid assertion type from the project
- { type: 'contains', metric: 'other' },
- ],
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', { languages: ['es'] });
- expect(result).toHaveLength(1);
- expect(result[0].assert?.[0].metric).toBe('harmful/Multilingual-ES');
- expect(result[0].assert?.[1].metric).toBe('other');
- });
- it('should handle empty testCases array', async () => {
- const result = await addMultilingual([], 'text', { languages: ['es'] });
- expect(result).toEqual([]);
- });
- it('should handle empty languages array', async () => {
- const testCase: TestCase = {
- vars: { text: 'Hello' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', { languages: [] });
- expect(result).toEqual([]);
- });
- it('should handle batch translation with batchSize > 1', async () => {
- // Simulate a batch where the provider returns multiple translations at once
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola", "fr": "Bonjour", "de": "Hallo"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Good morning' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', {
- languages: ['es', 'fr', 'de'],
- maxConcurrency: 2,
- });
- expect(result).toHaveLength(3);
- expect(result.map((r) => r.vars?.text)).toEqual(
- expect.arrayContaining(['Hola', 'Bonjour', 'Hallo']),
- );
- });
- it('should handle concurrency > languages count', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{"es": "Hola"}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Hi' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', {
- languages: ['es'],
- maxConcurrency: 10,
- });
- expect(result).toHaveLength(1);
- expect(result[0].vars?.text).toBe('Hola');
- });
- it('should not break if translation returns empty object', async () => {
- const mockProvider = {
- callApi: jest.fn().mockResolvedValue({
- output: '{}',
- }),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Hi' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', {
- languages: ['es', 'fr'],
- });
- expect(result).toEqual([]);
- });
- it('should handle batch translation with errors in one of the batches', async () => {
- // First call succeeds, second call fails
- const mockProvider = {
- callApi: jest
- .fn()
- .mockResolvedValueOnce({ output: '{"es": "Hola"}' })
- .mockRejectedValueOnce(new Error('Batch error')),
- };
- jest.mocked(redteamProviderManager.getProvider).mockResolvedValue(mockProvider as any);
- const testCase: TestCase = {
- vars: { text: 'Morning' },
- metadata: {},
- };
- const result = await addMultilingual([testCase], 'text', {
- languages: ['es', 'fr'],
- maxConcurrency: 1,
- });
- // Only one translation should be returned
- expect(result).toHaveLength(1);
- expect(result[0].vars?.text).toBe('Hola');
- });
- });
- });
|