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