|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { NetworkError, AuthenticationError, isNetworkError, isAuthenticationError } from '../errors' |
| 3 | + |
| 4 | +describe('NetworkError', () => { |
| 5 | + test('removes unhelpful browser error messages', () => { |
| 6 | + const error = new NetworkError('Is the computer able to access the url?') |
| 7 | + expect(error.message).toBe('') |
| 8 | + }) |
| 9 | + |
| 10 | + test('removes unhelpful text but preserves the rest', () => { |
| 11 | + const error = new NetworkError('Network error: Is the computer able to access the url?') |
| 12 | + expect(error.message).toBe('Network error:') |
| 13 | + }) |
| 14 | + |
| 15 | + test('removes unhelpful text from middle of message', () => { |
| 16 | + const error = new NetworkError('Unable to connect. Is the computer able to access the url? Verify connection.') |
| 17 | + expect(error.message).toBe('Unable to connect. Verify connection.') |
| 18 | + }) |
| 19 | + |
| 20 | + test('preserves helpful error messages', () => { |
| 21 | + const error = new NetworkError('Connection refused') |
| 22 | + expect(error.message).toBe('Connection refused') |
| 23 | + }) |
| 24 | + |
| 25 | + test('includes error code', () => { |
| 26 | + const error = new NetworkError('Test error') |
| 27 | + expect(error.code).toBe('NETWORK_ERROR') |
| 28 | + }) |
| 29 | + |
| 30 | + test('includes status when provided', () => { |
| 31 | + const error = new NetworkError('Server error', { status: 500 }) |
| 32 | + expect(error.status).toBe(500) |
| 33 | + }) |
| 34 | + |
| 35 | + test('includes originalError when provided', () => { |
| 36 | + const originalError = new Error('Original') |
| 37 | + const error = new NetworkError('Wrapped error', { originalError }) |
| 38 | + expect(error.originalError).toBe(originalError) |
| 39 | + }) |
| 40 | + |
| 41 | + test('includes streamTimedOut flag when provided', () => { |
| 42 | + const error = new NetworkError('Timeout', { streamTimedOut: true }) |
| 43 | + expect(error.streamTimedOut).toBe(true) |
| 44 | + }) |
| 45 | +}) |
| 46 | + |
| 47 | +describe('AuthenticationError', () => { |
| 48 | + test('creates error with status', () => { |
| 49 | + const error = new AuthenticationError('Auth failed', 401) |
| 50 | + expect(error.message).toBe('Auth failed') |
| 51 | + expect(error.status).toBe(401) |
| 52 | + expect(error.code).toBe('AUTH_FAILED') |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('Type guards', () => { |
| 57 | + test('isNetworkError identifies NetworkError instances', () => { |
| 58 | + const error = new NetworkError('Test') |
| 59 | + expect(isNetworkError(error)).toBe(true) |
| 60 | + expect(isNetworkError(new Error('Test'))).toBe(false) |
| 61 | + }) |
| 62 | + |
| 63 | + test('isAuthenticationError identifies AuthenticationError instances', () => { |
| 64 | + const error = new AuthenticationError('Test', 401) |
| 65 | + expect(isAuthenticationError(error)).toBe(true) |
| 66 | + expect(isAuthenticationError(new Error('Test'))).toBe(false) |
| 67 | + }) |
| 68 | +}) |
0 commit comments