Open History to see the last commit
1import { 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 const readWith = (divergence: { readonly divergedCommits?: number }) =>
69 Effect.gen(function* () {
70 const provider = yield* make;
71 return yield* provider.getChangeRequest({
72 cwd: "/w",
73 repository: "acme/web",
74 host: "gitlab.com",
75 number: 7,
76 });
77 }).pipe(
78 Effect.provide(
79 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
80 getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
81 getProjectMergeCapabilities: () =>
82 Effect.succeed({ merge: true, squash: true, rebase: true }),
83 }),
84 ),
85 );
86
87 it.effect("reads a counted divergence as a branch that has fallen behind", () =>
88 Effect.gen(function* () {
89 const changeRequest = yield* readWith({ divergedCommits: 3 });
90
91 expect(changeRequest.baseComparison).toBe("behind");
92 expect(changeRequest.behindBy).toBe(3);
93 }),
94 );
95
96 it.effect("says nothing at all where GitLab counted nothing", () =>
97 Effect.gen(function* () {
98 const changeRequest = yield* readWith({});
99
100 expect(changeRequest.baseComparison).toBe("unknown");
101 expect(changeRequest.behindBy).toBeUndefined();
102 }),
103 );
104});
105
106describe("optional activity reads", () => {
107 const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 };
108 const comment = {
109 id: "42",
110 kind: "issue-comment" as const,
111 author: null,
112 body: "Keep this comment",
113 createdAt: "2026-07-01T00:00:00Z",
114 url: null,
115 path: null,
116 reviewState: null,
117 };
118 const commit = {
119 oid: "abc",
120 messageHeadline: "Keep this commit",
121 committedDate: "2026-07-01T00:00:00Z",
122 };
123 const reads = {
124 listNotes: () => Effect.succeed({ comments: [comment], truncated: false }),
125 listCommits: () => Effect.succeed([commit]),
126 listDiscussions: () => Effect.succeed({ threads: [], truncated: false }),
127 listReactions: () => Effect.succeed({ reactions: [], reactionsByNoteId: new Map() }),
128 };
129 const context = {
130 operation: "execute" as const,
131 command: "glab" as const,
132 cwd: "/w",
133 cause: null,
134 };
135
136 it.effect.each([
137 {
138 operation: "listNotes",
139 error: new GitLabCli.GitLabCliAuthenticationError(context),
140 reason: "unauthenticated",
141 },
142 {
143 operation: "listCommits",
144 error: new GitLabCli.GitLabCliUnavailableError(context),
145 reason: "missing-tool",
146 },
147 ] as const)(
148 "reports $reason instead of successful partial activity",
149 ({ operation, error, reason }) =>
150 Effect.gen(function* () {
151 const provider = yield* make.pipe(
152 Effect.provide(
153 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
154 ...reads,
155 [operation]: () => Effect.fail(error),
156 }),
157 ),
158 );
159
160 expect(yield* Effect.result(provider.getChangeRequestActivity(reference))).toMatchObject({
161 _tag: "Failure",
162 failure: {
163 _tag: "PullRequestProviderError",
164 provider: "gitlab",
165 operation: "getChangeRequestActivity",
166 reason,
167 detail: error.detail,
168 cause: error,
169 },
170 });
171 }),
172 );
173
174 it.effect("keeps comments and commits when optional discussions and reactions fail", () =>
175 Effect.gen(function* () {
176 const provider = yield* make.pipe(
177 Effect.provide(
178 Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
179 ...reads,
180 listDiscussions: () =>
181 Effect.fail(
182 new GitLabPullRequestCli.GitLabMergeRequestReadError({
183 command: "glab",
184 cwd: "/w",
185 operation: "listDiscussions",
186 cause: null,
187 }),
188 ),
189 listReactions: () => Effect.fail(new GitLabCli.GitLabCliCommandError(context)),
190 }),
191 ),
192 );
193
194 expect(yield* provider.getChangeRequestActivity(reference)).toEqual({
195 comments: [{ ...comment, reactions: [] }],
196 commentCount: 1,
197 commentsTruncated: true,
198 reviewThreads: [],
199 reactions: [],
200 commits: [commit],
201 });
202 }),
203 );
204});