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
|
- import { describe, expect, it } from '@jest/globals';
- import yaml from 'js-yaml';
- import { renderRedteamConfig } from '../../../src/redteam/commands/init';
- import { type Strategy } from '../../../src/redteam/constants';
- import type { RedteamFileConfig } from '../../../src/redteam/types';
- describe('renderRedteamConfig', () => {
- it('should generate valid YAML that conforms to RedteamFileConfig', () => {
- const input = {
- purpose: 'Test chatbot security',
- numTests: 5,
- plugins: [{ id: 'rbac', numTests: 2 }],
- strategies: ['prompt-injection'] as Strategy[],
- prompts: ['Hello {{prompt}}'],
- providers: ['openai:gpt-4'],
- descriptions: {
- 'math-prompt': 'Basic prompt injection test',
- rbac: 'Basic RBAC test',
- },
- };
- const renderedConfig = renderRedteamConfig(input);
- expect(renderedConfig).toBeDefined();
- const parsedConfig = yaml.load(renderedConfig) as {
- description: string;
- prompts: string[];
- targets: string[];
- redteam: RedteamFileConfig;
- };
- expect(parsedConfig.redteam.purpose).toBe(input.purpose);
- expect(parsedConfig.redteam.numTests).toBe(input.numTests);
- expect(parsedConfig.redteam.plugins).toHaveLength(1);
- expect(parsedConfig.redteam.plugins?.[0]).toMatchObject({
- id: 'rbac',
- numTests: 2,
- });
- });
- it('should handle minimal configuration', () => {
- const input = {
- purpose: 'Basic test',
- numTests: 1,
- plugins: [],
- strategies: [],
- prompts: [],
- providers: [],
- descriptions: {},
- };
- const renderedConfig = renderRedteamConfig(input);
- const parsedConfig = yaml.load(renderedConfig) as {
- redteam: RedteamFileConfig;
- };
- expect(parsedConfig.redteam.purpose).toBe(input.purpose);
- expect(parsedConfig.redteam.numTests).toBe(input.numTests);
- expect(parsedConfig.redteam.plugins || []).toEqual([]);
- expect(parsedConfig.redteam.strategies || []).toEqual([]);
- });
- it('should include all provided plugins and strategies', () => {
- const input = {
- purpose: 'Test all plugins',
- numTests: 3,
- plugins: [
- { id: 'prompt-injection', numTests: 1 },
- { id: 'policy', numTests: 2 },
- ],
- strategies: ['basic', 'jailbreak'] as Strategy[],
- prompts: ['Test {{prompt}}'],
- providers: ['openai:gpt-4'],
- descriptions: {
- 'basic-injection': 'Basic test',
- 'advanced-injection': 'Advanced test',
- },
- };
- const renderedConfig = renderRedteamConfig(input);
- const parsedConfig = yaml.load(renderedConfig) as {
- redteam: RedteamFileConfig;
- };
- expect(parsedConfig.redteam.plugins).toHaveLength(2);
- expect(parsedConfig.redteam.strategies).toHaveLength(2);
- expect(parsedConfig.redteam.plugins).toEqual(
- expect.arrayContaining([
- { id: 'prompt-injection', numTests: 1 },
- { id: 'policy', numTests: 2 },
- ]),
- );
- expect(parsedConfig.redteam.strategies).toEqual(expect.arrayContaining(['basic', 'jailbreak']));
- });
- it('should handle custom provider configuration', () => {
- const input = {
- purpose: 'Test custom provider',
- numTests: 1,
- plugins: [],
- strategies: [],
- prompts: ['Test'],
- providers: [
- {
- id: 'custom-provider',
- label: 'Custom API',
- config: {
- apiKey: '{{CUSTOM_API_KEY}}',
- baseUrl: 'https://api.custom.com',
- },
- },
- ],
- descriptions: {},
- };
- const renderedConfig = renderRedteamConfig(input);
- const parsedConfig = yaml.load(renderedConfig) as {
- targets: Array<{
- id: string;
- label: string;
- config: Record<string, string>;
- }>;
- };
- expect(parsedConfig.targets).toBeDefined();
- expect(parsedConfig.targets[0]).toMatchObject({
- id: 'custom-provider',
- label: 'Custom API',
- config: {
- apiKey: '{{CUSTOM_API_KEY}}',
- baseUrl: 'https://api.custom.com',
- },
- });
- });
- });
|