Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubQuota.test.ts

83 lines3.8 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as TestClock from "effect/testing/TestClock";
4​
5import * as GitHubQuota from "./GitHubQuota.ts";
6import { CredentialScope } from "@t3tools/source-control-core/server/SourceControlRateLimit";
7​
8const RESET = Date.parse("2099-08-13T14:00:00Z");
9const headers = (remaining: number, resource = "graphql", reset = RESET) => ({
10 "x-ratelimit-resource": resource,
11 "x-ratelimit-limit": "5000",
12 "x-ratelimit-remaining": String(remaining),
13 "x-ratelimit-reset": String(reset / 1000),
14});
15​
16describe("GitHubQuota", () => {
17 it.effect("refuses a background request below the reserve, per quota and account", () =>
18 Effect.gen(function* () {
19 const quota = yield* GitHubQuota.GitHubQuota;
20 yield* quota.observe("github.com", headers(499));
21 const refused = yield* Effect.flip(quota.admit("github.com", "graphql"));
22 assert.strictEqual(refused.retryAt, RESET);
23 yield* quota.admit("github.com", "core");
24 yield* quota.admit("ghe.example", "graphql");
25 yield* quota.admit("github.com", "graphql").pipe(Effect.provideService(CredentialScope, "b"));
26 yield* quota.admit("github.com", "graphql", { allowReserve: true });
27 }).pipe(Effect.provide(GitHubQuota.layer)),
28 );
29​
30 it.effect("takes the latest balance in a window, and ignores an older window", () =>
31 Effect.gen(function* () {
32 const quota = yield* GitHubQuota.GitHubQuota;
33 yield* quota.observe("github.com", headers(400));
34 yield* Effect.flip(quota.admit("github.com", "graphql"));
35 // GitHub can report more left later in the same window, from another region.
36 yield* quota.observe("github.com", headers(900));
37 yield* quota.admit("github.com", "graphql");
38 yield* quota.observe("github.com", headers(400));
39 yield* Effect.flip(quota.admit("github.com", "graphql"));
40 yield* quota.observe("github.com", headers(4000, "graphql", RESET - 3_600_000));
41 yield* Effect.flip(quota.admit("github.com", "graphql"));
42 yield* quota.observe("github.com", headers(4000, "graphql", RESET + 3_600_000));
43 yield* quota.admit("github.com", "graphql");
44 }).pipe(Effect.provide(GitHubQuota.layer)),
45 );
46​
47 it.effect("lets interactive requests spend the reserve but not an empty quota", () =>
48 Effect.gen(function* () {
49 const quota = yield* GitHubQuota.GitHubQuota;
50 yield* quota.observe("github.com", headers(1));
51 yield* quota.admit("github.com", "graphql", { allowReserve: true });
52 yield* quota.observe("github.com", headers(0));
53 const refused = yield* Effect.flip(
54 quota.admit("github.com", "graphql", { allowReserve: true }),
55 );
56 assert.strictEqual(refused.retryAt, RESET);
57 }).pipe(Effect.provide(GitHubQuota.layer)),
58 );
59​
60 it.effect("spends nothing locally, so a free 304 cannot run the balance down", () =>
61 Effect.gen(function* () {
62 const quota = yield* GitHubQuota.GitHubQuota;
63 yield* quota.observe("github.com", headers(501, "core"));
64 // Conditional reads answer 304 with the balance unchanged, however many are sent.
65 for (let index = 0; index < 5; index++) {
66 yield* quota.admit("github.com", "core");
67 yield* quota.observe("github.com", headers(501, "core"));
68 }
69 yield* quota.admit("github.com", "core");
70 }).pipe(Effect.provide(GitHubQuota.layer)),
71 );
72​
73 it.effect("forgets a balance once its window resets, and ignores answers naming no quota", () =>
74 Effect.gen(function* () {
75 const quota = yield* GitHubQuota.GitHubQuota;
76 yield* quota.observe("github.com", headers(0));
77 yield* TestClock.setTime(RESET);
78 yield* quota.admit("github.com", "graphql");
79 yield* quota.observe("other.example", { "x-ratelimit-remaining": "0" });
80 yield* quota.admit("other.example", "graphql");
81 }).pipe(Effect.provide(GitHubQuota.layer)),
82 );
83});