|
| 1 | +/** |
| 2 | + * Tests for loader utilities |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from 'vitest'; |
| 6 | +import { filePathToUrl, formatValidationErrors } from './loader-utils.js'; |
| 7 | +import type { ValidationResult } from 'ums-lib'; |
| 8 | + |
| 9 | +describe('loader-utils', () => { |
| 10 | + describe('filePathToUrl', () => { |
| 11 | + it('should convert file path to file URL', () => { |
| 12 | + const filePath = '/path/to/module.ts'; |
| 13 | + const url = filePathToUrl(filePath); |
| 14 | + |
| 15 | + expect(url).toContain('file://'); |
| 16 | + expect(url).toContain('module.ts'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should handle paths with special characters', () => { |
| 20 | + const filePath = '/path/to/my module.ts'; |
| 21 | + const url = filePathToUrl(filePath); |
| 22 | + |
| 23 | + expect(url).toContain('file://'); |
| 24 | + expect(url).toContain('my%20module.ts'); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('formatValidationErrors', () => { |
| 29 | + it('should format validation errors with paths', () => { |
| 30 | + const validation: ValidationResult = { |
| 31 | + valid: false, |
| 32 | + errors: [ |
| 33 | + { path: 'metadata.name', message: 'Name is required' }, |
| 34 | + { path: 'components[0]', message: 'Invalid component' } |
| 35 | + ], |
| 36 | + warnings: [] |
| 37 | + }; |
| 38 | + |
| 39 | + const result = formatValidationErrors(validation); |
| 40 | + expect(result).toBe('metadata.name: Name is required; components[0]: Invalid component'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should use default path for errors without path', () => { |
| 44 | + const validation: ValidationResult = { |
| 45 | + valid: false, |
| 46 | + errors: [ |
| 47 | + { message: 'Invalid structure' } |
| 48 | + ], |
| 49 | + warnings: [] |
| 50 | + }; |
| 51 | + |
| 52 | + const result = formatValidationErrors(validation); |
| 53 | + expect(result).toBe('module: Invalid structure'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should use custom default path', () => { |
| 57 | + const validation: ValidationResult = { |
| 58 | + valid: false, |
| 59 | + errors: [ |
| 60 | + { message: 'Invalid structure' } |
| 61 | + ], |
| 62 | + warnings: [] |
| 63 | + }; |
| 64 | + |
| 65 | + const result = formatValidationErrors(validation, 'persona'); |
| 66 | + expect(result).toBe('persona: Invalid structure'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle mixed errors with and without paths', () => { |
| 70 | + const validation: ValidationResult = { |
| 71 | + valid: false, |
| 72 | + errors: [ |
| 73 | + { path: 'id', message: 'ID is required' }, |
| 74 | + { message: 'Missing schema version' }, |
| 75 | + { path: 'version', message: 'Invalid version format' } |
| 76 | + ], |
| 77 | + warnings: [] |
| 78 | + }; |
| 79 | + |
| 80 | + const result = formatValidationErrors(validation); |
| 81 | + expect(result).toBe('id: ID is required; module: Missing schema version; version: Invalid version format'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle single error', () => { |
| 85 | + const validation: ValidationResult = { |
| 86 | + valid: false, |
| 87 | + errors: [ |
| 88 | + { path: 'id', message: 'ID is required' } |
| 89 | + ], |
| 90 | + warnings: [] |
| 91 | + }; |
| 92 | + |
| 93 | + const result = formatValidationErrors(validation); |
| 94 | + expect(result).toBe('id: ID is required'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle empty errors array', () => { |
| 98 | + const validation: ValidationResult = { |
| 99 | + valid: true, |
| 100 | + errors: [], |
| 101 | + warnings: [] |
| 102 | + }; |
| 103 | + |
| 104 | + const result = formatValidationErrors(validation); |
| 105 | + expect(result).toBe(''); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments