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

bitbucketPullRequestJson.test.ts

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