|
1 | | -import {parseHumanReadableError} from './error-parsing.js' |
| 1 | +import {parseStructuredErrors} from './error-parsing.js' |
2 | 2 | import {describe, expect, test} from 'vitest' |
3 | 3 |
|
4 | | -describe('parseHumanReadableError', () => { |
5 | | - test('formats union errors with smart variant detection', () => { |
6 | | - const unionErrorObject = [ |
7 | | - { |
8 | | - code: 'invalid_union', |
9 | | - unionErrors: [ |
10 | | - { |
11 | | - issues: [ |
12 | | - { |
13 | | - code: 'invalid_type', |
14 | | - expected: 'array', |
15 | | - received: 'number', |
16 | | - path: ['web', 'roles'], |
17 | | - message: 'Expected array, received number', |
18 | | - }, |
19 | | - { |
20 | | - code: 'invalid_type', |
21 | | - expected: 'string', |
22 | | - received: 'undefined', |
23 | | - path: ['web', 'commands', 'build'], |
24 | | - message: 'Required', |
25 | | - }, |
26 | | - ], |
27 | | - name: 'ZodError', |
28 | | - }, |
29 | | - { |
30 | | - issues: [ |
31 | | - { |
32 | | - code: 'invalid_literal', |
33 | | - expected: "'frontend'", |
34 | | - received: 'number', |
35 | | - path: ['web', 'type'], |
36 | | - message: "Invalid literal value, expected 'frontend'", |
37 | | - }, |
38 | | - ], |
39 | | - name: 'ZodError', |
40 | | - }, |
41 | | - ], |
42 | | - path: ['web'], |
43 | | - message: 'Invalid input', |
44 | | - }, |
| 4 | +describe('parseStructuredErrors', () => { |
| 5 | + test('converts regular issues to structured format', () => { |
| 6 | + const issues = [ |
| 7 | + {path: ['name'], message: 'Required', code: 'invalid_type'}, |
| 8 | + {path: ['version'], message: 'Must be a string', code: 'invalid_type'}, |
45 | 9 | ] |
46 | 10 |
|
47 | | - const result = parseHumanReadableError(unionErrorObject) |
48 | | - |
49 | | - // Verify the enhanced format shows only the best matching variant's errors |
50 | | - // (Option 1 has both missing field + type error, so it's likely the intended one) |
51 | | - expect(result).toContain('[web.roles]: Expected array, received number') |
52 | | - expect(result).toContain('[web.commands.build]: Required') |
53 | | - |
54 | | - // Should NOT show confusing union variant breakdown |
55 | | - expect(result).not.toContain('Union validation failed') |
56 | | - expect(result).not.toContain('Option 1:') |
57 | | - expect(result).not.toContain('Option 2:') |
| 11 | + const result = parseStructuredErrors(issues) |
58 | 12 |
|
59 | | - // Should NOT show errors from the less likely option 2 |
60 | | - expect(result).not.toContain("[web.type]: Invalid literal value, expected 'frontend'") |
| 13 | + expect(result).toEqual([ |
| 14 | + {path: ['name'], message: 'Required', code: 'invalid_type'}, |
| 15 | + {path: ['version'], message: 'Must be a string', code: 'invalid_type'}, |
| 16 | + ]) |
61 | 17 | }) |
62 | 18 |
|
63 | | - test('handles regular non-union errors', () => { |
64 | | - const regularErrorObject = [ |
65 | | - { |
66 | | - path: ['name'], |
67 | | - message: 'Required field is missing', |
68 | | - }, |
69 | | - { |
70 | | - path: ['version'], |
71 | | - message: 'Must be a valid semver string', |
72 | | - }, |
73 | | - ] |
74 | | - |
75 | | - const result = parseHumanReadableError(regularErrorObject) |
76 | | - |
77 | | - // Verify regular errors still work as expected |
78 | | - expect(result).toBe('• [name]: Required field is missing\n• [version]: Must be a valid semver string\n') |
79 | | - expect(result).not.toContain('Union validation failed') |
80 | | - }) |
81 | | - |
82 | | - test('handles edge cases for union error detection', () => { |
83 | | - // Test case 1: Union error with no unionErrors array |
84 | | - const noUnionErrors = [ |
85 | | - { |
86 | | - code: 'invalid_union', |
87 | | - path: ['root'], |
88 | | - message: 'Invalid input', |
89 | | - }, |
90 | | - ] |
91 | | - |
92 | | - const result1 = parseHumanReadableError(noUnionErrors) |
93 | | - expect(result1).toBe('• [root]: Invalid input\n') |
94 | | - |
95 | | - // Test case 2: Union error with empty unionErrors array - should fall back to showing full union error |
96 | | - const emptyUnionErrors = [ |
97 | | - { |
98 | | - code: 'invalid_union', |
99 | | - unionErrors: [], |
100 | | - path: ['root'], |
101 | | - message: 'Invalid input', |
102 | | - }, |
103 | | - ] |
104 | | - |
105 | | - const result2 = parseHumanReadableError(emptyUnionErrors) |
106 | | - expect(result2).toContain("Configuration doesn't match any expected format:") |
107 | | - |
108 | | - // Test case 3: Union errors with variants that have no issues - results in empty string |
109 | | - const noIssuesVariants = [ |
110 | | - { |
111 | | - code: 'invalid_union', |
112 | | - unionErrors: [ |
113 | | - {issues: [], name: 'ZodError'}, |
114 | | - {issues: [], name: 'ZodError'}, |
115 | | - ], |
116 | | - path: ['root'], |
117 | | - message: 'Invalid input', |
118 | | - }, |
119 | | - ] |
120 | | - |
121 | | - const result3 = parseHumanReadableError(noIssuesVariants) |
122 | | - // When all variants have no issues, the best variant selection returns null issues |
123 | | - // resulting in no output, which falls back to the union error display |
124 | | - expect(result3).toContain("Configuration doesn't match any expected format:") |
125 | | - }) |
126 | | - |
127 | | - test('findBestMatchingVariant scoring logic works correctly', () => { |
128 | | - // Test various scoring scenarios by creating mock union errors |
129 | | - const scenarioWithMissingFields = [ |
| 19 | + test('selects best union variant based on scoring', () => { |
| 20 | + const issues = [ |
130 | 21 | { |
131 | 22 | code: 'invalid_union', |
132 | 23 | unionErrors: [ |
133 | 24 | { |
134 | | - // Variant with missing fields - should score highest |
135 | 25 | issues: [ |
136 | 26 | {path: ['supports_moto'], message: 'Required'}, |
137 | 27 | {path: ['merchant_label'], message: 'Required'}, |
138 | 28 | ], |
139 | 29 | name: 'ZodError', |
140 | 30 | }, |
141 | 31 | { |
142 | | - // Variant with only type errors - should score lower |
143 | 32 | issues: [ |
144 | 33 | {path: ['type'], message: 'Expected string, received number'}, |
145 | 34 | {path: ['id'], message: 'Expected number, received string'}, |
146 | 35 | ], |
147 | 36 | name: 'ZodError', |
148 | 37 | }, |
149 | | - { |
150 | | - // Variant with other errors - should score lowest |
151 | | - issues: [{path: ['url'], message: 'Invalid URL format'}], |
152 | | - name: 'ZodError', |
153 | | - }, |
154 | 38 | ], |
155 | 39 | path: [], |
156 | 40 | message: 'Invalid input', |
157 | 41 | }, |
158 | 42 | ] |
159 | 43 |
|
160 | | - const result = parseHumanReadableError(scenarioWithMissingFields) |
| 44 | + const result = parseStructuredErrors(issues) |
161 | 45 |
|
162 | | - // Should show only the variant with missing fields (highest score) |
163 | | - expect(result).toContain('[supports_moto]: Required') |
164 | | - expect(result).toContain('[merchant_label]: Required') |
165 | | - |
166 | | - // Should NOT show errors from other variants |
167 | | - expect(result).not.toContain('Expected string, received number') |
168 | | - expect(result).not.toContain('Invalid URL format') |
169 | | - expect(result).not.toContain('Union validation failed') |
| 46 | + expect(result).toEqual([ |
| 47 | + {path: ['supports_moto'], message: 'Required', code: 'invalid_union'}, |
| 48 | + {path: ['merchant_label'], message: 'Required', code: 'invalid_union'}, |
| 49 | + ]) |
170 | 50 | }) |
171 | 51 |
|
172 | | - test('handles undefined messages gracefully', () => { |
173 | | - const undefinedMessageError = [ |
174 | | - { |
175 | | - path: ['field'], |
176 | | - message: undefined, |
177 | | - }, |
| 52 | + test('falls back when all union variants have empty issues', () => { |
| 53 | + const issues = [ |
178 | 54 | { |
179 | | - path: [], |
180 | | - // message is completely missing |
| 55 | + code: 'invalid_union', |
| 56 | + unionErrors: [ |
| 57 | + {issues: [], name: 'ZodError'}, |
| 58 | + {issues: [], name: 'ZodError'}, |
| 59 | + ], |
| 60 | + path: ['root'], |
| 61 | + message: 'Invalid input', |
181 | 62 | }, |
182 | 63 | ] |
183 | 64 |
|
184 | | - const result = parseHumanReadableError(undefinedMessageError) |
| 65 | + const result = parseStructuredErrors(issues) |
185 | 66 |
|
186 | | - expect(result).toBe('• [field]: Unknown error\n• [root]: Unknown error\n') |
| 67 | + expect(result).toEqual([{path: ['root'], message: 'Invalid input', code: 'invalid_union'}]) |
187 | 68 | }) |
188 | 69 |
|
189 | | - test('handles mixed scoring scenarios', () => { |
190 | | - // Test scenario where we need to pick between variants with different error combinations |
191 | | - const mixedScenario = [ |
| 70 | + test('falls back when union has no unionErrors array', () => { |
| 71 | + const issues = [ |
192 | 72 | { |
193 | 73 | code: 'invalid_union', |
194 | | - unionErrors: [ |
195 | | - { |
196 | | - // Mix of missing fields and type errors - this should win due to missing fields |
197 | | - issues: [ |
198 | | - {path: ['required_field'], message: 'Required'}, |
199 | | - {path: ['wrong_type'], message: 'Expected string, received number'}, |
200 | | - ], |
201 | | - name: 'ZodError', |
202 | | - }, |
203 | | - { |
204 | | - // Only type errors - should lose |
205 | | - issues: [ |
206 | | - {path: ['field1'], message: 'Expected boolean, received string'}, |
207 | | - {path: ['field2'], message: 'Expected array, received object'}, |
208 | | - {path: ['field3'], message: 'Expected number, received string'}, |
209 | | - ], |
210 | | - name: 'ZodError', |
211 | | - }, |
212 | | - { |
213 | | - // Only other validation errors - should score lowest |
214 | | - issues: [ |
215 | | - {path: ['url'], message: 'Must be valid URL'}, |
216 | | - {path: ['email'], message: 'Must be valid email'}, |
217 | | - ], |
218 | | - name: 'ZodError', |
219 | | - }, |
220 | | - ], |
221 | | - path: [], |
| 74 | + path: ['field'], |
222 | 75 | message: 'Invalid input', |
223 | 76 | }, |
224 | 77 | ] |
225 | 78 |
|
226 | | - const result = parseHumanReadableError(mixedScenario) |
| 79 | + const result = parseStructuredErrors(issues) |
| 80 | + |
| 81 | + expect(result).toEqual([{path: ['field'], message: 'Invalid input', code: 'invalid_union'}]) |
| 82 | + }) |
| 83 | + |
| 84 | + test('handles missing message with fallback', () => { |
| 85 | + const issues = [{path: ['x'], code: 'custom'}] |
227 | 86 |
|
228 | | - // Should pick the variant with missing field (even though it has fewer total errors) |
229 | | - expect(result).toContain('[required_field]: Required') |
230 | | - expect(result).toContain('[wrong_type]: Expected string, received number') |
| 87 | + const result = parseStructuredErrors(issues) |
231 | 88 |
|
232 | | - // Should not show errors from other variants |
233 | | - expect(result).not.toContain('Expected boolean, received string') |
234 | | - expect(result).not.toContain('Must be valid URL') |
235 | | - expect(result).not.toContain('Union validation failed') |
| 89 | + expect(result).toEqual([{path: ['x'], message: 'Unknown error', code: 'custom'}]) |
236 | 90 | }) |
237 | 91 | }) |
0 commit comments