Files
t3code/apps/server/src/pullRequest/BitbucketPullRequestProvider.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

176 lines
6.8 KiB
TypeScript

import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Result from "effect/Result";
import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
import { decodePullRequestJson } from "./bitbucketPullRequestJson.ts";
import {
bitbucketProviderFailure,
bitbucketViewerPermissions,
make,
} from "./BitbucketPullRequestProvider.ts";
for (const operation of [
"getMergeability",
"listChecks",
"getRepositoryPermission",
"listComments",
"listCommits",
] as const) {
it.effect.each(["response", "body read"])(
`preserves rate limits from ${operation} on %s errors while recovering other optional-read failures`,
(variant) =>
Effect.gen(function* () {
const pullRequest = Result.getOrThrow(
decodePullRequestJson(`{
"id": 1, "title": "Check polling", "state": "OPEN",
"source": { "branch": { "name": "feature" } },
"destination": { "branch": { "name": "main" } },
"created_on": "2026-09-16T00:00:00Z",
"updated_on": "2026-09-16T00:00:00Z",
"links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
}`),
);
for (const status of [429, 403]) {
const provider = yield* make.pipe(
Effect.provide(
Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
getPullRequest: () => Effect.succeed(pullRequest),
getDiffStat: () => Effect.succeed({ additions: 0, deletions: 0, changedFiles: 0 }),
getMergeability: () => Effect.succeed("unknown" as const),
listChecks: () => Effect.succeed([]),
getRepositoryPermission: () => Effect.succeed(true),
listComments: () => Effect.succeed({ comments: [], threads: [], truncated: false }),
listCommits: () => Effect.succeed([]),
[operation]: () =>
Effect.fail(
variant === "response"
? new BitbucketApi.BitbucketResponseError({
operation: "request",
status,
responseBodyLength: 0,
retryAt: 120_000,
})
: new BitbucketApi.BitbucketResponseBodyReadError({
operation: "request",
status,
cause: new Error("response stream failed"),
retryAt: 120_000,
}),
),
}),
),
);
const reference = {
cwd: "/repo",
repository: "acme/web",
number: 1,
host: "bitbucket.org",
};
const result = yield* operation === "listComments" || operation === "listCommits"
? Effect.result(provider.getChangeRequestActivity(reference))
: Effect.result(provider.getChangeRequest(reference));
if (status === 429) {
expect(result).toMatchObject({
_tag: "Failure",
failure: { reason: "rate-limited", retryAt: 120_000 },
});
} else {
expect(result._tag).toBe("Success");
}
}
}),
);
}
it.effect("reads checks and PR state without diff, mergeability, or permission requests", () =>
Effect.gen(function* () {
const pullRequest = Result.getOrThrow(
decodePullRequestJson(`{
"id": 1, "title": "Checks", "state": "OPEN",
"source": { "branch": { "name": "feature" } },
"destination": { "branch": { "name": "main" } },
"created_on": "2026-09-16T00:00:00Z", "updated_on": "2026-09-16T00:00:00Z",
"links": { "html": { "href": "https://bitbucket.org/acme/web/pull-requests/1" } }
}`),
);
let limited = false;
const provider = yield* make.pipe(
Effect.provide(
Layer.mock(BitbucketPullRequestApi.BitbucketPullRequestApi)({
getPullRequest: () => Effect.succeed(pullRequest),
listChecks: () =>
limited
? Effect.fail(
new BitbucketApi.BitbucketResponseError({
operation: "request",
status: 429,
responseBodyLength: 0,
retryAt: 120_000,
}),
)
: Effect.succeed([
{ name: "build", status: "failure" as const, description: null, url: null },
]),
}),
),
);
const read = provider.getChangeRequestChecks;
if (read === undefined) return yield* Effect.die("checks read missing");
const input = { cwd: "/repo", repository: "acme/web", host: "bitbucket.org", number: 1 };
expect((yield* read(input)).checks[0]?.status).toBe("failure");
limited = true;
expect(yield* Effect.flip(read(input))).toMatchObject({
reason: "rate-limited",
retryAt: 120_000,
});
}),
);
describe("bitbucketProviderFailure", () => {
it("treats only an HTTP 401 as unusable credentials", () => {
const responseError = (status: number) =>
new BitbucketApi.BitbucketResponseError({
operation: "request",
status,
responseBodyLength: 0,
});
expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
});
});
describe("bitbucketViewerPermissions", () => {
it("offers both actions to credentials with write access", () => {
expect(bitbucketViewerPermissions({ canWrite: true })).toEqual({
actions: ["merge", "close"],
comment: true,
resolve: true,
verdicts: ["comment", "approve", "request-changes"],
// Bitbucket says nothing about who may set a reviewer, and an unreported permission is
// granted.
requestReviewers: true,
});
});
it("keeps merge from credentials that can only read the repository", () => {
expect(bitbucketViewerPermissions({ canWrite: false })).toEqual({
actions: ["close"],
comment: true,
resolve: true,
verdicts: ["comment", "approve", "request-changes"],
requestReviewers: true,
});
});
it("treats an author with read access as any other reader, which is all Bitbucket says", () => {
// The repository permission is the whole of what Bitbucket reports per account; it says
// nothing about who opened this pull request, and its author may decline it with read access
// alone — so declining stays offered rather than being taken from them.
expect(bitbucketViewerPermissions({ canWrite: false }).actions).toEqual(["close"]);
});
});