Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BitbucketPullRequestProvider.test.ts

175 lines6.8 KB

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 );
87​
88it.effect("reads checks and PR state without diff, mergeability, or permission requests", () =>
89 Effect.gen(function* () {
90 const pullRequest = Result.getOrThrow(
91 decodePullRequestJson(`{
92 "id": 1, "title": "Checks", "state": "OPEN",
93 "source": { "branch": { "name": "feature" } },
94 "destination": { "branch": { "name": "main" } },
95 "created_on": "2026-09-16T00:00:00Z", "updated_on": "2026-09-16T00:00:00Z",
96 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
97 }`),
98 );
99 let limited = false;
100 const provider = yield* make.pipe(
101 Effect.provide(
102 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
103 getPullRequest: () => Effect.succeed(pullRequest),
104 listChecks: () =>
105 limited
106 ? Effect.fail(
107 new BitbucketApi.BitbucketResponseError({
108 operation: "request",
109 status: 429,
110 responseBodyLength: 0,
111 retryAt: 120_000,
112 }),
113 )
114 : Effect.succeed([
115 { name: "build", status: "failure" as const, description: null, url: null },
116 ]),
117 }),
118 ),
119 );
120 const read = provider.getChangeRequestChecks;
121 if (read === undefined) return yield* Effect.die("checks read missing");
122 const input = { cwd: "/repo", repository: "acme/web", host: "bitbucket.org", number: 1 };
123 expect((yield* read(input)).checks[0]?.status).toBe("failure");
124 limited = true;
125 expect(yield* Effect.flip(read(input))).toMatchObject({
126 reason: "rate-limited",
127 retryAt: 120_000,
128 });
129 }),
130);
131​
132describe("bitbucketProviderFailure", () => {
133 it("treats only an HTTP 401 as unusable credentials", () => {
134 const responseError = (status: number) =>
135 new BitbucketApi.BitbucketResponseError({
136 operation: "request",
137 status,
138 responseBodyLength: 0,
139 });
140​
141 expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
142 expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
143 });
144});
145​
146describe("bitbucketViewerPermissions", () => {
147 it("offers both actions to credentials with write access", () => {
148 expect(bitbucketViewerPermissions({ canWrite: true })).toEqual({
149 actions: ["merge", "close"],
150 comment: true,
151 resolve: true,
152 verdicts: ["comment", "approve", "request-changes"],
153 // Bitbucket says nothing about who may set a reviewer, and an unreported permission is
154 // granted.
155 requestReviewers: true,
156 });
157 });
158​
159 it("keeps merge from credentials that can only read the repository", () => {
160 expect(bitbucketViewerPermissions({ canWrite: false })).toEqual({
161 actions: ["close"],
162 comment: true,
163 resolve: true,
164 verdicts: ["comment", "approve", "request-changes"],
165 requestReviewers: true,
166 });
167 });
168​
169 it("treats an author with read access as any other reader, which is all Bitbucket says", () => {
170 // The repository permission is the whole of what Bitbucket reports per account; it says
171 // nothing about who opened this pull request, and its author may decline it with read access
172 // alone — so declining stays offered rather than being taken from them.
173 expect(bitbucketViewerPermissions({ canWrite: false }).actions).toEqual(["close"]);
174 });
175});