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