|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { SampleGenerationError as BundleableSampleGenerationError } from './bundleable/SampleGenerationError.ts'; |
| 4 | +import { ErrorCode, isSampleGenerationError } from './error-codes.ts'; |
| 5 | +import { SampleGenerationError as MainSampleGenerationError } from './errors/SampleGenerationError.ts'; |
| 6 | + |
| 7 | +describe('error-codes', () => { |
| 8 | + describe('isSampleGenerationError', () => { |
| 9 | + it('returns true for bundleable SampleGenerationError', () => { |
| 10 | + const error = new BundleableSampleGenerationError( |
| 11 | + 'invalid sample', |
| 12 | + new Error('parse failed'), |
| 13 | + ); |
| 14 | + expect(isSampleGenerationError(error)).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns true for main SampleGenerationError', () => { |
| 18 | + const error = new MainSampleGenerationError( |
| 19 | + 'invalid sample', |
| 20 | + new Error('parse failed'), |
| 21 | + ); |
| 22 | + expect(isSampleGenerationError(error)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns false for plain Error', () => { |
| 26 | + const error = new Error('some error'); |
| 27 | + expect(isSampleGenerationError(error)).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns false for Error with different code', () => { |
| 31 | + const error = new Error('some error'); |
| 32 | + (error as Error & { code: string }).code = ErrorCode.AbortError; |
| 33 | + expect(isSampleGenerationError(error)).toBe(false); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns false for non-Error values', () => { |
| 37 | + expect(isSampleGenerationError(null)).toBe(false); |
| 38 | + expect(isSampleGenerationError(undefined)).toBe(false); |
| 39 | + expect(isSampleGenerationError('error string')).toBe(false); |
| 40 | + expect( |
| 41 | + isSampleGenerationError({ code: ErrorCode.SampleGenerationError }), |
| 42 | + ).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('narrows type correctly', () => { |
| 46 | + const error = new BundleableSampleGenerationError( |
| 47 | + 'test sample', |
| 48 | + new Error('cause'), |
| 49 | + ); |
| 50 | + |
| 51 | + expect(isSampleGenerationError(error)).toBe(true); |
| 52 | + // After the check above passes, TypeScript allows accessing these properties |
| 53 | + expect(error.code).toBe(ErrorCode.SampleGenerationError); |
| 54 | + expect(error.data.sample).toBe('test sample'); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments