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 // Arming a merge for later and taking the arming back answer to the same `can_merge`.
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 // GitLab says nothing about who may set a reviewer, and an unreported permission is granted.
26 requestReviewers: true,
27 // Rebase and nothing else: GitLab cannot merge a target branch into a source branch, so
28 // offering the choice would be offering something no request could carry out.
29 updateMethods: ["rebase"],
30 });
31 });
32
33 it("keeps merge, now and later, from a viewer GitLab says cannot", () => {
34 // `user.can_merge` already accounts for the role, the approval rules and a protected target
35 // branch, so it is the one answer here that does not have to be inferred.
36 expect(gitLabViewerPermissions({ viewerCanMerge: false })).toEqual({
37 actions: ["ready", "draft", "close", "reopen"],
38 comment: true,
39 resolve: true,
40 verdicts: ["comment", "approve"],
41 requestReviewers: true,
42 });
43 });
44
45 it("names no way of updating a branch it will not let this viewer update", () => {
46 // The action and the strategy behind it go together: a button offered with nothing to press
47 // it with, or a strategy left standing next to a withheld button, is a half-refusal.
48 expect(gitLabViewerPermissions({ viewerCanMerge: false }).updateMethods).toBeUndefined();
49 });
50
51 it("treats an author with read access as any other reader, which is all GitLab says", () => {
52 // Its REST API names no relationship between the viewer and the merge request beyond
53 // `can_merge`, so the four an author keeps stay offered to everyone rather than being taken
54 // from the one person entitled to them.
55 expect(gitLabViewerPermissions({ viewerCanMerge: false }).actions).toEqual([
56 "ready",
57 "draft",
58 "close",
59 "reopen",
60 ]);
61 });
62});
63
64describe("getChangeRequest base freshness", () => {
65 const detail = {
66 number: 7,
67 title: "Merge request 7",
68 url: "https://gitlab.com/acme/web/-/merge_requests/7",
69 author: null,
70 headBranch: "feat/page",
71 baseBranch: "main",
72 state: "open" as const,
73 isDraft: false,
74 mergeability: "mergeable" as const,
75 additions: 0,
76 deletions: 0,
77 createdAt: "2026-07-01T00:00:00Z",
78 updatedAt: "2026-07-02T00:00:00Z",
79 reviewRequestLogins: [],
80 labels: [],
81 body: "",
82 changedFiles: 1,
83 mergedAt: null,
84 closedAt: null,
85 reviewers: [],
86 checks: [],
87 viewerCanMerge: true,
88 reviewerIds: [],
89 };
90
91 const readWith = (divergence: { readonly divergedCommits?: number }) =>
92 Effect.gen(function* () {
93 const provider = yield* make;
94 return yield* provider.getChangeRequest({
95 cwd: "/w",
96 repository: "acme/web",
97 host: "gitlab.com",
98 number: 7,
99 });
100 }).pipe(
101 Effect.provide(
102 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
103 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
104 getProjectMergeCapabilities: () =>
105 Effect.succeed({ merge: true, squash: true, rebase: true }),
106 }),
107 ),
108 );
109
110 it.effect("reads a counted divergence as a branch that has fallen behind", () =>
111 Effect.gen(function* () {
112 const changeRequest = yield* readWith({ divergedCommits: 3 });
113
114 expect(changeRequest.baseComparison).toBe("behind");
115 expect(changeRequest.behindBy).toBe(3);
116 }),
117 );
118
119 it.effect("reads a divergence of none as a branch that is current", () =>
120 Effect.gen(function* () {
121 const changeRequest = yield* readWith({ divergedCommits: 0 });
122
123 expect(changeRequest.baseComparison).toBe("up-to-date");
124 expect(changeRequest.behindBy).toBe(0);
125 }),
126 );
127
128 it.effect("says nothing at all where GitLab counted nothing", () =>
129 Effect.gen(function* () {
130 // An install too old to answer has to leave the page silent rather than let it claim the
131 // branch is current, which is the one wrong thing this banner could say.
132 const changeRequest = yield* readWith({});
133
134 expect(changeRequest.baseComparison).toBe("unknown");
135 expect(changeRequest.behindBy).toBeUndefined();
136 }),
137 );
138});
139
140describe("rewriting what has already been said", () => {
141 const updateMergeRequest = vi.fn(() => Effect.void);
142 const updateNote = vi.fn(() => Effect.void);
143
144 const providerWith = make.pipe(
145 Effect.provide(
146 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ updateMergeRequest, updateNote }),
147 ),
148 );
149
150 it.effect("sends only the half of the merge request the reader rewrote", () =>
151 Effect.gen(function* () {
152 const provider = yield* providerWith;
153 assert.isDefined(provider.updateChangeRequest);
154
155 yield* provider.updateChangeRequest({
156 cwd: "/w",
157 repository: "acme/web",
158 host: "gitlab.com",
159 number: 7,
160 body: "What this changes.",
161 });
162
163 // GitLab calls it the description, and the title stays out of the request entirely.
164 expect(updateMergeRequest).toHaveBeenCalledWith({
165 cwd: "/w",
166 repository: "acme/web",
167 number: 7,
168 description: "What this changes.",
169 });
170 }),
171 );
172
173 it.effect("rewrites a positioned comment through the same note as any other", () =>
174 Effect.gen(function* () {
175 const provider = yield* providerWith;
176 assert.isDefined(provider.updateComment);
177
178 yield* provider.updateComment({
179 cwd: "/w",
180 repository: "acme/web",
181 host: "gitlab.com",
182 number: 7,
183 commentId: "42",
184 kind: "review-comment",
185 body: "Reworded.",
186 });
187
188 expect(updateNote).toHaveBeenCalledWith({
189 cwd: "/w",
190 repository: "acme/web",
191 number: 7,
192 noteId: "42",
193 body: "Reworded.",
194 });
195 }),
196 );
197});