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

GitHubCli.test.ts

291 lines8.7 KB

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