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
|
- import { Severity } from '../../src/redteam/constants';
- import { getRiskCategorySeverityMap, getUnifiedConfig } from '../../src/redteam/sharedFrontend';
- import type { Plugin } from '../../src/redteam/constants';
- import type { SavedRedteamConfig } from '../../src/redteam/types';
- describe('getRiskCategorySeverityMap', () => {
- it('should return default severity map when no plugins provided', () => {
- const result = getRiskCategorySeverityMap();
- expect(result).toBeDefined();
- expect(result['contracts']).toBe(Severity.Medium);
- });
- it('should override default severities with plugin severities', () => {
- const plugins = [
- { id: 'contracts' as Plugin, severity: Severity.High },
- { id: 'politics' as Plugin, severity: Severity.Critical },
- ];
- const result = getRiskCategorySeverityMap(plugins);
- expect(result['contracts']).toBe(Severity.High);
- expect(result['politics']).toBe(Severity.Critical);
- });
- it('should handle plugins without severity override', () => {
- const plugins = [
- { id: 'contracts' as Plugin },
- { id: 'politics' as Plugin, severity: Severity.Critical },
- ];
- const result = getRiskCategorySeverityMap(plugins);
- expect(result['contracts']).toBe(Severity.Medium); // Default severity
- expect(result['politics']).toBe(Severity.Critical);
- });
- });
- describe('getUnifiedConfig', () => {
- const baseConfig: SavedRedteamConfig = {
- description: 'Test config',
- prompts: ['test prompt'],
- target: {
- id: 'test-target',
- config: {
- sessionSource: 'test-session',
- stateful: true,
- apiKey: 'test-key',
- },
- },
- plugins: ['test-plugin'],
- strategies: ['basic'],
- purpose: 'testing',
- applicationDefinition: {},
- entities: [],
- };
- it('should transform config correctly', () => {
- const result = getUnifiedConfig(baseConfig);
- expect(result.description).toBe('Test config');
- expect(result.prompts).toEqual(['test prompt']);
- // @ts-ignore
- expect(result.targets[0].config.sessionSource).toBeUndefined();
- // @ts-ignore
- expect(result.targets[0].config.stateful).toBeUndefined();
- expect(result.redteam.purpose).toBe('testing');
- });
- it('should handle defaultTest transformation', () => {
- const configWithDefaultTest: SavedRedteamConfig = {
- ...baseConfig,
- defaultTest: {
- vars: { test: 'value' },
- options: { someOption: true },
- },
- };
- const result = getUnifiedConfig(configWithDefaultTest);
- expect(typeof result.defaultTest).toBe('object');
- expect(result.defaultTest).toBeDefined();
- // Type assertion since we've verified it's an object above
- const defaultTest = result.defaultTest as Exclude<
- typeof result.defaultTest,
- string | undefined
- >;
- expect(defaultTest.vars).toEqual({ test: 'value' });
- expect(defaultTest.options!.transformVars).toBe('{ ...vars, sessionId: context.uuid }');
- });
- it('should transform plugins correctly', () => {
- const configWithPlugins: SavedRedteamConfig = {
- ...baseConfig,
- plugins: ['simple-plugin', { id: 'complex-plugin', config: { setting: true } }],
- };
- const result = getUnifiedConfig(configWithPlugins);
- expect(result.redteam.plugins).toEqual([
- { id: 'simple-plugin' },
- { id: 'complex-plugin', config: { setting: true } },
- ]);
- });
- it('should transform strategies with stateful config', () => {
- const configWithStrategies: SavedRedteamConfig = {
- ...baseConfig,
- strategies: ['basic', 'goat', { id: 'custom', config: { option: true } }],
- };
- const result = getUnifiedConfig(configWithStrategies);
- expect(result.redteam.strategies).toEqual([
- { id: 'basic' },
- { id: 'goat', config: { stateful: true } },
- { id: 'custom', config: { option: true, stateful: true } },
- ]);
- });
- it('should handle maxConcurrency configuration', () => {
- const configWithMaxConcurrency: SavedRedteamConfig = {
- ...baseConfig,
- maxConcurrency: 5,
- };
- const result = getUnifiedConfig(configWithMaxConcurrency);
- expect(result.redteam.maxConcurrency).toBe(5);
- const configWithoutMaxConcurrency = getUnifiedConfig(baseConfig);
- expect(configWithoutMaxConcurrency.redteam.maxConcurrency).toBeUndefined();
- });
- it('should include testGenerationInstructions if provided', () => {
- const configWithInstructions: SavedRedteamConfig = {
- ...baseConfig,
- testGenerationInstructions: 'Generate more tests',
- };
- const result = getUnifiedConfig(configWithInstructions);
- expect(result.redteam.testGenerationInstructions).toBe('Generate more tests');
- });
- it('should omit plugin config if empty', () => {
- const configWithEmptyPluginConfig: SavedRedteamConfig = {
- ...baseConfig,
- plugins: [{ id: 'plugin-empty', config: {} }],
- };
- const result = getUnifiedConfig(configWithEmptyPluginConfig);
- expect(result.redteam.plugins).toEqual([{ id: 'plugin-empty' }]);
- });
- it('should omit strategy config if not needed', () => {
- const configWithSimpleStrategy: SavedRedteamConfig = {
- ...baseConfig,
- strategies: [{ id: 'basic', config: {} }],
- };
- const result = getUnifiedConfig(configWithSimpleStrategy);
- expect(result.redteam.strategies).toEqual([{ id: 'basic' }]);
- });
- it('should add stateful to multi-turn strategies if stateful is true', () => {
- const configWithMultiTurn: SavedRedteamConfig = {
- ...baseConfig,
- strategies: ['goat'],
- target: {
- ...baseConfig.target,
- config: {
- ...baseConfig.target.config,
- stateful: true,
- },
- },
- };
- const result = getUnifiedConfig(configWithMultiTurn);
- expect(result.redteam.strategies).toEqual([{ id: 'goat', config: { stateful: true } }]);
- });
- it('should not add stateful config for multi-turn strategies if stateful is false', () => {
- const configWithNonStateful: SavedRedteamConfig = {
- ...baseConfig,
- strategies: ['goat'],
- target: {
- ...baseConfig.target,
- config: {
- ...baseConfig.target.config,
- stateful: false,
- },
- },
- };
- const result = getUnifiedConfig(configWithNonStateful);
- expect(result.redteam.strategies).toEqual([{ id: 'goat' }]);
- });
- });
|