From ee85ad97495ca00d565143dfff585b57659f1d9a Mon Sep 17 00:00:00 2001 From: AINative Admin Date: Wed, 28 Oct 2026 16:45:32 -0700 Subject: [PATCH] Fix rate-limit test exceeding Jest's 10s timeout via a real 60s sleep client.ts's retry loop honors a real WWW-Authenticate/retry-after value with a genuine setTimeout() before retrying. This test mocked every request attempt to reject with retry-after: '60' and never used fake timers, so it genuinely slept close to 60 real seconds before Jest's 10s default timeout killed it -- deterministically, not flaky, in every environment (reproduced identically on core's copy, a clean local checkout of this repo, and the v3.2.0 release's CI run, which failed for exactly this reason and never published). Fix: wrap this one test in jest.useFakeTimers() / runAllTimersAsync() so the retry sleep resolves instantly instead of waiting in real time. No other test in this file needed the same fix -- it's the only one exercising the retry-after path with a reject-every-attempt mock. Found while re-publishing v3.2.0 after the x402 PR's own release (the first v3.2.0 tag) failed its gating test step for this exact reason. Refs AINative-Studio/core#9368 --- tests/client.test.ts | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/tests/client.test.ts b/tests/client.test.ts index 34b5a74..524fa39 100644 --- a/tests/client.test.ts +++ b/tests/client.test.ts @@ -162,16 +162,29 @@ describe('AINativeClient', () => { describe('advanced error handling', () => { it('should handle rate limit errors', async () => { - const mockRequest = client['httpClient'].request as jest.Mock; - const error: any = new Error('Rate limited'); - error.response = { - status: 429, - data: { message: 'Rate limit exceeded' }, - headers: { 'retry-after': '60' } - }; - mockRequest.mockRejectedValue(error); - - await expect(client.get('/test')).rejects.toThrow(RateLimitError); + // The client's retry loop honors the real `retry-after` value with a + // real setTimeout (client.ts's sleep()) before each retry -- with + // this mock rejecting every attempt and retry-after: '60', that's a + // genuine 60s wall-clock sleep per retry, deterministically exceeding + // Jest's default 10s test timeout. Use fake timers and advance past + // each sleep instead of waiting on it for real. + jest.useFakeTimers(); + try { + const mockRequest = client['httpClient'].request as jest.Mock; + const error: any = new Error('Rate limited'); + error.response = { + status: 429, + data: { message: 'Rate limit exceeded' }, + headers: { 'retry-after': '60' } + }; + mockRequest.mockRejectedValue(error); + + const resultPromise = expect(client.get('/test')).rejects.toThrow(RateLimitError); + await jest.runAllTimersAsync(); + await resultPromise; + } finally { + jest.useRealTimers(); + } }); it('should handle server errors', async () => {