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

init.test.ts 4.0 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
  1. import { describe, expect, it } from '@jest/globals';
  2. import yaml from 'js-yaml';
  3. import { renderRedteamConfig } from '../../../src/redteam/commands/init';
  4. import { type Strategy } from '../../../src/redteam/constants';
  5. import type { RedteamFileConfig } from '../../../src/redteam/types';
  6. describe('renderRedteamConfig', () => {
  7. it('should generate valid YAML that conforms to RedteamFileConfig', () => {
  8. const input = {
  9. purpose: 'Test chatbot security',
  10. numTests: 5,
  11. plugins: [{ id: 'rbac', numTests: 2 }],
  12. strategies: ['prompt-injection'] as Strategy[],
  13. prompts: ['Hello {{prompt}}'],
  14. providers: ['openai:gpt-4'],
  15. descriptions: {
  16. 'math-prompt': 'Basic prompt injection test',
  17. rbac: 'Basic RBAC test',
  18. },
  19. };
  20. const renderedConfig = renderRedteamConfig(input);
  21. expect(renderedConfig).toBeDefined();
  22. const parsedConfig = yaml.load(renderedConfig) as {
  23. description: string;
  24. prompts: string[];
  25. targets: string[];
  26. redteam: RedteamFileConfig;
  27. };
  28. expect(parsedConfig.redteam.purpose).toBe(input.purpose);
  29. expect(parsedConfig.redteam.numTests).toBe(input.numTests);
  30. expect(parsedConfig.redteam.plugins).toHaveLength(1);
  31. expect(parsedConfig.redteam.plugins?.[0]).toMatchObject({
  32. id: 'rbac',
  33. numTests: 2,
  34. });
  35. });
  36. it('should handle minimal configuration', () => {
  37. const input = {
  38. purpose: 'Basic test',
  39. numTests: 1,
  40. plugins: [],
  41. strategies: [],
  42. prompts: [],
  43. providers: [],
  44. descriptions: {},
  45. };
  46. const renderedConfig = renderRedteamConfig(input);
  47. const parsedConfig = yaml.load(renderedConfig) as {
  48. redteam: RedteamFileConfig;
  49. };
  50. expect(parsedConfig.redteam.purpose).toBe(input.purpose);
  51. expect(parsedConfig.redteam.numTests).toBe(input.numTests);
  52. expect(parsedConfig.redteam.plugins || []).toEqual([]);
  53. expect(parsedConfig.redteam.strategies || []).toEqual([]);
  54. });
  55. it('should include all provided plugins and strategies', () => {
  56. const input = {
  57. purpose: 'Test all plugins',
  58. numTests: 3,
  59. plugins: [
  60. { id: 'prompt-injection', numTests: 1 },
  61. { id: 'policy', numTests: 2 },
  62. ],
  63. strategies: ['basic', 'jailbreak'] as Strategy[],
  64. prompts: ['Test {{prompt}}'],
  65. providers: ['openai:gpt-4'],
  66. descriptions: {
  67. 'basic-injection': 'Basic test',
  68. 'advanced-injection': 'Advanced test',
  69. },
  70. };
  71. const renderedConfig = renderRedteamConfig(input);
  72. const parsedConfig = yaml.load(renderedConfig) as {
  73. redteam: RedteamFileConfig;
  74. };
  75. expect(parsedConfig.redteam.plugins).toHaveLength(2);
  76. expect(parsedConfig.redteam.strategies).toHaveLength(2);
  77. expect(parsedConfig.redteam.plugins).toEqual(
  78. expect.arrayContaining([
  79. { id: 'prompt-injection', numTests: 1 },
  80. { id: 'policy', numTests: 2 },
  81. ]),
  82. );
  83. expect(parsedConfig.redteam.strategies).toEqual(expect.arrayContaining(['basic', 'jailbreak']));
  84. });
  85. it('should handle custom provider configuration', () => {
  86. const input = {
  87. purpose: 'Test custom provider',
  88. numTests: 1,
  89. plugins: [],
  90. strategies: [],
  91. prompts: ['Test'],
  92. providers: [
  93. {
  94. id: 'custom-provider',
  95. label: 'Custom API',
  96. config: {
  97. apiKey: '{{CUSTOM_API_KEY}}',
  98. baseUrl: 'https://api.custom.com',
  99. },
  100. },
  101. ],
  102. descriptions: {},
  103. };
  104. const renderedConfig = renderRedteamConfig(input);
  105. const parsedConfig = yaml.load(renderedConfig) as {
  106. targets: Array<{
  107. id: string;
  108. label: string;
  109. config: Record<string, string>;
  110. }>;
  111. };
  112. expect(parsedConfig.targets).toBeDefined();
  113. expect(parsedConfig.targets[0]).toMatchObject({
  114. id: 'custom-provider',
  115. label: 'Custom API',
  116. config: {
  117. apiKey: '{{CUSTOM_API_KEY}}',
  118. baseUrl: 'https://api.custom.com',
  119. },
  120. });
  121. });
  122. });
Tip!

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

Comments

Loading...