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

config-schema.test.ts 7.5 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
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
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import Ajv from 'ajv';
  4. import addFormats from 'ajv-formats';
  5. describe('config-schema.json', () => {
  6. let schema: any;
  7. let ajv: Ajv;
  8. beforeAll(() => {
  9. // Read the schema file
  10. const schemaPath = path.join(__dirname, '..', 'site', 'static', 'config-schema.json');
  11. const schemaContent = fs.readFileSync(schemaPath, 'utf-8');
  12. schema = JSON.parse(schemaContent);
  13. // Initialize AJV for schema validation
  14. // Use strict: false to avoid issues with regex patterns
  15. ajv = new Ajv({
  16. strict: false,
  17. allErrors: true,
  18. validateFormats: false, // Disable format validation to avoid regex issues
  19. });
  20. addFormats(ajv);
  21. });
  22. it('should be valid JSON', () => {
  23. expect(schema).toBeDefined();
  24. expect(typeof schema).toBe('object');
  25. });
  26. it('should be a valid JSON Schema', () => {
  27. const valid = ajv.validateSchema(schema);
  28. if (!valid) {
  29. console.error('Schema validation errors:', ajv.errors);
  30. }
  31. expect(valid).toBe(true);
  32. });
  33. it('should have required top-level properties', () => {
  34. expect(schema).toHaveProperty('$ref');
  35. expect(schema).toHaveProperty('definitions');
  36. expect(schema.definitions).toHaveProperty('PromptfooConfigSchema');
  37. });
  38. describe('redteam plugin enums', () => {
  39. it('should not have duplicate entries in plugin enums', () => {
  40. const findPluginEnums = (
  41. obj: any,
  42. path: string = '',
  43. ): Array<{ path: string; values: string[] }> => {
  44. const results: Array<{ path: string; values: string[] }> = [];
  45. if (obj && typeof obj === 'object') {
  46. // Check if this is an enum array that looks like a plugin list
  47. if (Array.isArray(obj.enum) && obj.enum.length > 10 && obj.enum.includes('bias')) {
  48. results.push({ path, values: obj.enum });
  49. }
  50. for (const [key, value] of Object.entries(obj)) {
  51. if (key !== 'enum') {
  52. results.push(...findPluginEnums(value, path ? `${path}.${key}` : key));
  53. }
  54. }
  55. }
  56. return results;
  57. };
  58. const pluginEnums = findPluginEnums(schema);
  59. expect(pluginEnums.length).toBeGreaterThan(0);
  60. pluginEnums.forEach(({ path, values }) => {
  61. const uniqueValues = [...new Set(values)];
  62. const duplicates = values.filter((item, index) => values.indexOf(item) !== index);
  63. if (duplicates.length > 0) {
  64. console.error(`Duplicates found at ${path}:`, duplicates);
  65. }
  66. expect(values).toHaveLength(uniqueValues.length);
  67. });
  68. });
  69. it('should have consistent plugin lists across different locations', () => {
  70. const findAllEnums = (obj: any): string[][] => {
  71. const results: string[][] = [];
  72. if (obj && typeof obj === 'object') {
  73. if (Array.isArray(obj.enum) && obj.enum.length > 10 && obj.enum.includes('bias')) {
  74. results.push(obj.enum);
  75. }
  76. for (const value of Object.values(obj)) {
  77. results.push(...findAllEnums(value));
  78. }
  79. }
  80. return results;
  81. };
  82. const allEnums = findAllEnums(schema);
  83. // Should find at least 2 (one for string type, one for object id)
  84. expect(allEnums.length).toBeGreaterThanOrEqual(2);
  85. const firstEnum = allEnums[0]?.sort();
  86. expect(firstEnum).toBeDefined();
  87. for (let i = 1; i < allEnums.length; i++) {
  88. expect(allEnums[i].sort()).toEqual(firstEnum);
  89. }
  90. });
  91. it('should contain expected plugin entries', () => {
  92. const findPluginEnum = (obj: any): string[] | null => {
  93. if (obj && typeof obj === 'object') {
  94. if (Array.isArray(obj.enum) && obj.enum.includes('bias')) {
  95. return obj.enum;
  96. }
  97. for (const value of Object.values(obj)) {
  98. const result = findPluginEnum(value);
  99. if (result) {
  100. return result;
  101. }
  102. }
  103. }
  104. return null;
  105. };
  106. const pluginEnum = findPluginEnum(schema);
  107. expect(pluginEnum).not.toBeNull();
  108. // Use non-null assertion since we've already checked it's not null
  109. const plugins = pluginEnum!;
  110. expect(plugins).toContain('bias');
  111. expect(plugins).toContain('bias:age');
  112. expect(plugins).toContain('bias:disability');
  113. expect(plugins).toContain('bias:gender');
  114. expect(plugins).toContain('bias:race');
  115. expect(plugins).toContain('default');
  116. expect(plugins).toContain('harmful');
  117. expect(plugins).toContain('pii');
  118. const biasCount = plugins.filter((p) => p === 'bias').length;
  119. expect(biasCount).toBe(1);
  120. });
  121. });
  122. describe('schema structure', () => {
  123. it('should define UnifiedConfig properly', () => {
  124. const unifiedConfig = schema.definitions?.PromptfooConfigSchema;
  125. expect(unifiedConfig).toBeDefined();
  126. expect(unifiedConfig.type).toBe('object');
  127. expect(unifiedConfig.properties).toBeDefined();
  128. });
  129. it('should have redteam configuration', () => {
  130. const properties = schema.definitions?.PromptfooConfigSchema?.properties;
  131. expect(properties).toHaveProperty('redteam');
  132. const redteamConfig = properties?.redteam;
  133. expect(redteamConfig).toBeDefined();
  134. expect(redteamConfig.type).toBe('object');
  135. expect(redteamConfig.properties).toHaveProperty('plugins');
  136. expect(redteamConfig.properties).toHaveProperty('strategies');
  137. });
  138. it('should validate that plugin patterns are properly escaped', () => {
  139. const findPatterns = (obj: any): string[] => {
  140. const patterns: string[] = [];
  141. if (obj && typeof obj === 'object') {
  142. if (typeof obj.pattern === 'string') {
  143. patterns.push(obj.pattern);
  144. }
  145. for (const value of Object.values(obj)) {
  146. patterns.push(...findPatterns(value));
  147. }
  148. }
  149. return patterns;
  150. };
  151. const patterns = findPatterns(schema);
  152. const filePatterns = patterns.filter((p) => p.includes('file'));
  153. expect(filePatterns.length).toBeGreaterThan(0);
  154. filePatterns.forEach((pattern) => {
  155. expect(pattern).toMatch(/file.*\\/);
  156. });
  157. });
  158. });
  159. describe('performance', () => {
  160. it('should not be excessively large', () => {
  161. const schemaSize = JSON.stringify(schema).length;
  162. // Schema should be under 1MB
  163. expect(schemaSize).toBeLessThan(1024 * 1024);
  164. });
  165. });
  166. describe('generated schema integrity', () => {
  167. it('should match the structure generated by zod-to-json-schema', () => {
  168. // Verify the schema follows zod-to-json-schema conventions
  169. expect(schema.$ref).toBe('#/definitions/PromptfooConfigSchema');
  170. expect(schema.definitions).toBeDefined();
  171. // Check that the main schema definition exists
  172. const mainDef = schema.definitions.PromptfooConfigSchema;
  173. expect(mainDef).toBeDefined();
  174. expect(mainDef.type).toBe('object');
  175. });
  176. it('should regenerate to the same schema', async () => {
  177. // This test ensures the schema generation is deterministic
  178. // We'll check that key structures are present
  179. const properties = schema.definitions?.PromptfooConfigSchema?.properties;
  180. // Core properties that should always exist
  181. expect(properties).toHaveProperty('prompts');
  182. expect(properties).toHaveProperty('providers');
  183. expect(properties).toHaveProperty('tests');
  184. expect(properties).toHaveProperty('redteam');
  185. expect(properties).toHaveProperty('scenarios');
  186. expect(properties).toHaveProperty('defaultTest');
  187. });
  188. });
  189. });
Tip!

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

Comments

Loading...