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

GitHubCli.test.ts

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