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

bitbucketPullRequestJson.test.ts

377 lines11.9 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 decodeCommentsJson,
6 decodeCommitsJson,
7 decodeConflictsJson,
8 decodeDiffstatJson,
9 decodePullRequestJson,
10 decodePullRequestPageJson,
11 decodeRepositoryPermissionJson,
12 decodeStatusesJson,
13 decodeViewerJson,
14} from "./bitbucketPullRequestJson.ts";
15​
16/** Shaped after a real api.bitbucket.org pull request, trimmed to the fields that are read. */
17function pullRequest(overrides: Record<string, unknown> = {}): Record<string, unknown> {
18 return {
19 id: 897,
20 title: "Add trustabl-pipe",
21 description: "# Add trustabl-pipe",
22 state: "OPEN",
23 draft: false,
24 created_on: "2026-06-16T05:04:32.258456+00:00",
25 updated_on: "2026-06-16T05:04:33.750542+00:00",
26 author: { display_name: "Bilal Hassan", nickname: "bilal", type: "user" },
27 source: { branch: { name: "feat/page" }, repository: { full_name: "fork/web" } },
28 destination: { branch: { name: "master" } },
29 links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/897" } },
30 ...overrides,
31 };
33​
34function page(values: ReadonlyArray<unknown>, extra: Record<string, unknown> = {}): string {
35 return JSON.stringify({ pagelen: 50, page: 1, size: values.length, values, ...extra });
37​
38function expectSuccess<A>(result: Result.Result<A, unknown>): A {
39 expect(Result.isSuccess(result)).toBe(true);
40 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
41 return result.success;
43​
44describe("decodePullRequestPageJson", () => {
45 it("reads a pull request as a change request", () => {
46 const decoded = expectSuccess(decodePullRequestPageJson(page([pullRequest()])));
47​
48 expect(decoded.items).toHaveLength(1);
49 expect(decoded.items[0]).toMatchObject({
50 number: 897,
51 title: "Add trustabl-pipe",
52 url: "https://bitbucket.org/acme/web/pull-requests/897",
53 author: { login: "bilal", name: "Bilal Hassan" },
54 headBranch: "feat/page",
55 headRepositoryNameWithOwner: "fork/web",
56 baseBranch: "master",
57 state: "open",
58 isDraft: false,
59 // Bitbucket says nothing about conflicts on the pull request itself.
60 mergeability: "unknown",
61 });
62 expect(decoded.next).toBeNull();
63 });
64​
65 it("normalizes Bitbucket's offset timestamps, which the page sorts against other hosts", () => {
66 const decoded = expectSuccess(decodePullRequestPageJson(page([pullRequest()])));
67​
68 expect(decoded.items[0]).toMatchObject({
69 createdAt: "2026-06-16T05:04:32.258Z",
70 updatedAt: "2026-06-16T05:04:33.750Z",
71 });
72 });
73​
74 it("reports the next page as the whole URL Bitbucket sends", () => {
75 const next = "https://api.bitbucket.org/2.0/repositories/acme/web/pullrequests?page=2";
76 const decoded = expectSuccess(decodePullRequestPageJson(page([pullRequest()], { next })));
77​
78 expect(decoded.next).toBe(next);
79 });
80​
81 it.each([
82 ["MERGED", "merged"],
83 ["DECLINED", "closed"],
84 ["SUPERSEDED", "closed"],
85 ["OPEN", "open"],
86 ["something new", "open"],
87 ])("reads the %s state as %s", (state, expected) => {
88 const decoded = expectSuccess(decodePullRequestPageJson(page([pullRequest({ state })])));
89​
90 expect(decoded.items[0]?.state).toBe(expected);
91 });
92​
93 it("skips a malformed row rather than failing the page", () => {
94 const decoded = expectSuccess(
95 decodePullRequestPageJson(page([{ id: "not a number" }, pullRequest()])),
96 );
97​
98 expect(decoded.items).toHaveLength(1);
99 });
100​
101 it("fails when Bitbucket did not answer with a page", () => {
102 expect(Result.isFailure(decodePullRequestPageJson(JSON.stringify({ error: "nope" })))).toBe(
103 true,
104 );
105 });
106});
107​
108describe("decodePullRequestJson", () => {
109 it("reads reviewers as review requests", () => {
110 const decoded = expectSuccess(
111 decodePullRequestJson(
112 JSON.stringify(
113 pullRequest({
114 reviewers: [{ nickname: "julius", display_name: "Julius" }],
115 }),
116 ),
117 ),
118 );
119​
120 expect(decoded.reviewRequestLogins).toEqual(["julius"]);
121 expect(decoded.reviewers).toEqual([{ login: "julius", name: "Julius", avatarUrl: null }]);
122 });
123​
124 it("reads a participant's vote as a review", () => {
125 const decoded = expectSuccess(
126 decodePullRequestJson(
127 JSON.stringify(
128 pullRequest({
129 participants: [
130 {
131 user: { nickname: "julius", display_name: "Julius" },
132 role: "REVIEWER",
133 approved: true,
134 state: "approved",
135 participated_on: "2026-06-17T09:00:00+00:00",
136 },
137 // Added as a reviewer but has not voted, so there is no verdict to show.
138 {
139 user: { nickname: "sam", display_name: "Sam" },
140 role: "REVIEWER",
141 approved: false,
142 state: null,
143 participated_on: null,
144 },
145 ],
146 }),
147 ),
148 ),
149 );
150​
151 expect(decoded.reviews).toHaveLength(1);
152 expect(decoded.reviews[0]).toMatchObject({
153 kind: "review",
154 author: { login: "julius" },
155 reviewState: "approved",
156 createdAt: "2026-06-17T09:00:00.000Z",
157 });
158 });
159});
160​
161describe("decodeViewerJson", () => {
162 it("reads the signed-in nickname", () => {
163 const decoded = decodeViewerJson(JSON.stringify({ nickname: "bilal", display_name: "Bilal" }));
164​
165 expect(expectSuccess(decoded)).toBe("bilal");
166 });
167​
168 it("falls back to the display name, which app accounts have instead", () => {
169 const decoded = decodeViewerJson(JSON.stringify({ display_name: "Release Bot" }));
170​
171 expect(expectSuccess(decoded)).toBe("Release Bot");
172 });
173​
174 it("returns nothing when the account has neither", () => {
175 expect(expectSuccess(decodeViewerJson(JSON.stringify({})))).toBeNull();
176 });
177});
178​
179describe("decodeCommentsJson", () => {
180 it("keeps a posted comment and drops deleted and unposted ones", () => {
181 const decoded = expectSuccess(
182 decodeCommentsJson(
183 page([
184 {
185 id: 797230941,
186 content: { raw: "The issue is ready for review." },
187 user: { display_name: "Release Bot", type: "app_user" },
188 created_on: "2026-05-15T01:58:38.220690+00:00",
189 deleted: false,
190 pending: false,
191 links: { html: { href: "https://bitbucket.org/acme/web/pull-requests/892#c1" } },
192 },
193 {
194 id: 2,
195 content: { raw: "gone" },
196 created_on: "2026-05-15T02:00:00+00:00",
197 deleted: true,
198 },
199 {
200 id: 3,
201 content: { raw: "wip" },
202 created_on: "2026-05-15T02:00:00+00:00",
203 pending: true,
204 },
205 { id: 4, content: { raw: " " }, created_on: "2026-05-15T02:00:00+00:00" },
206 ]),
207 ),
208 );
209​
210 expect(decoded.comments).toHaveLength(1);
211 expect(decoded.comments[0]).toMatchObject({
212 id: "797230941",
213 kind: "issue-comment",
214 // An app account has no nickname, so its display name is the only handle it has.
215 author: { login: "Release Bot" },
216 createdAt: "2026-05-15T01:58:38.220Z",
217 });
218 });
219​
220 it("reads a comment pinned to a file as a review comment", () => {
221 const decoded = expectSuccess(
222 decodeCommentsJson(
223 page([
224 {
225 id: 5,
226 content: { raw: "Rename this." },
227 created_on: "2026-05-15T02:00:00+00:00",
228 inline: { path: "src/app.ts" },
229 },
230 ]),
231 ),
232 );
233​
234 expect(decoded.comments[0]).toMatchObject({ kind: "review-comment", path: "src/app.ts" });
235 });
236});
237​
238describe("decodeCommitsJson", () => {
239 it("returns commits oldest first with only the subject line", () => {
240 const decoded = expectSuccess(
241 decodeCommitsJson(
242 page([
243 { hash: "bbb", message: "second\n\nbody text\n", date: "2026-06-16T04:51:00+00:00" },
244 {
245 hash: "aaa",
246 message: "first\n",
247 date: "2026-06-16T04:50:49+00:00",
248 author: {
249 raw: "Ada Lovelace <ada@example.com>",
250 user: { nickname: "ada", display_name: "Ada Lovelace" },
251 },
252 },
253 ]),
254 ),
255 );
256​
257 expect(decoded.items.map((commit) => commit.oid)).toEqual(["aaa", "bbb"]);
258 expect(decoded.items[0]?.authors).toEqual([
259 { login: "ada", name: "Ada Lovelace", avatarUrl: null },
260 ]);
261 expect(decoded.items[1]?.messageHeadline).toBe("second");
262 expect(decoded.next).toBeNull();
263 });
264​
265 it("skips commits whose hash is empty", () => {
266 const decoded = expectSuccess(
267 decodeCommitsJson(
268 page([
269 { hash: " ", message: "invalid", date: "2026-06-16T04:51:00+00:00" },
270 { hash: "aaa", date: "2026-06-16T04:50:49+00:00" },
271 ]),
272 ),
273 );
274​
275 expect(decoded.items.map((commit) => commit.oid)).toEqual(["aaa"]);
276 });
277});
278​
279describe("decodeStatusesJson", () => {
280 it("reads a build status as a check", () => {
281 const decoded = expectSuccess(
282 decodeStatusesJson(
283 page([
284 {
285 key: "custom:check-version-and-pr",
286 name: "Pipeline - custom: check-version-and-pr",
287 state: "SUCCESSFUL",
288 description: "",
289 url: "https://bitbucket.org/acme/web/pipelines/results/8126",
290 },
291 ]),
292 ),
293 );
294​
295 expect(decoded).toEqual({
296 items: [
297 {
298 name: "Pipeline - custom: check-version-and-pr",
299 status: "success",
300 description: null,
301 url: "https://bitbucket.org/acme/web/pipelines/results/8126",
302 },
303 ],
304 next: null,
305 });
306 });
307​
308 it.each([
309 ["SUCCESSFUL", "success"],
310 ["FAILED", "failure"],
311 ["INPROGRESS", "pending"],
312 ["STOPPED", "cancelled"],
313 ["something new", "neutral"],
314 ])("reads the %s build state as %s", (state, expected) => {
315 const decoded = expectSuccess(decodeStatusesJson(page([{ name: "Pipeline", state }])));
316​
317 expect(decoded.items[0]?.status).toBe(expected);
318 });
319​
320 it("keeps two statuses that share a display name but have different keys", () => {
321 const decoded = expectSuccess(
322 decodeStatusesJson(
323 page([
324 { key: "build", name: "Pipeline", state: "SUCCESSFUL" },
325 { key: "deploy", name: "Pipeline", state: "FAILED" },
326 ]),
327 ),
328 );
329​
330 expect(decoded.items.map((check) => [check.name, check.status])).toEqual([
331 ["build / Pipeline", "success"],
332 ["deploy / Pipeline", "failure"],
333 ]);
334 });
335});
336​
337describe("decodeDiffstatJson", () => {
338 it("adds up the per-file counts", () => {
339 const decoded = expectSuccess(
340 decodeDiffstatJson(
341 page([
342 { lines_added: 9, lines_removed: 2 },
343 { lines_added: 32, lines_removed: 14 },
344 ]),
345 ),
346 );
347​
348 expect(decoded).toEqual({ additions: 41, deletions: 16, changedFiles: 2, next: null });
349 });
350});
351​
352describe("decodeConflictsJson", () => {
353 it("calls an empty conflict list mergeable", () => {
354 expect(expectSuccess(decodeConflictsJson(page([])))).toBe("mergeable");
355 });
356​
357 it("calls any reported conflict conflicting", () => {
358 expect(expectSuccess(decodeConflictsJson(page([{ path: "src/app.ts" }])))).toBe("conflicting");
359 });
360});
361​
362describe("repository permission decoding", () => {
363 const permissionPage = (permission: string) =>
364 page([{ type: "repository_permission", permission }]);
365​
366 it("counts admin and write as write, and read as not", () => {
367 expect(expectSuccess(decodeRepositoryPermissionJson(permissionPage("admin")))).toBe(true);
368 expect(expectSuccess(decodeRepositoryPermissionJson(permissionPage("write")))).toBe(true);
369 expect(expectSuccess(decodeRepositoryPermissionJson(permissionPage("read")))).toBe(false);
370 });
371​
372 it("grants write where Bitbucket named no permission at all", () => {
373 // An empty page is Bitbucket declining to say, which is an unknown standing rather than a
374 // refusal — and an unknown one is granted.
375 expect(expectSuccess(decodeRepositoryPermissionJson(page([])))).toBe(true);
376 });
377});