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

gitHubPullRequestJson.test.ts

959 lines30.6 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 buildReviewSubmissionJson,
6 buildReviewerRequestJson,
7 decodePullRequestActivityJson,
8 decodePullRequestDetailJson,
9 decodePullRequestFilesJson,
10 decodePullRequestListJson,
11 decodeRepositoryAccessJson,
12 decodeReviewerCandidatesJson,
13 decodeReviewThreadCommentsJson,
14 decodeReviewThreadsJson,
15 decodeViewerPermissionsJson,
16 reviewThreadConversation,
17} from "./gitHubPullRequestJson.ts";
18​
19function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {
20 return JSON.stringify(
21 entries.map((entry) => ({
22 number: 1,
23 title: "Add the pull requests page",
24 url: "https://github.com/pingdotgg/t3code/pull/1",
25 headRefName: "feat/page",
26 baseRefName: "main",
27 createdAt: "2026-07-01T00:00:00Z",
28 updatedAt: "2026-07-02T00:00:00Z",
29 ...entry,
30 })),
31 );
33​
34function expectSuccess<A>(result: Result.Result<A, unknown>): A {
35 expect(Result.isSuccess(result)).toBe(true);
36 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
37 return result.success;
39​
40describe("pull request list decoding", () => {
41 it("treats a merge timestamp as merged even when the state still says closed", () => {
42 const [entry] = expectSuccess(
43 decodePullRequestListJson(listJson([{ state: "CLOSED", mergedAt: "2026-07-03T00:00:00Z" }])),
44 ).items;
45 expect(entry?.state).toBe("merged");
46 });
47​
48 it("normalizes mergeability and defaults unknown values", () => {
49 const batch = expectSuccess(
50 decodePullRequestListJson(
51 listJson([{ mergeable: "CONFLICTING" }, { mergeable: "SOMETHING_NEW" }, {}]),
52 ),
53 );
54 expect(batch.items.map((entry) => entry.mergeability)).toEqual([
55 "conflicting",
56 "unknown",
57 "unknown",
58 ]);
59 });
60​
61 it("keeps user review requests and drops team ones, which are not logins", () => {
62 const [entry] = expectSuccess(
63 decodePullRequestListJson(
64 listJson([{ reviewRequests: [{ login: "octocat" }, { slug: "web-platform" }] }]),
65 ),
66 ).items;
67 expect(entry?.reviewRequestLogins).toEqual(["octocat"]);
68 });
69​
70 it("skips malformed entries but still counts them, so paging does not stop early", () => {
71 const raw = `[${listJson([{}]).slice(1, -1)},{"number":"not-a-number"}]`;
72 const batch = expectSuccess(decodePullRequestListJson(raw));
73 expect(batch.items).toHaveLength(1);
74 expect(batch.rawCount).toBe(2);
75 });
76});
77​
78describe("pull request detail decoding", () => {
79 const detailJson = JSON.stringify({
80 number: 7,
81 title: "Detail",
82 url: "https://github.com/pingdotgg/t3code/pull/7",
83 headRefName: "feat/detail",
84 baseRefName: "main",
85 createdAt: "2026-07-01T00:00:00Z",
86 updatedAt: "2026-07-05T00:00:00Z",
87 body: "Body",
88 statusCheckRollup: [
89 { __typename: "CheckRun", name: "build", status: "IN_PROGRESS" },
90 { __typename: "CheckRun", name: "test", status: "COMPLETED", conclusion: "FAILURE" },
91 { __typename: "StatusContext", context: "ci/legacy", state: "SUCCESS" },
92 ],
93 comments: [{ id: "c1", body: "second", createdAt: "2026-07-04T00:00:00Z" }],
94 reviews: [
95 { id: "r1", body: "first", state: "CHANGES_REQUESTED", submittedAt: "2026-07-03T00:00:00Z" },
96 { id: "r2", body: " ", state: "APPROVED", submittedAt: "2026-07-06T00:00:00Z" },
97 ],
98 commits: [
99 {
100 oid: "abc1234",
101 messageHeadline: "Ship the timeline",
102 committedDate: "2026-07-05T00:00:00Z",
103 authors: [
104 { login: "octocat", name: "Octo Cat", email: "octo@example.com" },
105 { name: "Pair Author", email: "pair@example.com" },
106 ],
107 },
108 ],
109 });
110​
111 it("maps check-run status and commit-status state onto one vocabulary", () => {
112 const detail = expectSuccess(decodePullRequestDetailJson(detailJson));
113 expect(detail.checks.map((check) => [check.name, check.status])).toEqual([
114 ["build", "pending"],
115 ["test", "failure"],
116 ["ci/legacy", "success"],
117 ]);
118 });
119​
120 it("merges reviews with comments in time order and keeps a bodyless approval", () => {
121 const detail = expectSuccess(decodePullRequestActivityJson(detailJson));
122 // r2 approved without writing anything, which is still the event worth seeing.
123 expect(detail.comments.map((comment) => comment.id)).toEqual(["r1", "c1", "r2"]);
124 expect(detail.comments.at(-1)?.reviewState).toBe("APPROVED");
125 });
126​
127 it("keeps every attributed commit author, including an unlinked signature", () => {
128 const detail = expectSuccess(decodePullRequestActivityJson(detailJson));
129 expect(detail.commits[0]?.authors).toEqual([
130 { login: "octocat", name: "Octo Cat", avatarUrl: null },
131 { login: "Pair Author", name: "Pair Author", avatarUrl: null },
132 ]);
133 });
134​
135 it("drops the bodyless review GitHub opens to hold line comments", () => {
136 const raw = JSON.parse(detailJson) as Record<string, unknown>;
137 const detail = expectSuccess(
138 decodePullRequestActivityJson(
139 JSON.stringify({
140 ...raw,
141 reviews: [
142 // What a reviewer leaving inline comments produces: a container with a state but
143 // nothing to read. Its comments come from the review threads instead.
144 { id: "r4", body: "", state: "COMMENTED", submittedAt: "2026-07-07T00:00:00Z" },
145 {
146 id: "r5",
147 body: "Looks good.",
148 state: "COMMENTED",
149 submittedAt: "2026-07-08T00:00:00Z",
150 },
151 ],
152 }),
153 ),
154 );
155​
156 expect(detail.comments.map((comment) => comment.id)).toEqual(["c1", "r5"]);
157 });
158​
159 it.each(["APPROVED", "CHANGES_REQUESTED", "DISMISSED"])(
160 "keeps a bodyless %s review, which is the event itself",
161 (state) => {
162 const raw = JSON.parse(detailJson) as Record<string, unknown>;
163 const detail = expectSuccess(
164 decodePullRequestActivityJson(
165 JSON.stringify({
166 ...raw,
167 reviews: [{ id: "r6", body: "", state, submittedAt: "2026-07-07T00:00:00Z" }],
168 }),
169 ),
170 );
171​
172 expect(detail.comments.map((comment) => comment.id)).toContain("r6");
173 },
174 );
175​
176 it("drops a review that carries neither a body nor a state", () => {
177 const raw = JSON.parse(detailJson) as Record<string, unknown>;
178 const detail = expectSuccess(
179 decodePullRequestActivityJson(
180 JSON.stringify({
181 ...raw,
182 reviews: [{ id: "r3", body: " ", submittedAt: "2026-07-07T00:00:00Z" }],
183 }),
184 ),
185 );
186 expect(detail.comments.map((comment) => comment.id)).toEqual(["c1"]);
187 });
188});
189​
190describe("review thread decoding", () => {
191 const threadsJson = (
192 nodes: ReadonlyArray<Record<string, unknown>>,
193 totalCount = nodes.length,
194 pageInfo: Record<string, unknown> = { hasNextPage: false, endCursor: null },
195 ): string =>
196 JSON.stringify({
197 data: { repository: { pullRequest: { reviewThreads: { totalCount, pageInfo, nodes } } } },
198 });
199​
200 /** The same query carries the review roster, so it is built alongside the threads. */
201 const reviewJson = (input: {
202 readonly requested?: ReadonlyArray<unknown>;
203 readonly reviewed?: ReadonlyArray<unknown>;
204 }): string =>
205 JSON.stringify({
206 data: {
207 repository: {
208 pullRequest: {
209 reviewThreads: { totalCount: 0, nodes: [] },
210 reviewRequests: {
211 nodes: (input.requested ?? []).map((r) => ({ requestedReviewer: r })),
212 },
213 latestReviews: { nodes: (input.reviewed ?? []).map((a) => ({ author: a })) },
214 },
215 },
216 },
217 });
218​
219 it("keeps a reviewer who has already reviewed, app or person, with their avatar", () => {
220 const result = expectSuccess(
221 decodeReviewThreadsJson(
222 reviewJson({
223 requested: [{ login: "julius", name: "Julius", avatarUrl: "https://avatars/j.png" }],
224 // An app that has reviewed is no longer an outstanding request, which is why asking
225 // only for requests reported nobody on a pull request a bot had reviewed.
226 reviewed: [{ login: "macroscopeapp", avatarUrl: "https://avatars/in/900172.png" }],
227 }),
228 ),
229 );
230​
231 expect(result.reviewers).toEqual([
232 { login: "julius", name: "Julius", avatarUrl: "https://avatars/j.png" },
233 { login: "macroscopeapp", name: null, avatarUrl: "https://avatars/in/900172.png" },
234 ]);
235 });
236​
237 it("carries per-commit line counts from the pull-request connection", () => {
238 const result = expectSuccess(
239 decodeReviewThreadsJson(
240 JSON.stringify({
241 data: {
242 repository: {
243 pullRequest: {
244 reviewThreads: { totalCount: 0, nodes: [] },
245 commits: {
246 nodes: [
247 { commit: { oid: "abc123", additions: 18, deletions: 7 } },
248 { commit: { oid: "def456", additions: 3, deletions: 0 } },
249 ],
250 },
251 },
252 },
253 },
254 }),
255 ),
256 );
257​
258 expect([...result.commitStats]).toEqual([
259 ["abc123", { additions: 18, deletions: 7 }],
260 ["def456", { additions: 3, deletions: 0 }],
261 ]);
262 });
263​
264 it("decodes the newest commits off the same connection, oldest to newest", () => {
265 const result = expectSuccess(
266 decodeReviewThreadsJson(
267 JSON.stringify({
268 data: {
269 repository: {
270 pullRequest: {
271 reviewThreads: { totalCount: 0, nodes: [] },
272 commits: {
273 nodes: [
274 {
275 commit: {
276 oid: "abc123",
277 messageHeadline: "Ship the timeline",
278 committedDate: "2026-07-05T00:00:00Z",
279 additions: 18,
280 deletions: 7,
281 authors: { nodes: [{ name: "Julius", user: { login: "julius" } }] },
282 },
283 },
284 {
285 commit: {
286 oid: "def456",
287 messageHeadline: "Fix the flaky test",
288 committedDate: "2026-07-06T00:00:00Z",
289 },
290 },
291 ],
292 },
293 },
294 },
295 },
296 }),
297 ),
298 );
299​
300 expect(result.commits).toEqual([
301 {
302 oid: "abc123",
303 messageHeadline: "Ship the timeline",
304 committedDate: "2026-07-05T00:00:00Z",
305 authors: [{ login: "julius", name: "Julius", avatarUrl: null }],
306 },
307 {
308 oid: "def456",
309 messageHeadline: "Fix the flaky test",
310 committedDate: "2026-07-06T00:00:00Z",
311 authors: [],
312 },
313 ]);
314 });
315​
316 it("lists someone who was asked and then answered only once", () => {
317 const result = expectSuccess(
318 decodeReviewThreadsJson(
319 reviewJson({
320 requested: [{ login: "julius", avatarUrl: "https://avatars/j.png" }],
321 reviewed: [{ login: "julius", avatarUrl: "https://avatars/j.png" }],
322 }),
323 ),
324 );
325​
326 expect(result.reviewers).toHaveLength(1);
327 });
328​
329 it("skips a team request, which names nobody to show", () => {
330 const result = expectSuccess(decodeReviewThreadsJson(reviewJson({ requested: [null] })));
331​
332 expect(result.reviewers).toEqual([]);
333 });
334​
335 it("keeps the conversation when a request is from a team, which has no login", () => {
336 // GraphQL answers with an empty object for a union member the query has no fragment for.
337 // Failing on it would take the whole response down, comments included.
338 const result = expectSuccess(
339 decodeReviewThreadsJson(
340 reviewJson({ requested: [{}, { login: "julius", avatarUrl: "https://avatars/j.png" }] }),
341 ),
342 );
343​
344 expect(result.reviewers).toEqual([
345 { login: "julius", name: null, avatarUrl: "https://avatars/j.png" },
346 ]);
347 });
348​
349 it("carries a resolved thread into the conversation, which was still said", () => {
350 const result = expectSuccess(
351 decodeReviewThreadsJson(
352 threadsJson([
353 {
354 id: "PRRT_a",
355 isResolved: false,
356 path: "apps/server/src/ws.ts",
357 comments: {
358 nodes: [{ id: "t1", body: "fix this", createdAt: "2026-07-01T00:00:00Z" }],
359 },
360 },
361 {
362 id: "PRRT_b",
363 isResolved: true,
364 path: "apps/web/src/main.tsx",
365 comments: { nodes: [{ id: "t2", body: "done", createdAt: "2026-07-01T00:00:00Z" }] },
366 },
367 ]),
368 ),
369 );
370 const comments = reviewThreadConversation(result.threads.map((entry) => entry.thread));
371 expect(comments.map((comment) => comment.id)).toEqual(["t1", "t2"]);
372 expect(comments[0]).toMatchObject({
373 id: "t1",
374 kind: "review-comment",
375 path: "apps/server/src/ws.ts",
376 });
377 });
378​
379 it("carries every reply, not only the remark each thread opened with", () => {
380 const result = expectSuccess(
381 decodeReviewThreadsJson(
382 threadsJson([
383 {
384 id: "PRRT_c",
385 isResolved: false,
386 path: "apps/server/src/ws.ts",
387 comments: {
388 nodes: [
389 { id: "t1", body: "fix this", createdAt: "2026-07-01T00:00:00Z" },
390 { id: "t2", body: "fixed", createdAt: "2026-07-01T01:00:00Z" },
391 ],
392 },
393 },
394 ]),
395 ),
396 );
397 const comments = reviewThreadConversation(result.threads.map((entry) => entry.thread));
398 expect(comments.map((comment) => comment.id)).toEqual(["t1", "t2"]);
399 });
400​
401 it("hands back the cursor the next page of threads carries on from", () => {
402 const result = expectSuccess(
403 decodeReviewThreadsJson(
404 threadsJson(
405 [
406 {
407 id: "PRRT_d",
408 path: "apps/server/src/ws.ts",
409 isResolved: false,
410 comments: { nodes: [{ id: "t1", createdAt: "2026-07-01T00:00:00Z" }] },
411 },
412 ],
413 80,
414 { hasNextPage: true, endCursor: "Y3Vyc29yOjE" },
415 ),
416 ),
417 );
418 expect(result.nextCursor).toBe("Y3Vyc29yOjE");
419 });
420​
421 it("keeps GitHub's own count of a thread whose comments were not all read", () => {
422 const result = expectSuccess(
423 decodeReviewThreadsJson(
424 threadsJson([
425 {
426 id: "PRRT_e",
427 path: "apps/server/src/ws.ts",
428 isResolved: false,
429 comments: {
430 totalCount: 140,
431 pageInfo: { hasNextPage: true, endCursor: "Y3Vyc29yOjI" },
432 nodes: [{ id: "t1", createdAt: "2026-07-01T00:00:00Z" }],
433 },
434 },
435 ]),
436 ),
437 );
438 expect(result.threads[0]).toMatchObject({
439 commentCount: 140,
440 nextCommentCursor: "Y3Vyc29yOjI",
441 });
442 });
443​
444 it("ends a thread's walk on the last page, which still names a cursor", () => {
445 const decoded = expectSuccess(
446 decodeReviewThreadCommentsJson(
447 JSON.stringify({
448 data: {
449 node: {
450 comments: {
451 pageInfo: { hasNextPage: false, endCursor: "Y3Vyc29yOjk" },
452 nodes: [{ id: "t9", body: "last", createdAt: "2026-07-01T00:00:00Z" }],
453 },
454 },
455 },
456 }),
457 ),
458 );
459 expect(decoded.comments.map((comment) => comment.id)).toEqual(["t9"]);
460 expect(decoded.nextCursor).toBeNull();
461 });
462});
463​
464describe("repository access decoding", () => {
465 const repositoryJson = (viewerPermission?: string | null) =>
466 JSON.stringify({
467 mergeCommitAllowed: true,
468 squashMergeAllowed: false,
469 rebaseMergeAllowed: true,
470 ...(viewerPermission === undefined ? {} : { viewerPermission }),
471 });
472​
473 it("reads the three settings gh reports", () => {
474 expect(
475 expectSuccess(decodeRepositoryAccessJson(repositoryJson("ADMIN"))).mergeCapabilities,
476 ).toEqual({ merge: true, squash: false, rebase: true });
477 });
478​
479 it("fails rather than defaulting open when a setting is missing", () => {
480 const decoded = decodeRepositoryAccessJson(JSON.stringify({ mergeCommitAllowed: true }));
481 expect(Result.isSuccess(decoded)).toBe(false);
482 });
483​
484 it("counts the roles that can push as write, and the ones that cannot as read", () => {
485 for (const permission of ["ADMIN", "MAINTAIN", "WRITE"]) {
486 expect(expectSuccess(decodeRepositoryAccessJson(repositoryJson(permission))).canWrite).toBe(
487 true,
488 );
489 }
490 for (const permission of ["TRIAGE", "READ", "NONE"]) {
491 expect(expectSuccess(decodeRepositoryAccessJson(repositoryJson(permission))).canWrite).toBe(
492 false,
493 );
494 }
495 });
496​
497 it("withholds write where gh names no permission, which is not a standing it gave", () => {
498 // The one place an unknown answer is not granted: a Merge button a reader cannot use wastes
499 // the press, where a missing one still leaves the pull request open on its host.
500 expect(expectSuccess(decodeRepositoryAccessJson(repositoryJson())).canWrite).toBe(false);
501 expect(expectSuccess(decodeRepositoryAccessJson(repositoryJson(null))).canWrite).toBe(false);
502 });
503});
504​
505describe("viewer permission decoding", () => {
506 const viewerJson = (repository: Record<string, unknown>) =>
507 JSON.stringify({ data: { repository } });
508​
509 it("reads the repository's role and the pull request's own viewer fields together", () => {
510 expect(
511 expectSuccess(
512 decodeViewerPermissionsJson(
513 viewerJson({
514 viewerPermission: "READ",
515 pullRequest: { viewerCanUpdate: true, viewerDidAuthor: true },
516 }),
517 ),
518 ),
519 ).toEqual({ canWrite: false, canUpdate: true, didAuthor: true });
520 });
521​
522 it("says no to a passer-by on a repository they can only read", () => {
523 expect(
524 expectSuccess(
525 decodeViewerPermissionsJson(
526 viewerJson({
527 viewerPermission: "READ",
528 pullRequest: { viewerCanUpdate: false, viewerDidAuthor: false },
529 }),
530 ),
531 ),
532 ).toEqual({ canWrite: false, canUpdate: false, didAuthor: false });
533 });
534​
535 it("reads silence as permission, but not as authorship", () => {
536 // A node the viewer cannot see comes back null. Updating is a permission, so an unknown
537 // answer grants it and lets the host refuse; authorship is a fact about who wrote the change,
538 // and claiming it for someone who did not is how an author's own rules get handed out.
539 expect(expectSuccess(decodeViewerPermissionsJson(viewerJson({ pullRequest: null })))).toEqual({
540 canWrite: false,
541 canUpdate: true,
542 didAuthor: false,
543 });
544 });
545});
546​
547describe("review thread decoding", () => {
548 const threadsJson = (
549 nodes: ReadonlyArray<Record<string, unknown>>,
550 pullRequest: Record<string, unknown> = {},
551 ) =>
552 JSON.stringify({
553 data: {
554 repository: {
555 pullRequest: {
556 reviewThreads: { totalCount: nodes.length, nodes },
557 author: null,
558 comments: { nodes: [] },
559 reviewRequests: { nodes: [] },
560 latestReviews: { nodes: [] },
561 ...pullRequest,
562 },
563 },
564 },
565 });
566​
567 it("carries what the reader may do with the pull request, off the conversation read", () => {
568 // The same response the threads arrive in, so knowing this costs no request of its own.
569 expect(
570 expectSuccess(
571 decodeReviewThreadsJson(
572 threadsJson([], { viewerCanUpdate: false, viewerDidAuthor: false }),
573 ),
574 ).viewer,
575 ).toEqual({ canUpdate: false, didAuthor: false });
576 expect(expectSuccess(decodeReviewThreadsJson(threadsJson([]))).viewer).toEqual({
577 canUpdate: true,
578 didAuthor: false,
579 });
580 });
581​
582 const comment = (id: string, body: string) => ({
583 id,
584 author: { login: "bilal", avatarUrl: "https://avatars/b.png" },
585 body,
586 createdAt: "2026-07-01T00:00:00Z",
587 url: `https://github.com/acme/web/pull/1#discussion_r${id}`,
588 });
589​
590 it("anchors a thread to its line and side, keeping the whole conversation", () => {
591 const reviewThreads = expectSuccess(
592 decodeReviewThreadsJson(
593 threadsJson([
594 {
595 id: "PRRT_1",
596 isResolved: false,
597 isOutdated: false,
598 path: "src/a.ts",
599 line: 42,
600 diffSide: "LEFT",
601 comments: { totalCount: 2, nodes: [comment("c1", "first"), comment("c2", "second")] },
602 },
603 ]),
604 ),
605 );
606 expect(reviewThreads.threads.map((entry) => entry.thread)).toEqual([
607 {
608 id: "PRRT_1",
609 path: "src/a.ts",
610 line: 42,
611 side: "left",
612 isResolved: false,
613 isOutdated: false,
614 comments: [
615 {
616 id: "c1",
617 author: { login: "bilal", name: null, avatarUrl: "https://avatars/b.png" },
618 body: "first",
619 createdAt: "2026-07-01T00:00:00Z",
620 url: "https://github.com/acme/web/pull/1#discussion_rc1",
621 },
622 {
623 id: "c2",
624 author: { login: "bilal", name: null, avatarUrl: "https://avatars/b.png" },
625 body: "second",
626 createdAt: "2026-07-01T00:00:00Z",
627 url: "https://github.com/acme/web/pull/1#discussion_rc2",
628 },
629 ],
630 },
631 ]);
632 });
633​
634 it("leaves an outdated thread without a line rather than pinning it to a stale one", () => {
635 const reviewThreads = expectSuccess(
636 decodeReviewThreadsJson(
637 threadsJson([
638 {
639 id: "PRRT_2",
640 isResolved: true,
641 isOutdated: true,
642 path: "src/a.ts",
643 // GitHub reports no current line once the thread has fallen off the diff.
644 line: null,
645 diffSide: "RIGHT",
646 comments: { totalCount: 1, nodes: [comment("c3", "stale")] },
647 },
648 ]),
649 ),
650 );
651 expect(reviewThreads.threads[0]?.thread).toMatchObject({
652 line: null,
653 isOutdated: true,
654 isResolved: true,
655 });
656 });
657​
658 it("keeps a resolved thread in the conversation as well as against its line", () => {
659 const decoded = expectSuccess(
660 decodeReviewThreadsJson(
661 threadsJson([
662 {
663 id: "PRRT_3",
664 isResolved: true,
665 path: "src/a.ts",
666 line: 7,
667 diffSide: "RIGHT",
668 comments: { totalCount: 1, nodes: [comment("c4", "done")] },
669 },
670 ]),
671 ),
672 );
673 // A resolved conversation is finished work, not unsaid work: the timeline reads it and the
674 // diff pins it to its line, the same as any other.
675 const threads = decoded.threads.map((entry) => entry.thread);
676 expect(reviewThreadConversation(threads).map((comment) => comment.id)).toEqual(["c4"]);
677 expect(threads).toHaveLength(1);
678 });
679});
680​
681describe("reviewer candidate decoding", () => {
682 const candidatesJson = (input: {
683 readonly assignable: ReadonlyArray<Record<string, unknown> | null>;
684 readonly requested?: ReadonlyArray<Record<string, unknown> | null>;
685 readonly author?: string;
686 readonly hasNextPage?: boolean;
687 }) =>
688 JSON.stringify({
689 data: {
690 repository: {
691 assignableUsers: {
692 pageInfo: { hasNextPage: input.hasNextPage ?? false },
693 nodes: input.assignable,
694 },
695 pullRequest: {
696 author: input.author === undefined ? null : { login: input.author },
697 reviewRequests: {
698 nodes: (input.requested ?? []).map((requestedReviewer) => ({ requestedReviewer })),
699 },
700 },
701 },
702 },
703 });
704​
705 it("leaves the author out of the people their own pull request can be sent to", () => {
706 const list = expectSuccess(
707 decodeReviewerCandidatesJson(
708 candidatesJson({
709 assignable: [{ login: "bilal" }, { login: "octocat", name: "The Octocat" }],
710 author: "bilal",
711 }),
712 ),
713 );
714 expect(list.candidates).toEqual([
715 {
716 id: "octocat",
717 kind: "user",
718 login: "octocat",
719 name: "The Octocat",
720 avatarUrl: null,
721 isRequested: false,
722 },
723 ]);
724 expect(list.truncated).toBe(false);
725 });
726​
727 it("marks whoever has already been asked, and leaves the rest to be asked", () => {
728 const list = expectSuccess(
729 decodeReviewerCandidatesJson(
730 candidatesJson({
731 assignable: [{ login: "octocat" }, { login: "hubot" }],
732 requested: [{ login: "octocat" }],
733 }),
734 ),
735 );
736 expect(list.candidates.map((candidate) => [candidate.login, candidate.isRequested])).toEqual([
737 ["octocat", true],
738 ["hubot", false],
739 ]);
740 });
741​
742 it("keeps a requested team apart from the people, so the request can be taken back", () => {
743 // A team is never among the assignable users, and a request that cannot be seen cannot be
744 // undone — so the ones GitHub reports are carried, marked as the teams they are.
745 const list = expectSuccess(
746 decodeReviewerCandidatesJson(
747 candidatesJson({
748 assignable: [{ login: "octocat" }],
749 requested: [{ slug: "reviewers", name: "Reviewers" }],
750 }),
751 ),
752 );
753 expect(list.candidates).toEqual([
754 {
755 id: "reviewers",
756 kind: "team",
757 login: "reviewers",
758 name: "Reviewers",
759 avatarUrl: null,
760 isRequested: true,
761 },
762 {
763 id: "octocat",
764 kind: "user",
765 login: "octocat",
766 name: null,
767 avatarUrl: null,
768 isRequested: false,
769 },
770 ]);
771 });
772​
773 it("says so when the repository has more people than the read asked for", () => {
774 expect(
775 expectSuccess(
776 decodeReviewerCandidatesJson(
777 candidatesJson({ assignable: [{ login: "octocat" }], hasNextPage: true }),
778 ),
779 ).truncated,
780 ).toBe(true);
781 });
782});
783​
784describe("reviewer request payload", () => {
785 it("sends people and teams in the two lists GitHub keeps them in", () => {
786 expect(
787 JSON.parse(
788 buildReviewerRequestJson([
789 { id: "octocat", kind: "user" },
790 { id: "reviewers", kind: "team" },
791 { id: "hubot", kind: "user" },
792 ]),
793 ),
794 ).toEqual({ reviewers: ["octocat", "hubot"], team_reviewers: ["reviewers"] });
795 });
796​
797 it("sends both lists even where one of them is empty, which is what GitHub reads", () => {
798 expect(JSON.parse(buildReviewerRequestJson([{ id: "octocat", kind: "user" }]))).toEqual({
799 reviewers: ["octocat"],
800 team_reviewers: [],
801 });
802 });
803});
804​
805describe("review submission payload", () => {
806 it("sends the verdict, the summary and every line comment in one body", () => {
807 const payload = JSON.parse(
808 buildReviewSubmissionJson({
809 verdict: "request-changes",
810 body: "Two things.",
811 comments: [
812 { path: "src/a.ts", line: 12, side: "right", body: "rename this" },
813 { path: "src/b.ts", line: 3, side: "left", body: "why remove?" },
814 ],
815 }),
816 ) as Record<string, unknown>;
817 expect(payload).toEqual({
818 event: "REQUEST_CHANGES",
819 body: "Two things.",
820 comments: [
821 { path: "src/a.ts", line: 12, side: "RIGHT", body: "rename this" },
822 { path: "src/b.ts", line: 3, side: "LEFT", body: "why remove?" },
823 ],
824 });
825 });
826​
827 it("sends an approval with no words and no comments", () => {
828 expect(
829 JSON.parse(buildReviewSubmissionJson({ verdict: "approve", body: "", comments: [] })),
830 ).toEqual({ event: "APPROVE", body: "", comments: [] });
831 });
832});
833​
834describe("decodePullRequestFilesJson", () => {
835 it("assembles a unified patch the files API does not return", () => {
836 const result = expectSuccess(
837 decodePullRequestFilesJson(
838 JSON.stringify([
839 { filename: "src/app.ts", status: "modified", patch: "@@ -1 +1 @@\n-old\n+new" },
840 ]),
841 ),
842 );
843​
844 expect(result.patch).toBe(
845 [
846 "diff --git a/src/app.ts b/src/app.ts",
847 "--- a/src/app.ts",
848 "+++ b/src/app.ts",
849 "@@ -1 +1 @@",
850 "-old",
851 "+new",
852 "",
853 ].join("\n"),
854 );
855 expect(result.truncated).toBe(false);
856 expect(result.rawCount).toBe(1);
857 });
858​
859 it("points an added file at /dev/null on the left and a removed one on the right", () => {
860 const result = expectSuccess(
861 decodePullRequestFilesJson(
862 JSON.stringify([
863 { filename: "src/new.ts", status: "added", patch: "@@ -0,0 +1 @@\n+hello" },
864 { filename: "src/gone.ts", status: "removed", patch: "@@ -1 +0,0 @@\n-bye" },
865 ]),
866 ),
867 );
868​
869 expect(result.patch).toBe(
870 [
871 "diff --git a/src/new.ts b/src/new.ts",
872 "new file mode 100644",
873 "--- /dev/null",
874 "+++ b/src/new.ts",
875 "@@ -0,0 +1 @@",
876 "+hello",
877 "diff --git a/src/gone.ts b/src/gone.ts",
878 "deleted file mode 100644",
879 "--- a/src/gone.ts",
880 "+++ /dev/null",
881 "@@ -1 +0,0 @@",
882 "-bye",
883 "",
884 ].join("\n"),
885 );
886 });
887​
888 it("names both paths of a rename, counting its hunks against the old one", () => {
889 const result = expectSuccess(
890 decodePullRequestFilesJson(
891 JSON.stringify([
892 {
893 filename: "src/new.ts",
894 status: "renamed",
895 previous_filename: "src/old.ts",
896 patch: "@@ -1 +1 @@\n-old\n+new",
897 },
898 ]),
899 ),
900 );
901​
902 expect(result.patch).toBe(
903 [
904 "diff --git a/src/old.ts b/src/new.ts",
905 "rename from src/old.ts",
906 "rename to src/new.ts",
907 "--- a/src/old.ts",
908 "+++ b/src/new.ts",
909 "@@ -1 +1 @@",
910 "-old",
911 "+new",
912 "",
913 ].join("\n"),
914 );
915 });
916​
917 it("still lists a file GitHub sent no hunks for, and says what was withheld", () => {
918 const result = expectSuccess(
919 decodePullRequestFilesJson(
920 JSON.stringify([
921 // Binary: it changed, and none of it can be shown.
922 { filename: "logo.png", status: "modified", additions: 4, deletions: 2 },
923 {
924 filename: "src/app.ts",
925 status: "modified",
926 additions: 1,
927 deletions: 1,
928 patch: "@@ -1 +1 @@\n-old\n+new",
929 },
930 ]),
931 ),
932 );
933​
934 // Dropping it would take the file out of the change altogether, not just its contents.
935 expect(result.patch).toContain("diff --git a/logo.png b/logo.png");
936 expect(result.patch).toContain("diff --git a/src/app.ts b/src/app.ts");
937 expect(result.truncated).toBe(true);
938 expect(result.rawCount).toBe(2);
939 });
940​
941 it("does not call a pure rename incomplete, since it has no hunks to withhold", () => {
942 const result = expectSuccess(
943 decodePullRequestFilesJson(
944 JSON.stringify([
945 {
946 filename: "src/new.ts",
947 previous_filename: "src/old.ts",
948 status: "renamed",
949 additions: 0,
950 deletions: 0,
951 },
952 ]),
953 ),
954 );
955​
956 expect(result.patch).toContain("rename from src/old.ts");
957 expect(result.truncated).toBe(false);
958 });
959});