-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathvalidation.test.ts
More file actions
551 lines (422 loc) · 16.6 KB
/
validation.test.ts
File metadata and controls
551 lines (422 loc) · 16.6 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { promises as fs } from 'fs';
import path from 'path';
import { Validator } from '../../src/core/validation/validator.js';
import {
ScenarioSchema,
RequirementSchema,
SpecSchema,
ChangeSchema,
DeltaSchema
} from '../../src/core/schemas/index.js';
describe('Validation Schemas', () => {
describe('ScenarioSchema', () => {
it('should validate a valid scenario', () => {
const scenario = {
rawText: 'Given a user is logged in\nWhen they click logout\nThen they are redirected to login page',
};
const result = ScenarioSchema.safeParse(scenario);
expect(result.success).toBe(true);
});
it('should reject scenario with empty text', () => {
const scenario = {
rawText: '',
};
const result = ScenarioSchema.safeParse(scenario);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Scenario text cannot be empty');
}
});
});
describe('RequirementSchema', () => {
it('should validate a valid requirement', () => {
const requirement = {
text: 'The system SHALL provide user authentication',
scenarios: [
{
rawText: 'Given a user with valid credentials\nWhen they submit the login form\nThen they are authenticated',
},
],
};
const result = RequirementSchema.safeParse(requirement);
expect(result.success).toBe(true);
});
it('should reject requirement without SHALL or MUST', () => {
const requirement = {
text: 'The system provides user authentication',
scenarios: [
{
rawText: 'Given a user\nWhen they login\nThen authenticated',
},
],
};
const result = RequirementSchema.safeParse(requirement);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Requirement must contain SHALL or MUST keyword');
}
});
it('should reject requirement without scenarios', () => {
const requirement = {
text: 'The system SHALL provide user authentication',
scenarios: [],
};
const result = RequirementSchema.safeParse(requirement);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Requirement must have at least one scenario');
}
});
});
describe('SpecSchema', () => {
it('should validate a valid spec', () => {
const spec = {
name: 'user-auth',
overview: 'This spec defines user authentication requirements',
requirements: [
{
text: 'The system SHALL provide user authentication',
scenarios: [
{
rawText: 'Given a user with valid credentials\nWhen they submit the login form\nThen they are authenticated',
},
],
},
],
};
const result = SpecSchema.safeParse(spec);
expect(result.success).toBe(true);
});
it('should reject spec without requirements', () => {
const spec = {
name: 'user-auth',
overview: 'This spec defines user authentication requirements',
requirements: [],
};
const result = SpecSchema.safeParse(spec);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Spec must have at least one requirement');
}
});
});
describe('ChangeSchema', () => {
it('should validate a valid change', () => {
const change = {
name: 'add-user-auth',
why: 'We need user authentication to secure the application and protect user data',
whatChanges: 'Add authentication module with login and logout capabilities',
deltas: [
{
spec: 'user-auth',
operation: 'ADDED',
description: 'Add new user authentication spec',
},
],
};
const result = ChangeSchema.safeParse(change);
expect(result.success).toBe(true);
});
it('should reject change with short why section', () => {
const change = {
name: 'add-user-auth',
why: 'Need auth',
whatChanges: 'Add authentication',
deltas: [
{
spec: 'user-auth',
operation: 'ADDED',
description: 'Add auth',
},
],
};
const result = ChangeSchema.safeParse(change);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Why section must be at least 50 characters');
}
});
it('should warn about too many deltas', () => {
const deltas = Array.from({ length: 11 }, (_, i) => ({
spec: `spec-${i}`,
operation: 'ADDED' as const,
description: `Add spec ${i}`,
}));
const change = {
name: 'massive-change',
why: 'This is a massive change that affects many parts of the system',
whatChanges: 'Update everything',
deltas,
};
const result = ChangeSchema.safeParse(change);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0].message).toBe('Consider splitting changes with more than 10 deltas');
}
});
});
});
describe('Validator', () => {
const testDir = path.join(process.cwd(), 'test-validation-tmp');
beforeEach(async () => {
await fs.mkdir(testDir, { recursive: true });
});
afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true });
});
describe('validateSpec', () => {
it('should validate a valid spec file', async () => {
const specContent = `# User Authentication Spec
## Purpose
This specification defines the requirements for user authentication in the system.
## Requirements
### The system SHALL provide secure user authentication
The system SHALL provide secure user authentication mechanisms.
#### Scenario: Successful login
Given a user with valid credentials
When they submit the login form
Then they are authenticated and redirected to the dashboard
### The system SHALL handle invalid login attempts
The system SHALL gracefully handle incorrect credentials.
#### Scenario: Invalid credentials
Given a user with invalid credentials
When they submit the login form
Then they see an error message`;
const specPath = path.join(testDir, 'spec.md');
await fs.writeFile(specPath, specContent);
const validator = new Validator();
const report = await validator.validateSpec(specPath);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
});
it('should detect missing overview section', async () => {
const specContent = `# User Authentication Spec
## Requirements
### The system SHALL provide secure user authentication
#### Scenario: Login
Given a user
When they login
Then authenticated`;
const specPath = path.join(testDir, 'spec.md');
await fs.writeFile(specPath, specContent);
const validator = new Validator();
const report = await validator.validateSpec(specPath);
expect(report.valid).toBe(false);
expect(report.summary.errors).toBeGreaterThan(0);
expect(report.issues.some(i => i.message.includes('Purpose'))).toBe(true);
});
});
describe('validateChange', () => {
it('should validate a valid change file', async () => {
const changeContent = `# Add User Authentication
## Why
We need to implement user authentication to secure the application and protect user data from unauthorized access.
## What Changes
- **user-auth:** Add new user authentication specification
- **api-endpoints:** Modify to include auth endpoints`;
const changePath = path.join(testDir, 'change.md');
await fs.writeFile(changePath, changeContent);
const validator = new Validator();
const report = await validator.validateChange(changePath);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
});
it('should detect missing why section', async () => {
const changeContent = `# Add User Authentication
## What Changes
- **user-auth:** Add new user authentication specification`;
const changePath = path.join(testDir, 'change.md');
await fs.writeFile(changePath, changeContent);
const validator = new Validator();
const report = await validator.validateChange(changePath);
expect(report.valid).toBe(false);
expect(report.summary.errors).toBeGreaterThan(0);
expect(report.issues.some(i => i.message.includes('Why'))).toBe(true);
});
});
describe('strict mode', () => {
it('should fail on warnings in strict mode', async () => {
const specContent = `# Test Spec
## Purpose
Brief overview
## Requirements
### The system SHALL do something
#### Scenario: Test
Given test
When action
Then result`;
const specPath = path.join(testDir, 'spec.md');
await fs.writeFile(specPath, specContent);
const validator = new Validator(true); // strict mode
const report = await validator.validateSpec(specPath);
expect(report.valid).toBe(false); // Should fail due to brief overview warning
});
it('should pass warnings in non-strict mode', async () => {
const specContent = `# Test Spec
## Purpose
Brief overview
## Requirements
### The system SHALL do something
#### Scenario: Test
Given test
When action
Then result`;
const specPath = path.join(testDir, 'spec.md');
await fs.writeFile(specPath, specContent);
const validator = new Validator(false); // non-strict mode
const report = await validator.validateSpec(specPath);
expect(report.valid).toBe(true); // Should pass despite warnings
expect(report.summary.warnings).toBeGreaterThan(0);
});
});
describe('validateChangeDeltaSpecs with metadata', () => {
it('should validate requirement with metadata before SHALL/MUST text', async () => {
const changeDir = path.join(testDir, 'test-change');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: Circuit Breaker State Management SHALL be implemented
**ID**: REQ-CB-001
**Priority**: P1 (High)
The system MUST implement a circuit breaker with three states.
#### Scenario: Normal operation
**Given** the circuit breaker is in CLOSED state
**When** a request is made
**Then** the request is executed normally`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(true);
const report = await validator.validateChangeDeltaSpecs(changeDir);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
});
it('should validate requirement with SHALL in text but not in header', async () => {
const changeDir = path.join(testDir, 'test-change-2');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: Error Handling
**ID**: REQ-ERR-001
**Priority**: P2
The system SHALL handle all errors gracefully.
#### Scenario: Error occurs
**Given** an error condition
**When** an error occurs
**Then** the error is logged and user is notified`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(true);
const report = await validator.validateChangeDeltaSpecs(changeDir);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
});
it('should warn when requirement text lacks SHALL/MUST', async () => {
const changeDir = path.join(testDir, 'test-change-3');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: Logging Feature
**ID**: REQ-LOG-001
The system will log all events.
#### Scenario: Event occurs
**Given** an event
**When** it occurs
**Then** it is logged`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(false); // non-strict mode
const report = await validator.validateChangeDeltaSpecs(changeDir);
// Should pass with warning
expect(report.valid).toBe(true);
expect(report.summary.warnings).toBeGreaterThan(0);
expect(report.issues.some(i => i.level === 'WARNING' && i.message.includes('should contain SHALL or MUST'))).toBe(true);
});
it('should fail in strict mode when requirement lacks SHALL/MUST', async () => {
const changeDir = path.join(testDir, 'test-change-3-strict');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: Logging Feature
**ID**: REQ-LOG-001
The system will log all events.
#### Scenario: Event occurs
**Given** an event
**When** it occurs
**Then** it is logged`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(true); // strict mode
const report = await validator.validateChangeDeltaSpecs(changeDir);
// Should fail because strict mode treats warnings as errors
expect(report.valid).toBe(false);
expect(report.issues.some(i => i.level === 'WARNING' && i.message.includes('should contain SHALL or MUST'))).toBe(true);
});
it('should validate non-English requirement text with warning', async () => {
const changeDir = path.join(testDir, 'test-change-chinese');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: 日志功能
**ID**: REQ-LOG-001
系统必须记录所有事件。
#### Scenario: 事件发生
**Given** 有一个事件
**When** 事件发生
**Then** 事件被记录`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(false);
const report = await validator.validateChangeDeltaSpecs(changeDir);
// Should pass with warning (no SHALL/MUST in Chinese text)
expect(report.valid).toBe(true);
expect(report.summary.warnings).toBeGreaterThan(0);
expect(report.issues.some(i => i.level === 'WARNING' && i.message.includes('should contain SHALL or MUST'))).toBe(true);
});
it('should handle requirements without metadata fields', async () => {
const changeDir = path.join(testDir, 'test-change-4');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## ADDED Requirements
### Requirement: Simple Feature
The system SHALL implement this feature.
#### Scenario: Basic usage
**Given** a condition
**When** an action occurs
**Then** a result happens`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(true);
const report = await validator.validateChangeDeltaSpecs(changeDir);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
});
it('should treat delta headers case-insensitively', async () => {
const changeDir = path.join(testDir, 'test-change-mixed-case');
const specsDir = path.join(changeDir, 'specs', 'test-spec');
await fs.mkdir(specsDir, { recursive: true });
const deltaSpec = `# Test Spec
## Added Requirements
### Requirement: Mixed Case Handling
The system MUST support mixed case delta headers.
#### Scenario: Case insensitive parsing
**Given** a delta file with mixed case headers
**When** validation runs
**Then** the delta is detected`;
const specPath = path.join(specsDir, 'spec.md');
await fs.writeFile(specPath, deltaSpec);
const validator = new Validator(true);
const report = await validator.validateChangeDeltaSpecs(changeDir);
expect(report.valid).toBe(true);
expect(report.summary.errors).toBe(0);
expect(report.summary.warnings).toBe(0);
expect(report.summary.info).toBe(0);
});
});
});