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

BitbucketPullRequestProvider.test.ts

213 lines7.9 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 "./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 { state: "MERGED", mergedAt: "2025-06-16T23:40:46.222Z", closedAt: null },
17 { state: "DECLINED", mergedAt: null, closedAt: "2025-06-16T23:40:46.222Z" },
18 { state: "OPEN", mergedAt: null, closedAt: null },
19])("reads the $state time from closed_on", ({ state, mergedAt, closedAt }) =>
20 Effect.gen(function* () {
21 const pullRequest = Result.getOrThrow(
22 decodePullRequestJson(
23 JSON.stringify({
24 id: 52,
25 title: "Release",
26 state,
27 source: { branch: { name: "release" } },
28 destination: { branch: { name: "master" } },
29 created_on: "2025-06-16T06:20:58.415108+00:00",
30 // Comments after a merge move this, so it is not the merge time.
31 updated_on: "2025-06-20T10:00:00.000000+00:00",
32 closed_on: state === "OPEN" ? null : "2025-06-16T23:40:46.222238+00:00",
33 links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/52" } },
34 }),
35 ),
36 );
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"),
43 listChecks: () => Effect.succeed([]),
44 getRepositoryPermission: () => Effect.succeed(true),
45 }),
46 ),
47 );
48 const detail = yield* provider.getChangeRequest({
49 cwd: "/repo",
50 repository: "acme/web",
51 host: "bitbucket.org",
52 number: 52,
53 });
54​
55 expect({ mergedAt: detail.mergedAt, closedAt: detail.closedAt }).toEqual({
56 mergedAt,
57 closedAt,
58 });
59 }),
60);
61​
62for (const operation of [
63 "getMergeability",
64 "listChecks",
65 "getRepositoryPermission",
66 "listComments",
67 "listCommits",
68] as const) {
69 it.effect.each(["response", "body read"])(
70 `preserves rate limits from ${operation} on %s errors while recovering other optional-read failures`,
71 (variant) =>
72 Effect.gen(function* () {
73 const pullRequest = Result.getOrThrow(
74 decodePullRequestJson(`{
75 "id": 1, "title": "Check polling", "state": "OPEN",
76 "source": { "branch": { "name": "feature" } },
77 "destination": { "branch": { "name": "main" } },
78 "created_on": "2026-09-16T00:00:00Z",
79 "updated_on": "2026-09-16T00:00:00Z",
80 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
81 }`),
82 );
83 for (const status of [429, 403]) {
84 const provider = yield* make.pipe(
85 Effect.provide(
86 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
87 getPullRequest: () => Effect.succeed(pullRequest),
88 getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
89 getMergeability: () => Effect.succeed("unknown" as const),
90 listChecks: () => Effect.succeed([]),
91 getRepositoryPermission: () => Effect.succeed(true),
92 listComments: () => Effect.succeed({ comments: [], threads: [], truncated: false }),
93 listCommits: () => Effect.succeed([]),
94 [operation]: () =>
95 Effect.fail(
96 variant === "response"
97 ? new BitbucketApi.BitbucketResponseError({
98 operation: "request",
99 status,
100 responseBodyLength: 0,
101 retryAt: 120_000,
102 })
103 : new BitbucketApi.BitbucketResponseBodyReadError({
104 operation: "request",
105 status,
106 cause: new Error("response stream failed"),
107 retryAt: 120_000,
108 }),
109 ),
110 }),
111 ),
112 );
113 const reference = {
114 cwd: "/repo",
115 repository: "acme/web",
116 number: 1,
117 host: "bitbucket.org",
118 };
119 const result = yield* operation === "listComments" || operation === "listCommits"
120 ? Effect.result(provider.getChangeRequestActivity(reference))
121 : Effect.result(provider.getChangeRequest(reference));
122 if (status === 429) {
123 expect(result).toMatchObject({
124 _tag: "Failure",
125 failure: { reason: "rate-limited", retryAt: 120_000 },
126 });
127 } else {
128 expect(result._tag).toBe("Success");
129 }
130 }
131 }),
132 );
134​
135it.effect("reads checks and PR state without diff, mergeability, or permission requests", () =>
136 Effect.gen(function* () {
137 const pullRequest = Result.getOrThrow(
138 decodePullRequestJson(`{
139 "id": 1, "title": "Checks", "state": "OPEN",
140 "source": { "branch": { "name": "feature" } },
141 "destination": { "branch": { "name": "main" } },
142 "created_on": "2026-09-16T00:00:00Z", "updated_on": "2026-09-16T00:00:00Z",
143 "links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
144 }`),
145 );
146 let limited = false;
147 const provider = yield* make.pipe(
148 Effect.provide(
149 Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
150 getPullRequest: () => Effect.succeed(pullRequest),
151 listChecks: () =>
152 limited
153 ? Effect.fail(
154 new BitbucketApi.BitbucketResponseError({
155 operation: "request",
156 status: 429,
157 responseBodyLength: 0,
158 retryAt: 120_000,
159 }),
160 )
161 : Effect.succeed([
162 { name: "build", status: "failure" as const, description: null, url: null },
163 ]),
164 }),
165 ),
166 );
167 const read = provider.getChangeRequestChecks;
168 if (read === undefined) return yield* Effect.die("checks read missing");
169 const input = { cwd: "/repo", repository: "acme/web", host: "bitbucket.org", number: 1 };
170 expect((yield* read(input)).checks[0]?.status).toBe("failure");
171 limited = true;
172 expect(yield* Effect.flip(read(input))).toMatchObject({
173 reason: "rate-limited",
174 retryAt: 120_000,
175 });
176 }),
177);
178​
179describe("bitbucketProviderFailure", () => {
180 it("treats only an HTTP 401 as unusable credentials", () => {
181 const responseError = (status: number) =>
182 new BitbucketApi.BitbucketResponseError({
183 operation: "request",
184 status,
185 responseBodyLength: 0,
186 });
187​
188 expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
189 expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
190 });
191});
192​
193describe("bitbucketViewerPermissions", () => {
194 it("offers both actions to credentials with write access", () => {
195 expect(bitbucketViewerPermissions({ canWrite: true })).toEqual({
196 actions: ["merge", "close"],
197 comment: true,
198 resolve: true,
199 verdicts: ["comment", "approve", "request-changes"],
200 requestReviewers: true,
201 });
202 });
203​
204 it("keeps merge from credentials that can only read the repository", () => {
205 expect(bitbucketViewerPermissions({ canWrite: false })).toEqual({
206 actions: ["close"],
207 comment: true,
208 resolve: true,
209 verdicts: ["comment", "approve", "request-changes"],
210 requestReviewers: true,
211 });
212 });
213});