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

GitLabCli.test.ts

327 lines9.1 KB

1import { assert, it } from "@effect/vitest";
2import { Effect, Layer } from "effect";
3import { ChildProcessSpawner } from "effect/unstable/process";
4import { afterEach, expect, vi } from "vitest";
5​
6import { VcsProcessExitError } from "@t3tools/contracts";
7​
8import { VcsProcess, type VcsProcessOutput, type VcsProcessShape } from "../vcs/VcsProcess.ts";
9import * as GitLabCli from "./GitLabCli.ts";
10​
11const mockedRun = vi.fn<VcsProcessShape["run"]>();
12const layer = it.layer(
13 GitLabCli.layer.pipe(
14 Layer.provide(
15 Layer.mock(VcsProcess)({
16 run: mockedRun,
17 }),
18 ),
19 ),
20);
21​
22function processOutput(stdout: string): VcsProcessOutput {
23 return {
24 exitCode: ChildProcessSpawner.ExitCode(0),
25 stdout,
26 stderr: "",
27 stdoutTruncated: false,
28 stderrTruncated: false,
29 };
31​
32afterEach(() => {
33 mockedRun.mockReset();
34});
35​
36layer("GitLabCli.layer", (it) => {
37 it.effect("parses merge request view output", () =>
38 Effect.gen(function* () {
39 mockedRun.mockReturnValueOnce(
40 Effect.succeed(
41 processOutput(
42 JSON.stringify({
43 iid: 42,
44 title: "Add MR thread creation",
45 web_url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
46 target_branch: "main",
47 source_branch: "feature/mr-threads",
48 state: "opened",
49 source_project_id: 101,
50 target_project_id: 100,
51 source_project: {
52 path_with_namespace: "octocat/t3code",
53 },
54 }),
55 ),
56 ),
57 );
58​
59 const result = yield* Effect.gen(function* () {
60 const glab = yield* GitLabCli.GitLabCli;
61 return yield* glab.getMergeRequest({
62 cwd: "/repo",
63 reference: "42",
64 });
65 });
66​
67 assert.deepStrictEqual(result, {
68 number: 42,
69 title: "Add MR thread creation",
70 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
71 baseRefName: "main",
72 headRefName: "feature/mr-threads",
73 state: "open",
74 isCrossRepository: true,
75 headRepositoryNameWithOwner: "octocat/t3code",
76 headRepositoryOwnerLogin: "octocat",
77 });
78 expect(mockedRun).toHaveBeenCalledWith(
79 expect.objectContaining({
80 command: "glab",
81 cwd: "/repo",
82 args: ["mr", "view", "42", "--output", "json"],
83 }),
84 );
85 }),
86 );
87​
88 it.effect("skips invalid entries when parsing MR lists", () =>
89 Effect.gen(function* () {
90 mockedRun.mockReturnValueOnce(
91 Effect.succeed(
92 processOutput(
93 JSON.stringify([
94 {
95 iid: 0,
96 title: "invalid",
97 web_url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/0",
98 target_branch: "main",
99 source_branch: "feature/invalid",
100 },
101 {
102 iid: 43,
103 title: " Valid MR ",
104 web_url: " https://gitlab.com/pingdotgg/t3code/-/merge_requests/43 ",
105 target_branch: " main ",
106 source_branch: " feature/mr-list ",
107 state: "merged",
108 },
109 ]),
110 ),
111 ),
112 );
113​
114 const result = yield* Effect.gen(function* () {
115 const glab = yield* GitLabCli.GitLabCli;
116 return yield* glab.listMergeRequests({
117 cwd: "/repo",
118 headSelector: "feature/mr-list",
119 state: "all",
120 });
121 });
122​
123 assert.deepStrictEqual(result, [
124 {
125 number: 43,
126 title: "Valid MR",
127 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/43",
128 baseRefName: "main",
129 headRefName: "feature/mr-list",
130 state: "merged",
131 },
132 ]);
133 expect(mockedRun).toHaveBeenCalledWith(
134 expect.objectContaining({
135 command: "glab",
136 cwd: "/repo",
137 args: [
138 "mr",
139 "list",
140 "--source-branch",
141 "feature/mr-list",
142 "--all",
143 "--per-page",
144 "20",
145 "--output",
146 "json",
147 ],
148 }),
149 );
150 }),
151 );
152​
153 it.effect("reads repository clone URLs", () =>
154 Effect.gen(function* () {
155 mockedRun.mockReturnValueOnce(
156 Effect.succeed(
157 processOutput(
158 JSON.stringify({
159 path_with_namespace: "octocat/t3code",
160 web_url: "https://gitlab.com/octocat/t3code",
161 http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
162 ssh_url_to_repo: "git@gitlab.com:octocat/t3code.git",
163 }),
164 ),
165 ),
166 );
167​
168 const result = yield* Effect.gen(function* () {
169 const glab = yield* GitLabCli.GitLabCli;
170 return yield* glab.getRepositoryCloneUrls({
171 cwd: "/repo",
172 repository: "octocat/t3code",
173 });
174 });
175​
176 assert.deepStrictEqual(result, {
177 nameWithOwner: "octocat/t3code",
178 url: "https://gitlab.com/octocat/t3code",
179 sshUrl: "git@gitlab.com:octocat/t3code.git",
180 });
181 }),
182 );
183​
184 it.effect("creates merge requests through the GitLab API without placing the body in argv", () =>
185 Effect.gen(function* () {
186 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
187​
188 const glab = yield* GitLabCli.GitLabCli;
189 yield* glab.createMergeRequest({
190 cwd: "/repo",
191 baseBranch: "main",
192 headSelector: "owner:feature/provider",
193 title: "Provider MR",
194 bodyFile: "/tmp/t3-mr-body.md",
195 });
196​
197 expect(mockedRun).toHaveBeenCalledWith(
198 expect.objectContaining({
199 command: "glab",
200 cwd: "/repo",
201 args: [
202 "api",
203 "--method",
204 "POST",
205 "projects/:fullpath/merge_requests",
206 "--raw-field",
207 "source_branch=feature/provider",
208 "--raw-field",
209 "target_branch=main",
210 "--raw-field",
211 "title=Provider MR",
212 "--field",
213 "description=@/tmp/t3-mr-body.md",
214 ],
215 }),
216 );
217 }),
218 );
219​
220 it.effect("creates repositories under an explicit namespace", () =>
221 Effect.gen(function* () {
222 mockedRun
223 .mockReturnValueOnce(Effect.succeed(processOutput(JSON.stringify({ id: 1234 }))))
224 .mockReturnValueOnce(
225 Effect.succeed(
226 processOutput(
227 JSON.stringify({
228 path_with_namespace: "octocat/t3code",
229 web_url: "https://gitlab.com/octocat/t3code",
230 http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
231 ssh_url_to_repo: "git@gitlab.com:octocat/t3code.git",
232 }),
233 ),
234 ),
235 );
236​
237 const glab = yield* GitLabCli.GitLabCli;
238 const result = yield* glab.createRepository({
239 cwd: "/repo",
240 repository: "octocat/t3code",
241 visibility: "public",
242 });
243​
244 assert.deepStrictEqual(result, {
245 nameWithOwner: "octocat/t3code",
246 url: "https://gitlab.com/octocat/t3code",
247 sshUrl: "git@gitlab.com:octocat/t3code.git",
248 });
249 expect(mockedRun).toHaveBeenNthCalledWith(
250 1,
251 expect.objectContaining({
252 command: "glab",
253 cwd: "/repo",
254 args: ["api", "namespaces/octocat"],
255 }),
256 );
257 expect(mockedRun).toHaveBeenNthCalledWith(
258 2,
259 expect.objectContaining({
260 command: "glab",
261 cwd: "/repo",
262 args: [
263 "api",
264 "--method",
265 "POST",
266 "projects",
267 "--raw-field",
268 "path=t3code",
269 "--raw-field",
270 "name=t3code",
271 "--raw-field",
272 "visibility=public",
273 "--raw-field",
274 "namespace_id=1234",
275 ],
276 }),
277 );
278 }),
279 );
280​
281 it.effect("does not pass unsupported force flags when checking out merge requests", () =>
282 Effect.gen(function* () {
283 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
284​
285 const glab = yield* GitLabCli.GitLabCli;
286 yield* glab.checkoutMergeRequest({
287 cwd: "/repo",
288 reference: "42",
289 force: true,
290 });
291​
292 expect(mockedRun).toHaveBeenCalledWith(
293 expect.objectContaining({
294 command: "glab",
295 cwd: "/repo",
296 args: ["mr", "checkout", "42"],
297 }),
298 );
299 }),
300 );
301​
302 it.effect("surfaces a friendly error when the merge request is not found", () =>
303 Effect.gen(function* () {
304 mockedRun.mockReturnValueOnce(
305 Effect.fail(
306 new VcsProcessExitError({
307 operation: "GitLabCli.execute",
308 command: "glab mr view 4888",
309 cwd: "/repo",
310 exitCode: 1,
311 detail: "GET 404 merge request not found",
312 }),
313 ),
314 );
315​
316 const error = yield* Effect.gen(function* () {
317 const glab = yield* GitLabCli.GitLabCli;
318 return yield* glab.getMergeRequest({
319 cwd: "/repo",
320 reference: "4888",
321 });
322 }).pipe(Effect.flip);
323​
324 assert.equal(error.message.includes("Merge request not found"), true);
325 }),
326 );
327});