forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-messages.test.ts
More file actions
467 lines (415 loc) · 16.3 KB
/
Copy patherror-messages.test.ts
File metadata and controls
467 lines (415 loc) · 16.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import { describe, it, expect } from 'vitest'
import {
mapError,
categoryLabel,
categoryColor,
type ErrorCategory,
} from '@/lib/error-messages'
describe('mapError', () => {
describe('Soroban contract errors (codes 1-20)', () => {
// 1 - InvalidAmount
it('maps Error(Contract, #1) to InvalidAmount', () => {
const result = mapError('Error(Contract, #1)')
expect(result.message).toBe('Invalid stream amount')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('positive')
})
// 2 - InvalidTimeRange
it('maps Error(Contract, #2) to InvalidTimeRange', () => {
const result = mapError('Error(Contract, #2)')
expect(result.message).toBe('Invalid time range')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('start and end times')
})
// 3 - InvalidCliff
it('maps Error(Contract, #3) to InvalidCliff', () => {
const result = mapError('Error(Contract, #3)')
expect(result.message).toBe('Invalid cliff configuration')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('between start and end')
})
// 4 - SelfStream
it('maps Error(Contract, #4) to SelfStream', () => {
const result = mapError('Error(Contract, #4)')
expect(result.message).toBe('Cannot create self-directed stream')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('different recipient')
})
// 5 - StreamNotFound
it('maps Error(Contract, #5) to StreamNotFound', () => {
const result = mapError('Error(Contract, #5)')
expect(result.message).toBe('Stream not found')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('does not exist')
})
// 6 - StreamCancelled
it('maps Error(Contract, #6) to StreamCancelled', () => {
const result = mapError('Error(Contract, #6)')
expect(result.message).toBe('Stream has been cancelled')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('withdrawals or modifications')
})
// 7 - Unauthorized
it('maps Error(Contract, #7) to Unauthorized', () => {
const result = mapError('Error(Contract, #7)')
expect(result.message).toBe('Unauthorized operation')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('permission')
})
// 8 - InsufficientFunds
it('maps Error(Contract, #8) to InsufficientFunds', () => {
const result = mapError('Error(Contract, #8)')
expect(result.message).toBe('Insufficient funds in stream')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('Top up')
})
// 9 - StreamEnded
it('maps Error(Contract, #9) to StreamEnded', () => {
const result = mapError('Error(Contract, #9)')
expect(result.message).toBe('Stream has ended')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('withdraw remaining')
})
// 10 - SameRecipient
it('maps Error(Contract, #10) to SameRecipient', () => {
const result = mapError('Error(Contract, #10)')
expect(result.message).toBe('Recipient already set')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('same address')
})
// 11 - BatchSizeExceeded
it('maps Error(Contract, #11) to BatchSizeExceeded', () => {
const result = mapError('Error(Contract, #11)')
expect(result.message).toBe('Batch size exceeds maximum')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('maximum of 20')
})
// 12 - BatchEmpty
it('maps Error(Contract, #12) to BatchEmpty', () => {
const result = mapError('Error(Contract, #12)')
expect(result.message).toBe('Batch cannot be empty')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('at least one')
})
// 13 - ArithmeticOverflow
it('maps Error(Contract, #13) to ArithmeticOverflow', () => {
const result = mapError('Error(Contract, #13)')
expect(result.message).toBe('Arithmetic overflow in calculation')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('reducing the amount')
})
// 14 - AlreadyInitialized
it('maps Error(Contract, #14) to AlreadyInitialized', () => {
const result = mapError('Error(Contract, #14)')
expect(result.message).toBe('Contract already initialized')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('only happen once')
})
// 15 - NotInitialized
it('maps Error(Contract, #15) to NotInitialized', () => {
const result = mapError('Error(Contract, #15)')
expect(result.message).toBe('Contract not initialized')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('not been initialized')
})
// 16 - ContractPaused
it('maps Error(Contract, #16) to ContractPaused', () => {
const result = mapError('Error(Contract, #16)')
expect(result.message).toBe('Contract is paused')
expect(result.category).toBe('network')
expect(result.suggestion).toContain('maintenance')
})
// 17 - DurationExceedsMaximum
it('maps Error(Contract, #17) to DurationExceedsMaximum', () => {
const result = mapError('Error(Contract, #17)')
expect(result.message).toBe('Stream duration exceeds maximum')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('shorter duration')
})
// 18 - InvalidRecipient
it('maps Error(Contract, #18) to InvalidRecipient', () => {
const result = mapError('Error(Contract, #18)')
expect(result.message).toBe('Invalid recipient address')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('invalid or cannot receive')
})
// 19 - RateIsZero
it('maps Error(Contract, #19) to RateIsZero', () => {
const result = mapError('Error(Contract, #19)')
expect(result.message).toBe('Stream amount too small for duration')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('Increase the amount')
})
// 20 - StreamNotEligibleForCleanup
it('maps Error(Contract, #20) to StreamNotEligibleForCleanup', () => {
const result = mapError('Error(Contract, #20)')
expect(result.message).toBe('Stream not eligible for cleanup')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('cancelled or drained')
})
it('handles optional hash prefix in error codes', () => {
const withHash = mapError('Error(Contract, #1)')
const withoutHash = mapError('Error(Contract, 1)')
expect(withHash.message).toBe(withoutHash.message)
expect(withHash.category).toBe(withoutHash.category)
})
it('matches with varying whitespace before the code', () => {
const result = mapError('Error(Contract, #1)')
expect(result.message).toBe('Invalid stream amount')
})
})
describe('non-contract error patterns', () => {
it('maps insufficient XLM balance patterns', () => {
const cases = [
'insufficient balance',
'insufficient funds',
'balance too low',
'balance is too low',
'not enough',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Insufficient XLM balance')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('Top up')
}
})
it('maps token approval expired patterns', () => {
const cases = [
'approval expired',
'approval expiry date',
'allowance expired',
'allowance expiry',
'expired approval',
'expired allowance',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Token approval expired')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('approve')
}
})
it('maps stream cancelled patterns (generic)', () => {
const cases = [
'stream has been cancelled',
'cancel the stream',
'stream is cancelled',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Stream has been cancelled')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('withdrawals')
}
})
it('maps withdraw amount exceeded patterns', () => {
const validCases = [
'withdraw amount exceeds balance',
'amount exceed the withdrawable amount',
'exceeds withdrawable amount',
]
for (const errorMsg of validCases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Withdraw amount exceeds available balance')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('less than or equal')
}
})
it('maps simulation failed patterns', () => {
const cases = [
'simulation failed',
'simulate error',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Transaction simulation failed')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('invalid')
}
})
it('maps on-chain failure patterns', () => {
const cases = [
'failed on chain',
'transaction failed onchain',
'transaction failed on-chain',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Transaction rejected by the network')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('rejected')
}
})
})
describe('wallet errors', () => {
it('maps user rejection patterns', () => {
const cases = [
'user rejected transaction',
'rejected by user',
'user denied',
'transaction rejected',
'user cancel',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Transaction rejected in wallet')
expect(result.category).toBe('wallet')
expect(result.suggestion).toContain('approve')
}
})
it('maps wallet not connected patterns', () => {
const result = mapError('wallet not connected')
expect(result.message).toBe('Wallet not connected')
expect(result.category).toBe('wallet')
expect(result.suggestion).toContain('Connect')
})
})
describe('network errors', () => {
it('maps timeout patterns', () => {
const cases = ['timeout', 'timed out', 'connection timeout', 'network timeout']
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Network request timed out')
expect(result.category).toBe('network')
expect(result.suggestion).toContain('connection')
}
})
it('maps rate limit errors', () => {
const result = mapError('429 Too Many Requests')
expect(result.message).toBe('Rate limit reached')
expect(result.category).toBe('network')
expect(result.suggestion).toContain('Wait')
})
it('maps service unavailable errors', () => {
const cases = [
'503 Service Unavailable',
'service unavailable',
'server error',
'rpc unavailable',
'RPC is unavailable',
]
for (const errorMsg of cases) {
const result = mapError(errorMsg)
expect(result.message).toBe('Stellar network temporarily unavailable')
expect(result.category).toBe('network')
expect(result.suggestion).toContain('minutes')
}
})
})
describe('user input errors', () => {
it('maps insufficient balance patterns', () => {
const result = mapError('insufficient balance')
expect(result.message).toBe('Insufficient XLM balance')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('Top up')
})
it('maps withdraw amount exceeded', () => {
const result = mapError('withdraw amount exceeds balance')
expect(result.message).toBe('Withdraw amount exceeds available balance')
expect(result.category).toBe('user')
expect(result.suggestion).toContain('less than or equal')
})
})
describe('fallback behavior', () => {
it('returns generic error for unmatched patterns', () => {
const result = mapError('some random unknown error')
expect(result.message).toBe('Transaction failed')
expect(result.category).toBe('contract')
expect(result.suggestion).toContain('unexpected')
expect(result.details).toBe('some random unknown error')
})
it('handles Error objects', () => {
const error = new Error('custom error message')
const result = mapError(error)
expect(result.details).toBe('custom error message')
})
it('handles non-string, non-Error values', () => {
const result = mapError(123)
expect(result.details).toBe('123')
})
})
describe('case insensitivity', () => {
it('matches patterns regardless of case', () => {
const lower = mapError('user rejected')
const upper = mapError('USER REJECTED')
const mixed = mapError('UsEr ReJeCtEd')
expect(lower.message).toBe(upper.message)
expect(upper.message).toBe(mixed.message)
expect(lower.category).toBe('wallet')
})
})
describe('details field', () => {
it('includes original error message in details', () => {
const originalMessage = 'Error(Contract, 1) - Invalid amount provided'
const result = mapError(originalMessage)
expect(result.details).toBe(originalMessage)
})
})
})
/**
* Tests for the categoryLabel utility function.
* Covers all four ErrorCategory values: 'user', 'network', 'contract', 'wallet'.
*/
describe('categoryLabel', () => {
it('returns "Input error" for the "user" category', () => {
expect(categoryLabel('user')).toBe('Input error')
})
it('returns "Network error" for the "network" category', () => {
expect(categoryLabel('network')).toBe('Network error')
})
it('returns "Contract error" for the "contract" category', () => {
expect(categoryLabel('contract')).toBe('Contract error')
})
it('returns "Wallet error" for the "wallet" category', () => {
expect(categoryLabel('wallet')).toBe('Wallet error')
})
it('covers all ErrorCategory values exhaustively', () => {
const categories: ErrorCategory[] = ['user', 'network', 'contract', 'wallet']
const labels = categories.map((c) => categoryLabel(c))
expect(labels).toEqual([
'Input error',
'Network error',
'Contract error',
'Wallet error',
])
})
})
/**
* Tests for the categoryColor utility function.
* Covers all four ErrorCategory values: 'user', 'network', 'contract', 'wallet'.
*/
describe('categoryColor', () => {
it('returns amber color for the "user" category', () => {
const color = categoryColor('user')
expect(color).toContain('amber')
expect(color).toMatch(/text-amber-\d+/)
})
it('returns blue color for the "network" category', () => {
const color = categoryColor('network')
expect(color).toContain('blue')
expect(color).toMatch(/text-blue-\d+/)
})
it('returns destructive color for the "contract" category', () => {
const color = categoryColor('contract')
expect(color).toBe('text-destructive')
})
it('returns purple color for the "wallet" category', () => {
const color = categoryColor('wallet')
expect(color).toContain('purple')
expect(color).toMatch(/text-purple-\d+/)
})
it('includes dark mode variants for user, network, and wallet', () => {
expect(categoryColor('user')).toContain('dark:')
expect(categoryColor('network')).toContain('dark:')
expect(categoryColor('wallet')).toContain('dark:')
})
it('covers all ErrorCategory values exhaustively', () => {
const categories: ErrorCategory[] = ['user', 'network', 'contract', 'wallet']
const colors = categories.map((c) => categoryColor(c))
// Each category has a distinct color
expect(new Set(colors).size).toBe(4)
// All return strings
colors.forEach((c) => expect(typeof c).toBe('string'))
})
})