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