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