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