-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
598 lines (500 loc) · 19.2 KB
/
errors.test.ts
File metadata and controls
598 lines (500 loc) · 19.2 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import { describe, it, expect } from 'vitest';
import {
UMSError,
UMSValidationError,
ModuleLoadError,
PersonaLoadError,
BuildError,
isUMSError,
isValidationError,
ID_VALIDATION_ERRORS,
SCHEMA_VALIDATION_ERRORS,
} from './errors.js';
describe('errors', () => {
describe('UMSError', () => {
it('should create basic UMS error', () => {
const error = new UMSError('test message', 'TEST_CODE');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(UMSError);
expect(error.name).toBe('UMSError');
expect(error.message).toBe('test message');
expect(error.code).toBe('TEST_CODE');
expect(error.context).toBeUndefined();
});
it('should create UMS error with context', () => {
const error = new UMSError('test message', 'TEST_CODE', 'test context');
expect(error.message).toBe('test message');
expect(error.code).toBe('TEST_CODE');
expect(error.context).toBe('test context');
});
it('should handle empty strings', () => {
const error = new UMSError('', '', '');
expect(error.message).toBe('');
expect(error.code).toBe('');
expect(error.context).toBe('');
});
it('should handle undefined context explicitly', () => {
const error = new UMSError('test', 'CODE', undefined);
expect(error.context).toBeUndefined();
});
it('should maintain error stack trace', () => {
const error = new UMSError('test', 'CODE');
expect(error.stack).toBeDefined();
expect(error.stack).toContain('UMSError');
});
});
describe('UMSValidationError', () => {
it('should create basic validation error', () => {
const error = new UMSValidationError('validation failed');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(UMSError);
expect(error).toBeInstanceOf(UMSValidationError);
expect(error.name).toBe('UMSValidationError');
expect(error.message).toBe('validation failed');
expect(error.code).toBe('VALIDATION_ERROR');
expect(error.path).toBeUndefined();
expect(error.section).toBeUndefined();
expect(error.context).toBeUndefined();
});
it('should create validation error with path', () => {
const error = new UMSValidationError(
'validation failed',
'frontmatter.name'
);
expect(error.path).toBe('frontmatter.name');
expect(error.section).toBeUndefined();
expect(error.context).toBeUndefined();
});
it('should create validation error with section', () => {
const error = new UMSValidationError(
'validation failed',
undefined,
'Section 4.1'
);
expect(error.path).toBeUndefined();
expect(error.section).toBe('Section 4.1');
expect(error.context).toBeUndefined();
});
it('should create validation error with context', () => {
const error = new UMSValidationError(
'validation failed',
undefined,
undefined,
'module parsing'
);
expect(error.path).toBeUndefined();
expect(error.section).toBeUndefined();
expect(error.context).toBe('module parsing');
});
it('should create validation error with all optional parameters', () => {
const error = new UMSValidationError(
'validation failed',
'metadata.description',
'Section 3.2',
'schema validation'
);
expect(error.message).toBe('validation failed');
expect(error.path).toBe('metadata.description');
expect(error.section).toBe('Section 3.2');
expect(error.context).toBe('schema validation');
expect(error.code).toBe('VALIDATION_ERROR');
});
it('should handle explicitly undefined parameters', () => {
const error = new UMSValidationError(
'test',
undefined,
undefined,
undefined
);
expect(error.path).toBeUndefined();
expect(error.section).toBeUndefined();
expect(error.context).toBeUndefined();
});
});
describe('ModuleLoadError', () => {
it('should create basic module load error', () => {
const error = new ModuleLoadError('failed to load module');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(UMSError);
expect(error).toBeInstanceOf(ModuleLoadError);
expect(error.name).toBe('ModuleLoadError');
expect(error.message).toBe('failed to load module');
expect(error.code).toBe('MODULE_LOAD_ERROR');
expect(error.filePath).toBeUndefined();
expect(error.context).toBeUndefined();
});
it('should create module load error with file path', () => {
const error = new ModuleLoadError(
'failed to load module',
'/path/to/module.module.yml'
);
expect(error.filePath).toBe('/path/to/module.module.yml');
expect(error.context).toBeUndefined();
});
it('should create module load error with context', () => {
const error = new ModuleLoadError(
'failed to load module',
undefined,
'YAML parsing'
);
expect(error.filePath).toBeUndefined();
expect(error.context).toBe('YAML parsing');
});
it('should create module load error with all parameters', () => {
const error = new ModuleLoadError(
'failed to load module',
'/path/to/module.module.yml',
'file system error'
);
expect(error.message).toBe('failed to load module');
expect(error.filePath).toBe('/path/to/module.module.yml');
expect(error.context).toBe('file system error');
expect(error.code).toBe('MODULE_LOAD_ERROR');
});
});
describe('PersonaLoadError', () => {
it('should create basic persona load error', () => {
const error = new PersonaLoadError('failed to load persona');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(UMSError);
expect(error).toBeInstanceOf(PersonaLoadError);
expect(error.name).toBe('PersonaLoadError');
expect(error.message).toBe('failed to load persona');
expect(error.code).toBe('PERSONA_LOAD_ERROR');
expect(error.filePath).toBeUndefined();
expect(error.context).toBeUndefined();
});
it('should create persona load error with file path', () => {
const error = new PersonaLoadError(
'failed to load persona',
'/path/to/persona.persona.yml'
);
expect(error.filePath).toBe('/path/to/persona.persona.yml');
});
it('should create persona load error with all parameters', () => {
const error = new PersonaLoadError(
'failed to load persona',
'/path/to/persona.persona.yml',
'schema validation'
);
expect(error.message).toBe('failed to load persona');
expect(error.filePath).toBe('/path/to/persona.persona.yml');
expect(error.context).toBe('schema validation');
expect(error.code).toBe('PERSONA_LOAD_ERROR');
});
});
describe('BuildError', () => {
it('should create basic build error', () => {
const error = new BuildError('build failed');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(UMSError);
expect(error).toBeInstanceOf(BuildError);
expect(error.name).toBe('BuildError');
expect(error.message).toBe('build failed');
expect(error.code).toBe('BUILD_ERROR');
expect(error.context).toBeUndefined();
});
it('should create build error with context', () => {
const error = new BuildError('build failed', 'persona compilation');
expect(error.message).toBe('build failed');
expect(error.context).toBe('persona compilation');
expect(error.code).toBe('BUILD_ERROR');
});
});
describe('Type Guards', () => {
describe('isUMSError', () => {
it('should return true for UMSError instances', () => {
const error = new UMSError('test', 'CODE');
expect(isUMSError(error)).toBe(true);
});
it('should return true for UMSError subclasses', () => {
const validationError = new UMSValidationError('test');
const moduleLoadError = new ModuleLoadError('test');
const personaLoadError = new PersonaLoadError('test');
const buildError = new BuildError('test');
expect(isUMSError(validationError)).toBe(true);
expect(isUMSError(moduleLoadError)).toBe(true);
expect(isUMSError(personaLoadError)).toBe(true);
expect(isUMSError(buildError)).toBe(true);
});
it('should return false for regular Error instances', () => {
const error = new Error('test');
expect(isUMSError(error)).toBe(false);
});
it('should return false for non-error values', () => {
expect(isUMSError(null)).toBe(false);
expect(isUMSError(undefined)).toBe(false);
expect(isUMSError('string')).toBe(false);
expect(isUMSError(123)).toBe(false);
expect(isUMSError({})).toBe(false);
expect(isUMSError([])).toBe(false);
});
it('should return false for objects that look like UMSError but are not', () => {
const fakeError = {
name: 'UMSError',
message: 'test',
code: 'TEST',
};
expect(isUMSError(fakeError)).toBe(false);
});
});
describe('isValidationError', () => {
it('should return true for UMSValidationError instances', () => {
const error = new UMSValidationError('test');
expect(isValidationError(error)).toBe(true);
});
it('should return false for other UMSError subclasses', () => {
const moduleLoadError = new ModuleLoadError('test');
const personaLoadError = new PersonaLoadError('test');
const buildError = new BuildError('test');
const umsError = new UMSError('test', 'CODE');
expect(isValidationError(moduleLoadError)).toBe(false);
expect(isValidationError(personaLoadError)).toBe(false);
expect(isValidationError(buildError)).toBe(false);
expect(isValidationError(umsError)).toBe(false);
});
it('should return false for regular Error instances', () => {
const error = new Error('test');
expect(isValidationError(error)).toBe(false);
});
it('should return false for non-error values', () => {
expect(isValidationError(null)).toBe(false);
expect(isValidationError(undefined)).toBe(false);
expect(isValidationError('string')).toBe(false);
expect(isValidationError(123)).toBe(false);
expect(isValidationError({})).toBe(false);
expect(isValidationError([])).toBe(false);
});
});
});
describe('ID_VALIDATION_ERRORS', () => {
it('should have constant string values', () => {
expect(ID_VALIDATION_ERRORS.INVALID_CHARS).toBe(
'Module ID contains invalid characters'
);
expect(ID_VALIDATION_ERRORS.EMPTY_SEGMENT).toBe(
'Module ID contains empty path segment'
);
expect(ID_VALIDATION_ERRORS.LEADING_SLASH).toBe(
'Module ID cannot start with a slash'
);
expect(ID_VALIDATION_ERRORS.TRAILING_SLASH).toBe(
'Module ID cannot end with a slash'
);
expect(ID_VALIDATION_ERRORS.CONSECUTIVE_SLASHES).toBe(
'Module ID cannot contain consecutive slashes'
);
});
describe('Function-based error messages', () => {
describe('invalidFormat', () => {
it('should return formatted invalid format message', () => {
const result = ID_VALIDATION_ERRORS.invalidFormat('bad-id');
expect(result).toBe('Invalid module ID format: bad-id');
});
});
describe('uppercaseCharacters', () => {
it('should return formatted uppercase characters message', () => {
const result =
ID_VALIDATION_ERRORS.uppercaseCharacters('Foundation/module');
expect(result).toBe(
'Module ID contains uppercase characters: Foundation/module'
);
});
});
describe('specialCharacters', () => {
it('should return formatted special characters message', () => {
const result = ID_VALIDATION_ERRORS.specialCharacters(
'foundation/logic/test@module'
);
expect(result).toBe(
'Module ID contains special characters: foundation/logic/test@module'
);
});
});
describe('emptySegment', () => {
it('should return formatted empty segment message', () => {
const result =
ID_VALIDATION_ERRORS.emptySegment('foundation//module');
expect(result).toBe(
"Module ID 'foundation//module' contains empty path segment"
);
});
});
describe('invalidCharacters', () => {
it('should return formatted invalid characters message', () => {
const result = ID_VALIDATION_ERRORS.invalidCharacters(
'foundation/logic/test_module'
);
expect(result).toBe(
"Module ID 'foundation/logic/test_module' contains invalid characters"
);
});
});
});
});
describe('SCHEMA_VALIDATION_ERRORS', () => {
it('should have constant string values', () => {
expect(SCHEMA_VALIDATION_ERRORS.MISSING_FRONTMATTER).toBe(
'Module file must contain YAML frontmatter'
);
expect(SCHEMA_VALIDATION_ERRORS.INVALID_YAML).toBe(
'Invalid YAML syntax in frontmatter'
);
expect(SCHEMA_VALIDATION_ERRORS.MISSING_REQUIRED_FIELD).toBe(
'Missing required field'
);
expect(SCHEMA_VALIDATION_ERRORS.INVALID_FIELD_TYPE).toBe(
'Invalid field type'
);
expect(SCHEMA_VALIDATION_ERRORS.INVALID_ENUM_VALUE).toBe(
'Invalid enum value'
);
});
describe('Function-based error messages', () => {
describe('missingField', () => {
it('should return formatted missing field message', () => {
const result = SCHEMA_VALIDATION_ERRORS.missingField('name');
expect(result).toBe('Missing required field: name');
});
});
describe('wrongType', () => {
it('should return formatted wrong type message', () => {
const result = SCHEMA_VALIDATION_ERRORS.wrongType(
'description',
'string',
'number'
);
expect(result).toBe(
"Field 'description' expected string, got number"
);
});
});
describe('duplicateModuleId', () => {
it('should return formatted duplicate module ID message', () => {
const result = SCHEMA_VALIDATION_ERRORS.duplicateModuleId(
'foundation/logic/reasoning',
'core'
);
expect(result).toBe(
"Duplicate module ID 'foundation/logic/reasoning' in group 'core'"
);
});
});
describe('invalidEnumValue', () => {
it('should return formatted invalid enum value message', () => {
const validValues = ['procedure', 'specification', 'pattern'];
const result = SCHEMA_VALIDATION_ERRORS.invalidEnumValue(
'schema',
'invalid',
validValues
);
expect(result).toBe(
"Invalid value 'invalid' for schema. Valid values: procedure, specification, pattern"
);
});
});
describe('wrongSchemaVersion', () => {
it('should return formatted wrong schema version message', () => {
const result = SCHEMA_VALIDATION_ERRORS.wrongSchemaVersion('0.5');
expect(result).toBe(
"Invalid schema version '0.5', expected '1.0', '2.0', '2.1', or '2.2'"
);
});
});
describe('invalidShape', () => {
it('should return formatted invalid shape message', () => {
const validShapes = [
'procedure',
'specification',
'pattern',
'checklist',
'data',
'rule',
];
const result = SCHEMA_VALIDATION_ERRORS.invalidShape(
'unknown',
validShapes
);
expect(result).toBe(
"Invalid shape 'unknown'. Valid shapes: procedure, specification, pattern, checklist, data, rule"
);
});
});
describe('undeclaredDirective', () => {
it('should return formatted undeclared directive message', () => {
const declared = ['goal', 'process', 'constraints'];
const result = SCHEMA_VALIDATION_ERRORS.undeclaredDirective(
'invalid',
declared
);
expect(result).toBe(
"Undeclared directive 'invalid'. Declared directives: goal, process, constraints"
);
});
});
describe('missingRequiredDirective', () => {
it('should return formatted missing required directive message', () => {
const result =
SCHEMA_VALIDATION_ERRORS.missingRequiredDirective('goal');
expect(result).toBe('Missing required directive: goal');
});
});
describe('invalidDirectiveType', () => {
it('should return formatted invalid directive type message', () => {
const result = SCHEMA_VALIDATION_ERRORS.invalidDirectiveType(
'goal',
'string',
'number'
);
expect(result).toBe("Directive 'goal' expected string, got number");
});
});
});
});
describe('Error Chaining and Inheritance', () => {
it('should properly chain error causes', () => {
const originalError = new Error('original error');
const umsError = new UMSError('wrapped error', 'WRAP_ERROR');
umsError.cause = originalError;
expect(umsError.cause).toBe(originalError);
});
it('should maintain instanceof relationships', () => {
const validationError = new UMSValidationError('test');
expect(validationError instanceof Error).toBe(true);
expect(validationError instanceof UMSError).toBe(true);
expect(validationError instanceof UMSValidationError).toBe(true);
});
it('should have proper constructor names', () => {
const errors = [
new UMSError('test', 'CODE'),
new UMSValidationError('test'),
new ModuleLoadError('test'),
new PersonaLoadError('test'),
new BuildError('test'),
];
expect(errors[0].constructor.name).toBe('UMSError');
expect(errors[1].constructor.name).toBe('UMSValidationError');
expect(errors[2].constructor.name).toBe('ModuleLoadError');
expect(errors[3].constructor.name).toBe('PersonaLoadError');
expect(errors[4].constructor.name).toBe('BuildError');
});
});
describe('Edge Cases', () => {
it('should handle very long error messages', () => {
const longMessage = 'a'.repeat(10000);
const error = new UMSError(longMessage, 'LONG_MESSAGE');
expect(error.message).toBe(longMessage);
expect(error.message.length).toBe(10000);
});
it('should handle special characters in error messages', () => {
const specialMessage = 'Error with "quotes" and \n newlines \t tabs';
const error = new UMSValidationError(specialMessage);
expect(error.message).toBe(specialMessage);
});
it('should handle Unicode characters', () => {
const unicodeMessage = 'Error with unicode: 🚨 ñáéíóú 中文';
const error = new BuildError(unicodeMessage);
expect(error.message).toBe(unicodeMessage);
});
});
});