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

sharedFrontend.test.ts 6.2 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
  1. import { Severity } from '../../src/redteam/constants';
  2. import { getRiskCategorySeverityMap, getUnifiedConfig } from '../../src/redteam/sharedFrontend';
  3. import type { Plugin } from '../../src/redteam/constants';
  4. import type { SavedRedteamConfig } from '../../src/redteam/types';
  5. describe('getRiskCategorySeverityMap', () => {
  6. it('should return default severity map when no plugins provided', () => {
  7. const result = getRiskCategorySeverityMap();
  8. expect(result).toBeDefined();
  9. expect(result['contracts']).toBe(Severity.Medium);
  10. });
  11. it('should override default severities with plugin severities', () => {
  12. const plugins = [
  13. { id: 'contracts' as Plugin, severity: Severity.High },
  14. { id: 'politics' as Plugin, severity: Severity.Critical },
  15. ];
  16. const result = getRiskCategorySeverityMap(plugins);
  17. expect(result['contracts']).toBe(Severity.High);
  18. expect(result['politics']).toBe(Severity.Critical);
  19. });
  20. it('should handle plugins without severity override', () => {
  21. const plugins = [
  22. { id: 'contracts' as Plugin },
  23. { id: 'politics' as Plugin, severity: Severity.Critical },
  24. ];
  25. const result = getRiskCategorySeverityMap(plugins);
  26. expect(result['contracts']).toBe(Severity.Medium); // Default severity
  27. expect(result['politics']).toBe(Severity.Critical);
  28. });
  29. });
  30. describe('getUnifiedConfig', () => {
  31. const baseConfig: SavedRedteamConfig = {
  32. description: 'Test config',
  33. prompts: ['test prompt'],
  34. target: {
  35. id: 'test-target',
  36. config: {
  37. sessionSource: 'test-session',
  38. stateful: true,
  39. apiKey: 'test-key',
  40. },
  41. },
  42. plugins: ['test-plugin'],
  43. strategies: ['basic'],
  44. purpose: 'testing',
  45. applicationDefinition: {},
  46. entities: [],
  47. };
  48. it('should transform config correctly', () => {
  49. const result = getUnifiedConfig(baseConfig);
  50. expect(result.description).toBe('Test config');
  51. expect(result.prompts).toEqual(['test prompt']);
  52. // @ts-ignore
  53. expect(result.targets[0].config.sessionSource).toBeUndefined();
  54. // @ts-ignore
  55. expect(result.targets[0].config.stateful).toBeUndefined();
  56. expect(result.redteam.purpose).toBe('testing');
  57. });
  58. it('should handle defaultTest transformation', () => {
  59. const configWithDefaultTest: SavedRedteamConfig = {
  60. ...baseConfig,
  61. defaultTest: {
  62. vars: { test: 'value' },
  63. options: { someOption: true },
  64. },
  65. };
  66. const result = getUnifiedConfig(configWithDefaultTest);
  67. expect(typeof result.defaultTest).toBe('object');
  68. expect(result.defaultTest).toBeDefined();
  69. // Type assertion since we've verified it's an object above
  70. const defaultTest = result.defaultTest as Exclude<
  71. typeof result.defaultTest,
  72. string | undefined
  73. >;
  74. expect(defaultTest.vars).toEqual({ test: 'value' });
  75. expect(defaultTest.options!.transformVars).toBe('{ ...vars, sessionId: context.uuid }');
  76. });
  77. it('should transform plugins correctly', () => {
  78. const configWithPlugins: SavedRedteamConfig = {
  79. ...baseConfig,
  80. plugins: ['simple-plugin', { id: 'complex-plugin', config: { setting: true } }],
  81. };
  82. const result = getUnifiedConfig(configWithPlugins);
  83. expect(result.redteam.plugins).toEqual([
  84. { id: 'simple-plugin' },
  85. { id: 'complex-plugin', config: { setting: true } },
  86. ]);
  87. });
  88. it('should transform strategies with stateful config', () => {
  89. const configWithStrategies: SavedRedteamConfig = {
  90. ...baseConfig,
  91. strategies: ['basic', 'goat', { id: 'custom', config: { option: true } }],
  92. };
  93. const result = getUnifiedConfig(configWithStrategies);
  94. expect(result.redteam.strategies).toEqual([
  95. { id: 'basic' },
  96. { id: 'goat', config: { stateful: true } },
  97. { id: 'custom', config: { option: true, stateful: true } },
  98. ]);
  99. });
  100. it('should handle maxConcurrency configuration', () => {
  101. const configWithMaxConcurrency: SavedRedteamConfig = {
  102. ...baseConfig,
  103. maxConcurrency: 5,
  104. };
  105. const result = getUnifiedConfig(configWithMaxConcurrency);
  106. expect(result.redteam.maxConcurrency).toBe(5);
  107. const configWithoutMaxConcurrency = getUnifiedConfig(baseConfig);
  108. expect(configWithoutMaxConcurrency.redteam.maxConcurrency).toBeUndefined();
  109. });
  110. it('should include testGenerationInstructions if provided', () => {
  111. const configWithInstructions: SavedRedteamConfig = {
  112. ...baseConfig,
  113. testGenerationInstructions: 'Generate more tests',
  114. };
  115. const result = getUnifiedConfig(configWithInstructions);
  116. expect(result.redteam.testGenerationInstructions).toBe('Generate more tests');
  117. });
  118. it('should omit plugin config if empty', () => {
  119. const configWithEmptyPluginConfig: SavedRedteamConfig = {
  120. ...baseConfig,
  121. plugins: [{ id: 'plugin-empty', config: {} }],
  122. };
  123. const result = getUnifiedConfig(configWithEmptyPluginConfig);
  124. expect(result.redteam.plugins).toEqual([{ id: 'plugin-empty' }]);
  125. });
  126. it('should omit strategy config if not needed', () => {
  127. const configWithSimpleStrategy: SavedRedteamConfig = {
  128. ...baseConfig,
  129. strategies: [{ id: 'basic', config: {} }],
  130. };
  131. const result = getUnifiedConfig(configWithSimpleStrategy);
  132. expect(result.redteam.strategies).toEqual([{ id: 'basic' }]);
  133. });
  134. it('should add stateful to multi-turn strategies if stateful is true', () => {
  135. const configWithMultiTurn: SavedRedteamConfig = {
  136. ...baseConfig,
  137. strategies: ['goat'],
  138. target: {
  139. ...baseConfig.target,
  140. config: {
  141. ...baseConfig.target.config,
  142. stateful: true,
  143. },
  144. },
  145. };
  146. const result = getUnifiedConfig(configWithMultiTurn);
  147. expect(result.redteam.strategies).toEqual([{ id: 'goat', config: { stateful: true } }]);
  148. });
  149. it('should not add stateful config for multi-turn strategies if stateful is false', () => {
  150. const configWithNonStateful: SavedRedteamConfig = {
  151. ...baseConfig,
  152. strategies: ['goat'],
  153. target: {
  154. ...baseConfig.target,
  155. config: {
  156. ...baseConfig.target.config,
  157. stateful: false,
  158. },
  159. },
  160. };
  161. const result = getUnifiedConfig(configWithNonStateful);
  162. expect(result.redteam.strategies).toEqual([{ id: 'goat' }]);
  163. });
  164. });
Tip!

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

Comments

Loading...