|
| 1 | +import { describe, expect, test } from '@jest/globals'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { formatZodIssue, formatZodIssues, formatZodError } from './format-zod-error'; |
| 4 | +import { deepStrict } from './deep-strict'; |
| 5 | +import { CustomLlmDefinitionSchema } from '@kilocode/db/schema-types'; |
| 6 | + |
| 7 | +describe('formatZodIssue', () => { |
| 8 | + test('formats issue with path', () => { |
| 9 | + const formatted = formatZodIssue({ |
| 10 | + code: 'invalid_type', |
| 11 | + path: ['internal_id'], |
| 12 | + message: 'Too small', |
| 13 | + }); |
| 14 | + expect(formatted).toBe('internal_id: Too small'); |
| 15 | + }); |
| 16 | + |
| 17 | + test('formats issue with empty path without leading colon', () => { |
| 18 | + const formatted = formatZodIssue({ |
| 19 | + code: 'custom', |
| 20 | + path: [], |
| 21 | + message: 'Invalid configuration', |
| 22 | + }); |
| 23 | + expect(formatted).toBe('Invalid configuration'); |
| 24 | + }); |
| 25 | + |
| 26 | + test('formats unrecognized_keys at top level', () => { |
| 27 | + const formatted = formatZodIssue({ |
| 28 | + code: 'unrecognized_keys', |
| 29 | + path: [], |
| 30 | + keys: ['dispaly_name'], |
| 31 | + }); |
| 32 | + expect(formatted).toBe('Unrecognized key: "dispaly_name"'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('formats multiple unrecognized_keys with nested path', () => { |
| 36 | + const formatted = formatZodIssue({ |
| 37 | + code: 'unrecognized_keys', |
| 38 | + path: ['pricing'], |
| 39 | + keys: ['extra1', 'extra2'], |
| 40 | + }); |
| 41 | + expect(formatted).toBe('pricing: Unrecognized keys: "extra1", "extra2"'); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('formatZodIssues with unions', () => { |
| 46 | + const schema = deepStrict(CustomLlmDefinitionSchema); |
| 47 | + |
| 48 | + test('formats initial definition errors cleanly without ": Invalid input"', () => { |
| 49 | + const initialDefinition = { |
| 50 | + internal_id: '', |
| 51 | + display_name: '', |
| 52 | + context_length: 0, |
| 53 | + max_completion_tokens: 0, |
| 54 | + base_url: '', |
| 55 | + api_key: '', |
| 56 | + organization_ids: [], |
| 57 | + }; |
| 58 | + |
| 59 | + const result = schema.safeParse(initialDefinition); |
| 60 | + expect(result.success).toBe(false); |
| 61 | + if (!result.success) { |
| 62 | + const messages = formatZodIssues(result.error.issues); |
| 63 | + expect(messages).not.toContain(': Invalid input'); |
| 64 | + expect(messages).not.toContain('Invalid input'); |
| 65 | + expect(messages).toEqual( |
| 66 | + expect.arrayContaining([ |
| 67 | + expect.stringContaining('internal_id'), |
| 68 | + expect.stringContaining('base_url'), |
| 69 | + ]) |
| 70 | + ); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + test('picks the API key branch when api_key is present and fields have typos', () => { |
| 75 | + const typoInput = { |
| 76 | + internal_id: 'model-1', |
| 77 | + dispaly_name: 'Typo Name', |
| 78 | + context_length: 1000, |
| 79 | + max_completion_tokens: 100, |
| 80 | + base_url: 'https://example.com', |
| 81 | + api_key: 'secret', |
| 82 | + organization_ids: [], |
| 83 | + }; |
| 84 | + |
| 85 | + const result = schema.safeParse(typoInput); |
| 86 | + expect(result.success).toBe(false); |
| 87 | + if (!result.success) { |
| 88 | + const messages = formatZodIssues(result.error.issues); |
| 89 | + expect(messages).not.toContain(': Invalid input'); |
| 90 | + expect(messages).toContain('Unrecognized key: "dispaly_name"'); |
| 91 | + expect(messages.some(m => m.includes('display_name'))).toBe(true); |
| 92 | + expect(messages.some(m => m.includes('google_service_account'))).toBe(false); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + test('picks the Google service account branch when google_service_account is present', () => { |
| 97 | + const gsaInput = { |
| 98 | + internal_id: 'model-1', |
| 99 | + display_name: 'Vertex Gemini', |
| 100 | + context_length: 1000, |
| 101 | + max_completion_tokens: 100, |
| 102 | + base_url: 'https://example.com', |
| 103 | + organization_ids: [], |
| 104 | + google_service_account: { |
| 105 | + type: 'service_account', |
| 106 | + project_id: 'proj', |
| 107 | + private_key_id: 'key-id', |
| 108 | + private_key: 'pk', |
| 109 | + client_email: 'invalid-email', |
| 110 | + client_id: '123', |
| 111 | + auth_uri: 'https://accounts.google.com', |
| 112 | + token_uri: 'https://oauth2.googleapis.com', |
| 113 | + auth_provider_x509_cert_url: 'https://example.com', |
| 114 | + client_x509_cert_url: 'https://example.com', |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + const result = schema.safeParse(gsaInput); |
| 119 | + expect(result.success).toBe(false); |
| 120 | + if (!result.success) { |
| 121 | + const messages = formatZodIssues(result.error.issues); |
| 122 | + expect(messages).not.toContain(': Invalid input'); |
| 123 | + expect(messages.some(m => m.includes('google_service_account.client_email'))).toBe(true); |
| 124 | + expect(messages.some(m => m.includes('api_key'))).toBe(false); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + test('shows both authentication options when neither is provided', () => { |
| 129 | + const noAuthInput = { |
| 130 | + internal_id: 'model-1', |
| 131 | + display_name: 'Model 1', |
| 132 | + context_length: 1000, |
| 133 | + max_completion_tokens: 100, |
| 134 | + base_url: 'https://example.com', |
| 135 | + organization_ids: [], |
| 136 | + }; |
| 137 | + |
| 138 | + const result = schema.safeParse(noAuthInput); |
| 139 | + expect(result.success).toBe(false); |
| 140 | + if (!result.success) { |
| 141 | + const messages = formatZodIssues(result.error.issues); |
| 142 | + expect(messages).not.toContain(': Invalid input'); |
| 143 | + expect(messages.some(m => m.includes('api_key'))).toBe(true); |
| 144 | + expect(messages.some(m => m.includes('google_service_account'))).toBe(true); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test('handles nested unions with path prefixes', () => { |
| 149 | + const nestedSchema = z.object({ |
| 150 | + config: z.union([ |
| 151 | + z.object({ mode: z.literal('a'), aValue: z.string() }), |
| 152 | + z.object({ mode: z.literal('b'), bValue: z.number() }), |
| 153 | + ]), |
| 154 | + }); |
| 155 | + |
| 156 | + const result = nestedSchema.safeParse({ config: { mode: 'a', aValue: 123 } }); |
| 157 | + expect(result.success).toBe(false); |
| 158 | + if (!result.success) { |
| 159 | + const messages = formatZodIssues(result.error.issues); |
| 160 | + expect(messages).toEqual(['config.aValue: Invalid input: expected string, received number']); |
| 161 | + } |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('formatZodError', () => { |
| 166 | + test('formats ZodError instance', () => { |
| 167 | + const schema = z.object({ name: z.string() }); |
| 168 | + const result = schema.safeParse({ name: 123 }); |
| 169 | + if (!result.success) { |
| 170 | + const formatted = formatZodError(result.error); |
| 171 | + expect(formatted).toBe('name: Invalid input: expected string, received number'); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + test('formats JSON-encoded TRPC issues string', () => { |
| 176 | + const jsonError = JSON.stringify([ |
| 177 | + { |
| 178 | + code: 'custom', |
| 179 | + message: 'public_id must start with "custom-llm/"', |
| 180 | + path: ['public_id'], |
| 181 | + }, |
| 182 | + ]); |
| 183 | + const formatted = formatZodError(new Error(jsonError)); |
| 184 | + expect(formatted).toBe('public_id: public_id must start with "custom-llm/"'); |
| 185 | + }); |
| 186 | + |
| 187 | + test('formats TRPC data.zodError shape', () => { |
| 188 | + const trpcError = { |
| 189 | + message: 'BAD_REQUEST', |
| 190 | + data: { |
| 191 | + zodError: { |
| 192 | + formErrors: [], |
| 193 | + fieldErrors: { |
| 194 | + public_id: ['public_id is required'], |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }; |
| 199 | + const formatted = formatZodError(trpcError); |
| 200 | + expect(formatted).toBe('public_id: public_id is required'); |
| 201 | + }); |
| 202 | + |
| 203 | + test('falls back to plain Error message', () => { |
| 204 | + const formatted = formatZodError(new Error('Network error')); |
| 205 | + expect(formatted).toBe('Network error'); |
| 206 | + }); |
| 207 | +}); |
0 commit comments