Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Effect, Layer } from "effect";
3import { ChildProcessSpawner } from "effect/unstable/process";
4import { VcsProcessExitError, type VcsError } from "@t3tools/contracts";
5import { afterEach, describe, expect, vi } from "vitest";
6
7import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "../vcs/VcsProcess.ts";
8import * as GitHubCli from "./GitHubCli.ts";
9
10const processOutput = (stdout: string): VcsProcessOutput => ({
11 exitCode: ChildProcessSpawner.ExitCode(0),
12 stdout,
13 stderr: "",
14 stdoutTruncated: false,
15 stderrTruncated: false,
16});
17
18const mockRun = vi.fn<(input: VcsProcessInput) => Effect.Effect<VcsProcessOutput, VcsError>>();
19
20const layer = GitHubCli.layer.pipe(
21 Layer.provide(
22 Layer.mock(VcsProcess)({
23 run: mockRun,
24 }),
25 ),
26);
27
28afterEach(() => {
29 mockRun.mockReset();
30});
31
32describe("GitHubCli.layer", () => {
33 it.effect("parses pull request view output", () =>
34 Effect.gen(function* () {
35 mockRun.mockReturnValueOnce(
36 Effect.succeed(
37 processOutput(
38 JSON.stringify({
39 number: 42,
40 title: "Add PR thread creation",
41 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
42 baseRefName: "main",
43 headRefName: "feature/pr-threads",
44 state: "OPEN",
45 mergedAt: null,
46 isCrossRepository: true,
47 headRepository: {
48 nameWithOwner: "octocat/codething-mvp",
49 },
50 headRepositoryOwner: {
51 login: "octocat",
52 },
53 }),
54 ),
55 ),
56 );
57
58 const gh = yield* GitHubCli.GitHubCli;
59 const result = yield* gh.getPullRequest({
60 cwd: "/repo",
61 reference: "#42",
62 });
63
64 assert.deepStrictEqual(result, {
65 number: 42,
66 title: "Add PR thread creation",
67 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
68 baseRefName: "main",
69 headRefName: "feature/pr-threads",
70 state: "open",
71 isCrossRepository: true,
72 headRepositoryNameWithOwner: "octocat/codething-mvp",
73 headRepositoryOwnerLogin: "octocat",
74 });
75 expect(mockRun).toHaveBeenCalledWith({
76 operation: "GitHubCli.execute",
77 command: "gh",
78 args: [
79 "pr",
80 "view",
81 "#42",
82 "--json",
83 "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
84 ],
85 cwd: "/repo",
86 timeoutMs: 30_000,
87 });
88 }).pipe(Effect.provide(layer)),
89 );
90
91 it.effect("trims pull request fields decoded from gh json", () =>
92 Effect.gen(function* () {
93 mockRun.mockReturnValueOnce(
94 Effect.succeed(
95 processOutput(
96 JSON.stringify({
97 number: 42,
98 title: " Add PR thread creation \n",
99 url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
100 baseRefName: " main ",
101 headRefName: "\tfeature/pr-threads\t",
102 state: "OPEN",
103 mergedAt: null,
104 isCrossRepository: true,
105 headRepository: {
106 nameWithOwner: " octocat/codething-mvp ",
107 },
108 headRepositoryOwner: {
109 login: " octocat ",
110 },
111 }),
112 ),
113 ),
114 );
115
116 const gh = yield* GitHubCli.GitHubCli;
117 const result = yield* gh.getPullRequest({
118 cwd: "/repo",
119 reference: "#42",
120 });
121
122 assert.deepStrictEqual(result, {
123 number: 42,
124 title: "Add PR thread creation",
125 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
126 baseRefName: "main",
127 headRefName: "feature/pr-threads",
128 state: "open",
129 isCrossRepository: true,
130 headRepositoryNameWithOwner: "octocat/codething-mvp",
131 headRepositoryOwnerLogin: "octocat",
132 });
133 }).pipe(Effect.provide(layer)),
134 );
135
136 it.effect("skips invalid entries when parsing pr lists", () =>
137 Effect.gen(function* () {
138 mockRun.mockReturnValueOnce(
139 Effect.succeed(
140 processOutput(
141 JSON.stringify([
142 {
143 number: 0,
144 title: "invalid",
145 url: "https://github.com/pingdotgg/codething-mvp/pull/0",
146 baseRefName: "main",
147 headRefName: "feature/invalid",
148 },
149 {
150 number: 43,
151 title: " Valid PR ",
152 url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
153 baseRefName: " main ",
154 headRefName: " feature/pr-list ",
155 headRepository: {
156 nameWithOwner: " ",
157 },
158 headRepositoryOwner: {
159 login: " ",
160 },
161 },
162 ]),
163 ),
164 ),
165 );
166
167 const gh = yield* GitHubCli.GitHubCli;
168 const result = yield* gh.listOpenPullRequests({
169 cwd: "/repo",
170 headSelector: "feature/pr-list",
171 });
172
173 assert.deepStrictEqual(result, [
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 state: "open",
181 },
182 ]);
183 }).pipe(Effect.provide(layer)),
184 );
185
186 it.effect("reads repository clone URLs", () =>
187 Effect.gen(function* () {
188 mockRun.mockReturnValueOnce(
189 Effect.succeed(
190 processOutput(
191 JSON.stringify({
192 nameWithOwner: "octocat/codething-mvp",
193 url: "https://github.com/octocat/codething-mvp",
194 sshUrl: "git@github.com:octocat/codething-mvp.git",
195 }),
196 ),
197 ),
198 );
199
200 const gh = yield* GitHubCli.GitHubCli;
201 const result = yield* gh.getRepositoryCloneUrls({
202 cwd: "/repo",
203 repository: "octocat/codething-mvp",
204 });
205
206 assert.deepStrictEqual(result, {
207 nameWithOwner: "octocat/codething-mvp",
208 url: "https://github.com/octocat/codething-mvp",
209 sshUrl: "git@github.com:octocat/codething-mvp.git",
210 });
211 }).pipe(Effect.provide(layer)),
212 );
213
214 it.effect("creates repositories and parses clone URLs from create output", () =>
215 Effect.gen(function* () {
216 mockRun.mockReturnValueOnce(
217 Effect.succeed(
218 processOutput(
219 "✓ Created repository octocat/codething-mvp on github.com\nhttps://github.com/octocat/codething-mvp\n",
220 ),
221 ),
222 );
223
224 const gh = yield* GitHubCli.GitHubCli;
225 const result = yield* gh.createRepository({
226 cwd: "/repo",
227 repository: "octocat/codething-mvp",
228 visibility: "private",
229 });
230
231 assert.deepStrictEqual(result, {
232 nameWithOwner: "octocat/codething-mvp",
233 url: "https://github.com/octocat/codething-mvp",
234 sshUrl: "git@github.com:octocat/codething-mvp.git",
235 });
236 expect(mockRun).toHaveBeenCalledTimes(1);
237 expect(mockRun).toHaveBeenNthCalledWith(1, {
238 operation: "GitHubCli.execute",
239 command: "gh",
240 args: ["repo", "create", "octocat/codething-mvp", "--private"],
241 cwd: "/repo",
242 timeoutMs: 30_000,
243 });
244 }).pipe(Effect.provide(layer)),
245 );
246
247 it.effect("falls back to constructed URLs when create output omits a URL", () =>
248 Effect.gen(function* () {
249 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
250
251 const gh = yield* GitHubCli.GitHubCli;
252 const result = yield* gh.createRepository({
253 cwd: "/repo",
254 repository: "octocat/codething-mvp",
255 visibility: "private",
256 });
257
258 assert.deepStrictEqual(result, {
259 nameWithOwner: "octocat/codething-mvp",
260 url: "https://github.com/octocat/codething-mvp",
261 sshUrl: "git@github.com:octocat/codething-mvp.git",
262 });
263 }).pipe(Effect.provide(layer)),
264 );
265
266 it.effect("surfaces a friendly error when the pull request is not found", () =>
267 Effect.gen(function* () {
268 mockRun.mockReturnValueOnce(
269 Effect.fail(
270 new VcsProcessExitError({
271 operation: "GitHubCli.execute",
272 command: "gh pr view",
273 cwd: "/repo",
274 exitCode: 1,
275 detail:
276 "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
277 }),
278 ),
279 );
280
281 const gh = yield* GitHubCli.GitHubCli;
282 const error = yield* gh
283 .getPullRequest({
284 cwd: "/repo",
285 reference: "4888",
286 })
287 .pipe(Effect.flip);
288
289 assert.equal(error.message.includes("Pull request not found"), true);
290 }).pipe(Effect.provide(layer)),
291 );
292});