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

GitHubCli.test.ts

406 lines12.9 KB

1import { assert, it, afterEach, describe, expect, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as PlatformError from "effect/PlatformError";
5import { ChildProcessSpawner } from "effect/unstable/process";
6import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
7​
8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import * as GitHubCli from "./GitHubCli.ts";
10​
11const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
12 exitCode: ChildProcessSpawner.ExitCode(0),
13 stdout,
14 stderr: "",
15 stdoutTruncated: false,
16 stderrTruncated: false,
17});
18​
19const mockRun = vi.fn<VcsProcess.VcsProcess["Service"]["run"]>();
20​
21const layer = GitHubCli.layer.pipe(
22 Layer.provide(
23 Layer.mock(VcsProcess.VcsProcess)({
24 run: mockRun,
25 }),
26 ),
27);
28​
29afterEach(() => {
30 mockRun.mockReset();
31});
32​
33describe("GitHubCli.layer", () => {
34 it("does not classify a missing cwd as an unavailable gh executable", () => {
35 const context = { command: "gh", cwd: "/repo" } as const;
36 const missingCwd = new VcsProcessSpawnError({
37 operation: "GitHubCli.execute",
38 command: "gh",
39 cwd: context.cwd,
40 cause: PlatformError.systemError({
41 _tag: "NotFound",
42 module: "FileSystem",
43 method: "access",
44 pathOrDescriptor: context.cwd,
45 }),
46 });
47​
48 const commandFailure = GitHubCli.fromVcsError(context, missingCwd);
49​
50 assert.equal(commandFailure._tag, "GitHubCliCommandError");
51 assert.strictEqual(commandFailure.cause, missingCwd);
52 assert.notProperty(commandFailure, "operation");
53 });
54​
55 it.effect("parses pull request view output", () =>
56 Effect.gen(function* () {
57 mockRun.mockReturnValueOnce(
58 Effect.succeed(
59 processOutput(
60 // @effect-diagnostics-next-line preferSchemaOverJson:off
61 JSON.stringify({
62 number: 42,
63 title: "Add PR thread creation",
64 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
65 baseRefName: "main",
66 headRefName: "feature/pr-threads",
67 state: "OPEN",
68 mergedAt: null,
69 isCrossRepository: true,
70 headRepository: {
71 nameWithOwner: "octocat/codething-mvp",
72 },
73 headRepositoryOwner: {
74 login: "octocat",
75 },
76 }),
77 ),
78 ),
79 );
80​
81 const gh = yield* GitHubCli.GitHubCli;
82 const result = yield* gh.getPullRequest({
83 cwd: "/repo",
84 reference: "#42",
85 });
86​
87 assert.deepStrictEqual(result, {
88 number: 42,
89 title: "Add PR thread creation",
90 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
91 baseRefName: "main",
92 headRefName: "feature/pr-threads",
93 state: "open",
94 isCrossRepository: true,
95 headRepositoryNameWithOwner: "octocat/codething-mvp",
96 headRepositoryOwnerLogin: "octocat",
97 });
98 expect(mockRun).toHaveBeenCalledWith({
99 operation: "GitHubCli.execute",
100 command: "gh",
101 args: [
102 "pr",
103 "view",
104 "#42",
105 "--json",
106 "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
107 ],
108 cwd: "/repo",
109 timeoutMs: 30_000,
110 });
111 }).pipe(Effect.provide(layer)),
112 );
113​
114 it.effect("trims pull request fields decoded from gh json", () =>
115 Effect.gen(function* () {
116 mockRun.mockReturnValueOnce(
117 Effect.succeed(
118 processOutput(
119 // @effect-diagnostics-next-line preferSchemaOverJson:off
120 JSON.stringify({
121 number: 42,
122 title: " Add PR thread creation \n",
123 url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
124 baseRefName: " main ",
125 headRefName: "\tfeature/pr-threads\t",
126 state: "OPEN",
127 mergedAt: null,
128 isCrossRepository: true,
129 headRepository: {
130 nameWithOwner: " octocat/codething-mvp ",
131 },
132 headRepositoryOwner: {
133 login: " octocat ",
134 },
135 }),
136 ),
137 ),
138 );
139​
140 const gh = yield* GitHubCli.GitHubCli;
141 const result = yield* gh.getPullRequest({
142 cwd: "/repo",
143 reference: "#42",
144 });
145​
146 assert.deepStrictEqual(result, {
147 number: 42,
148 title: "Add PR thread creation",
149 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
150 baseRefName: "main",
151 headRefName: "feature/pr-threads",
152 state: "open",
153 isCrossRepository: true,
154 headRepositoryNameWithOwner: "octocat/codething-mvp",
155 headRepositoryOwnerLogin: "octocat",
156 });
157 }).pipe(Effect.provide(layer)),
158 );
159​
160 it.effect("skips invalid entries when parsing pr lists", () =>
161 Effect.gen(function* () {
162 mockRun.mockReturnValueOnce(
163 Effect.succeed(
164 processOutput(
165 // @effect-diagnostics-next-line preferSchemaOverJson:off
166 JSON.stringify([
167 {
168 number: 0,
169 title: "invalid",
170 url: "https://github.com/pingdotgg/codething-mvp/pull/0",
171 baseRefName: "main",
172 headRefName: "feature/invalid",
173 },
174 {
175 number: 43,
176 title: " Valid PR ",
177 url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
178 baseRefName: " main ",
179 headRefName: " feature/pr-list ",
180 headRepository: {
181 nameWithOwner: " ",
182 },
183 headRepositoryOwner: {
184 login: " ",
185 },
186 },
187 ]),
188 ),
189 ),
190 );
191​
192 const gh = yield* GitHubCli.GitHubCli;
193 const result = yield* gh.listOpenPullRequests({
194 cwd: "/repo",
195 headSelector: "feature/pr-list",
196 });
197​
198 assert.deepStrictEqual(result, [
199 {
200 number: 43,
201 title: "Valid PR",
202 url: "https://github.com/pingdotgg/codething-mvp/pull/43",
203 baseRefName: "main",
204 headRefName: "feature/pr-list",
205 state: "open",
206 },
207 ]);
208 }).pipe(Effect.provide(layer)),
209 );
210​
211 it.effect("keeps pull requests from gh versions without headRepository.nameWithOwner", () =>
212 // gh < 2.47 (e.g. Ubuntu-packaged 2.46) exports headRepository as
213 // {id, name} only. These entries must decode instead of being dropped,
214 // with nameWithOwner rebuilt from the owner login.
215 Effect.gen(function* () {
216 mockRun.mockReturnValueOnce(
217 Effect.succeed(
218 processOutput(
219 // @effect-diagnostics-next-line preferSchemaOverJson:off
220 JSON.stringify([
221 {
222 number: 2829,
223 title: "Codex turn mapping",
224 url: "https://github.com/pingdotgg/codething-mvp/pull/2829",
225 baseRefName: "main",
226 headRefName: "t3code/codex-turn-mapping",
227 state: "OPEN",
228 mergedAt: null,
229 isCrossRepository: false,
230 headRepository: {
231 id: "R_kgDORLtfbQ",
232 name: "codething-mvp",
233 },
234 headRepositoryOwner: {
235 id: "MDEyOk9yZ2FuaXphdGlvbjg5MTkxNzI3",
236 login: "pingdotgg",
237 },
238 },
239 ]),
240 ),
241 ),
242 );
243​
244 const gh = yield* GitHubCli.GitHubCli;
245 const result = yield* gh.listOpenPullRequests({
246 cwd: "/repo",
247 headSelector: "t3code/codex-turn-mapping",
248 });
249​
250 assert.deepStrictEqual(result, [
251 {
252 number: 2829,
253 title: "Codex turn mapping",
254 url: "https://github.com/pingdotgg/codething-mvp/pull/2829",
255 baseRefName: "main",
256 headRefName: "t3code/codex-turn-mapping",
257 state: "open",
258 isCrossRepository: false,
259 headRepositoryNameWithOwner: "pingdotgg/codething-mvp",
260 headRepositoryOwnerLogin: "pingdotgg",
261 },
262 ]);
263 }).pipe(Effect.provide(layer)),
264 );
265​
266 it.effect("reads repository clone URLs", () =>
267 Effect.gen(function* () {
268 mockRun.mockReturnValueOnce(
269 Effect.succeed(
270 processOutput(
271 // @effect-diagnostics-next-line preferSchemaOverJson:off
272 JSON.stringify({
273 nameWithOwner: "octocat/codething-mvp",
274 url: "https://github.com/octocat/codething-mvp",
275 sshUrl: "git@github.com:octocat/codething-mvp.git",
276 }),
277 ),
278 ),
279 );
280​
281 const gh = yield* GitHubCli.GitHubCli;
282 const result = yield* gh.getRepositoryCloneUrls({
283 cwd: "/repo",
284 repository: "octocat/codething-mvp",
285 });
286​
287 assert.deepStrictEqual(result, {
288 nameWithOwner: "octocat/codething-mvp",
289 url: "https://github.com/octocat/codething-mvp",
290 sshUrl: "git@github.com:octocat/codething-mvp.git",
291 });
292 }).pipe(Effect.provide(layer)),
293 );
294​
295 it.effect("creates repositories and parses clone URLs from create output", () =>
296 Effect.gen(function* () {
297 mockRun.mockReturnValueOnce(
298 Effect.succeed(
299 processOutput(
300 "✓ Created repository octocat/codething-mvp on github.com\nhttps://github.com/octocat/codething-mvp\n",
301 ),
302 ),
303 );
304​
305 const gh = yield* GitHubCli.GitHubCli;
306 const result = yield* gh.createRepository({
307 cwd: "/repo",
308 repository: "octocat/codething-mvp",
309 visibility: "private",
310 });
311​
312 assert.deepStrictEqual(result, {
313 nameWithOwner: "octocat/codething-mvp",
314 url: "https://github.com/octocat/codething-mvp",
315 sshUrl: "git@github.com:octocat/codething-mvp.git",
316 });
317 expect(mockRun).toHaveBeenCalledTimes(1);
318 expect(mockRun).toHaveBeenNthCalledWith(1, {
319 operation: "GitHubCli.execute",
320 command: "gh",
321 args: ["repo", "create", "octocat/codething-mvp", "--private"],
322 cwd: "/repo",
323 timeoutMs: 30_000,
324 });
325 }).pipe(Effect.provide(layer)),
326 );
327​
328 it.effect("falls back to constructed URLs when create output omits a URL", () =>
329 Effect.gen(function* () {
330 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
331​
332 const gh = yield* GitHubCli.GitHubCli;
333 const result = yield* gh.createRepository({
334 cwd: "/repo",
335 repository: "octocat/codething-mvp",
336 visibility: "private",
337 });
338​
339 assert.deepStrictEqual(result, {
340 nameWithOwner: "octocat/codething-mvp",
341 url: "https://github.com/octocat/codething-mvp",
342 sshUrl: "git@github.com:octocat/codething-mvp.git",
343 });
344 }).pipe(Effect.provide(layer)),
345 );
346​
347 it.effect("surfaces a friendly error when the pull request is not found", () =>
348 Effect.gen(function* () {
349 const cause = new VcsProcessExitError({
350 operation: "GitHubCli.execute",
351 command: "gh pr view",
352 cwd: "/repo",
353 exitCode: 1,
354 failureKind: "not-found",
355 detail:
356 "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
357 });
358 mockRun.mockReturnValueOnce(Effect.fail(cause));
359​
360 const gh = yield* GitHubCli.GitHubCli;
361 const error = yield* gh
362 .getPullRequest({
363 cwd: "/repo",
364 reference: "4888",
365 })
366 .pipe(Effect.flip);
367​
368 assert.equal(error.message.includes("Pull request not found"), true);
369 assert.strictEqual(error._tag, "GitHubPullRequestNotFoundError");
370 assert.strictEqual(error.command, "gh");
371 assert.strictEqual(error.cwd, "/repo");
372 assert.strictEqual(error.cause, cause);
373 assert.equal(error.message.includes(cause.detail), false);
374 }).pipe(Effect.provide(layer)),
375 );
376​
377 it.effect("surfaces an actionable rate-limit error without exposing provider stderr", () =>
378 Effect.gen(function* () {
379 const cause = new VcsProcessExitError({
380 operation: "GitHubCli.execute",
381 command: "gh",
382 cwd: "/repo",
383 exitCode: 1,
384 failureKind: "rate-limited",
385 detail: "API rate limit exceeded.",
386 stderrLength: 82,
387 stderrTruncated: false,
388 });
389 mockRun.mockReturnValueOnce(Effect.fail(cause));
390​
391 const gh = yield* GitHubCli.GitHubCli;
392 const error = yield* gh
393 .listOpenPullRequests({
394 cwd: "/repo",
395 headSelector: "feature/rate-limited",
396 })
397 .pipe(Effect.flip);
398​
399 assert.strictEqual(error._tag, "GitHubCliRateLimitError");
400 assert.include(error.detail, "GitHub API rate limit exceeded");
401 assert.include(error.detail, "gh api rate_limit");
402 assert.strictEqual(error.cause, cause);
403 assert.notInclude(error.message, "user ID");
404 }).pipe(Effect.provide(layer)),
405 );
406});