UtilityTabResults.utils.test.ts80 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { getSqlErrorLines } from './UtilityTabResults.utils' |
| 4 | |
| 5 | describe('getSqlErrorLines', () => { |
| 6 | it('returns formattedError lines when present', () => { |
| 7 | const lines = getSqlErrorLines({ |
| 8 | message: 'permission denied for table users', |
| 9 | formattedError: |
| 10 | 'ERROR: 42501: permission denied for table users\n' + |
| 11 | 'HINT: To grant access to anon on a specific table:\n' + |
| 12 | ' GRANT SELECT ON TABLE public.users TO anon;', |
| 13 | }) |
| 14 | |
| 15 | expect(lines).toEqual([ |
| 16 | 'ERROR: 42501: permission denied for table users', |
| 17 | 'HINT: To grant access to anon on a specific table:', |
| 18 | ' GRANT SELECT ON TABLE public.users TO anon;', |
| 19 | ]) |
| 20 | }) |
| 21 | |
| 22 | it('strips empty lines from formattedError', () => { |
| 23 | const lines = getSqlErrorLines({ |
| 24 | formattedError: 'ERROR: boom\n\nHINT: retry\n', |
| 25 | }) |
| 26 | |
| 27 | expect(lines).toEqual(['ERROR: boom', 'HINT: retry']) |
| 28 | }) |
| 29 | |
| 30 | it('falls back to message lines when formattedError is missing and message is multi-line', () => { |
| 31 | const lines = getSqlErrorLines({ |
| 32 | message: |
| 33 | 'ERROR: 42501: permission denied for table users\n' + |
| 34 | 'HINT: To grant access to anon on a specific table:\n' + |
| 35 | ' GRANT SELECT ON TABLE public.users TO anon;', |
| 36 | }) |
| 37 | |
| 38 | expect(lines).toEqual([ |
| 39 | 'ERROR: 42501: permission denied for table users', |
| 40 | 'HINT: To grant access to anon on a specific table:', |
| 41 | ' GRANT SELECT ON TABLE public.users TO anon;', |
| 42 | ]) |
| 43 | }) |
| 44 | |
| 45 | it('returns empty array for a single-line message so callers render the fallback', () => { |
| 46 | const lines = getSqlErrorLines({ message: 'permission denied for table users' }) |
| 47 | expect(lines).toEqual([]) |
| 48 | }) |
| 49 | |
| 50 | it('returns empty array when both fields are missing', () => { |
| 51 | expect(getSqlErrorLines({})).toEqual([]) |
| 52 | }) |
| 53 | |
| 54 | it('returns empty array when message is an empty string', () => { |
| 55 | expect(getSqlErrorLines({ message: '' })).toEqual([]) |
| 56 | }) |
| 57 | |
| 58 | it('returns empty array when message only contains whitespace newlines', () => { |
| 59 | // Only empty segments after filtering — treated as single-line |
| 60 | expect(getSqlErrorLines({ message: '\n\n' })).toEqual([]) |
| 61 | }) |
| 62 | |
| 63 | it('prefers formattedError even when message is also multi-line', () => { |
| 64 | const lines = getSqlErrorLines({ |
| 65 | message: 'message line 1\nmessage line 2', |
| 66 | formattedError: 'formatted line 1\nformatted line 2', |
| 67 | }) |
| 68 | |
| 69 | expect(lines).toEqual(['formatted line 1', 'formatted line 2']) |
| 70 | }) |
| 71 | |
| 72 | it('falls through to message when formattedError is empty string', () => { |
| 73 | const lines = getSqlErrorLines({ |
| 74 | message: 'ERROR: line 1\nHINT: line 2', |
| 75 | formattedError: '', |
| 76 | }) |
| 77 | |
| 78 | expect(lines).toEqual(['ERROR: line 1', 'HINT: line 2']) |
| 79 | }) |
| 80 | }) |