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
|
- import path from 'path';
- import cliState from '../../../src/cliState';
- import { importModule } from '../../../src/esm';
- import logger from '../../../src/logger';
- import { loadStrategy, validateStrategies } from '../../../src/redteam/strategies';
- import type { RedteamStrategyObject, TestCaseWithPlugin } from '../../../src/types';
- jest.mock('../../../src/cliState');
- jest.mock('../../../src/esm', () => ({
- importModule: jest.fn(),
- }));
- describe('validateStrategies', () => {
- beforeEach(() => {
- jest.resetAllMocks();
- });
- it('should validate valid strategies', async () => {
- const validStrategies: RedteamStrategyObject[] = [
- { id: 'basic' },
- { id: 'base64' },
- { id: 'video' },
- { id: 'morse' },
- { id: 'piglatin' },
- { id: 'camelcase' },
- { id: 'emoji' },
- { id: 'mischievous-user' },
- ];
- await expect(validateStrategies(validStrategies)).resolves.toBeUndefined();
- });
- it('should validate basic strategy with enabled config', async () => {
- const strategies: RedteamStrategyObject[] = [{ id: 'basic', config: { enabled: true } }];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should throw error for invalid basic strategy config', async () => {
- const strategies: RedteamStrategyObject[] = [
- { id: 'basic', config: { enabled: 'not-a-boolean' as any } },
- ];
- await expect(validateStrategies(strategies)).rejects.toThrow(
- 'Basic strategy enabled config must be a boolean',
- );
- });
- it('should skip validation for file:// strategies', async () => {
- const strategies: RedteamStrategyObject[] = [{ id: 'file://custom.js' }];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should exit for invalid strategies', async () => {
- const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
- const invalidStrategies: RedteamStrategyObject[] = [{ id: 'invalid-strategy' }];
- await validateStrategies(invalidStrategies);
- expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Invalid strategy(s)'));
- expect(mockExit).toHaveBeenCalledWith(1);
- });
- });
- describe('loadStrategy', () => {
- beforeEach(() => {
- jest.resetAllMocks();
- });
- it('should load predefined strategy', async () => {
- const strategy = await loadStrategy('basic');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('basic');
- });
- it('should load video strategy', async () => {
- const strategy = await loadStrategy('video');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('video');
- expect(typeof strategy.action).toBe('function');
- });
- it('should call video strategy action with correct parameters', async () => {
- const strategy = await loadStrategy('video');
- const testCases: TestCaseWithPlugin[] = [
- { vars: { test: 'value' }, metadata: { pluginId: 'test' } },
- ];
- const injectVar = 'inject';
- const config = {};
- await strategy.action(testCases, injectVar, config);
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Adding video encoding'));
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Added'));
- });
- it('should load morse strategy', async () => {
- const strategy = await loadStrategy('morse');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('morse');
- });
- it('should load piglatin strategy', async () => {
- const strategy = await loadStrategy('piglatin');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('piglatin');
- });
- it('should load camelcase strategy', async () => {
- const strategy = await loadStrategy('camelcase');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('camelcase');
- });
- it('should load emoji strategy', async () => {
- const strategy = await loadStrategy('emoji');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('emoji');
- });
- it('should call emoji strategy action with correct parameters', async () => {
- const strategy = await loadStrategy('emoji');
- const testCases: TestCaseWithPlugin[] = [
- { vars: { test: 'value' }, metadata: { pluginId: 'test' } },
- ];
- const injectVar = 'inject';
- const config = {};
- await strategy.action(testCases, injectVar, config);
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Adding emoji encoding'));
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Added'));
- });
- it('should load mischievous user strategy', async () => {
- const strategy = await loadStrategy('mischievous-user');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('mischievous-user');
- expect(typeof strategy.action).toBe('function');
- });
- it('should call mischievous user strategy action with correct parameters', async () => {
- const strategy = await loadStrategy('mischievous-user');
- const testCases: TestCaseWithPlugin[] = [
- { vars: { test: 'value' }, metadata: { pluginId: 'test' } },
- ];
- const injectVar = 'inject';
- const config = {};
- await strategy.action(testCases, injectVar, config);
- expect(logger.debug).toHaveBeenCalledWith(
- expect.stringContaining('Adding mischievous user test cases'),
- );
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Added'));
- });
- it('should throw error for non-existent strategy', async () => {
- await expect(loadStrategy('non-existent')).rejects.toThrow('Strategy not found: non-existent');
- });
- it('should load custom file strategy', async () => {
- const customStrategy = {
- id: 'custom',
- action: jest.fn(),
- };
- jest.mocked(importModule).mockResolvedValue(customStrategy);
- (cliState as any).basePath = '/test/path';
- const strategy = await loadStrategy('file://custom.js');
- expect(strategy).toEqual(customStrategy);
- });
- it('should throw error for non-js custom file', async () => {
- await expect(loadStrategy('file://custom.txt')).rejects.toThrow(
- 'Custom strategy file must be a JavaScript file',
- );
- });
- it('should throw error for invalid custom strategy', async () => {
- jest.mocked(importModule).mockResolvedValue({});
- await expect(loadStrategy('file://invalid.js')).rejects.toThrow(
- "Custom strategy in invalid.js must export an object with 'key' and 'action' properties",
- );
- });
- it('should use absolute path for custom strategy', async () => {
- const customStrategy = {
- id: 'custom',
- action: jest.fn(),
- };
- jest.mocked(importModule).mockResolvedValue(customStrategy);
- await loadStrategy('file:///absolute/path/custom.js');
- expect(importModule).toHaveBeenCalledWith('/absolute/path/custom.js');
- });
- it('should use relative path from basePath for custom strategy', async () => {
- const customStrategy = {
- id: 'custom',
- action: jest.fn(),
- };
- jest.mocked(importModule).mockResolvedValue(customStrategy);
- (cliState as any).basePath = '/base/path';
- await loadStrategy('file://relative/custom.js');
- expect(importModule).toHaveBeenCalledWith(path.join('/base/path', 'relative/custom.js'));
- });
- });
- describe('custom strategy validation', () => {
- it('should validate simple custom strategy', async () => {
- const strategies: RedteamStrategyObject[] = [{ id: 'custom' }];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should validate custom strategy variants with compound IDs', async () => {
- const strategies: RedteamStrategyObject[] = [
- { id: 'custom:aggressive' },
- { id: 'custom:greeting-strategy' },
- { id: 'custom:multi-word-variant' },
- { id: 'custom:snake_case_variant' },
- ];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should validate custom strategies with config', async () => {
- const strategies: RedteamStrategyObject[] = [
- {
- id: 'custom:configured',
- config: {
- strategyText: 'Custom strategy text',
- stateful: true,
- temperature: 0.8,
- },
- },
- ];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should validate mixed strategies including custom variants', async () => {
- const strategies: RedteamStrategyObject[] = [
- { id: 'basic' },
- { id: 'custom' },
- { id: 'custom:variant1' },
- { id: 'crescendo' },
- { id: 'custom:variant2', config: { strategyText: 'Custom text' } },
- ];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should validate custom strategies with complex variant names', async () => {
- const strategies: RedteamStrategyObject[] = [
- { id: 'custom:very-long-complex-variant-name-with-many-hyphens' },
- { id: 'custom:variant_with_underscores_and_numbers_123' },
- { id: 'custom:CamelCaseVariant' },
- { id: 'custom:variant.with.dots' },
- ];
- await expect(validateStrategies(strategies)).resolves.toBeUndefined();
- });
- it('should reject invalid custom-like strategy patterns', async () => {
- const strategies: RedteamStrategyObject[] = [
- { id: 'invalid-strategy' },
- { id: 'custom-invalid' },
- { id: 'custom_invalid' },
- { id: 'notcustom:variant' },
- ];
- await validateStrategies(strategies);
- expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Invalid strategy(s)'));
- });
- });
- describe('custom strategy loading', () => {
- it('should load simple custom strategy', async () => {
- const strategy = await loadStrategy('custom');
- expect(strategy).toBeDefined();
- expect(strategy.id).toBe('custom');
- });
- it('should call custom strategy action with correct parameters including strategyId', async () => {
- const strategy = await loadStrategy('custom');
- const testCases: TestCaseWithPlugin[] = [
- { vars: { test: 'value' }, metadata: { pluginId: 'test' } },
- ];
- const injectVar = 'inject';
- const config = { strategyText: 'Test strategy' };
- await strategy.action(testCases, injectVar, config, 'custom:test');
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Adding Custom'));
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Added'));
- });
- });
|