|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + KERNEL_ERROR_PATTERN, |
| 5 | + isKernelError, |
| 6 | + getKernelErrorCode, |
| 7 | + isFatalKernelError, |
| 8 | +} from './vat-observable-errors.ts'; |
| 9 | + |
| 10 | +describe('KERNEL_ERROR_PATTERN', () => { |
| 11 | + it.each([ |
| 12 | + ['[KERNEL:OBJECT_REVOKED] Target object has been revoked', true], |
| 13 | + ['[KERNEL:VAT_FATAL:ILLEGAL_SYSCALL] Fatal syscall violation', true], |
| 14 | + ['[KERNEL:CONNECTION_LOST] Remote connection lost', true], |
| 15 | + ['Some other error', false], |
| 16 | + ['KERNEL:OBJECT_REVOKED', false], |
| 17 | + ['[KERNEL:lowercase] bad code', false], |
| 18 | + ])('matches %j -> %j', (message, expected) => { |
| 19 | + expect(KERNEL_ERROR_PATTERN.test(message)).toBe(expected); |
| 20 | + }); |
| 21 | +}); |
| 22 | + |
| 23 | +describe('isKernelError', () => { |
| 24 | + it('returns true for an Error with a kernel error message', () => { |
| 25 | + expect(isKernelError(Error('[KERNEL:OBJECT_DELETED] Target deleted'))).toBe( |
| 26 | + true, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns true for a fatal kernel error', () => { |
| 31 | + expect( |
| 32 | + isKernelError(Error('[KERNEL:VAT_FATAL:INTERNAL_ERROR] Something broke')), |
| 33 | + ).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns false for a plain Error', () => { |
| 37 | + expect(isKernelError(Error('just a normal error'))).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns false for non-Error values', () => { |
| 41 | + expect(isKernelError('string')).toBe(false); |
| 42 | + expect(isKernelError(null)).toBe(false); |
| 43 | + expect(isKernelError(undefined)).toBe(false); |
| 44 | + expect(isKernelError(42)).toBe(false); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('getKernelErrorCode', () => { |
| 49 | + it('extracts expected error codes', () => { |
| 50 | + expect(getKernelErrorCode(Error('[KERNEL:OBJECT_REVOKED] revoked'))).toBe( |
| 51 | + 'OBJECT_REVOKED', |
| 52 | + ); |
| 53 | + expect(getKernelErrorCode(Error('[KERNEL:CONNECTION_LOST] lost'))).toBe( |
| 54 | + 'CONNECTION_LOST', |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('extracts fatal error codes', () => { |
| 59 | + expect( |
| 60 | + getKernelErrorCode(Error('[KERNEL:VAT_FATAL:ILLEGAL_SYSCALL] bad')), |
| 61 | + ).toBe('ILLEGAL_SYSCALL'); |
| 62 | + expect( |
| 63 | + getKernelErrorCode(Error('[KERNEL:VAT_FATAL:INTERNAL_ERROR] broken')), |
| 64 | + ).toBe('INTERNAL_ERROR'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns undefined for non-kernel errors', () => { |
| 68 | + expect(getKernelErrorCode(Error('normal error'))).toBeUndefined(); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('isFatalKernelError', () => { |
| 73 | + it('returns true for fatal kernel errors', () => { |
| 74 | + expect( |
| 75 | + isFatalKernelError( |
| 76 | + Error('[KERNEL:VAT_FATAL:ILLEGAL_SYSCALL] bad syscall'), |
| 77 | + ), |
| 78 | + ).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns false for expected kernel errors', () => { |
| 82 | + expect(isFatalKernelError(Error('[KERNEL:OBJECT_REVOKED] revoked'))).toBe( |
| 83 | + false, |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('returns false for non-kernel errors', () => { |
| 88 | + expect(isFatalKernelError(Error('normal error'))).toBe(false); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('round-trip', () => { |
| 93 | + it('constructs and detects a kernel error message', () => { |
| 94 | + const code = 'OBJECT_DELETED'; |
| 95 | + const detail = 'Target object has no owner; it may have been deleted'; |
| 96 | + const message = `[KERNEL:${code}] ${detail}`; |
| 97 | + const error = Error(message); |
| 98 | + |
| 99 | + expect(isKernelError(error)).toBe(true); |
| 100 | + expect(getKernelErrorCode(error)).toBe(code); |
| 101 | + expect(isFatalKernelError(error)).toBe(false); |
| 102 | + expect(error.message).toBe(`[KERNEL:OBJECT_DELETED] ${detail}`); |
| 103 | + }); |
| 104 | + |
| 105 | + it('constructs and detects a fatal kernel error message', () => { |
| 106 | + const code = 'INTERNAL_ERROR'; |
| 107 | + const detail = 'Internal kernel error'; |
| 108 | + const message = `[KERNEL:VAT_FATAL:${code}] ${detail}`; |
| 109 | + const error = Error(message); |
| 110 | + |
| 111 | + expect(isKernelError(error)).toBe(true); |
| 112 | + expect(getKernelErrorCode(error)).toBe(code); |
| 113 | + expect(isFatalKernelError(error)).toBe(true); |
| 114 | + }); |
| 115 | +}); |
0 commit comments