From aa1170ed69a70e869f6dd8abd64d9e209d8d2ac4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 17 Jul 2026 06:40:54 +0000 Subject: [PATCH] test: add tests for handleRateLimit Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- .../__tests__/github/handleRateLimit.test.ts | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/lib/__tests__/github/handleRateLimit.test.ts diff --git a/src/lib/__tests__/github/handleRateLimit.test.ts b/src/lib/__tests__/github/handleRateLimit.test.ts new file mode 100644 index 00000000..b8b5c6ef --- /dev/null +++ b/src/lib/__tests__/github/handleRateLimit.test.ts @@ -0,0 +1,63 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import { handleRateLimit } from "../../github"; +import { RateLimitError } from "../../types"; + +describe("handleRateLimit", () => { + beforeEach(() => { + vi.useFakeTimers(); + vi.setSystemTime(new Date("2024-01-01T00:00:00Z")); // 1704067200 in seconds + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("throws RateLimitError with valid X-RateLimit-Reset header", () => { + const validTimestamp = 1704070800; // 1 hour later + const res = { + headers: new Headers({ "X-RateLimit-Reset": validTimestamp.toString() }) + } as unknown as Response; + + try { + handleRateLimit(res); + expect.fail("Should throw RateLimitError"); + } catch (e) { + expect(e).toBeInstanceOf(RateLimitError); + expect((e as RateLimitError).resetAt.getTime()).toBe(validTimestamp * 1000); + } + }); + + it("throws RateLimitError with fallback timestamp when header is missing", () => { + const res = { + headers: new Headers() // Empty headers + } as unknown as Response; + + const currentSeconds = Math.floor(Date.now() / 1000); + const expectedFallback = currentSeconds + 3600; + + try { + handleRateLimit(res); + expect.fail("Should throw RateLimitError"); + } catch (e) { + expect(e).toBeInstanceOf(RateLimitError); + expect((e as RateLimitError).resetAt.getTime()).toBe(expectedFallback * 1000); + } + }); + + it("throws RateLimitError with fallback timestamp when header is invalid", () => { + const res = { + headers: new Headers({ "X-RateLimit-Reset": "invalid_timestamp" }) + } as unknown as Response; + + const currentSeconds = Math.floor(Date.now() / 1000); + const expectedFallback = currentSeconds + 3600; + + try { + handleRateLimit(res); + expect.fail("Should throw RateLimitError"); + } catch (e) { + expect(e).toBeInstanceOf(RateLimitError); + expect((e as RateLimitError).resetAt.getTime()).toBe(expectedFallback * 1000); + } + }); +});