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

GitLabCli.test.ts

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