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

BitbucketPullRequestProvider.listing.test.ts

88 lines3.1 KB

1import { assert, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import * as BitbucketApi from "./BitbucketApi.ts";
6import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
7import * as BitbucketPullRequestProvider from "./BitbucketPullRequestProvider.ts";
8​
9it.effect("pages through rows that share an update instant by excluding those already sent", () =>
10 Effect.gen(function* () {
11 const boundary = "2026-10-12T00:00:00.000Z";
12 const older = "2026-10-11T00:00:00.000Z";
13 const rows = [1, 2, 3, 4, 5].map((id) => ({
14 id,
15 title: `Pull request ${id}`,
16 state: "OPEN",
17 created_on: older,
18 updated_on: id <= 3 ? boundary : older,
19 source: { branch: { name: `feat/${id}` } },
20 destination: { branch: { name: "main" } },
21 links: { html: { href: `https://bitbucket.org/acme/web/pull-requests/${id}` } },
22 }));
23 const filters: string[] = [];
24 const provider = yield* BitbucketPullRequestProvider.make.pipe(
25 Effect.provide(
26 BitbucketPullRequestApi.layer.pipe(
27 Layer.provide(
28 Layer.mock(BitbucketApi.BitbucketApi)({
29 request: ({ url }) => {
30 const filter =
31 new URL(url, "https://api.bitbucket.org").searchParams.get("q") ?? "";
32 filters.push(filter);
33 const updatedBefore = /updated_on <= (\S+)/.exec(filter)?.[1];
34 const excluded = new Set(
35 Array.from(filter.matchAll(/id != (\d+)/g), (match) => Number(match[1])),
36 );
37 const values = rows.filter(
38 (row) =>
39 (updatedBefore === undefined || row.updated_on <= updatedBefore) &&
40 !excluded.has(row.id),
41 );
42 return Effect.succeed({ body: JSON.stringify({ values }), truncated: false });
43 },
44 }),
45 ),
46 ),
47 ),
48 );
49 const list = (cursor?: {
50 readonly updatedBefore: string;
51 readonly delivered: number;
52 readonly seenAt: ReadonlyArray<number>;
53 }) =>
54 provider.listChangeRequests({
55 cwd: "/w",
56 repository: "acme/web",
57 host: "bitbucket.org",
58 state: "open",
59 involvement: "all",
60 viewer: "bilal",
61 limit: 2,
62 ...(cursor === undefined ? {} : { cursor }),
63 });
64​
65 const first = yield* list();
66 assert.deepStrictEqual(
67 first.items.map((item) => item.number),
68 [1, 2],
69 );
70 assert.isTrue(first.truncated);
71​
72 const second = yield* list({ updatedBefore: boundary, delivered: 2, seenAt: [1, 2] });
73 assert.deepStrictEqual(
74 second.items.map((item) => item.number),
75 [3, 4],
76 );
77 assert.include(filters.at(-1), `updated_on <= ${boundary}`);
78 assert.include(filters.at(-1), "id != 1");
79 assert.include(filters.at(-1), "id != 2");
80​
81 const last = yield* list({ updatedBefore: older, delivered: 4, seenAt: [4] });
82 assert.deepStrictEqual(
83 last.items.map((item) => item.number),
84 [5],
85 );
86 assert.isFalse(last.truncated);
87 }),
88);