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 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 };
25}
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("surfaces a friendly error when the merge request is not found", () =>
157 Effect.gen(function* () {
158 const cause = new VcsProcessExitError({
159 operation: "GitLabCli.execute",
160 command: "glab",
161 cwd: "/repo",
162 exitCode: 1,
163 detail: "GET 404 merge request not found",
164 failureKind: "not-found",
165 });
166 mockedRun.mockReturnValueOnce(Effect.fail(cause));
167
168 const error = yield* Effect.gen(function* () {
169 const glab = yield* GitLabCli.GitLabCli;
170 return yield* glab.getMergeRequest({
171 cwd: "/repo",
172 reference: "4888",
173 });
174 }).pipe(Effect.flip);
175
176 assert.equal(error.message.includes("Merge request 4888 was not found"), true);
177 assert.strictEqual(error._tag, "GitLabMergeRequestNotFoundError");
178 assert.strictEqual(error.command, "glab");
179 assert.strictEqual(error.cwd, "/repo");
180 assert.strictEqual(error.cause, cause);
181 assert.equal(error.message.includes(cause.detail), false);
182 }),
183 );
184
185 it.effect("keeps non-merge-request not-found failures generic", () =>
186 Effect.gen(function* () {
187 const cause = new VcsProcessExitError({
188 operation: "GitLabCli.execute",
189 command: "glab",
190 cwd: "/repo",
191 exitCode: 1,
192 detail: "GET 404 project not found",
193 failureKind: "not-found",
194 });
195 mockedRun.mockReturnValueOnce(Effect.fail(cause));
196
197 const error = yield* Effect.gen(function* () {
198 const glab = yield* GitLabCli.GitLabCli;
199 return yield* glab.getRepositoryCloneUrls({
200 cwd: "/repo",
201 repository: "missing/project",
202 });
203 }).pipe(Effect.flip);
204
205 assert.strictEqual(error._tag, "GitLabCliCommandError");
206 assert.strictEqual(error.cause, cause);
207 }),
208 );
209
210 it.effect("preserves rate-limit failures as a distinct error", () =>
211 Effect.gen(function* () {
212 const cause = new VcsProcessExitError({
213 operation: "GitLabCli.execute",
214 command: "glab",
215 cwd: "/repo",
216 exitCode: 1,
217 detail: "API rate limit exceeded.",
218 failureKind: "rate-limited",
219 });
220 mockedRun.mockReturnValueOnce(Effect.fail(cause));
221
222 const glab = yield* GitLabCli.GitLabCli;
223 const error = yield* glab
224 .execute({ cwd: "/repo", args: ["api", "projects"] })
225 .pipe(Effect.flip);
226
227 assert.strictEqual(error._tag, "GitLabCliRateLimitError");
228 assert.strictEqual(error.cause, cause);
229 }),
230 );
231});