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";
4import * as Result from "effect/Result";
5
6import * as BitbucketApi from "./BitbucketApi.ts";
7import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
8import { decodePullRequestJson } from "./bitbucketPullRequestJson.ts";
9import {
10 bitbucketProviderFailure,
11 bitbucketViewerPermissions,
12 make,
13} from "./BitbucketPullRequestProvider.ts";
14
15it.effect.each([
16 {
17 metadata: "source commit hash",
18 commit: { hash: "9f3a7c2d8b1e6f045a9c3d7e2b8f1a6c0d4e5f93" },
19 headSha: "9f3a7c2d8b1e6f045a9c3d7e2b8f1a6c0d4e5f93",
20 },
21 {
22 metadata: "short source commit hash",
23 commit: { hash: "9f3a7c2d8b1e" },
24 headSha: "9f3a7c2d8b1e",
25 },
26 { metadata: "absent commit", commit: undefined, headSha: undefined },
27 { metadata: "null commit", commit: null, headSha: undefined },
28 { metadata: "absent hash", commit: {}, headSha: undefined },
29 { metadata: "null hash", commit: { hash: null }, headSha: undefined },
30])("reads change request details with $metadata", ({ commit, headSha }) =>
31 Effect.gen(function* () {
32 const pullRequest = Result.getOrThrow(
33 decodePullRequestJson(
34 JSON.stringify({
35 id: 897,
36 title: "Add trustabl-pipe",
37 state: "OPEN",
38 source: {
39 branch: { name: "feat/page" },
40 commit,
41 repository: { full_name: "fork/web" },
42 },
43 destination: { branch: { name: "master" }, commit: { hash: "base-commit" } },
44 created_on: "2026-06-16T05:04:32.258456+00:00",
45 updated_on: "2026-06-16T05:04:33.750542+00:00",
46 links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/897" } },
47 }),
48 ),
49 );
50 const provider = yield* make.pipe(
51 Effect.provide(
52 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
53 getPullRequest: () => Effect.succeed(pullRequest),
54 getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
55 getMergeability: () => Effect.succeed("unknown"),
56 listChecks: () => Effect.succeed([]),
57 getRepositoryPermission: () => Effect.succeed(true),
58 }),
59 ),
60 );
61 const detail = yield* provider.getChangeRequest({
62 cwd: "/repo",
63 repository: "acme/web",
64 host: "bitbucket.org",
65 number: 897,
66 });
67
68 expect(detail).toMatchObject({ number: 897, headBranch: "feat/page" });
69 expect(detail.headSha).toBe(headSha);
70 }),
71);
72
73it.effect.each([
74 { state: "MERGED", mergedAt: "2025-06-16T23:40:46.222Z", closedAt: null },
75 { state: "DECLINED", mergedAt: null, closedAt: "2025-06-16T23:40:46.222Z" },
76 { state: "OPEN", mergedAt: null, closedAt: null },
77])("reads the $state time from closed_on", ({ state, mergedAt, closedAt }) =>
78 Effect.gen(function* () {
79 const pullRequest = Result.getOrThrow(
80 decodePullRequestJson(
81 JSON.stringify({
82 id: 52,
83 title: "Release",
84 state,
85 source: { branch: { name: "release" } },
86 destination: { branch: { name: "master" } },
87 created_on: "2025-06-16T06:20:58.415108+00:00",
88 // Comments after a merge move this, so it is not the merge time.
89 updated_on: "2025-06-20T10:00:00.000000+00:00",
90 closed_on: state === "OPEN" ? null : "2025-06-16T23:40:46.222238+00:00",
91 links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/52" } },
92 }),
93 ),
94 );
95 const provider = yield* make.pipe(
96 Effect.provide(
97 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
98 getPullRequest: () => Effect.succeed(pullRequest),
99 getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
100 getMergeability: () => Effect.succeed("unknown"),
101 listChecks: () => Effect.succeed([]),
102 getRepositoryPermission: () => Effect.succeed(true),
103 }),
104 ),
105 );
106 const detail = yield* provider.getChangeRequest({
107 cwd: "/repo",
108 repository: "acme/web",
109 host: "bitbucket.org",
110 number: 52,
111 });
112
113 expect({ mergedAt: detail.mergedAt, closedAt: detail.closedAt }).toEqual({
114 mergedAt,
115 closedAt,
116 });
117 }),
118);
119
120for (const operation of [
121 "getMergeability",
122 "listChecks",
123 "getRepositoryPermission",
124 "listComments",
125 "listCommits",
126] as const) {
127 it.effect.each(["response", "body read"])(
128 `preserves rate limits from ${operation} on %s errors while recovering other optional-read failures`,
129 (variant) =>
130 Effect.gen(function* () {
131 const pullRequest = Result.getOrThrow(
132 decodePullRequestJson(`{
133 "id": 1, "title": "Check polling", "state": "OPEN",
134 "source": { "branch": { "name": "feature" } },
135 "destination": { "branch": { "name": "main" } },
136 "created_on": "2026-09-16T00:00:00Z",
137 "updated_on": "2026-09-16T00:00:00Z",
138 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
139 }`),
140 );
141 for (const status of [429, 403]) {
142 const provider = yield* make.pipe(
143 Effect.provide(
144 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
145 getPullRequest: () => Effect.succeed(pullRequest),
146 getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
147 getMergeability: () => Effect.succeed("unknown" as const),
148 listChecks: () => Effect.succeed([]),
149 getRepositoryPermission: () => Effect.succeed(true),
150 listComments: () => Effect.succeed({ comments: [], threads: [], truncated: false }),
151 listCommits: () => Effect.succeed([]),
152 [operation]: () =>
153 Effect.fail(
154 variant === "response"
155 ? new BitbucketApi.BitbucketResponseError({
156 operation: "request",
157 status,
158 responseBodyLength: 0,
159 retryAt: 120_000,
160 })
161 : new BitbucketApi.BitbucketResponseBodyReadError({
162 operation: "request",
163 status,
164 cause: new Error("response stream failed"),
165 retryAt: 120_000,
166 }),
167 ),
168 }),
169 ),
170 );
171 const reference = {
172 cwd: "/repo",
173 repository: "acme/web",
174 number: 1,
175 host: "bitbucket.org",
176 };
177 const result = yield* operation === "listComments" || operation === "listCommits"
178 ? Effect.result(provider.getChangeRequestActivity(reference))
179 : Effect.result(provider.getChangeRequest(reference));
180 if (status === 429) {
181 expect(result).toMatchObject({
182 _tag: "Failure",
183 failure: { reason: "rate-limited", retryAt: 120_000 },
184 });
185 } else {
186 expect(result._tag).toBe("Success");
187 }
188 }
189 }),
190 );
191}
192
193it.effect("reads checks and PR state without diff, mergeability, or permission requests", () =>
194 Effect.gen(function* () {
195 const pullRequest = Result.getOrThrow(
196 decodePullRequestJson(`{
197 "id": 1, "title": "Checks", "state": "OPEN",
198 "source": { "branch": { "name": "feature" } },
199 "destination": { "branch": { "name": "main" } },
200 "created_on": "2026-09-16T00:00:00Z", "updated_on": "2026-09-16T00:00:00Z",
201 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
202 }`),
203 );
204 let limited = false;
205 const provider = yield* make.pipe(
206 Effect.provide(
207 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
208 getPullRequest: () => Effect.succeed(pullRequest),
209 listChecks: () =>
210 limited
211 ? Effect.fail(
212 new BitbucketApi.BitbucketResponseError({
213 operation: "request",
214 status: 429,
215 responseBodyLength: 0,
216 retryAt: 120_000,
217 }),
218 )
219 : Effect.succeed([
220 { name: "build", status: "failure" as const, description: null, url: null },
221 ]),
222 }),
223 ),
224 );
225 const read = provider.getChangeRequestChecks;
226 if (read === undefined) return yield* Effect.die("checks read missing");
227 const input = { cwd: "/repo", repository: "acme/web", host: "bitbucket.org", number: 1 };
228 expect((yield* read(input)).checks[0]?.status).toBe("failure");
229 limited = true;
230 expect(yield* Effect.flip(read(input))).toMatchObject({
231 reason: "rate-limited",
232 retryAt: 120_000,
233 });
234 }),
235);
236
237describe("bitbucketProviderFailure", () => {
238 it("treats only an HTTP 401 as unusable credentials", () => {
239 const responseError = (status: number) =>
240 new BitbucketApi.BitbucketResponseError({
241 operation: "request",
242 status,
243 responseBodyLength: 0,
244 });
245
246 expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
247 expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
248 });
249});
250
251describe("bitbucketViewerPermissions", () => {
252 it("offers both actions to credentials with write access", () => {
253 expect(bitbucketViewerPermissions({ canWrite: true })).toEqual({
254 actions: ["merge", "close"],
255 comment: true,
256 resolve: true,
257 verdicts: ["comment", "approve", "request-changes"],
258 requestReviewers: true,
259 });
260 });
261
262 it("keeps merge from credentials that can only read the repository", () => {
263 expect(bitbucketViewerPermissions({ canWrite: false })).toEqual({
264 actions: ["close"],
265 comment: true,
266 resolve: true,
267 verdicts: ["comment", "approve", "request-changes"],
268 requestReviewers: true,
269 });
270 });
271
272 it("treats an author with read access as any other reader, which is all Bitbucket says", () => {
273 expect(bitbucketViewerPermissions({ canWrite: false }).actions).toEqual(["close"]);
274 });
275});