Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { Effect } from "effect";
3import { afterEach, expect, vi } from "vitest";
4
5vi.mock("../../processRunner", () => ({
6 runProcess: vi.fn(),
7}));
8
9import { runProcess } from "../../processRunner";
10import { GitHubCli } from "../Services/GitHubCli.ts";
11import { GitHubCliLive } from "./GitHubCli.ts";
12
13const mockedRunProcess = vi.mocked(runProcess);
14const layer = it.layer(GitHubCliLive);
15
16afterEach(() => {
17 mockedRunProcess.mockReset();
18});
19
20layer("GitHubCliLive", (it) => {
21 it.effect("parses pull request view output", () =>
22 Effect.gen(function* () {
23 mockedRunProcess.mockResolvedValueOnce({
24 stdout: JSON.stringify({
25 number: 42,
26 title: "Add PR thread creation",
27 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
28 baseRefName: "main",
29 headRefName: "feature/pr-threads",
30 state: "OPEN",
31 mergedAt: null,
32 isCrossRepository: true,
33 headRepository: {
34 nameWithOwner: "octocat/codething-mvp",
35 },
36 headRepositoryOwner: {
37 login: "octocat",
38 },
39 }),
40 stderr: "",
41 code: 0,
42 signal: null,
43 timedOut: false,
44 });
45
46 const result = yield* Effect.gen(function* () {
47 const gh = yield* GitHubCli;
48 return yield* gh.getPullRequest({
49 cwd: "/repo",
50 reference: "#42",
51 });
52 });
53
54 assert.deepStrictEqual(result, {
55 number: 42,
56 title: "Add PR thread creation",
57 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
58 baseRefName: "main",
59 headRefName: "feature/pr-threads",
60 state: "open",
61 isCrossRepository: true,
62 headRepositoryNameWithOwner: "octocat/codething-mvp",
63 headRepositoryOwnerLogin: "octocat",
64 });
65 expect(mockedRunProcess).toHaveBeenCalledWith(
66 "gh",
67 [
68 "pr",
69 "view",
70 "#42",
71 "--json",
72 "number,title,url,baseRefName,headRefName,state,mergedAt,isCrossRepository,headRepository,headRepositoryOwner",
73 ],
74 expect.objectContaining({ cwd: "/repo" }),
75 );
76 }),
77 );
78
79 it.effect("trims pull request fields decoded from gh json", () =>
80 Effect.gen(function* () {
81 mockedRunProcess.mockResolvedValueOnce({
82 stdout: JSON.stringify({
83 number: 42,
84 title: " Add PR thread creation \n",
85 url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
86 baseRefName: " main ",
87 headRefName: "\tfeature/pr-threads\t",
88 state: "OPEN",
89 mergedAt: null,
90 isCrossRepository: true,
91 headRepository: {
92 nameWithOwner: " octocat/codething-mvp ",
93 },
94 headRepositoryOwner: {
95 login: " octocat ",
96 },
97 }),
98 stderr: "",
99 code: 0,
100 signal: null,
101 timedOut: false,
102 });
103
104 const result = yield* Effect.gen(function* () {
105 const gh = yield* GitHubCli;
106 return yield* gh.getPullRequest({
107 cwd: "/repo",
108 reference: "#42",
109 });
110 });
111
112 assert.deepStrictEqual(result, {
113 number: 42,
114 title: "Add PR thread creation",
115 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
116 baseRefName: "main",
117 headRefName: "feature/pr-threads",
118 state: "open",
119 isCrossRepository: true,
120 headRepositoryNameWithOwner: "octocat/codething-mvp",
121 headRepositoryOwnerLogin: "octocat",
122 });
123 }),
124 );
125
126 it.effect("skips invalid entries when parsing pr lists", () =>
127 Effect.gen(function* () {
128 mockedRunProcess.mockResolvedValueOnce({
129 stdout: JSON.stringify([
130 {
131 number: 0,
132 title: "invalid",
133 url: "https://github.com/pingdotgg/codething-mvp/pull/0",
134 baseRefName: "main",
135 headRefName: "feature/invalid",
136 },
137 {
138 number: 43,
139 title: " Valid PR ",
140 url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
141 baseRefName: " main ",
142 headRefName: " feature/pr-list ",
143 headRepository: {
144 nameWithOwner: " ",
145 },
146 headRepositoryOwner: {
147 login: " ",
148 },
149 },
150 ]),
151 stderr: "",
152 code: 0,
153 signal: null,
154 timedOut: false,
155 });
156
157 const result = yield* Effect.gen(function* () {
158 const gh = yield* GitHubCli;
159 return yield* gh.listOpenPullRequests({
160 cwd: "/repo",
161 headSelector: "feature/pr-list",
162 });
163 });
164
165 assert.deepStrictEqual(result, [
166 {
167 number: 43,
168 title: "Valid PR",
169 url: "https://github.com/pingdotgg/codething-mvp/pull/43",
170 baseRefName: "main",
171 headRefName: "feature/pr-list",
172 state: "open",
173 },
174 ]);
175 }),
176 );
177
178 it.effect("reads repository clone URLs", () =>
179 Effect.gen(function* () {
180 mockedRunProcess.mockResolvedValueOnce({
181 stdout: JSON.stringify({
182 nameWithOwner: "octocat/codething-mvp",
183 url: "https://github.com/octocat/codething-mvp",
184 sshUrl: "git@github.com:octocat/codething-mvp.git",
185 }),
186 stderr: "",
187 code: 0,
188 signal: null,
189 timedOut: false,
190 });
191
192 const result = yield* Effect.gen(function* () {
193 const gh = yield* GitHubCli;
194 return yield* gh.getRepositoryCloneUrls({
195 cwd: "/repo",
196 repository: "octocat/codething-mvp",
197 });
198 });
199
200 assert.deepStrictEqual(result, {
201 nameWithOwner: "octocat/codething-mvp",
202 url: "https://github.com/octocat/codething-mvp",
203 sshUrl: "git@github.com:octocat/codething-mvp.git",
204 });
205 }),
206 );
207
208 it.effect("surfaces a friendly error when the pull request is not found", () =>
209 Effect.gen(function* () {
210 mockedRunProcess.mockRejectedValueOnce(
211 new Error(
212 "GraphQL: Could not resolve to a PullRequest with the number of 4888. (repository.pullRequest)",
213 ),
214 );
215
216 const error = yield* Effect.gen(function* () {
217 const gh = yield* GitHubCli;
218 return yield* gh.getPullRequest({
219 cwd: "/repo",
220 reference: "4888",
221 });
222 }).pipe(Effect.flip);
223
224 assert.equal(error.message.includes("Pull request not found"), true);
225 }),
226 );
227});