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

githubGraphQlBudget.test.ts

189 lines7.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): string {
13 return JSON.stringify({
14 data: {
15 viewer: { login: "bilal" },
16 rateLimit: { cost: 14, 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("ignores a response from an older reset window", () =>
90 Effect.gen(function* () {
91 yield* TestClock.setTime(BEFORE_RESET);
92 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
93 yield* budget.observe("github.com", rateLimit(400, 5_000, NEXT_RESET_AT));
94 yield* budget.observe("github.com", rateLimit(1_000, 5_000, RESET_AT));
95​
96 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
97 expect(error).toMatchObject({
98 _tag: "SourceControlRateLimitPausedError",
99 retryAt: Date.parse(NEXT_RESET_AT),
100 });
101 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
102 );
103​
104 it.effect("accepts a response from a later reset window", () =>
105 Effect.gen(function* () {
106 yield* TestClock.setTime(BEFORE_RESET);
107 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
108 yield* budget.observe("github.com", rateLimit(1_000));
109 yield* budget.observe("github.com", rateLimit(400, 5_000, NEXT_RESET_AT));
110​
111 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
112 expect(error).toMatchObject({
113 _tag: "SourceControlRateLimitPausedError",
114 retryAt: Date.parse(NEXT_RESET_AT),
115 });
116 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
117 );
118​
119 it.effect("allows reads above the reserve", () =>
120 Effect.gen(function* () {
121 yield* TestClock.setTime(BEFORE_RESET);
122 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
123 yield* budget.observe("github.com", rateLimit(515));
124​
125 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
126 "rateLimit",
127 );
128 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
129 );
130​
131 it.effect("allows an interactive read to use the reserve", () =>
132 Effect.gen(function* () {
133 yield* TestClock.setTime(BEFORE_RESET);
134 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
135 yield* budget.observe("github.com", rateLimit(500));
136​
137 expect(
138 yield* budget.query("github.com", "query { viewer { login } }", {
139 allowReserve: true,
140 }),
141 ).toContain("rateLimit");
142 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
143 );
144​
145 it.effect("reserves an admitted query cost before its response", () =>
146 Effect.gen(function* () {
147 yield* TestClock.setTime(BEFORE_RESET);
148 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
149 yield* budget.observe("github.com", rateLimit(514));
150​
151 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
152 "rateLimit",
153 );
154 const error = yield* Effect.flip(budget.query("github.com", "query { viewer { login } }"));
155​
156 expect(error).toMatchObject({
157 _tag: "SourceControlRateLimitPausedError",
158 retryAt: Date.parse(RESET_AT),
159 });
160 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
161 );
162​
163 it.effect("ignores malformed or partial rate metadata", () =>
164 Effect.gen(function* () {
165 yield* TestClock.setTime(BEFORE_RESET);
166 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
167 yield* budget.observe("github.com", "{");
168 yield* budget.observe(
169 "github.com",
170 '{"data":{"rateLimit":{"limit":0,"remaining":-1,"resetAt":"never"}}}',
171 );
172​
173 expect(yield* budget.query("github.com", "query { viewer { login } }")).toContain(
174 "rateLimit",
175 );
176 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
177 );
178​
179 it.effect("does not add a read field to a mutation", () =>
180 Effect.gen(function* () {
181 yield* TestClock.setTime(BEFORE_RESET);
182 const budget = yield* GitHubGraphQlBudget.GitHubGraphQlBudget;
183 const mutation = "mutation { addComment(input: {}) { clientMutationId } }";
184 yield* budget.observe("github.com", rateLimit(0));
185​
186 expect(yield* budget.query("github.com", mutation)).toBe(mutation);
187 }).pipe(Effect.provide(GitHubGraphQlBudget.layer)),
188 );
189});