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

gitHubConditionalChecks.test.ts

219 lines7.0 KB

1import { expect, it } from "@effect/vitest";
2import * as TestClock from "effect/testing/TestClock";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Schema from "effect/Schema";
6import * as Redacted from "effect/Redacted";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import type { PullRequestCheck } from "@t3tools/contracts";
9​
10import * as GitHubCli from "../sourceControl/GitHubCli.ts";
11import { makeChecksRevalidator } from "./gitHubConditionalChecks.ts";
12​
13const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
14​
15const reference = { cwd: "/repo", repository: "acme/web", host: "github.com", number: 1 };
16const credential = {
17 host: "github.com",
18 token: Redacted.make("token"),
19 credentialFingerprint: "one",
20};
21​
22it.effect(
23 "reuses unchanged checks and catches reruns, later pages, fork approvals, pushes and account changes",
24 () =>
25 Effect.gen(function* () {
26 let sha = "a".repeat(40);
27 let changed = "";
28 let reads = 0;
29 const requests: string[] = [];
30 const revalidate = yield* makeChecksRevalidator.pipe(
31 Effect.provide(
32 Layer.mock(GitHubCli.GitHubCli)({
33 execute: ({ args }) =>
34 Effect.sync(() => {
35 const endpoint = args[1]!;
36 requests.push(endpoint);
37 const head = endpoint.endsWith("/pulls/1");
38 const modified =
39 !args.includes("-H") || (endpoint.includes(changed) && changed !== "");
40 const next = endpoint.includes("check-runs") && endpoint.endsWith("page=1");
41 return {
42 exitCode: ChildProcessSpawner.ExitCode(modified ? 0 : 1),
43 stdout: modified
44 ? `HTTP/2.0 200 OK\r\nEtag: "${sha}-${changed}"\r\n${next ? 'Link: <https://api.github.com/next>; rel="next"\r\n' : ""}\r\n${head ? encodeJson({ head: { sha }, base: { repo: { id: 1 } }, headRepositoryId: 2 }) : ""}`
45 : "HTTP/2.0 304 Not Modified\r\n\r\n",
46 stderr: "",
47 stdoutTruncated: false,
48 stderrTruncated: false,
49 };
50 }),
51 }),
52 ),
53 );
54 const read = Effect.sync(() => {
55 reads++;
56 return {
57 state: "open" as const,
58 checks: [
59 { name: "build", status: "success", description: null, url: null },
60 ] satisfies PullRequestCheck[],
61 headSha: sha,
62 workflowApprovalsRequired: 0,
63 };
64 });
65 const poll = (identity = credential) =>
66 revalidate(reference, read).pipe(
67 Effect.provideService(GitHubCli.PinnedGitHubCredential, identity),
68 );
69 yield* poll();
70 expect(reads).toBe(1);
71 requests.length = 0;
72 yield* poll();
73 expect(reads).toBe(1);
74 expect(requests).toHaveLength(5);
75 for (const endpoint of [
76 "check-runs?filter=all&per_page=100&page=2",
77 "/status",
78 "/actions/runs",
79 "/pulls/1",
80 ]) {
81 changed = endpoint;
82 yield* poll();
83 changed = "";
84 yield* poll();
85 }
86 expect(reads).toBe(5);
87 sha = "b".repeat(40);
88 changed = "/pulls/1";
89 requests.length = 0;
90 yield* poll();
91 expect(reads).toBe(6);
92 expect(
93 requests
94 .filter((endpoint) => endpoint.includes("/commits/"))
95 .every((endpoint) => endpoint.includes(sha)),
96 ).toBe(true);
97 changed = "";
98 yield* poll({ ...credential, credentialFingerprint: "two" });
99 expect(reads).toBe(7);
100 yield* TestClock.adjust("5 minutes");
101 yield* poll();
102 expect(reads).toBe(8);
103 }),
104);
105​
106it.effect("does not retain failed or incomplete reads, and supports hosts without ETags", () =>
107 Effect.gen(function* () {
108 const sha = "a".repeat(40);
109 let etags = true;
110 let unavailable = false;
111 let fail = true;
112 let complete = true;
113 let reads = 0;
114 const revalidate = yield* makeChecksRevalidator.pipe(
115 Effect.provide(
116 Layer.mock(GitHubCli.GitHubCli)({
117 execute: ({ args }) =>
118 unavailable
119 ? Effect.fail(
120 new GitHubCli.GitHubCliCommandError({
121 command: "gh",
122 cwd: "/repo",
123 cause: undefined,
124 httpStatus: 502,
125 }),
126 )
127 : Effect.succeed({
128 exitCode: ChildProcessSpawner.ExitCode(0),
129 stdout:
130 args.includes("-H") && etags
131 ? "HTTP/2.0 304 Not Modified\n\n"
132 : `HTTP/2.0 200 OK\n${etags ? 'Etag: "one"\n' : ""}\n${args[1]!.endsWith("/pulls/1") ? encodeJson({ head: { sha }, base: { repo: { id: 1 } }, headRepositoryId: 1 }) : ""}`,
133 stderr: "",
134 stdoutTruncated: false,
135 stderrTruncated: false,
136 }),
137 }),
138 ),
139 );
140 const read = Effect.suspend(() => {
141 reads++;
142 return fail
143 ? Effect.fail(
144 new GitHubCli.GitHubCliCommandError({ command: "gh", cwd: "/repo", cause: undefined }),
145 )
146 : Effect.succeed({
147 state: "open" as const,
148 checks: [],
149 headSha: sha,
150 ...(complete ? { workflowApprovalsRequired: 0 } : {}),
151 });
152 });
153 const poll = () =>
154 revalidate(reference, read).pipe(
155 Effect.provideService(GitHubCli.PinnedGitHubCredential, credential),
156 );
157 yield* poll().pipe(Effect.flip);
158 fail = false;
159 complete = false;
160 yield* poll();
161 yield* poll();
162 expect(reads).toBe(3);
163 complete = true;
164 yield* poll();
165 yield* poll();
166 expect(reads).toBe(4);
167 yield* TestClock.adjust("5 minutes");
168 complete = false;
169 yield* poll();
170 yield* poll();
171 expect(reads).toBe(6);
172 complete = true;
173 unavailable = true;
174 yield* poll();
175 expect(reads).toBe(7);
176 unavailable = false;
177 yield* poll();
178 yield* poll();
179 expect(reads).toBe(8);
180 etags = false;
181 yield* poll();
182 yield* poll();
183 expect(reads).toBe(10);
184 }),
185);
186​
187it.effect("falls back when REST checks are unavailable without retrying unsupported probes", () =>
188 Effect.gen(function* () {
189 let probes = 0;
190 let reads = 0;
191 const revalidate = yield* makeChecksRevalidator.pipe(
192 Effect.provide(
193 Layer.mock(GitHubCli.GitHubCli)({
194 execute: () => {
195 probes++;
196 return Effect.fail(
197 new GitHubCli.GitHubCliCommandError({
198 command: "gh",
199 cwd: "/repo",
200 cause: undefined,
201 httpStatus: 404,
202 }),
203 );
204 },
205 }),
206 ),
207 );
208 const read = Effect.sync(() => {
209 reads++;
210 return { state: "open" as const, checks: [] };
211 });
212 for (let tick = 0; tick < 2; tick++)
213 yield* revalidate(reference, read).pipe(
214 Effect.provideService(GitHubCli.PinnedGitHubCredential, credential),
215 );
216 expect(probes).toBe(1);
217 expect(reads).toBe(2);
218 }),
219);