-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 test: add test for handleRateLimit #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||||||||||||||||||||||||||||||||||
| import { handleRateLimit } from "@/lib/github"; | ||||||||||||||||||||||||||||||||||
| import { RateLimitError } from "@/lib/types"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| describe("handleRateLimit", () => { | ||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||
| vi.useFakeTimers(); | ||||||||||||||||||||||||||||||||||
| vi.setSystemTime(new Date("2024-01-01T00:00:00Z")); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||||||||||
| vi.useRealTimers(); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| it("throws RateLimitError using X-RateLimit-Reset header when available", () => { | ||||||||||||||||||||||||||||||||||
| const timestamp = 1704069000; // 2024-01-01T00:30:00Z | ||||||||||||||||||||||||||||||||||
| const headers = new Headers(); | ||||||||||||||||||||||||||||||||||
| headers.set("X-RateLimit-Reset", timestamp.toString()); | ||||||||||||||||||||||||||||||||||
| const mockResponse = { | ||||||||||||||||||||||||||||||||||
| headers | ||||||||||||||||||||||||||||||||||
| } as unknown as Response; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(() => handleRateLimit(mockResponse)).toThrow(RateLimitError); | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| handleRateLimit(mockResponse); | ||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||
| expect(e).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||||||||||
| const error = e as RateLimitError; | ||||||||||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(timestamp * 1000); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| it("throws RateLimitError using default time (1 hour from now) when header is missing", () => { | ||||||||||||||||||||||||||||||||||
| const headers = new Headers(); | ||||||||||||||||||||||||||||||||||
| const mockResponse = { | ||||||||||||||||||||||||||||||||||
| headers | ||||||||||||||||||||||||||||||||||
| } as unknown as Response; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const expectedTimestamp = Math.floor(Date.now() / 1000) + 3600; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(() => handleRateLimit(mockResponse)).toThrow(RateLimitError); | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| handleRateLimit(mockResponse); | ||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||
| expect(e).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||||||||||
| const error = e as RateLimitError; | ||||||||||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(expectedTimestamp * 1000); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current test pattern executes
Suggested change
References
|
||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| it("throws RateLimitError using default time when header is invalid", () => { | ||||||||||||||||||||||||||||||||||
| const headers = new Headers(); | ||||||||||||||||||||||||||||||||||
| headers.set("X-RateLimit-Reset", "invalid"); | ||||||||||||||||||||||||||||||||||
| const mockResponse = { | ||||||||||||||||||||||||||||||||||
| headers | ||||||||||||||||||||||||||||||||||
| } as unknown as Response; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const expectedTimestamp = Math.floor(Date.now() / 1000) + 3600; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(() => handleRateLimit(mockResponse)).toThrow(RateLimitError); | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| handleRateLimit(mockResponse); | ||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||
| expect(e).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||||||||||
| const error = e as RateLimitError; | ||||||||||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(expectedTimestamp * 1000); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+60
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current test pattern executes
Suggested change
References
|
||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current test pattern executes
handleRateLimittwice. Optimizing this by executing the function once inside atry-catchblock ensures the test is more robust and correctly asserts the error state, which aligns with maintaining reliable error handling coverage.References