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

GitLabCli.test.ts

276 lines8.6 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 const classifyFailure = mockedRun.mock.calls.at(-1)?.[0].classifyFailure;
86 assert.strictEqual(
87 classifyFailure?.(
88 "x gitlab.example.test has not been authenticated with glab. Run `glab auth login`",
89 ),
90 "authentication",
91 );
92 assert.strictEqual(
93 classifyFailure?.(
94 'Failed to get merge request 42: oauth2: "invalid_grant" "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."',
95 ),
96 "authentication",
97 );
98 }),
99 );
100​
101 it.effect("skips invalid entries when parsing MR lists", () =>
102 Effect.gen(function* () {
103 mockedRun.mockReturnValueOnce(
104 Effect.succeed(
105 processOutput(
106 JSON.stringify([
107 {
108 iid: 0,
109 title: "invalid",
110 web_url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/0",
111 target_branch: "main",
112 source_branch: "feature/invalid",
113 },
114 {
115 iid: 43,
116 title: " Valid MR ",
117 web_url: " https://gitlab.com/pingdotgg/t3code/-/merge_requests/43 ",
118 target_branch: " main ",
119 source_branch: " feature/mr-list ",
120 state: "merged",
121 merged_at: "2026-08-23T11:00:00Z",
122 },
123 ]),
124 ),
125 ),
126 );
127​
128 const result = yield* Effect.gen(function* () {
129 const glab = yield* GitLabCli.GitLabCli;
130 return yield* glab.listMergeRequests({
131 cwd: "/repo",
132 headSelector: "feature/mr-list",
133 state: "all",
134 });
135 });
136​
137 assert.deepStrictEqual(result, [
138 {
139 number: 43,
140 title: "Valid MR",
141 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/43",
142 baseRefName: "main",
143 headRefName: "feature/mr-list",
144 state: "merged",
145 closedAt: null,
146 mergedAt: "2026-08-23T11:00:00Z",
147 },
148 ]);
149 expect(mockedRun).toHaveBeenCalledWith(
150 expect.objectContaining({
151 command: "glab",
152 cwd: "/repo",
153 args: [
154 "mr",
155 "list",
156 "--source-branch",
157 "feature/mr-list",
158 "--all",
159 "--per-page",
160 "20",
161 "--output",
162 "json",
163 ],
164 }),
165 );
166 }),
167 );
168​
169 it.effect("creates same-project MRs without a repository lookup", () =>
170 Effect.gen(function* () {
171 mockedRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
172 const glab = yield* GitLabCli.GitLabCli;
173 yield* glab.createMergeRequest({
174 cwd: "/repo",
175 baseBranch: "main",
176 headSelector: "feature/fix",
177 title: "Fix remote flow",
178 bodyFile: "/tmp/body.md",
179 });
180 expect(mockedRun).toHaveBeenCalledExactlyOnceWith(
181 expect.objectContaining({
182 args: [
183 "api",
184 "--method",
185 "POST",
186 "projects/:fullpath/merge_requests",
187 "--raw-field",
188 "source_branch=feature/fix",
189 "--raw-field",
190 "target_branch=main",
191 "--raw-field",
192 "title=Fix remote flow",
193 "--field",
194 "description=@/tmp/body.md",
195 ],
196 }),
197 );
198 }),
199 );
200​
201 it.effect("surfaces a friendly error when the merge request is not found", () =>
202 Effect.gen(function* () {
203 const cause = new VcsProcessExitError({
204 operation: "GitLabCli.execute",
205 command: "glab",
206 cwd: "/repo",
207 exitCode: 1,
208 detail: "GET 404 merge request not found",
209 failureKind: "not-found",
210 });
211 mockedRun.mockReturnValueOnce(Effect.fail(cause));
212​
213 const error = yield* Effect.gen(function* () {
214 const glab = yield* GitLabCli.GitLabCli;
215 return yield* glab.getMergeRequest({
216 cwd: "/repo",
217 reference: "4888",
218 });
219 }).pipe(Effect.flip);
220​
221 assert.equal(error.message.includes("Merge request 4888 was not found"), true);
222 assert.strictEqual(error._tag, "GitLabMergeRequestNotFoundError");
223 assert.strictEqual(error.command, "glab");
224 assert.strictEqual(error.cwd, "/repo");
225 assert.strictEqual(error.cause, cause);
226 assert.equal(error.message.includes(cause.detail), false);
227 }),
228 );
229​
230 it.effect("keeps non-merge-request not-found failures generic", () =>
231 Effect.gen(function* () {
232 const cause = new VcsProcessExitError({
233 operation: "GitLabCli.execute",
234 command: "glab",
235 cwd: "/repo",
236 exitCode: 1,
237 detail: "GET 404 project not found",
238 failureKind: "not-found",
239 });
240 mockedRun.mockReturnValueOnce(Effect.fail(cause));
241​
242 const error = yield* Effect.gen(function* () {
243 const glab = yield* GitLabCli.GitLabCli;
244 return yield* glab.getRepositoryCloneUrls({
245 cwd: "/repo",
246 repository: "missing/project",
247 });
248 }).pipe(Effect.flip);
249​
250 assert.strictEqual(error._tag, "GitLabCliCommandError");
251 assert.strictEqual(error.cause, cause);
252 }),
253 );
254​
255 it.effect("preserves rate-limit failures as a distinct error", () =>
256 Effect.gen(function* () {
257 const cause = new VcsProcessExitError({
258 operation: "GitLabCli.execute",
259 command: "glab",
260 cwd: "/repo",
261 exitCode: 1,
262 detail: "API rate limit exceeded.",
263 failureKind: "rate-limited",
264 });
265 mockedRun.mockReturnValueOnce(Effect.fail(cause));
266​
267 const glab = yield* GitLabCli.GitLabCli;
268 const error = yield* glab
269 .execute({ cwd: "/repo", args: ["api", "projects"] })
270 .pipe(Effect.flip);
271​
272 assert.strictEqual(error._tag, "GitLabCliRateLimitError");
273 assert.strictEqual(error.cause, cause);
274 }),
275 );
276});