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

GitLabPullRequestProvider.test.ts

367 lines11.0 KB

1import { assert, describe, expect, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import * as GitLabCli from "./GitLabCli.ts";
6import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
7import { gitLabViewerPermissions, make } from "./GitLabPullRequestProvider.ts";
8​
9describe("gitLabViewerPermissions", () => {
10 it("offers everything to a viewer GitLab says can merge", () => {
11 expect(gitLabViewerPermissions({ viewerCanMerge: true })).toEqual({
12 actions: [
13 "merge",
14 "ready",
15 "draft",
16 "close",
17 "reopen",
18 "update-branch",
19 "enable-auto-merge",
20 "disable-auto-merge",
21 ],
22 comment: true,
23 resolve: true,
24 verdicts: ["comment", "approve"],
25 requestReviewers: true,
26 updateMethods: ["rebase"],
27 });
28 });
29​
30 it("keeps merge, now and later, from a viewer GitLab says cannot", () => {
31 expect(gitLabViewerPermissions({ viewerCanMerge: false })).toEqual({
32 actions: ["ready", "draft", "close", "reopen"],
33 comment: true,
34 resolve: true,
35 verdicts: ["comment", "approve"],
36 requestReviewers: true,
37 });
38 });
39​
40 it("names no way of updating a branch it will not let this viewer update", () => {
41 expect(gitLabViewerPermissions({ viewerCanMerge: false }).updateMethods).toBeUndefined();
42 });
43​
44 it("treats an author with read access as any other reader, which is all GitLab says", () => {
45 expect(gitLabViewerPermissions({ viewerCanMerge: false }).actions).toEqual([
46 "ready",
47 "draft",
48 "close",
49 "reopen",
50 ]);
51 });
52});
53​
54describe("getChangeRequest base freshness", () => {
55 const detail = {
56 number: 7,
57 title: "Merge request 7",
58 url: "https://gitlab.com/acme/web/-/merge_requests/7",
59 author: null,
60 headBranch: "feat/page",
61 baseBranch: "main",
62 state: "open" as const,
63 isDraft: false,
64 mergeability: "mergeable" as const,
65 additions: 0,
66 deletions: 0,
67 createdAt: "2026-07-01T00:00:00Z",
68 updatedAt: "2026-07-02T00:00:00Z",
69 reviewRequestLogins: [],
70 labels: [],
71 body: "",
72 changedFiles: 1,
73 mergedAt: null,
74 closedAt: null,
75 reviewers: [],
76 checks: [],
77 viewerCanMerge: true,
78 reviewerIds: [],
79 };
80​
81 it.effect("reads checks without fetching project settings", () =>
82 Effect.gen(function* () {
83 const provider = yield* make.pipe(
84 Effect.provide(
85 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
86 getMergeRequestDetail: () => Effect.succeed(detail),
87 }),
88 ),
89 );
90 const read = provider.getChangeRequestChecks;
91 if (read === undefined) return yield* Effect.die("checks read missing");
92 expect(
93 yield* read({ cwd: "/w", repository: "group/subgroup/web", host: "gitlab.com", number: 7 }),
94 ).toEqual({ state: "open", checks: [] });
95 }),
96 );
97​
98 const readWith = (divergence: { readonly divergedCommits?: number }) =>
99 Effect.gen(function* () {
100 const provider = yield* make;
101 return yield* provider.getChangeRequest({
102 cwd: "/w",
103 repository: "acme/web",
104 host: "gitlab.com",
105 number: 7,
106 });
107 }).pipe(
108 Effect.provide(
109 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
110 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
111 getProjectMergeCapabilities: () =>
112 Effect.succeed({ merge: true, squash: true, rebase: true }),
113 }),
114 ),
115 );
116​
117 it.effect("reads a counted divergence as a branch that has fallen behind", () =>
118 Effect.gen(function* () {
119 const changeRequest = yield* readWith({ divergedCommits: 3 });
120​
121 expect(changeRequest.baseComparison).toBe("behind");
122 expect(changeRequest.behindBy).toBe(3);
123 }),
124 );
125​
126 it.effect("reads a divergence of none as a branch that is current", () =>
127 Effect.gen(function* () {
128 const changeRequest = yield* readWith({ divergedCommits: 0 });
129​
130 expect(changeRequest.baseComparison).toBe("up-to-date");
131 expect(changeRequest.behindBy).toBe(0);
132 }),
133 );
134​
135 it.effect("says nothing at all where GitLab counted nothing", () =>
136 Effect.gen(function* () {
137 const changeRequest = yield* readWith({});
138​
139 expect(changeRequest.baseComparison).toBe("unknown");
140 expect(changeRequest.behindBy).toBeUndefined();
141 }),
142 );
143});
144​
145describe("rewriting what has already been said", () => {
146 const updateMergeRequest = vi.fn(() => Effect.void);
147 const updateNote = vi.fn(() => Effect.void);
148​
149 const providerWith = make.pipe(
150 Effect.provide(
151 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ updateMergeRequest, updateNote }),
152 ),
153 );
154​
155 it.effect("sends only the half of the merge request the reader rewrote", () =>
156 Effect.gen(function* () {
157 const provider = yield* providerWith;
158 assert.isDefined(provider.updateChangeRequest);
159​
160 yield* provider.updateChangeRequest({
161 cwd: "/w",
162 repository: "acme/web",
163 host: "gitlab.com",
164 number: 7,
165 body: "What this changes.",
166 });
167​
168 expect(updateMergeRequest).toHaveBeenCalledWith({
169 cwd: "/w",
170 repository: "acme/web",
171 number: 7,
172 description: "What this changes.",
173 });
174 }),
175 );
176​
177 it.effect("rewrites a positioned comment through the same note as any other", () =>
178 Effect.gen(function* () {
179 const provider = yield* providerWith;
180 assert.isDefined(provider.updateComment);
181​
182 yield* provider.updateComment({
183 cwd: "/w",
184 repository: "acme/web",
185 host: "gitlab.com",
186 number: 7,
187 commentId: "42",
188 kind: "review-comment",
189 body: "Reworded.",
190 });
191​
192 expect(updateNote).toHaveBeenCalledWith({
193 cwd: "/w",
194 repository: "acme/web",
195 number: 7,
196 noteId: "42",
197 body: "Reworded.",
198 });
199 }),
200 );
201});
202​
203describe("expanding unchanged lines", () => {
204 const change = {
205 cwd: "/w",
206 repository: "acme/web",
207 host: "gitlab.com",
208 number: 7,
209 changeType: "change" as const,
210 oldPath: "src/page.ts",
211 newPath: "src/page.ts",
212 };
213​
214 it.effect("reads both sides of a file through the merge request or one of its commits", () =>
215 Effect.gen(function* () {
216 const getMergeRequestDiffFileContents = vi.fn(() =>
217 Effect.succeed({ oldContents: "before\n", newContents: "after\n" }),
218 );
219 const provider = yield* make.pipe(
220 Effect.provide(
221 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
222 getMergeRequestDiffFileContents,
223 }),
224 ),
225 );
226 assert.isDefined(provider.getDiffFileContents);
227 const oneCommit = { ...change, commit: "a1b2c3d" };
228​
229 expect(yield* provider.getDiffFileContents(change)).toEqual({
230 oldContents: "before\n",
231 newContents: "after\n",
232 });
233 yield* provider.getDiffFileContents(oneCommit);
234 expect(getMergeRequestDiffFileContents).toHaveBeenNthCalledWith(1, change);
235 expect(getMergeRequestDiffFileContents).toHaveBeenNthCalledWith(2, oneCommit);
236 }),
237 );
238​
239 it.effect("reports a file GitLab cannot expand as a failed read", () =>
240 Effect.gen(function* () {
241 const provider = yield* make.pipe(
242 Effect.provide(
243 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
244 getMergeRequestDiffFileContents: () =>
245 Effect.fail(
246 new GitLabPullRequestCli.GitLabDiffFileContentsUnavailableError({
247 command: "glab",
248 cwd: "/w",
249 path: "assets/logo.png",
250 reason: "binary",
251 }),
252 ),
253 }),
254 ),
255 );
256 assert.isDefined(provider.getDiffFileContents);
257​
258 expect(yield* Effect.flip(provider.getDiffFileContents(change))).toMatchObject({
259 _tag: "PullRequestProviderError",
260 provider: "gitlab",
261 operation: "getDiffFileContents",
262 reason: "failed",
263 detail: "The diff file 'assets/logo.png' is binary.",
264 });
265 }),
266 );
267});
268​
269describe("optional activity reads", () => {
270 const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
271 const comment = {
272 id: "42",
273 kind: "issue-comment" as const,
274 author: null,
275 body: "Keep this comment",
276 createdAt: "2026-07-01T00:00:00Z",
277 url: null,
278 path: null,
279 reviewState: null,
280 };
281 const commit = {
282 oid: "abc",
283 messageHeadline: "Keep this commit",
284 committedDate: "2026-07-01T00:00:00Z",
285 };
286 const reads = {
287 listNotes: () => Effect.succeed({ comments: [comment], truncated: false }),
288 listCommits: () => Effect.succeed([commit]),
289 listDiscussions: () => Effect.succeed({ threads: [], truncated: false }),
290 listReactions: () => Effect.succeed({ reactions: [], reactionsByNoteId: new Map() }),
291 };
292 const context = {
293 operation: "execute" as const,
294 command: "glab" as const,
295 cwd: "/w",
296 cause: null,
297 };
298​
299 it.effect.each([
300 {
301 operation: "listNotes",
302 error: new GitLabCli.GitLabCliAuthenticationError(context),
303 reason: "unauthenticated",
304 },
305 {
306 operation: "listCommits",
307 error: new GitLabCli.GitLabCliUnavailableError(context),
308 reason: "missing-tool",
309 },
310 ] as const)(
311 "reports $reason instead of successful partial activity",
312 ({ operation, error, reason }) =>
313 Effect.gen(function* () {
314 const provider = yield* make.pipe(
315 Effect.provide(
316 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
317 ...reads,
318 [operation]: () => Effect.fail(error),
319 }),
320 ),
321 );
322​
323 expect(yield* Effect.result(provider.getChangeRequestActivity(reference))).toMatchObject({
324 _tag: "Failure",
325 failure: {
326 _tag: "PullRequestProviderError",
327 provider: "gitlab",
328 operation: "getChangeRequestActivity",
329 reason,
330 detail: error.detail,
331 cause: error,
332 },
333 });
334 }),
335 );
336​
337 it.effect("keeps comments and commits when optional discussions and reactions fail", () =>
338 Effect.gen(function* () {
339 const provider = yield* make.pipe(
340 Effect.provide(
341 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
342 ...reads,
343 listDiscussions: () =>
344 Effect.fail(
345 new GitLabPullRequestCli.GitLabMergeRequestReadError({
346 command: "glab",
347 cwd: "/w",
348 operation: "listDiscussions",
349 cause: null,
350 }),
351 ),
352 listReactions: () => Effect.fail(new GitLabCli.GitLabCliCommandError(context)),
353 }),
354 ),
355 );
356​
357 expect(yield* provider.getChangeRequestActivity(reference)).toEqual({
358 comments: [{ ...comment, reactions: [] }],
359 commentCount: 1,
360 commentsTruncated: true,
361 reviewThreads: [],
362 reactions: [],
363 commits: [commit],
364 });
365 }),
366 );
367});