Open History to see the last commit
1import { assert, describe, expect, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4
5import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
6import { gitLabViewerPermissions, make } from "./GitLabPullRequestProvider.ts";
7
8describe("gitLabViewerPermissions", () => {
9 it("offers everything to a viewer GitLab says can merge", () => {
10 expect(gitLabViewerPermissions({ viewerCanMerge: true })).toEqual({
11 actions: [
12 "merge",
13 "ready",
14 "draft",
15 "close",
16 "reopen",
17 "update-branch",
18 "enable-auto-merge",
19 "disable-auto-merge",
20 ],
21 comment: true,
22 resolve: true,
23 verdicts: ["comment", "approve"],
24 requestReviewers: true,
25 updateMethods: ["rebase"],
26 });
27 });
28
29 it("keeps merge, now and later, from a viewer GitLab says cannot", () => {
30 expect(gitLabViewerPermissions({ viewerCanMerge: false })).toEqual({
31 actions: ["ready", "draft", "close", "reopen"],
32 comment: true,
33 resolve: true,
34 verdicts: ["comment", "approve"],
35 requestReviewers: true,
36 });
37 });
38
39 it("names no way of updating a branch it will not let this viewer update", () => {
40 expect(gitLabViewerPermissions({ viewerCanMerge: false }).updateMethods).toBeUndefined();
41 });
42
43 it("treats an author with read access as any other reader, which is all GitLab says", () => {
44 expect(gitLabViewerPermissions({ viewerCanMerge: false }).actions).toEqual([
45 "ready",
46 "draft",
47 "close",
48 "reopen",
49 ]);
50 });
51});
52
53describe("getChangeRequest base freshness", () => {
54 const detail = {
55 number: 7,
56 title: "Merge request 7",
57 url: "https://gitlab.com/acme/web/-/merge_requests/7",
58 author: null,
59 headBranch: "feat/page",
60 baseBranch: "main",
61 state: "open" as const,
62 isDraft: false,
63 mergeability: "mergeable" as const,
64 additions: 0,
65 deletions: 0,
66 createdAt: "2026-07-01T00:00:00Z",
67 updatedAt: "2026-07-02T00:00:00Z",
68 reviewRequestLogins: [],
69 labels: [],
70 body: "",
71 changedFiles: 1,
72 mergedAt: null,
73 closedAt: null,
74 reviewers: [],
75 checks: [],
76 viewerCanMerge: true,
77 reviewerIds: [],
78 };
79
80 it.effect("reads checks without fetching project settings", () =>
81 Effect.gen(function* () {
82 const provider = yield* make.pipe(
83 Effect.provide(
84 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
85 getMergeRequestDetail: () => Effect.succeed(detail),
86 }),
87 ),
88 );
89 const read = provider.getChangeRequestChecks;
90 if (read === undefined) return yield* Effect.die("checks read missing");
91 expect(
92 yield* read({ cwd: "/w", repository: "group/subgroup/web", host: "gitlab.com", number: 7 }),
93 ).toEqual({ state: "open", checks: [] });
94 }),
95 );
96
97 const readWith = (divergence: { readonly divergedCommits?: number }) =>
98 Effect.gen(function* () {
99 const provider = yield* make;
100 return yield* provider.getChangeRequest({
101 cwd: "/w",
102 repository: "acme/web",
103 host: "gitlab.com",
104 number: 7,
105 });
106 }).pipe(
107 Effect.provide(
108 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
109 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
110 getProjectMergeCapabilities: () =>
111 Effect.succeed({ merge: true, squash: true, rebase: true }),
112 }),
113 ),
114 );
115
116 it.effect("reads a counted divergence as a branch that has fallen behind", () =>
117 Effect.gen(function* () {
118 const changeRequest = yield* readWith({ divergedCommits: 3 });
119
120 expect(changeRequest.baseComparison).toBe("behind");
121 expect(changeRequest.behindBy).toBe(3);
122 }),
123 );
124
125 it.effect("reads a divergence of none as a branch that is current", () =>
126 Effect.gen(function* () {
127 const changeRequest = yield* readWith({ divergedCommits: 0 });
128
129 expect(changeRequest.baseComparison).toBe("up-to-date");
130 expect(changeRequest.behindBy).toBe(0);
131 }),
132 );
133
134 it.effect("says nothing at all where GitLab counted nothing", () =>
135 Effect.gen(function* () {
136 const changeRequest = yield* readWith({});
137
138 expect(changeRequest.baseComparison).toBe("unknown");
139 expect(changeRequest.behindBy).toBeUndefined();
140 }),
141 );
142});
143
144describe("rewriting what has already been said", () => {
145 const updateMergeRequest = vi.fn(() => Effect.void);
146 const updateNote = vi.fn(() => Effect.void);
147
148 const providerWith = make.pipe(
149 Effect.provide(
150 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ updateMergeRequest, updateNote }),
151 ),
152 );
153
154 it.effect("sends only the half of the merge request the reader rewrote", () =>
155 Effect.gen(function* () {
156 const provider = yield* providerWith;
157 assert.isDefined(provider.updateChangeRequest);
158
159 yield* provider.updateChangeRequest({
160 cwd: "/w",
161 repository: "acme/web",
162 host: "gitlab.com",
163 number: 7,
164 body: "What this changes.",
165 });
166
167 expect(updateMergeRequest).toHaveBeenCalledWith({
168 cwd: "/w",
169 repository: "acme/web",
170 number: 7,
171 description: "What this changes.",
172 });
173 }),
174 );
175
176 it.effect("rewrites a positioned comment through the same note as any other", () =>
177 Effect.gen(function* () {
178 const provider = yield* providerWith;
179 assert.isDefined(provider.updateComment);
180
181 yield* provider.updateComment({
182 cwd: "/w",
183 repository: "acme/web",
184 host: "gitlab.com",
185 number: 7,
186 commentId: "42",
187 kind: "review-comment",
188 body: "Reworded.",
189 });
190
191 expect(updateNote).toHaveBeenCalledWith({
192 cwd: "/w",
193 repository: "acme/web",
194 number: 7,
195 noteId: "42",
196 body: "Reworded.",
197 });
198 }),
199 );
200});
201
202describe("expanding unchanged lines", () => {
203 const change = {
204 cwd: "/w",
205 repository: "acme/web",
206 host: "gitlab.com",
207 number: 7,
208 changeType: "change" as const,
209 oldPath: "src/page.ts",
210 newPath: "src/page.ts",
211 };
212
213 it.effect("reads both sides of a file through the merge request or one of its commits", () =>
214 Effect.gen(function* () {
215 const getMergeRequestDiffFileContents = vi.fn(() =>
216 Effect.succeed({ oldContents: "before\n", newContents: "after\n" }),
217 );
218 const provider = yield* make.pipe(
219 Effect.provide(
220 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
221 getMergeRequestDiffFileContents,
222 }),
223 ),
224 );
225 assert.isDefined(provider.getDiffFileContents);
226 const oneCommit = { ...change, commit: "a1b2c3d" };
227
228 expect(yield* provider.getDiffFileContents(change)).toEqual({
229 oldContents: "before\n",
230 newContents: "after\n",
231 });
232 yield* provider.getDiffFileContents(oneCommit);
233 expect(getMergeRequestDiffFileContents).toHaveBeenNthCalledWith(1, change);
234 expect(getMergeRequestDiffFileContents).toHaveBeenNthCalledWith(2, oneCommit);
235 }),
236 );
237
238 it.effect("reports a file GitLab cannot expand as a failed read", () =>
239 Effect.gen(function* () {
240 const provider = yield* make.pipe(
241 Effect.provide(
242 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
243 getMergeRequestDiffFileContents: () =>
244 Effect.fail(
245 new GitLabPullRequestCli.GitLabDiffFileContentsUnavailableError({
246 command: "glab",
247 cwd: "/w",
248 path: "assets/logo.png",
249 reason: "binary",
250 }),
251 ),
252 }),
253 ),
254 );
255 assert.isDefined(provider.getDiffFileContents);
256
257 expect(yield* Effect.flip(provider.getDiffFileContents(change))).toMatchObject({
258 _tag: "PullRequestProviderError",
259 provider: "gitlab",
260 operation: "getDiffFileContents",
261 reason: "failed",
262 detail: "The diff file 'assets/logo.png' is binary.",
263 });
264 }),
265 );
266});