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 "../sourceControl/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
15for (const operation of [
16 "getMergeability",
17 "listChecks",
18 "getRepositoryPermission",
19 "listComments",
20 "listCommits",
21] as const) {
22 it.effect.each(["response", "body read"])(
23 `preserves rate limits from ${operation} on %s errors while recovering other optional-read failures`,
24 (variant) =>
25 Effect.gen(function* () {
26 const pullRequest = Result.getOrThrow(
27 decodePullRequestJson(`{
28 "id": 1, "title": "Check polling", "state": "OPEN",
29 "source": { "branch": { "name": "feature" } },
30 "destination": { "branch": { "name": "main" } },
31 "created_on": "2026-09-16T00:00:00Z",
32 "updated_on": "2026-09-16T00:00:00Z",
33 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
34 }`),
35 );
36 for (const status of [429, 403]) {
37 const provider = yield* make.pipe(
38 Effect.provide(
39 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
40 getPullRequest: () => Effect.succeed(pullRequest),
41 getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
42 getMergeability: () => Effect.succeed("unknown" as const),
43 listChecks: () => Effect.succeed([]),
44 getRepositoryPermission: () => Effect.succeed(true),
45 listComments: () => Effect.succeed({ comments: [], threads: [], truncated: false }),
46 listCommits: () => Effect.succeed([]),
47 [operation]: () =>
48 Effect.fail(
49 variant === "response"
50 ? new BitbucketApi.BitbucketResponseError({
51 operation: "request",
52 status,
53 responseBodyLength: 0,
54 retryAt: 120_000,
55 })
56 : new BitbucketApi.BitbucketResponseBodyReadError({
57 operation: "request",
58 status,
59 cause: new Error("response stream failed"),
60 retryAt: 120_000,
61 }),
62 ),
63 }),
64 ),
65 );
66 const reference = {
67 cwd: "/repo",
68 repository: "acme/web",
69 number: 1,
70 host: "bitbucket.org",
71 };
72 const result = yield* operation === "listComments" || operation === "listCommits"
73 ? Effect.result(provider.getChangeRequestActivity(reference))
74 : Effect.result(provider.getChangeRequest(reference));
75 if (status === 429) {
76 expect(result).toMatchObject({
77 _tag: "Failure",
78 failure: { reason: "rate-limited", retryAt: 120_000 },
79 });
80 } else {
81 expect(result._tag).toBe("Success");
82 }
83 }
84 }),
85 );
86}
87
88describe("bitbucketProviderFailure", () => {
89 it("treats only an HTTP 401 as unusable credentials", () => {
90 const responseError = (status: number) =>
91 new BitbucketApi.BitbucketResponseError({
92 operation: "request",
93 status,
94 responseBodyLength: 0,
95 });
96
97 expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
98 expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
99 });
100});
101
102describe("bitbucketViewerPermissions", () => {
103 it("offers both actions to credentials with write access", () => {
104 expect(bitbucketViewerPermissions({ canWrite: true })).toEqual({
105 actions: ["merge", "close"],
106 comment: true,
107 resolve: true,
108 verdicts: ["comment", "approve", "request-changes"],
109 // Bitbucket says nothing about who may set a reviewer, and an unreported permission is
110 // granted.
111 requestReviewers: true,
112 });
113 });
114
115 it("keeps merge from credentials that can only read the repository", () => {
116 expect(bitbucketViewerPermissions({ canWrite: false })).toEqual({
117 actions: ["close"],
118 comment: true,
119 resolve: true,
120 verdicts: ["comment", "approve", "request-changes"],
121 requestReviewers: true,
122 });
123 });
124
125 it("treats an author with read access as any other reader, which is all Bitbucket says", () => {
126 // The repository permission is the whole of what Bitbucket reports per account; it says
127 // nothing about who opened this pull request, and its author may decline it with read access
128 // alone — so declining stays offered rather than being taken from them.
129 expect(bitbucketViewerPermissions({ canWrite: false }).actions).toEqual(["close"]);
130 });
131});