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

GitHubCli.test.ts

321 lines10.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 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("reads repository clone URLs", () =>
212 Effect.gen(function* () {
213 mockRun.mockReturnValueOnce(
214 Effect.succeed(
215 processOutput(
216 // @effect-diagnostics-next-line preferSchemaOverJson:off
217 JSON.stringify({
218 nameWithOwner: "octocat/codething-mvp",
219 url: "https://github.com/octocat/codething-mvp",
220 sshUrl: "git@github.com:octocat/codething-mvp.git",
221 }),
222 ),
223 ),
224 );
225​
226 const gh = yield* GitHubCli.GitHubCli;
227 const result = yield* gh.getRepositoryCloneUrls({
228 cwd: "/repo",
229 repository: "octocat/codething-mvp",
230 });
231​
232 assert.deepStrictEqual(result, {
233 nameWithOwner: "octocat/codething-mvp",
234 url: "https://github.com/octocat/codething-mvp",
235 sshUrl: "git@github.com:octocat/codething-mvp.git",
236 });
237 }).pipe(Effect.provide(layer)),
238 );
239​
240 it.effect("creates repositories and parses clone URLs from create output", () =>
241 Effect.gen(function* () {
242 mockRun.mockReturnValueOnce(
243 Effect.succeed(
244 processOutput(
245 "✓ Created repository octocat/codething-mvp on github.com\nhttps://github.com/octocat/codething-mvp\n",
246 ),
247 ),
248 );
249​
250 const gh = yield* GitHubCli.GitHubCli;
251 const result = yield* gh.createRepository({
252 cwd: "/repo",
253 repository: "octocat/codething-mvp",
254 visibility: "private",
255 });
256​
257 assert.deepStrictEqual(result, {
258 nameWithOwner: "octocat/codething-mvp",
259 url: "https://github.com/octocat/codething-mvp",
260 sshUrl: "git@github.com:octocat/codething-mvp.git",
261 });
262 expect(mockRun).toHaveBeenCalledTimes(1);
263 expect(mockRun).toHaveBeenNthCalledWith(1, {
264 operation: "GitHubCli.execute",
265 command: "gh",
266 args: ["repo", "create", "octocat/codething-mvp", "--private"],
267 cwd: "/repo",
268 timeoutMs: 30_000,
269 });
270 }).pipe(Effect.provide(layer)),
271 );
272​
273 it.effect("falls back to constructed URLs when create output omits a URL", () =>
274 Effect.gen(function* () {
275 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
276​
277 const gh = yield* GitHubCli.GitHubCli;
278 const result = yield* gh.createRepository({
279 cwd: "/repo",
280 repository: "octocat/codething-mvp",
281 visibility: "private",
282 });
283​
284 assert.deepStrictEqual(result, {
285 nameWithOwner: "octocat/codething-mvp",
286 url: "https://github.com/octocat/codething-mvp",
287 sshUrl: "git@github.com:octocat/codething-mvp.git",
288 });
289 }).pipe(Effect.provide(layer)),
290 );
291​
292 it.effect("surfaces a friendly error when the pull request is not found", () =>
293 Effect.gen(function* () {
294 const cause = new VcsProcessExitError({
295 operation: "GitHubCli.execute",
296 command: "gh pr view",
297 cwd: "/repo",
298 exitCode: 1,
299 failureKind: "not-found",
300 detail:
301 "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
302 });
303 mockRun.mockReturnValueOnce(Effect.fail(cause));
304​
305 const gh = yield* GitHubCli.GitHubCli;
306 const error = yield* gh
307 .getPullRequest({
308 cwd: "/repo",
309 reference: "4888",
310 })
311 .pipe(Effect.flip);
312​
313 assert.equal(error.message.includes("Pull request not found"), true);
314 assert.strictEqual(error._tag, "GitHubPullRequestNotFoundError");
315 assert.strictEqual(error.command, "gh");
316 assert.strictEqual(error.cwd, "/repo");
317 assert.strictEqual(error.cause, cause);
318 assert.equal(error.message.includes(cause.detail), false);
319 }).pipe(Effect.provide(layer)),
320 );
321});