-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhardware-error.test.ts
More file actions
353 lines (291 loc) · 11.3 KB
/
Copy pathhardware-error.test.ts
File metadata and controls
353 lines (291 loc) · 11.3 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { HardwareWalletError } from './hardware-error';
import { ErrorCode, Severity, Category } from './hardware-errors-enums';
describe('HardwareWalletError', () => {
const mockOptions = {
code: ErrorCode.UserRejected,
severity: Severity.Warning,
category: Category.UserAction,
userMessage: 'Transaction was rejected',
};
describe('constructor', () => {
it('creates an error with required properties', () => {
const error = new HardwareWalletError('Test error', mockOptions);
expect(error.message).toBe('Test error');
expect(error.name).toBe('HardwareWalletError');
expect(error.code).toBe(ErrorCode.UserRejected);
expect(error.severity).toBe(Severity.Warning);
expect(error.category).toBe(Category.UserAction);
expect(error.userMessage).toBe('Transaction was rejected');
});
it('generates a unique error ID', () => {
const error1 = new HardwareWalletError('Test error 1', mockOptions);
const error2 = new HardwareWalletError('Test error 2', mockOptions);
expect(error1.id).toBeDefined();
expect(error2.id).toBeDefined();
expect(error1.id).not.toBe(error2.id);
expect(error1.id).toMatch(/^err_[a-z0-9]+_[a-z0-9]+$/u);
});
it('sets timestamp to current date', () => {
const before = new Date();
const error = new HardwareWalletError('Test error', mockOptions);
const after = new Date();
expect(error.timestamp.getTime()).toBeGreaterThanOrEqual(
before.getTime(),
);
expect(error.timestamp.getTime()).toBeLessThanOrEqual(after.getTime());
});
it('sets optional properties when provided', () => {
const cause = new Error('Original error');
const metadata = { deviceId: '12345', attempt: 1 };
const error = new HardwareWalletError('Test error', {
...mockOptions,
cause,
metadata,
});
expect(error.cause).toBe(cause);
expect(error.metadata).toStrictEqual(metadata);
});
it('works with instanceof checks', () => {
const error = new HardwareWalletError('Test error', mockOptions);
expect(error instanceof HardwareWalletError).toBe(true);
expect(error instanceof Error).toBe(true);
});
});
describe('isCritical', () => {
it('returns true for CRITICAL severity', () => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
severity: Severity.Critical,
});
expect(error.isCritical()).toBe(true);
});
it('returns false for non-CRITICAL severity', () => {
const severities = [Severity.Err, Severity.Warning, Severity.Info];
severities.forEach((severity) => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
severity,
});
expect(error.isCritical()).toBe(false);
});
});
});
describe('isWarning', () => {
it('returns true for WARNING severity', () => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
severity: Severity.Warning,
});
expect(error.isWarning()).toBe(true);
});
it('returns false for non-WARNING severity', () => {
const severities = [Severity.Err, Severity.Critical, Severity.Info];
severities.forEach((severity) => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
severity,
});
expect(error.isWarning()).toBe(false);
});
});
});
describe('withMetadata', () => {
it('creates a new error with additional metadata', () => {
const originalMetadata = { deviceId: '12345' };
const originalError = new HardwareWalletError('Test error', {
...mockOptions,
metadata: originalMetadata,
});
const additionalMetadata = { attempt: 1, timestamp: Date.now() };
const newError = originalError.withMetadata(additionalMetadata);
expect(newError.metadata).toStrictEqual({
...originalMetadata,
...additionalMetadata,
});
expect(originalError.metadata).toStrictEqual(originalMetadata); // Original unchanged
expect(newError).not.toBe(originalError); // New instance
});
it('creates metadata when original has none', () => {
const originalError = new HardwareWalletError('Test error', mockOptions);
const metadata = { deviceId: '12345' };
const newError = originalError.withMetadata(metadata);
expect(newError.metadata).toStrictEqual(metadata);
});
it('overrides existing metadata keys', () => {
const originalError = new HardwareWalletError('Test error', {
...mockOptions,
metadata: { key: 'old', other: 'value' },
});
const newError = originalError.withMetadata({ key: 'new' });
expect(newError.metadata).toStrictEqual({ key: 'new', other: 'value' });
});
it('preserves all other properties', () => {
const cause = new Error('Original error');
const originalError = new HardwareWalletError('Test error', {
...mockOptions,
cause,
});
const newError = originalError.withMetadata({ extra: 'data' });
expect(newError.message).toBe(originalError.message);
expect(newError.code).toBe(originalError.code);
expect(newError.severity).toBe(originalError.severity);
expect(newError.category).toBe(originalError.category);
expect(newError.userMessage).toBe(originalError.userMessage);
expect(newError.cause).toBe(originalError.cause);
});
});
describe('toJSON', () => {
it('serializes all properties to JSON', () => {
const cause = new Error('Original error');
const metadata = { deviceId: '12345' };
const error = new HardwareWalletError('Test error', {
...mockOptions,
cause,
metadata,
});
const json = error.toJSON();
expect(json.id).toBe(error.id);
expect(json.name).toBe('HardwareWalletError');
expect(json.message).toBe('Test error');
expect(json.code).toBe(ErrorCode.UserRejected);
expect(json.severity).toBe(Severity.Warning);
expect(json.category).toBe(Category.UserAction);
expect(json.userMessage).toBe('Transaction was rejected');
expect(json.timestamp).toBe(error.timestamp.toISOString());
expect(json.metadata).toStrictEqual(metadata);
});
it('serializes cause when present', () => {
const cause = new Error('Original error');
const error = new HardwareWalletError('Test error', {
...mockOptions,
cause,
});
const json = error.toJSON();
expect(json.cause).toStrictEqual({
name: 'Error',
message: 'Original error',
});
});
it('does not include cause when not present', () => {
const error = new HardwareWalletError('Test error', mockOptions);
const json = error.toJSON();
expect(json.cause).toBeUndefined();
});
it('handles undefined optional properties', () => {
const error = new HardwareWalletError('Test error', mockOptions);
const json = error.toJSON();
expect(json.metadata).toBeUndefined();
expect(json.cause).toBeUndefined();
});
});
describe('toString', () => {
it('returns a user-friendly string representation', () => {
const error = new HardwareWalletError('Test error', mockOptions);
const result = error.toString();
expect(result).toBe(
'HardwareWalletError [UserRejected:2000]: Test error',
);
});
it('works with different error codes and messages', () => {
const error = new HardwareWalletError('Internal error', {
...mockOptions,
code: ErrorCode.Unknown,
userMessage: 'An internal error occurred',
});
const result = error.toString();
expect(result).toBe(
'HardwareWalletError [Unknown:99999]: Internal error',
);
});
it('falls back to UNKNOWN name for unmapped numeric codes', () => {
const error = new HardwareWalletError('Weird error', {
...mockOptions,
code: 123456 as unknown as ErrorCode,
userMessage: 'Something strange happened',
});
expect(error.toString()).toBe(
'HardwareWalletError [Unknown:123456]: Weird error',
);
});
});
describe('toDetailedString', () => {
it('returns a detailed string with all information', () => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
});
const result = error.toDetailedString();
expect(result).toContain('HardwareWalletError [UserRejected:2000]');
expect(result).toContain(
'HardwareWalletError [UserRejected:2000]: Test error',
);
expect(result).toContain('User Message: Transaction was rejected');
expect(result).toContain('Severity: Warning');
expect(result).toContain('Category: UserAction');
expect(result).toContain('Timestamp:');
});
it('includes metadata when present', () => {
const metadata = { deviceId: '12345', attempt: 1 };
const error = new HardwareWalletError('Test error', {
...mockOptions,
metadata,
});
const result = error.toDetailedString();
expect(result).toContain('Metadata:');
expect(result).toContain('"deviceId": "12345"');
expect(result).toContain('"attempt": 1');
});
it('includes cause when present', () => {
const cause = new Error('Original error');
const error = new HardwareWalletError('Test error', {
...mockOptions,
cause,
});
const result = error.toDetailedString();
expect(result).toContain('Caused by: Original error');
});
it('does not include optional fields when not present', () => {
const error = new HardwareWalletError('Test error', mockOptions);
const result = error.toDetailedString();
expect(result).not.toContain('Metadata:');
expect(result).not.toContain('Caused by:');
});
it('does not include metadata section when metadata is empty', () => {
const error = new HardwareWalletError('Test error', {
...mockOptions,
metadata: {},
});
const result = error.toDetailedString();
expect(result).not.toContain('Metadata:');
});
});
describe('error scenarios', () => {
it('handles critical authentication errors', () => {
const error = new HardwareWalletError('Device blocked', {
code: ErrorCode.AuthenticationDeviceBlocked,
severity: Severity.Critical,
category: Category.Authentication,
userMessage: 'Device is blocked due to too many failed attempts',
});
expect(error.isCritical()).toBe(true);
});
it('handles retryable connection errors', () => {
const error = new HardwareWalletError('Connection timeout', {
code: ErrorCode.ConnectionTimeout,
severity: Severity.Err,
category: Category.Connection,
userMessage: 'Connection timed out',
});
expect(error.isCritical()).toBe(false);
});
it('handles user action warnings', () => {
const error = new HardwareWalletError('User confirmation required', {
code: ErrorCode.UserConfirmationRequired,
severity: Severity.Warning,
category: Category.UserAction,
userMessage: 'Please confirm the action on your device',
});
expect(error.isWarning()).toBe(true);
expect(error.isCritical()).toBe(false);
});
});
});