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