Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/lib/__tests__/github/handleRateLimit.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Comment on lines +21 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using expect.fail() inside a try block where the catch block catches all errors is a test anti-pattern. If handleRateLimit(res) does not throw, expect.fail() will throw an AssertionError, which is then caught by the catch block. The assertion expect(e).toBeInstanceOf(RateLimitError) will then fail, masking the original expect.fail() message and making debugging confusing.

A cleaner and safer pattern is to catch the error into a local variable and perform assertions on it outside the try/catch block.

    let error: unknown;
    try {
      handleRateLimit(res);
    } catch (e) {
      error = e;
    }

    expect(error).toBeInstanceOf(RateLimitError);
    expect((error 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);
}
Comment on lines +38 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using expect.fail() inside a try block where the catch block catches all errors is a test anti-pattern. If handleRateLimit(res) does not throw, expect.fail() will throw an AssertionError, which is then caught by the catch block. The assertion expect(e).toBeInstanceOf(RateLimitError) will then fail, masking the original expect.fail() message and making debugging confusing.

A cleaner and safer pattern is to catch the error into a local variable and perform assertions on it outside the try/catch block.

    let error: unknown;
    try {
      handleRateLimit(res);
    } catch (e) {
      error = e;
    }

    expect(error).toBeInstanceOf(RateLimitError);
    expect((error 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);
}
Comment on lines +55 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using expect.fail() inside a try block where the catch block catches all errors is a test anti-pattern. If handleRateLimit(res) does not throw, expect.fail() will throw an AssertionError, which is then caught by the catch block. The assertion expect(e).toBeInstanceOf(RateLimitError) will then fail, masking the original expect.fail() message and making debugging confusing.

A cleaner and safer pattern is to catch the error into a local variable and perform assertions on it outside the try/catch block.

    let error: unknown;
    try {
      handleRateLimit(res);
    } catch (e) {
      error = e;
    }

    expect(error).toBeInstanceOf(RateLimitError);
    expect((error as RateLimitError).resetAt.getTime()).toBe(expectedFallback * 1000);

});
});
Loading