-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathretry.test.ts
More file actions
757 lines (630 loc) · 23.3 KB
/
retry.test.ts
File metadata and controls
757 lines (630 loc) · 23.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ApiError } from '@google/genai';
import type { HttpError } from './retry.js';
import { retryWithBackoff } from './retry.js';
import { setSimulate429 } from './testUtils.js';
// Helper to create a mock function that fails a certain number of times
const createFailingFunction = (
failures: number,
successValue: string = 'success',
) => {
let attempts = 0;
return vi.fn(async () => {
attempts++;
if (attempts <= failures) {
// Simulate a retryable error
const error: HttpError = new Error(`Simulated error attempt ${attempts}`);
error.status = 500; // Simulate a server error
throw error;
}
return successValue;
});
};
// Custom error for testing non-retryable conditions
class NonRetryableError extends Error {
constructor(message: string) {
super(message);
this.name = 'NonRetryableError';
}
}
describe('retryWithBackoff', () => {
beforeEach(() => {
vi.useFakeTimers();
// Disable 429 simulation for tests
setSimulate429(false);
// Suppress unhandled promise rejection warnings for tests that expect errors
console.warn = vi.fn();
});
afterEach(() => {
vi.restoreAllMocks();
vi.useRealTimers();
});
it('should return the result on the first attempt if successful', async () => {
const mockFn = createFailingFunction(0);
const result = await retryWithBackoff(mockFn);
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should retry and succeed if failures are within maxAttempts', async () => {
const mockFn = createFailingFunction(2);
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync(); // Ensure all delays and retries complete
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(3);
});
it('should throw an error if all attempts fail', async () => {
const mockFn = createFailingFunction(3);
// 1. Start the retryable operation, which returns a promise.
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
// 2. Run timers and await expectation in parallel.
await Promise.all([
expect(promise).rejects.toThrow('Simulated error attempt 3'),
vi.runAllTimersAsync(),
]);
// 3. Finally, assert the number of calls.
expect(mockFn).toHaveBeenCalledTimes(3);
});
it('should default to 5 maxAttempts if no options are provided', async () => {
// This function will fail more than 5 times to ensure all retries are used.
const mockFn = createFailingFunction(10);
const promise = retryWithBackoff(mockFn);
// Expect it to fail with the error from the 5th attempt.
await Promise.all([
expect(promise).rejects.toThrow('Simulated error attempt 5'),
vi.runAllTimersAsync(),
]);
expect(mockFn).toHaveBeenCalledTimes(5);
});
it('should default to 5 maxAttempts if options.maxAttempts is undefined', async () => {
// This function will fail more than 5 times to ensure all retries are used.
const mockFn = createFailingFunction(10);
const promise = retryWithBackoff(mockFn, { maxAttempts: undefined });
// Expect it to fail with the error from the 5th attempt.
await Promise.all([
expect(promise).rejects.toThrow('Simulated error attempt 5'),
vi.runAllTimersAsync(),
]);
expect(mockFn).toHaveBeenCalledTimes(5);
});
it('should not retry if shouldRetry returns false', async () => {
const mockFn = vi.fn(async () => {
throw new NonRetryableError('Non-retryable error');
});
const shouldRetryOnError = (error: Error) =>
!(error instanceof NonRetryableError);
const promise = retryWithBackoff(mockFn, {
shouldRetryOnError,
initialDelayMs: 10,
});
await expect(promise).rejects.toThrow('Non-retryable error');
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should throw an error if maxAttempts is not a positive number', async () => {
const mockFn = createFailingFunction(1);
// Test with 0
await expect(retryWithBackoff(mockFn, { maxAttempts: 0 })).rejects.toThrow(
'maxAttempts must be a positive number.',
);
// The function should not be called at all if validation fails
expect(mockFn).not.toHaveBeenCalled();
});
it('should use default shouldRetry if not provided, retrying on ApiError 429', async () => {
const mockFn = vi.fn(async () => {
throw new ApiError({ message: 'Too Many Requests', status: 429 });
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 2,
initialDelayMs: 10,
});
await Promise.all([
expect(promise).rejects.toThrow('Too Many Requests'),
vi.runAllTimersAsync(),
]);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should use default shouldRetry if not provided, not retrying on ApiError 400', async () => {
const mockFn = vi.fn(async () => {
throw new ApiError({ message: 'Bad Request', status: 400 });
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 2,
initialDelayMs: 10,
});
await expect(promise).rejects.toThrow('Bad Request');
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should use default shouldRetry if not provided, retrying on generic error with status 429', async () => {
const mockFn = vi.fn(async () => {
const error = Object.assign(new Error('Too Many Requests'), {
status: 429,
});
throw error;
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 2,
initialDelayMs: 10,
});
// Run timers and await expectation in parallel.
await Promise.all([
expect(promise).rejects.toThrow('Too Many Requests'),
vi.runAllTimersAsync(),
]);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should use default shouldRetry if not provided, not retrying on generic error with status 400', async () => {
const mockFn = vi.fn(async () => {
const error = Object.assign(new Error('Bad Request'), { status: 400 });
throw error;
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 2,
initialDelayMs: 10,
});
await expect(promise).rejects.toThrow('Bad Request');
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should respect maxDelayMs', async () => {
const mockFn = createFailingFunction(3);
const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
const promise = retryWithBackoff(mockFn, {
maxAttempts: 4,
initialDelayMs: 100,
maxDelayMs: 250, // Max delay is less than 100 * 2 * 2 = 400
});
await vi.advanceTimersByTimeAsync(1000); // Advance well past all delays
await promise;
const delays = setTimeoutSpy.mock.calls.map((call) => call[1] as number);
// Delays should be around initial, initial*2, maxDelay (due to cap)
// Jitter makes exact assertion hard, so we check ranges / caps
expect(delays.length).toBe(3);
expect(delays[0]).toBeGreaterThanOrEqual(100 * 0.7);
expect(delays[0]).toBeLessThanOrEqual(100 * 1.3);
expect(delays[1]).toBeGreaterThanOrEqual(200 * 0.7);
expect(delays[1]).toBeLessThanOrEqual(200 * 1.3);
// The third delay should be capped by maxDelayMs (250ms), accounting for jitter
expect(delays[2]).toBeGreaterThanOrEqual(250 * 0.7);
expect(delays[2]).toBeLessThanOrEqual(250 * 1.3);
});
it('should handle jitter correctly, ensuring varied delays', async () => {
let mockFn = createFailingFunction(5);
const setTimeoutSpy = vi.spyOn(global, 'setTimeout');
// Run retryWithBackoff multiple times to observe jitter
const runRetry = () =>
retryWithBackoff(mockFn, {
maxAttempts: 2, // Only one retry, so one delay
initialDelayMs: 100,
maxDelayMs: 1000,
});
// We expect rejections as mockFn fails 5 times
const promise1 = runRetry();
// Run timers and await expectation in parallel.
await Promise.all([
expect(promise1).rejects.toThrow(),
vi.runAllTimersAsync(),
]);
const firstDelaySet = setTimeoutSpy.mock.calls.map(
(call) => call[1] as number,
);
setTimeoutSpy.mockClear(); // Clear calls for the next run
// Reset mockFn to reset its internal attempt counter for the next run
mockFn = createFailingFunction(5); // Re-initialize with 5 failures
const promise2 = runRetry();
// Run timers and await expectation in parallel.
await Promise.all([
expect(promise2).rejects.toThrow(),
vi.runAllTimersAsync(),
]);
const secondDelaySet = setTimeoutSpy.mock.calls.map(
(call) => call[1] as number,
);
// Check that the delays are not exactly the same due to jitter
// This is a probabilistic test, but with +/-30% jitter, it's highly likely they differ.
// Verify delays were captured
expect(firstDelaySet.length).toBeGreaterThan(0);
expect(secondDelaySet.length).toBeGreaterThan(0);
// Check the first delay of each set
expect(firstDelaySet[0]).not.toBe(secondDelaySet[0]);
// Ensure delays are within the expected jitter range [70, 130] for initialDelayMs = 100
[...firstDelaySet, ...secondDelaySet].forEach((d) => {
expect(d).toBeGreaterThanOrEqual(100 * 0.7);
expect(d).toBeLessThanOrEqual(100 * 1.3);
});
});
describe('network transient errors', () => {
it('should retry on undici "terminated" error', async () => {
let attempts = 0;
const mockFn = vi.fn(async () => {
attempts++;
if (attempts === 1) {
// Simulate undici termination error
const error = new TypeError('terminated');
throw error;
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync();
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should retry on connection terminated error', async () => {
let attempts = 0;
const mockFn = vi.fn(async () => {
attempts++;
if (attempts === 1) {
throw new Error('connection terminated');
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync();
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should retry on ECONNRESET error code', async () => {
let attempts = 0;
const mockFn = vi.fn(async () => {
attempts++;
if (attempts === 1) {
const error = Object.assign(new Error('socket hang up'), {
code: 'ECONNRESET',
});
throw error;
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync();
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should retry on fetch failed error', async () => {
let attempts = 0;
const mockFn = vi.fn(async () => {
attempts++;
if (attempts === 1) {
throw new Error('fetch failed');
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync();
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should retry on "fetch failed sending request" error', async () => {
let attempts = 0;
const mockFn = vi.fn(async () => {
attempts++;
if (attempts === 1) {
throw new Error(
'exception TypeError: fetch failed sending request body',
);
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await vi.runAllTimersAsync();
const result = await promise;
expect(result).toBe('success');
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should not retry on non-transient errors', async () => {
const mockFn = vi.fn(async () => {
throw new Error('Some non-transient error');
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 10,
});
await expect(promise).rejects.toThrow('Some non-transient error');
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
// Skipped: llxprt doesn't have automatic model fallback (multi-provider design)
describe.skip('Flash model fallback for OAuth users', () => {
it('should trigger fallback for OAuth personal users after persistent 429 errors', async () => {
const fallbackCallback = vi.fn().mockResolvedValue('gemini-2.5-flash');
let fallbackOccurred = false;
const mockFn = vi.fn().mockImplementation(async () => {
if (!fallbackOccurred) {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: async (authType?: string) => {
fallbackOccurred = true;
return await fallbackCallback(authType);
},
authType: 'oauth-personal',
});
// Advance all timers to complete retries
await vi.runAllTimersAsync();
// Should succeed after fallback
await expect(promise).resolves.toBe('success');
// Verify callback was called with correct auth type
expect(fallbackCallback).toHaveBeenCalledWith('oauth-personal');
// Should retry again after fallback
expect(mockFn).toHaveBeenCalledTimes(3); // 2 initial attempts + 1 after fallback
});
it('should NOT trigger fallback for API key users', async () => {
const fallbackCallback = vi.fn();
const mockFn = vi.fn(async () => {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: fallbackCallback,
authType: 'gemini-api-key',
});
// Handle the promise properly to avoid unhandled rejections
const resultPromise = promise.catch((error) => error);
await vi.runAllTimersAsync();
const result = await resultPromise;
// Should fail after all retries without fallback
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Rate limit exceeded');
// Callback should not be called for API key users
expect(fallbackCallback).not.toHaveBeenCalled();
});
it('should reset attempt counter and continue after successful fallback', async () => {
let fallbackCalled = false;
const fallbackCallback = vi.fn().mockImplementation(async () => {
fallbackCalled = true;
return 'gemini-2.5-flash';
});
const mockFn = vi.fn().mockImplementation(async () => {
if (!fallbackCalled) {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
}
return 'success';
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: fallbackCallback,
authType: 'oauth-personal',
});
await vi.runAllTimersAsync();
await expect(promise).resolves.toBe('success');
expect(fallbackCallback).toHaveBeenCalledOnce();
});
it('should continue with original error if fallback is rejected', async () => {
const fallbackCallback = vi.fn().mockResolvedValue(null); // User rejected fallback
const mockFn = vi.fn(async () => {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: fallbackCallback,
authType: 'oauth-personal',
});
// Handle the promise properly to avoid unhandled rejections
const resultPromise = promise.catch((error) => error);
await vi.runAllTimersAsync();
const result = await resultPromise;
// Should fail with original error when fallback is rejected
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Rate limit exceeded');
expect(fallbackCallback).toHaveBeenCalledWith(
'oauth-personal',
expect.any(Error),
);
});
it('should handle mixed error types (only count consecutive 429s)', async () => {
const fallbackCallback = vi.fn().mockResolvedValue('gemini-2.5-flash');
let attempts = 0;
let fallbackOccurred = false;
const mockFn = vi.fn().mockImplementation(async () => {
attempts++;
if (fallbackOccurred) {
return 'success';
}
if (attempts === 1) {
// First attempt: 500 error (resets consecutive count)
const error: HttpError = new Error('Server error');
error.status = 500;
throw error;
} else {
// Remaining attempts: 429 errors
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
}
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 5,
initialDelayMs: 100,
onPersistent429: async (authType?: string) => {
fallbackOccurred = true;
return await fallbackCallback(authType);
},
authType: 'oauth-personal',
});
await vi.runAllTimersAsync();
await expect(promise).resolves.toBe('success');
// Should trigger fallback after 2 consecutive 429s (attempts 2-3)
expect(fallbackCallback).toHaveBeenCalledWith('oauth-personal');
});
});
describe('bucket failover integration', () => {
/**
* @requirement PLAN-20251213issue490 Bucket failover
* @scenario Bucket failover on 429 errors
* @given A request that consistently returns 429 for first bucket
* @when onPersistent429 callback returns true (switch succeeded)
* @then Retry should continue with new bucket
*/
it('should call onPersistent429 callback on first 429 error', async () => {
vi.useFakeTimers();
let attempt = 0;
let failoverCalled = false;
const mockFn = vi.fn(async () => {
attempt++;
if (attempt <= 1) {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
}
return 'success after bucket switch';
});
const failoverCallback = vi.fn(async () => {
failoverCalled = true;
return true; // Indicate bucket switch succeeded
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 1,
initialDelayMs: 100,
onPersistent429: failoverCallback,
authType: 'oauth-bucket',
});
await vi.runAllTimersAsync();
await expect(promise).resolves.toBe('success after bucket switch');
// onPersistent429 should be called after the first 429 error
expect(failoverCallback).toHaveBeenCalled();
expect(failoverCalled).toBe(true);
expect(mockFn).toHaveBeenCalledTimes(2);
});
/**
* @requirement PLAN-20251213issue490 Bucket failover
* @scenario Bucket failover on 402 errors
* @given A request that returns 402 for first bucket
* @when onPersistent429 callback returns true (switch succeeded)
* @then Retry should continue with new bucket
*/
it('should call onPersistent429 callback on first 402 error', async () => {
vi.useFakeTimers();
let attempt = 0;
const mockFn = vi.fn(async () => {
attempt++;
if (attempt === 1) {
const error: HttpError = new Error('Payment Required');
error.status = 402;
throw error;
}
return 'success after bucket switch';
});
const failoverCallback = vi.fn(async () => true);
const promise = retryWithBackoff(mockFn, {
maxAttempts: 1,
initialDelayMs: 100,
onPersistent429: failoverCallback,
authType: 'oauth-bucket',
});
await vi.runAllTimersAsync();
await expect(promise).resolves.toBe('success after bucket switch');
expect(failoverCallback).toHaveBeenCalledWith(
'oauth-bucket',
expect.any(Error),
);
expect(mockFn).toHaveBeenCalledTimes(2);
});
/**
* @requirement PLAN-20251213issue490 Bucket failover
* @scenario Bucket failover on 401 errors
* @given A request that returns 401 for first bucket
* @when onPersistent429 callback returns true (switch succeeded)
* @then Retry should continue with new bucket after refresh retry
*/
it('should retry once on 401 before bucket failover', async () => {
vi.useFakeTimers();
let failoverCalled = false;
const mockFn = vi.fn(async () => {
if (!failoverCalled) {
const error: HttpError = new Error('Unauthorized');
error.status = 401;
throw error;
}
return 'success after bucket switch';
});
const failoverCallback = vi.fn(async () => {
failoverCalled = true;
return true;
});
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: failoverCallback,
authType: 'oauth-bucket',
});
await vi.runAllTimersAsync();
await expect(promise).resolves.toBe('success after bucket switch');
expect(failoverCallback).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledTimes(3);
});
/**
* @requirement PLAN-20251213issue490 Bucket failover
* @scenario All buckets exhausted
* @given A request that returns 429 and all bucket switches fail
* @when onPersistent429 callback returns false (no more buckets)
* @then Should throw error after exhausting retries
*/
it('should throw when onPersistent429 returns false (no more buckets)', async () => {
vi.useFakeTimers();
const mockFn = vi.fn(async () => {
const error: HttpError = new Error('Rate limit exceeded');
error.status = 429;
throw error;
});
const failoverCallback = vi.fn(async () => false); // No more buckets available
const promise = retryWithBackoff(mockFn, {
maxAttempts: 3,
initialDelayMs: 100,
onPersistent429: failoverCallback,
authType: 'oauth-bucket',
});
// Properly handle the rejection
const resultPromise = promise.catch((error) => error);
await vi.runAllTimersAsync();
const result = await resultPromise;
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Rate limit exceeded');
expect(failoverCallback).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
});