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

GitLabCli.test.ts

395 lines11.5 KB

1import { assert, it, afterEach, expect, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { ChildProcessSpawner } from "effect/process";
5​
6import { VcsProcessExitError } from "@t3tools/contracts";
7​
8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import * as GitLabCli from "./GitLabCli.ts";
10​
11const mockedRun = vi.fn<VcsProcess.VcsProcess["Service"]["run"]>();
12const layer = it.layer(
13 GitLabCli.layer.pipe(
14 Layer.provide(
15 Layer.mock(VcsProcess.VcsProcess)({
16 run: mockedRun,
17 }),
18 ),
19 ),
20);
21​
22function processOutput(stdout: string): VcsProcess.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 // @effect-diagnostics-next-line preferSchemaOverJson:off
43 JSON.stringify({
44 iid: 42,
45 title: "Add MR thread creation",
46 web_url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
47 target_branch: "main",
48 source_branch: "feature/mr-threads",
49 state: "closed",
50 closed_at: "2026-08-23T10:00:00Z",
51 source_project_id: 101,
52 target_project_id: 100,
53 source_project: {
54 path_with_namespace: "octocat/t3code",
55 },
56 }),
57 ),
58 ),
59 );
60​
61 const result = yield* Effect.gen(function* () {
62 const glab = yield* GitLabCli.GitLabCli;
63 return yield* glab.getMergeRequest({
64 cwd: "/repo",
65 reference: "42",
66 });
67 });
68​
69 assert.deepStrictEqual(result, {
70 number: 42,
71 title: "Add MR thread creation",
72 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
73 baseRefName: "main",
74 headRefName: "feature/mr-threads",
75 state: "closed",
76 closedAt: "2026-08-23T10:00:00Z",
77 mergedAt: null,
78 isCrossRepository: true,
79 headRepositoryNameWithOwner: "octocat/t3code",
80 headRepositoryOwnerLogin: "octocat",
81 });
82 expect(mockedRun).toHaveBeenCalledWith(
83 expect.objectContaining({
84 command: "glab",
85 cwd: "/repo",
86 args: ["mr", "view", "42", "--output", "json"],
87 }),
88 );
89 }),
90 );
91​
92 it.effect("skips invalid entries when parsing MR lists", () =>
93 Effect.gen(function* () {
94 mockedRun.mockReturnValueOnce(
95 Effect.succeed(
96 processOutput(
97 // @effect-diagnostics-next-line preferSchemaOverJson:off
98 JSON.stringify([
99 {
100 iid: 0,
101 title: "invalid",
102 web_url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/0",
103 target_branch: "main",
104 source_branch: "feature/invalid",
105 },
106 {
107 iid: 43,
108 title: " Valid MR ",
109 web_url: " https://gitlab.com/pingdotgg/t3code/-/merge_requests/43 ",
110 target_branch: " main ",
111 source_branch: " feature/mr-list ",
112 state: "merged",
113 merged_at: "2026-08-23T11:00:00Z",
114 },
115 ]),
116 ),
117 ),
118 );
119​
120 const result = yield* Effect.gen(function* () {
121 const glab = yield* GitLabCli.GitLabCli;
122 return yield* glab.listMergeRequests({
123 cwd: "/repo",
124 headSelector: "feature/mr-list",
125 state: "all",
126 });
127 });
128​
129 assert.deepStrictEqual(result, [
130 {
131 number: 43,
132 title: "Valid MR",
133 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/43",
134 baseRefName: "main",
135 headRefName: "feature/mr-list",
136 state: "merged",
137 closedAt: null,
138 mergedAt: "2026-08-23T11:00:00Z",
139 },
140 ]);
141 expect(mockedRun).toHaveBeenCalledWith(
142 expect.objectContaining({
143 command: "glab",
144 cwd: "/repo",
145 args: [
146 "mr",
147 "list",
148 "--source-branch",
149 "feature/mr-list",
150 "--all",
151 "--per-page",
152 "20",
153 "--output",
154 "json",
155 ],
156 }),
157 );
158 }),
159 );
160​
161 it.effect("reads repository clone URLs", () =>
162 Effect.gen(function* () {
163 mockedRun.mockReturnValueOnce(
164 Effect.succeed(
165 processOutput(
166 // @effect-diagnostics-next-line preferSchemaOverJson:off
167 JSON.stringify({
168 path_with_namespace: "octocat/t3code",
169 web_url: "https://gitlab.com/octocat/t3code",
170 http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
171 ssh_url_to_repo: "git@gitlab.com:octocat/t3code.git",
172 }),
173 ),
174 ),
175 );
176​
177 const result = yield* Effect.gen(function* () {
178 const glab = yield* GitLabCli.GitLabCli;
179 return yield* glab.getRepositoryCloneUrls({
180 cwd: "/repo",
181 repository: "octocat/t3code",
182 });
183 });
184​
185 assert.deepStrictEqual(result, {
186 nameWithOwner: "octocat/t3code",
187 url: "https://gitlab.com/octocat/t3code",
188 sshUrl: "git@gitlab.com:octocat/t3code.git",
189 });
190 }),
191 );
192​
193 it.effect("creates merge requests through the GitLab API without placing the body in argv", () =>
194 Effect.gen(function* () {
195 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
196​
197 const glab = yield* GitLabCli.GitLabCli;
198 yield* glab.createMergeRequest({
199 cwd: "/repo",
200 baseBranch: "main",
201 headSelector: "owner:feature/provider",
202 title: "Provider MR",
203 bodyFile: "/tmp/t3-mr-body.md",
204 });
205​
206 expect(mockedRun).toHaveBeenCalledWith(
207 expect.objectContaining({
208 command: "glab",
209 cwd: "/repo",
210 args: [
211 "api",
212 "--method",
213 "POST",
214 "projects/:fullpath/merge_requests",
215 "--raw-field",
216 "source_branch=feature/provider",
217 "--raw-field",
218 "target_branch=main",
219 "--raw-field",
220 "title=Provider MR",
221 "--field",
222 "description=@/tmp/t3-mr-body.md",
223 ],
224 }),
225 );
226 }),
227 );
228​
229 it.effect("creates repositories under an explicit namespace", () =>
230 Effect.gen(function* () {
231 mockedRun
232​
233 .mockReturnValueOnce(
234 Effect.succeed(
235 processOutput(
236 // @effect-diagnostics-next-line preferSchemaOverJson:off
237 JSON.stringify({ id: 1234 }),
238 ),
239 ),
240 )
241 .mockReturnValueOnce(
242 Effect.succeed(
243 processOutput(
244 // @effect-diagnostics-next-line preferSchemaOverJson:off
245 JSON.stringify({
246 path_with_namespace: "octocat/t3code",
247 web_url: "https://gitlab.com/octocat/t3code",
248 http_url_to_repo: "https://gitlab.com/octocat/t3code.git",
249 ssh_url_to_repo: "git@gitlab.com:octocat/t3code.git",
250 }),
251 ),
252 ),
253 );
254​
255 const glab = yield* GitLabCli.GitLabCli;
256 const result = yield* glab.createRepository({
257 cwd: "/repo",
258 repository: "octocat/t3code",
259 visibility: "public",
260 });
261​
262 assert.deepStrictEqual(result, {
263 nameWithOwner: "octocat/t3code",
264 url: "https://gitlab.com/octocat/t3code",
265 sshUrl: "git@gitlab.com:octocat/t3code.git",
266 });
267 expect(mockedRun).toHaveBeenNthCalledWith(
268 1,
269 expect.objectContaining({
270 command: "glab",
271 cwd: "/repo",
272 args: ["api", "namespaces/octocat"],
273 }),
274 );
275 expect(mockedRun).toHaveBeenNthCalledWith(
276 2,
277 expect.objectContaining({
278 command: "glab",
279 cwd: "/repo",
280 args: [
281 "api",
282 "--method",
283 "POST",
284 "projects",
285 "--raw-field",
286 "path=t3code",
287 "--raw-field",
288 "name=t3code",
289 "--raw-field",
290 "visibility=public",
291 "--raw-field",
292 "namespace_id=1234",
293 ],
294 }),
295 );
296 }),
297 );
298​
299 it.effect("does not pass unsupported force flags when checking out merge requests", () =>
300 Effect.gen(function* () {
301 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
302​
303 const glab = yield* GitLabCli.GitLabCli;
304 yield* glab.checkoutMergeRequest({
305 cwd: "/repo",
306 reference: "42",
307 force: true,
308 });
309​
310 expect(mockedRun).toHaveBeenCalledWith(
311 expect.objectContaining({
312 command: "glab",
313 cwd: "/repo",
314 args: ["mr", "checkout", "42"],
315 }),
316 );
317 }),
318 );
319​
320 it.effect("surfaces a friendly error when the merge request is not found", () =>
321 Effect.gen(function* () {
322 const cause = new VcsProcessExitError({
323 operation: "GitLabCli.execute",
324 command: "glab",
325 cwd: "/repo",
326 exitCode: 1,
327 detail: "GET 404 merge request not found",
328 failureKind: "not-found",
329 });
330 mockedRun.mockReturnValueOnce(Effect.fail(cause));
331​
332 const error = yield* Effect.gen(function* () {
333 const glab = yield* GitLabCli.GitLabCli;
334 return yield* glab.getMergeRequest({
335 cwd: "/repo",
336 reference: "4888",
337 });
338 }).pipe(Effect.flip);
339​
340 assert.equal(error.message.includes("Merge request 4888 was not found"), true);
341 assert.strictEqual(error._tag, "GitLabMergeRequestNotFoundError");
342 assert.strictEqual(error.command, "glab");
343 assert.strictEqual(error.cwd, "/repo");
344 assert.strictEqual(error.cause, cause);
345 assert.equal(error.message.includes(cause.detail), false);
346 }),
347 );
348​
349 it.effect("keeps non-merge-request not-found failures generic", () =>
350 Effect.gen(function* () {
351 const cause = new VcsProcessExitError({
352 operation: "GitLabCli.execute",
353 command: "glab",
354 cwd: "/repo",
355 exitCode: 1,
356 detail: "GET 404 project not found",
357 failureKind: "not-found",
358 });
359 mockedRun.mockReturnValueOnce(Effect.fail(cause));
360​
361 const error = yield* Effect.gen(function* () {
362 const glab = yield* GitLabCli.GitLabCli;
363 return yield* glab.getRepositoryCloneUrls({
364 cwd: "/repo",
365 repository: "missing/project",
366 });
367 }).pipe(Effect.flip);
368​
369 assert.strictEqual(error._tag, "GitLabCliCommandError");
370 assert.strictEqual(error.cause, cause);
371 }),
372 );
373​
374 it.effect("preserves rate-limit failures as a distinct error", () =>
375 Effect.gen(function* () {
376 const cause = new VcsProcessExitError({
377 operation: "GitLabCli.execute",
378 command: "glab",
379 cwd: "/repo",
380 exitCode: 1,
381 detail: "API rate limit exceeded.",
382 failureKind: "rate-limited",
383 });
384 mockedRun.mockReturnValueOnce(Effect.fail(cause));
385​
386 const glab = yield* GitLabCli.GitLabCli;
387 const error = yield* glab
388 .execute({ cwd: "/repo", args: ["api", "projects"] })
389 .pipe(Effect.flip);
390​
391 assert.strictEqual(error._tag, "GitLabCliRateLimitError");
392 assert.strictEqual(error.cause, cause);
393 }),
394 );
395});