-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.test.ts
More file actions
97 lines (78 loc) · 3.17 KB
/
Copy patherrors.test.ts
File metadata and controls
97 lines (78 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {
HardwareWalletError,
ErrorCode as ErrorCodeEnum,
} from '@metamask/hw-wallet-sdk';
import {
createLedgerError,
isKnownLedgerError,
getLedgerErrorMapping,
} from './errors';
describe('createLedgerError', () => {
describe('known error codes', () => {
it('creates HardwareWalletError for user rejection (0x6985)', () => {
const error = createLedgerError('0x6985');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.code).toBe(ErrorCodeEnum.UserRejected);
expect(error.message).toBe('User rejected action on device');
});
it('creates HardwareWalletError for device locked (0x5515)', () => {
const error = createLedgerError('0x5515');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.code).toBe(ErrorCodeEnum.AuthenticationDeviceLocked);
expect(error.message).toBe('Device is locked');
});
it('includes context in message when provided', () => {
const error = createLedgerError('0x6985', 'during sign transaction');
expect(error.message).toBe(
'User rejected action on device (during sign transaction)',
);
});
it('uses error message as userMessage when userMessage is not provided in mapping', () => {
// 0x9000 is success code which doesn't have a userMessage in the mapping
const error = createLedgerError('0x9000');
expect(error).toBeInstanceOf(HardwareWalletError);
// userMessage should fallback to the message
expect(error.userMessage).toBeDefined();
});
});
describe('unknown error codes', () => {
it('creates fallback error for unknown code without context', () => {
const error = createLedgerError('0x9999');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.code).toBe(ErrorCodeEnum.Unknown);
expect(error.message).toBe('Unknown Ledger error: 0x9999');
});
it('creates fallback error for unknown code with context', () => {
const error = createLedgerError('0x9999', 'during operation');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.code).toBe(ErrorCodeEnum.Unknown);
expect(error.message).toBe(
'Unknown Ledger error: 0x9999 (during operation)',
);
});
});
});
describe('isKnownLedgerError', () => {
it('returns true for known error codes', () => {
expect(isKnownLedgerError('0x6985')).toBe(true);
expect(isKnownLedgerError('0x5515')).toBe(true);
expect(isKnownLedgerError('0x6a80')).toBe(true);
});
it('returns false for unknown error codes', () => {
expect(isKnownLedgerError('0x9999')).toBe(false);
expect(isKnownLedgerError('invalid')).toBe(false);
expect(isKnownLedgerError('')).toBe(false);
});
});
describe('getLedgerErrorMapping', () => {
it('returns mapping for known error codes', () => {
const mapping = getLedgerErrorMapping('0x6985');
expect(mapping).toBeDefined();
expect(mapping?.code).toBe(ErrorCodeEnum.UserRejected);
expect(mapping?.message).toBe('User rejected action on device');
});
it('returns undefined for unknown error codes', () => {
const mapping = getLedgerErrorMapping('0x9999');
expect(mapping).toBeUndefined();
});
});