From aa67c33aa556bd1542c38dd2d6415c72061584f2 Mon Sep 17 00:00:00 2001 From: "lia-by-librechat[bot]" <328778573+lia-by-librechat[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 08:35:01 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=95=9B=20test:=20Control=20the=20Code=20A?= =?UTF-8?q?PI=20Retry=20Budget=20Clock=20(#17338)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lia --- packages/api/src/utils/code.spec.ts | 49 ++++++++++++++++++----------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/api/src/utils/code.spec.ts b/packages/api/src/utils/code.spec.ts index f471114224..844f51f3e5 100644 --- a/packages/api/src/utils/code.spec.ts +++ b/packages/api/src/utils/code.spec.ts @@ -90,26 +90,39 @@ describe('withCodeApiRateLimit', () => { }); it('waits out a rate limit the budget can absorb, then finishes', async () => { - /* A multi-request operation would otherwise abandon the work already - * done the moment a limiter window closes mid-flight. */ - const budget = createCodeApiRateLimitBudget(5_000); - const waits: number[] = []; - let attempts = 0; - const attempt = jest.fn(async () => { - attempts++; - if (attempts === 1) { - throw rateLimited({ headers: { 'retry-after': '1' } }); - } - return 'ok'; - }); + jest.useFakeTimers(); + try { + /* A multi-request operation would otherwise abandon the work already + * done the moment a limiter window closes mid-flight. */ + const budget = createCodeApiRateLimitBudget(5_000); + const waits: number[] = []; + let attempts = 0; + const attempt = jest.fn(async () => { + attempts++; + if (attempts === 1) { + throw rateLimited({ headers: { 'retry-after': '1' } }); + } + return 'ok'; + }); + const pending = withCodeApiRateLimit({ + attempt, + label: 'reading', + budget, + onWait: (ms) => waits.push(ms), + }); - await expect( - withCodeApiRateLimit({ attempt, label: 'reading', budget, onWait: (ms) => waits.push(ms) }), - ).resolves.toBe('ok'); + await jest.advanceTimersByTimeAsync(999); + expect(attempts).toBe(1); + expect(budget.waitedMs).toBe(0); + await jest.advanceTimersByTimeAsync(1); + await expect(pending).resolves.toBe('ok'); - expect(attempts).toBe(2); - expect(waits).toEqual([1_000]); - expect(budget.waitedMs).toBeGreaterThanOrEqual(1_000); + expect(attempts).toBe(2); + expect(waits).toEqual([1_000]); + expect(budget.waitedMs).toBe(1_000); + } finally { + jest.useRealTimers(); + } }); it('names the limit when the wait exceeds the remaining budget', async () => {