|
1 | 1 | import "fake-indexeddb/auto"; |
2 | 2 | import { describe, it, expect, vi, beforeEach } from "vitest"; |
3 | 3 | import { createRoot } from "solid-js"; |
4 | | -import { createGitHubClient, cachedRequest, getClient, initClientWatcher, getGraphqlRateLimit, updateGraphqlRateLimit } from "../../src/app/services/github"; |
| 4 | +import { createGitHubClient, cachedRequest, getClient, initClientWatcher, getGraphqlRateLimit, updateGraphqlRateLimit, updateRateLimitFromHeaders, getCoreRateLimit } from "../../src/app/services/github"; |
5 | 5 | import { clearCache } from "../../src/app/stores/cache"; |
6 | 6 |
|
7 | 7 | // ── createGitHubClient ─────────────────────────────────────────────────────── |
@@ -328,4 +328,56 @@ describe("getGraphqlRateLimit", () => { |
328 | 328 | expect(rl!.limit).toBe(5000); |
329 | 329 | expect(rl!.remaining).toBe(3000); |
330 | 330 | }); |
| 331 | + |
| 332 | + it("falls back to previous limit when zero is provided", () => { |
| 333 | + updateGraphqlRateLimit({ limit: 5000, remaining: 100, resetAt: "2024-06-01T12:00:00Z" }); |
| 334 | + updateGraphqlRateLimit({ limit: 0, remaining: 50, resetAt: "2024-06-01T13:00:00Z" }); |
| 335 | + const rl = getGraphqlRateLimit(); |
| 336 | + expect(rl!.limit).toBe(5000); |
| 337 | + expect(rl!.remaining).toBe(50); |
| 338 | + }); |
| 339 | + |
| 340 | + it("falls back to previous limit when negative is provided", () => { |
| 341 | + updateGraphqlRateLimit({ limit: 10000, remaining: 100, resetAt: "2024-06-01T12:00:00Z" }); |
| 342 | + updateGraphqlRateLimit({ limit: -1, remaining: 50, resetAt: "2024-06-01T13:00:00Z" }); |
| 343 | + const rl = getGraphqlRateLimit(); |
| 344 | + expect(rl!.limit).toBe(10000); |
| 345 | + }); |
| 346 | +}); |
| 347 | + |
| 348 | +// ── updateRateLimitFromHeaders ─────────────────────────────────────────────── |
| 349 | + |
| 350 | +describe("updateRateLimitFromHeaders", () => { |
| 351 | + it("parses limit from x-ratelimit-limit header", () => { |
| 352 | + updateRateLimitFromHeaders({ |
| 353 | + "x-ratelimit-remaining": "4500", |
| 354 | + "x-ratelimit-reset": String(Math.floor(Date.now() / 1000) + 3600), |
| 355 | + "x-ratelimit-limit": "10000", |
| 356 | + }); |
| 357 | + const rl = getCoreRateLimit(); |
| 358 | + expect(rl).not.toBeNull(); |
| 359 | + expect(rl!.limit).toBe(10000); |
| 360 | + expect(rl!.remaining).toBe(4500); |
| 361 | + }); |
| 362 | + |
| 363 | + it("falls back to 5000 when x-ratelimit-limit header is absent", () => { |
| 364 | + updateRateLimitFromHeaders({ |
| 365 | + "x-ratelimit-remaining": "4999", |
| 366 | + "x-ratelimit-reset": String(Math.floor(Date.now() / 1000) + 3600), |
| 367 | + }); |
| 368 | + const rl = getCoreRateLimit(); |
| 369 | + expect(rl).not.toBeNull(); |
| 370 | + expect(rl!.limit).toBe(5000); |
| 371 | + }); |
| 372 | + |
| 373 | + it("falls back to 5000 when x-ratelimit-limit header is malformed", () => { |
| 374 | + updateRateLimitFromHeaders({ |
| 375 | + "x-ratelimit-remaining": "4999", |
| 376 | + "x-ratelimit-reset": String(Math.floor(Date.now() / 1000) + 3600), |
| 377 | + "x-ratelimit-limit": "abc", |
| 378 | + }); |
| 379 | + const rl = getCoreRateLimit(); |
| 380 | + expect(rl).not.toBeNull(); |
| 381 | + expect(rl!.limit).toBe(5000); |
| 382 | + }); |
331 | 383 | }); |
0 commit comments