|
| 1 | +import { LEDGER_ERROR_MAPPINGS } from './hardware-error-mappings'; |
| 2 | +import { ErrorCode, Severity, Category } from './hardware-errors-enums'; |
| 3 | + |
| 4 | +describe('HARDWARE_ERROR_MAPPINGS', () => { |
| 5 | + describe('Ledger mappings', () => { |
| 6 | + const errorMappings = LEDGER_ERROR_MAPPINGS; |
| 7 | + |
| 8 | + it('has errorMappings object', () => { |
| 9 | + expect(errorMappings).toBeDefined(); |
| 10 | + expect(typeof errorMappings).toBe('object'); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('success codes', () => { |
| 14 | + it('map 0x9000 to success', () => { |
| 15 | + const mapping = errorMappings['0x9000']; |
| 16 | + expect(mapping).toBeDefined(); |
| 17 | + expect(mapping.code).toBe(ErrorCode.Success); |
| 18 | + expect(mapping.severity).toBe(Severity.Info); |
| 19 | + expect(mapping.category).toBe(Category.Success); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('authentication errors', () => { |
| 24 | + it('map 0x6300 to authentication failed', () => { |
| 25 | + const mapping = errorMappings['0x6300']; |
| 26 | + expect(mapping.code).toBe(ErrorCode.AuthenticationFailed); |
| 27 | + expect(mapping.severity).toBe(Severity.Err); |
| 28 | + expect(mapping.category).toBe(Category.Authentication); |
| 29 | + expect(mapping.userMessage).toBeDefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('map 0x63c0 to PIN attempts remaining', () => { |
| 33 | + const mapping = errorMappings['0x63c0']; |
| 34 | + expect(mapping.code).toBe(ErrorCode.AuthenticationPinAttemptsRemaining); |
| 35 | + expect(mapping.severity).toBe(Severity.Warning); |
| 36 | + }); |
| 37 | + |
| 38 | + it('map 0x5515 to device locked', () => { |
| 39 | + const mapping = errorMappings['0x5515']; |
| 40 | + expect(mapping.code).toBe(ErrorCode.AuthenticationDeviceLocked); |
| 41 | + expect(mapping.severity).toBe(Severity.Err); |
| 42 | + expect(mapping.userMessage).toContain('unlock'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('map 0x9840 to device blocked', () => { |
| 46 | + const mapping = errorMappings['0x9840']; |
| 47 | + expect(mapping.code).toBe(ErrorCode.AuthenticationDeviceBlocked); |
| 48 | + expect(mapping.severity).toBe(Severity.Critical); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('user action errors', () => { |
| 53 | + it('map 0x6985 to user rejected', () => { |
| 54 | + const mapping = errorMappings['0x6985']; |
| 55 | + expect(mapping.code).toBe(ErrorCode.UserRejected); |
| 56 | + expect(mapping.severity).toBe(Severity.Warning); |
| 57 | + expect(mapping.category).toBe(Category.UserAction); |
| 58 | + }); |
| 59 | + |
| 60 | + it('map 0x5501 to user refused', () => { |
| 61 | + const mapping = errorMappings['0x5501']; |
| 62 | + expect(mapping.code).toBe(ErrorCode.UserRejected); |
| 63 | + expect(mapping.severity).toBe(Severity.Warning); |
| 64 | + }); |
| 65 | + }); |
| 66 | + describe('connection errors', () => { |
| 67 | + it('map 0x650f to connection issue', () => { |
| 68 | + const mapping = errorMappings['0x650f']; |
| 69 | + expect(mapping.code).toBe(ErrorCode.ConnectionClosed); |
| 70 | + expect(mapping.category).toBe(Category.Connection); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('has valid structure for all mappings', () => { |
| 75 | + Object.entries(errorMappings).forEach(([_, mapping]) => { |
| 76 | + expect(mapping).toHaveProperty('code'); |
| 77 | + expect(mapping).toHaveProperty('message'); |
| 78 | + expect(mapping).toHaveProperty('severity'); |
| 79 | + expect(mapping).toHaveProperty('category'); |
| 80 | + |
| 81 | + const numericErrorCodes = Object.values(ErrorCode).filter( |
| 82 | + (value): value is number => typeof value === 'number', |
| 83 | + ); |
| 84 | + expect(numericErrorCodes).toContain(mapping.code); |
| 85 | + expect(Object.values(Severity)).toContain(mapping.severity); |
| 86 | + expect(Object.values(Category)).toContain(mapping.category); |
| 87 | + expect(typeof mapping.message).toBe('string'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('has valid userMessage when present', () => { |
| 92 | + const mappingsWithUserMessage = Object.values(errorMappings).filter( |
| 93 | + (mapping): mapping is typeof mapping & { userMessage: string } => |
| 94 | + 'userMessage' in mapping && |
| 95 | + typeof mapping.userMessage === 'string' && |
| 96 | + mapping.userMessage.length > 0, |
| 97 | + ); |
| 98 | + expect(mappingsWithUserMessage.length).toBeGreaterThan(0); |
| 99 | + mappingsWithUserMessage.forEach((mapping) => { |
| 100 | + expect(typeof mapping.userMessage).toBe('string'); |
| 101 | + expect(mapping.userMessage.length).toBeGreaterThan(0); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments