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

githubGraphQlBudget.test.ts

210 lines8.2 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as TestClock from "effect/testing/TestClock";
4​
5import * as GitHubGraphQlBudget from "./githubGraphQlBudget.ts";
6​
7const RESET_AT = "2026-08-13T14:00:00.000Z";
8const NEXT_RESET_AT = "2026-08-13T15:00:00.000Z";
9const BEFORE_RESET = Date.parse("2026-08-13T13:30:00.000Z");
10const AFTER_RESET = Date.parse("2026-08-13T14:00:01.000Z");
11​
12function rateLimit(remaining: number, limit = 5_000, resetAt = RESET_AT, cost = 14): string {
13 return JSON.stringify({
14 data: {
15 viewer: { login: "bilal" },
16 rateLimit: { cost, limit, remaining, resetAt },
17 },
18 });
20​
21describe("GitHub GraphQL budget", () => {
22 it.effect("adds rate metadata to a read query", () =>
23 Effect.gen(function* () {
24 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
25​
26 const query = yield* budget.query(
27 "github.com",
28 'query { repository(owner: "acme", name: "web") { name } }',
29 );
30​
31 expect(query).toContain("rateLimit { cost limit remaining resetAt }");
32 expect(query).toContain('repository(owner: "acme", name: "web") { name }');
33 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
34 );
35​
36 it.effect("protects the last ten percent with the shared provider cooldown error", () =>
37 Effect.gen(function* () {
38 yield* TestClock.setTime(BEFORE_RESET);
39 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
40 yield* budget.observe("github.com", rateLimit(500));
41​
42 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
43 expect(error).toMatchObject({
44 _tag: "SourceControlRateLimitPausedError",
45 provider: "github",
46 host: "github.com",
47 retryAt: Date.parse(RESET_AT),
48 });
49 expect(error.message).toBe(
50 "github requests to github.com are paused until the rate limit resets.",
51 );
52​
53 yield* TestClock.setTime(AFTER_RESET);
54 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
55 "rateLimit",
56 );
57 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
58 );
59​
60 it.effect("keeps hosts isolated", () =>
61 Effect.gen(function* () {
62 yield* TestClock.setTime(BEFORE_RESET);
63 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
64 yield* budget.observe("github.com", rateLimit(0));
65​
66 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
67 expect(error._tag).toBe("SourceControlRateLimitPausedError");
68 expect(yield* budget.query("github.example.com", "query { viewer { login } }")).toContain(
69 "rateLimit",
70 );
71 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
72 );
73​
74 it.effect("keeps the lower remaining value from out-of-order responses", () =>
75 Effect.gen(function* () {
76 yield* TestClock.setTime(BEFORE_RESET);
77 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
78 yield* budget.observe("github.com", rateLimit(400));
79 yield* budget.observe("github.com", rateLimit(600));
80​
81 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
82 expect(error).toMatchObject({
83 _tag: "SourceControlRateLimitPausedError",
84 retryAt: Date.parse(RESET_AT),
85 });
86 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
87 );
88​
89 it.effect("learns a cheaper observed cost without restoring reserved quota", () =>
90 Effect.gen(function* () {
91 yield* TestClock.setTime(BEFORE_RESET);
92 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
93 const query = "query { viewer { login } }";
94 yield* budget.observe("github.com", rateLimit(512, 5_000, RESET_AT, 8));
95 yield* budget.query("github.com", query);
96 // The admission reserved eight points, but the completed read only cost one.
97 yield* budget.observe("github.com", rateLimit(511, 5_000, RESET_AT, 1));
98​
99 for (let count = 0; count < 4; count += 1) {
100 expect(yield* budget.query("github.com", query)).toContain("rateLimit");
101 }
102 const error = yield* Effect.flip(budget.query("github.com", query));
103 expect(error).toMatchObject({
104 _tag: "SourceControlRateLimitPausedError",
105 retryAt: Date.parse(RESET_AT),
106 });
107 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
108 );
109​
110 it.effect("ignores a response from an older reset window", () =>
111 Effect.gen(function* () {
112 yield* TestClock.setTime(BEFORE_RESET);
113 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
114 yield* budget.observe("github.com", rateLimit(400, 5_000, NEXT_RESET_AT));
115 yield* budget.observe("github.com", rateLimit(1_000, 5_000, RESET_AT));
116​
117 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
118 expect(error).toMatchObject({
119 _tag: "SourceControlRateLimitPausedError",
120 retryAt: Date.parse(NEXT_RESET_AT),
121 });
122 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
123 );
124​
125 it.effect("accepts a response from a later reset window", () =>
126 Effect.gen(function* () {
127 yield* TestClock.setTime(BEFORE_RESET);
128 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
129 yield* budget.observe("github.com", rateLimit(1_000));
130 yield* budget.observe("github.com", rateLimit(400, 5_000, NEXT_RESET_AT));
131​
132 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
133 expect(error).toMatchObject({
134 _tag: "SourceControlRateLimitPausedError",
135 retryAt: Date.parse(NEXT_RESET_AT),
136 });
137 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
138 );
139​
140 it.effect("allows reads above the reserve", () =>
141 Effect.gen(function* () {
142 yield* TestClock.setTime(BEFORE_RESET);
143 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
144 yield* budget.observe("github.com", rateLimit(515));
145​
146 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
147 "rateLimit",
148 );
149 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
150 );
151​
152 it.effect("allows an interactive read to use the reserve", () =>
153 Effect.gen(function* () {
154 yield* TestClock.setTime(BEFORE_RESET);
155 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
156 yield* budget.observe("github.com", rateLimit(500));
157​
158 expect(
159 yield* budget.query("github.com", "query { viewer { login } }", {
160 allowReserve: true,
161 }),
162 ).toContain("rateLimit");
163 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
164 );
165​
166 it.effect("reserves an admitted query cost before its response", () =>
167 Effect.gen(function* () {
168 yield* TestClock.setTime(BEFORE_RESET);
169 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
170 yield* budget.observe("github.com", rateLimit(514));
171​
172 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
173 "rateLimit",
174 );
175 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
176​
177 expect(error).toMatchObject({
178 _tag: "SourceControlRateLimitPausedError",
179 retryAt: Date.parse(RESET_AT),
180 });
181 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
182 );
183​
184 it.effect("ignores malformed or partial rate metadata", () =>
185 Effect.gen(function* () {
186 yield* TestClock.setTime(BEFORE_RESET);
187 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
188 yield* budget.observe("github.com", "{");
189 yield* budget.observe(
190 "github.com",
191 '{"data":{"rateLimit":{"limit":0,"remaining":-1,"resetAt":"never"}}}',
192 );
193​
194 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
195 "rateLimit",
196 );
197 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
198 );
199​
200 it.effect("does not add a read field to a mutation", () =>
201 Effect.gen(function* () {
202 yield* TestClock.setTime(BEFORE_RESET);
203 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
204 const mutation = "mutation { addComment(input: {}) { clientMutationId } }";
205 yield* budget.observe("github.com", rateLimit(0));
206​
207 expect(yield* budget.query("github.com", mutation)).toBe(mutation);
208 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
209 );
210});