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