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";
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
41describe("getChangeRequest base freshness", () => {
42 const detail = {
43 number: 7,
44 title: "Merge request 7",
45 url: "https://gitlab.com/acme/web/-/merge_requests/7",
46 author: null,
47 headBranch: "feat/page",
48 baseBranch: "main",
49 state: "open" as const,
50 isDraft: false,
51 mergeability: "mergeable" as const,
52 additions: 0,
53 deletions: 0,
54 createdAt: "2026-07-01T00:00:00Z",
55 updatedAt: "2026-07-02T00:00:00Z",
56 reviewRequestLogins: [],
57 labels: [],
58 body: "",
59 changedFiles: 1,
60 mergedAt: null,
61 closedAt: null,
62 reviewers: [],
63 checks: [],
64 viewerCanMerge: true,
65 reviewerIds: [],
66 };
67
68 it.effect.each(["getChangeRequestPreview", "getChangeRequestSummary"] as const)(
69 "%s preserves merge request fields without fetching project settings",
70 (operation) =>
71 Effect.gen(function* () {
72 const provider = yield* make.pipe(
73 Effect.provide(
74 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
75 getMergeRequestDetail: () => Effect.succeed(detail),
76 }),
77 ),
78 );
79 const read = provider[operation];
80 assert.isDefined(read);
81 expect(
82 yield* read({ cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }),
83 ).toEqual(detail);
84 }),
85 );
86
87 const readWith = (divergence: { readonly divergedCommits?: number }) =>
88 Effect.gen(function* () {
89 const provider = yield* make;
90 return yield* provider.getChangeRequest({
91 cwd: "/w",
92 repository: "acme/web",
93 host: "gitlab.com",
94 number: 7,
95 });
96 }).pipe(
97 Effect.provide(
98 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
99 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
100 getProjectMergeCapabilities: () =>
101 Effect.succeed({ merge: true, squash: true, rebase: true }),
102 }),
103 ),
104 );
105
106 it.effect("reads a counted divergence as a branch that has fallen behind", () =>
107 Effect.gen(function* () {
108 const changeRequest = yield* readWith({ divergedCommits: 3 });
109
110 expect(changeRequest.baseComparison).toBe("behind");
111 expect(changeRequest.behindBy).toBe(3);
112 }),
113 );
114
115 it.effect("says nothing at all where GitLab counted nothing", () =>
116 Effect.gen(function* () {
117 const changeRequest = yield* readWith({});
118
119 expect(changeRequest.baseComparison).toBe("unknown");
120 expect(changeRequest.behindBy).toBeUndefined();
121 }),
122 );
123});
124
125describe("optional activity reads", () => {
126 const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
127 const comment = {
128 id: "42",
129 kind: "issue-comment" as const,
130 author: null,
131 body: "Keep this comment",
132 createdAt: "2026-07-01T00:00:00Z",
133 url: null,
134 path: null,
135 reviewState: null,
136 };
137 const commit = {
138 oid: "abc",
139 messageHeadline: "Keep this commit",
140 committedDate: "2026-07-01T00:00:00Z",
141 };
142 const reads = {
143 listNotes: () => Effect.succeed({ comments: [comment], truncated: false }),
144 listCommits: () => Effect.succeed([commit]),
145 listDiscussions: () => Effect.succeed({ threads: [], truncated: false }),
146 listReactions: () => Effect.succeed({ reactions: [], reactionsByNoteId: new Map() }),
147 };
148 const context = {
149 operation: "execute" as const,
150 command: "glab" as const,
151 cwd: "/w",
152 cause: null,
153 };
154
155 it.effect.each([
156 {
157 operation: "listNotes",
158 error: new GitLabCli.GitLabCliAuthenticationError(context),
159 reason: "unauthenticated",
160 },
161 {
162 operation: "listCommits",
163 error: new GitLabCli.GitLabCliUnavailableError(context),
164 reason: "missing-tool",
165 },
166 ] as const)(
167 "reports $reason instead of successful partial activity",
168 ({ operation, error, reason }) =>
169 Effect.gen(function* () {
170 const provider = yield* make.pipe(
171 Effect.provide(
172 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
173 ...reads,
174 [operation]: () => Effect.fail(error),
175 }),
176 ),
177 );
178
179 expect(yield* Effect.result(provider.getChangeRequestActivity(reference))).toMatchObject({
180 _tag: "Failure",
181 failure: {
182 _tag: "PullRequestProviderError",
183 provider: "gitlab",
184 operation: "getChangeRequestActivity",
185 reason,
186 detail: error.detail,
187 cause: error,
188 },
189 });
190 }),
191 );
192
193 it.effect("keeps comments and commits when optional discussions and reactions fail", () =>
194 Effect.gen(function* () {
195 const provider = yield* make.pipe(
196 Effect.provide(
197 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
198 ...reads,
199 listDiscussions: () =>
200 Effect.fail(
201 new GitLabPullRequestCli.GitLabMergeRequestReadError({
202 command: "glab",
203 cwd: "/w",
204 operation: "listDiscussions",
205 cause: null,
206 }),
207 ),
208 listReactions: () => Effect.fail(new GitLabCli.GitLabCliCommandError(context)),
209 }),
210 ),
211 );
212
213 expect(yield* provider.getChangeRequestActivity(reference)).toEqual({
214 comments: [{ ...comment, reactions: [] }],
215 commentCount: 1,
216 commentsTruncated: true,
217 reviewThreads: [],
218 reactions: [],
219 commits: [commit],
220 });
221 }),
222 );
223});