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

GitHubPullRequestProvider.test.ts

968 lines31.0 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Redacted from "effect/Redacted";
5import type { PullRequestReaction } from "@t3tools/contracts";
6​
7import * as GitHubCli from "../sourceControl/GitHubCli.ts";
8import * as GitHubPullRequestCli from "./GitHubPullRequestCli.ts";
9import {
10 gitHubViewerPermissions,
11 loginAvatarUrl,
12 make,
13 makeProvider,
14} from "./GitHubPullRequestProvider.ts";
15import type { GitHubReviewThreadComments } from "./gitHubPullRequestJson.ts";
16​
17it.effect("maps credential verification failures without relabeling operation failures", () =>
18 Effect.gen(function* () {
19 let verificationFails = true;
20 let operations = 0;
21 const provider = yield* make.pipe(
22 Effect.provide(
23 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
24 withVerifiedCredential: (_input, use) =>
25 verificationFails
26 ? Effect.fail(
27 new GitHubPullRequestCli.GitHubViewerLoginUnavailableError({
28 command: "gh",
29 cwd: "/w",
30 }),
31 )
32 : use({ accountId: "123", viewer: "viewer", credentialFingerprint: "fingerprint" }),
33 }),
34 ),
35 );
36 const verify = provider.withVerifiedCredential;
37 if (verify === undefined)
38 return yield* Effect.die("credential verification was not implemented");
39 const input = { cwd: "/w", host: "github.com" };
40 const operation = () =>
41 Effect.sync(() => {
42 operations++;
43 }).pipe(Effect.andThen(Effect.fail("operation-failed")));
44 expect(yield* verify(input, operation).pipe(Effect.flip)).toMatchObject({
45 _tag: "PullRequestProviderError",
46 operation: "routeIdentity",
47 });
48 expect(operations).toBe(0);
49 verificationFails = false;
50 expect(yield* verify(input, operation).pipe(Effect.flip)).toBe("operation-failed");
51 expect(operations).toBe(1);
52 }),
53);
54​
55it.effect("uses one narrow read for a linked pull request summary", () =>
56 Effect.gen(function* () {
57 let summaryReads = 0;
58 const provider = yield* make.pipe(
59 Effect.provide(
60 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
61 getPullRequestSummary: () =>
62 Effect.sync(() => {
63 summaryReads += 1;
64 return {
65 number: 7,
66 title: "Summary",
67 url: "https://github.com/acme/web/pull/7",
68 headBranch: "feat/summary",
69 baseBranch: "main",
70 state: "open" as const,
71 updatedAt: "2026-08-24T12:34:56.000Z",
72 author: { login: "octocat", name: null, avatarUrl: null },
73 };
74 }),
75 }),
76 ),
77 );
78​
79 const readSummary = provider.getChangeRequestSummary;
80 if (readSummary === undefined) return yield* Effect.die("summary read was not implemented");
81 const summary = yield* readSummary({
82 cwd: "/w",
83 repository: "acme/web",
84 host: "github.com",
85 number: 7,
86 });
87​
88 expect(summary.state).toBe("open");
89 expect(summaryReads).toBe(1);
90 // The author's avatar comes from the login-shaped URL, not a second request.
91 expect(summary.author?.avatarUrl).toBe("https://github.com/octocat.png?size=80");
92 }),
93);
94​
95it.effect("declares host-native stacks and passes the one the CLI reads through", () =>
96 Effect.gen(function* () {
97 const stack = {
98 id: "42",
99 number: 3,
100 url: "https://github.com/acme/web/stacks/3",
101 base: "main",
102 layers: [
103 { number: 6, headBranch: "feat/one", state: "merged" as const },
104 { number: 7, headBranch: "feat/two", state: "open" as const },
105 ],
106 };
107 const provider = yield* make.pipe(
108 Effect.provide(
109 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
110 getPullRequestStack: (input) => Effect.succeed(input.number === 7 ? stack : null),
111 }),
112 ),
113 );
114​
115 expect(provider.capabilities.stacks).toBe(true);
116 const readStack = provider.getChangeRequestStack;
117 if (readStack === undefined) return yield* Effect.die("stack read was not implemented");
118 const ref = { cwd: "/w", repository: "acme/web", host: "github.com" };
119 expect(yield* readStack({ ...ref, number: 7 })).toEqual(stack);
120 expect(yield* readStack({ ...ref, number: 8 })).toBeNull();
121 }),
122);
123​
124it.effect("reports a failed stack read against its own operation", () =>
125 Effect.gen(function* () {
126 const provider = yield* make.pipe(
127 Effect.provide(
128 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
129 getPullRequestStack: () =>
130 Effect.fail(
131 new GitHubPullRequestCli.GitHubPullRequestReadError({
132 command: "gh",
133 cwd: "/w",
134 operation: "getPullRequestStack",
135 cause: new Error("unreadable"),
136 }),
137 ),
138 }),
139 ),
140 );
141​
142 const readStack = provider.getChangeRequestStack;
143 if (readStack === undefined) return yield* Effect.die("stack read was not implemented");
144 const error = yield* Effect.flip(
145 readStack({ cwd: "/w", repository: "acme/web", host: "github.com", number: 7 }),
146 );
147​
148 expect(error.operation).toBe("getChangeRequestStack");
149 expect(error.reason).toBe("failed");
150 }),
151);
152​
153describe("gitHubViewerPermissions", () => {
154 it("offers everything to a viewer who can write to the repository", () => {
155 expect(
156 gitHubViewerPermissions({
157 canWrite: true,
158 canTriage: true,
159 canUpdate: true,
160 didAuthor: false,
161 }),
162 ).toEqual({
163 // Arming a merge for later is the merge, so it travels with it.
164 actions: [
165 "merge",
166 "enable-auto-merge",
167 "disable-auto-merge",
168 "revert",
169 "approve-workflows",
170 "ready",
171 "draft",
172 "close",
173 "reopen",
174 ],
175 comment: true,
176 resolve: true,
177 stackRebase: true,
178 verdicts: ["comment", "approve", "request-changes"],
179 requestReviewers: true,
180 labels: true,
181 });
182 });
183​
184 it("leaves a passer-by on a repository they can only read nothing but the review", () => {
185 // Every open-source pull request somebody else opened: GitHub says no to all five actions
186 // and to resolving, and yes to commenting and to every verdict.
187 expect(
188 gitHubViewerPermissions({
189 canWrite: false,
190 canTriage: false,
191 canUpdate: false,
192 didAuthor: false,
193 }),
194 ).toEqual({
195 actions: [],
196 comment: true,
197 resolve: false,
198 verdicts: ["comment", "approve", "request-changes"],
199 // Asking somebody else to review is the one thing read access never stretches to.
200 requestReviewers: false,
201 labels: false,
202 });
203 });
204​
205 it("lets a triager label without letting them merge or ask for a review", () => {
206 const permissions = gitHubViewerPermissions({
207 canWrite: false,
208 canTriage: true,
209 canUpdate: false,
210 didAuthor: false,
211 });
212 expect(permissions.labels).toBe(true);
213 expect(permissions.requestReviewers).toBe(false);
214 expect(permissions.actions).toEqual([]);
215 });
216​
217 it("keeps an author's own pull request theirs to close, with read access and no more", () => {
218 expect(
219 gitHubViewerPermissions({
220 canWrite: false,
221 canTriage: false,
222 canUpdate: true,
223 didAuthor: true,
224 }),
225 ).toEqual({
226 // Merging is the one thing writing is needed for, now or later; the rest an author may do.
227 actions: ["ready", "draft", "close", "reopen"],
228 comment: true,
229 resolve: true,
230 // GitHub refuses an author's approval of their own change, so the page does not offer one.
231 verdicts: ["comment"],
232 requestReviewers: false,
233 labels: false,
234 });
235 });
236​
237 it.effect("uses the small viewer-access read for core permissions", () =>
238 Effect.gen(function* () {
239 const provider = yield* make;
240 const detail = yield* provider.getChangeRequest({
241 cwd: "/w",
242 repository: "acme/web",
243 host: "github.com",
244 number: 7,
245 });
246​
247 expect(detail.viewerPermissions).toEqual({
248 actions: ["ready", "draft", "close", "reopen"],
249 comment: true,
250 resolve: false,
251 verdicts: ["comment", "approve", "request-changes"],
252 requestReviewers: false,
253 labels: false,
254 });
255 expect(detail.workflowApprovalsRequired).toBeUndefined();
256 expect(detail.checks).toContainEqual({
257 name: "Workflow approval status",
258 status: "action-required",
259 description: "GitHub could not determine whether workflows are awaiting approval.",
260 url: null,
261 });
262 for (const fingerprint of ["broad", "restricted", "broad"]) {
263 const scoped = yield* provider
264 .getChangeRequest({
265 cwd: "/w",
266 repository: "acme/web",
267 host: "github.com",
268 number: 7,
269 })
270 .pipe(
271 Effect.provideService(GitHubCli.PinnedGitHubCredential, {
272 host: "github.com",
273 token: Redacted.make("credential"),
274 credentialFingerprint: fingerprint,
275 }),
276 );
277 expect(scoped.mergeCapabilities.squash).toBe(fingerprint !== "restricted");
278 }
279 }).pipe(
280 Effect.provide(
281 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
282 getPullRequestDetail: () =>
283 Effect.succeed({
284 authorId: null,
285 number: 7,
286 title: "Pull request 7",
287 url: "https://github.com/acme/web/pull/7",
288 author: null,
289 isCrossRepository: true,
290 headRepositoryOwner: null,
291 headBranch: "feat/page",
292 baseBranch: "main",
293 state: "open",
294 isDraft: false,
295 mergeability: "mergeable",
296 reviewDecision: null,
297 additions: 1,
298 deletions: 1,
299 createdAt: "2026-07-01T00:00:00Z",
300 updatedAt: "2026-07-02T00:00:00Z",
301 reviewRequestLogins: [],
302 hasTeamReviewRequest: false,
303 checksState: null,
304 labels: [],
305 body: "",
306 changedFiles: 1,
307 mergedAt: null,
308 closedAt: null,
309 checks: [],
310 comments: [],
311 commits: [],
312 }),
313 getRepositoryAccess: () =>
314 GitHubCli.PinnedGitHubCredential.pipe(
315 Effect.map((credential) => ({
316 canWrite: false,
317 mergeCapabilities: {
318 merge: true,
319 squash: credential?.credentialFingerprint !== "restricted",
320 rebase: true,
321 },
322 })),
323 ),
324 getViewerAccess: () =>
325 Effect.succeed({
326 canWrite: false,
327 canTriage: false,
328 canUpdate: true,
329 didAuthor: false,
330 }),
331 }),
332 ),
333 ),
334 );
335​
336 it.effect("keeps fork workflows awaiting approval out of the passing state", () =>
337 Effect.gen(function* () {
338 const provider = yield* make;
339 const detail = yield* provider.getChangeRequest({
340 cwd: "/w",
341 repository: "acme/web",
342 host: "github.com",
343 number: 7,
344 });
345​
346 expect(detail.workflowApprovalsRequired).toBe(1);
347 expect(detail.checks).toEqual([
348 {
349 name: "manual gate",
350 status: "action-required",
351 description: null,
352 url: "https://example.com/manual-gate",
353 },
354 {
355 name: "build",
356 status: "success",
357 description: null,
358 url: null,
359 },
360 {
361 name: "contributor tests",
362 status: "action-required",
363 description: "A maintainer must approve this workflow before it can run.",
364 url: "https://github.com/acme/web/actions/runs/123",
365 },
366 ]);
367 }).pipe(
368 Effect.provide(
369 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
370 getPullRequestDetail: () =>
371 Effect.succeed({
372 authorId: null,
373 number: 7,
374 title: "Pull request 7",
375 url: "https://github.com/acme/web/pull/7",
376 author: null,
377 isCrossRepository: true,
378 headRepositoryOwner: "octocat",
379 headSha: "abc123",
380 headBranch: "feat/page",
381 baseBranch: "main",
382 state: "open",
383 isDraft: false,
384 mergeability: "mergeable",
385 reviewDecision: null,
386 additions: 1,
387 deletions: 1,
388 createdAt: "2026-07-01T00:00:00Z",
389 updatedAt: "2026-07-02T00:00:00Z",
390 reviewRequestLogins: [],
391 hasTeamReviewRequest: false,
392 checksState: "passing",
393 labels: [],
394 body: "",
395 changedFiles: 1,
396 mergedAt: null,
397 closedAt: null,
398 checks: [
399 {
400 name: "manual gate",
401 status: "action-required",
402 description: null,
403 url: "https://example.com/manual-gate",
404 },
405 { name: "build", status: "success", description: null, url: null },
406 ],
407 comments: [],
408 commits: [],
409 }),
410 listWorkflowRunsRequiringApproval: () =>
411 Effect.succeed([
412 {
413 id: 123,
414 name: "contributor tests",
415 url: "https://github.com/acme/web/actions/runs/123",
416 },
417 ]),
418 getPullRequestBaseComparison: () =>
419 Effect.succeed({ behindBy: 0, viewerCanUpdate: true }),
420 getRepositoryAccess: () =>
421 Effect.succeed({
422 canWrite: true,
423 mergeCapabilities: { merge: true, squash: true, rebase: true },
424 }),
425 getViewerAccess: () =>
426 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
427 }),
428 ),
429 ),
430 );
431});
432​
433const openDetail = {
434 authorId: null,
435 number: 7,
436 title: "Pull request 7",
437 url: "https://github.com/acme/web/pull/7",
438 author: null,
439 isCrossRepository: true,
440 headRepositoryOwner: "acme",
441 headSha: "abc123",
442 headBranch: "feat/page",
443 baseBranch: "main",
444 state: "open" as const,
445 isDraft: false,
446 mergeability: "mergeable" as const,
447 reviewDecision: null,
448 additions: 1,
449 deletions: 1,
450 createdAt: "2026-07-01T00:00:00Z",
451 updatedAt: "2026-07-02T00:00:00Z",
452 reviewRequestLogins: [],
453 hasTeamReviewRequest: false,
454 checksState: null,
455 labels: [],
456 body: "",
457 changedFiles: 1,
458 mergedAt: null,
459 closedAt: null,
460 checks: [],
461 comments: [],
462 commits: [],
463};
464​
465it.effect("does not classify same-repository gates as fork workflow approvals", () =>
466 Effect.gen(function* () {
467 const provider = yield* make;
468 const detail = yield* provider.getChangeRequest({
469 cwd: "/w",
470 repository: "acme/web",
471 host: "github.com",
472 number: 7,
473 });
474​
475 expect(detail.workflowApprovalsRequired).toBe(0);
476 expect(detail.checks).toEqual([]);
477 }).pipe(
478 Effect.provide(
479 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
480 getPullRequestDetail: () => Effect.succeed({ ...openDetail, isCrossRepository: false }),
481 getPullRequestBaseComparison: () => Effect.succeed({ behindBy: 0, viewerCanUpdate: true }),
482 listWorkflowRunsRequiringApproval: () =>
483 Effect.die("same-repository pull requests must not probe fork workflow approvals"),
484 getRepositoryAccess: () =>
485 Effect.succeed({
486 canWrite: true,
487 mergeCapabilities: { merge: true, squash: true, rebase: true },
488 }),
489 getViewerAccess: () =>
490 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
491 }),
492 ),
493 ),
494);
495​
496it.effect("keeps an unsafe workflow approval scope visible as unknown", () =>
497 Effect.gen(function* () {
498 const provider = yield* make;
499 const detail = yield* provider.getChangeRequest({
500 cwd: "/w",
501 repository: "acme/web",
502 host: "github.com",
503 number: 7,
504 });
505​
506 expect(detail.workflowApprovalsRequired).toBeUndefined();
507 expect(detail.checks).toEqual([
508 {
509 name: "Workflow approval status",
510 status: "action-required",
511 description: "GitHub could not determine whether workflows are awaiting approval.",
512 url: null,
513 },
514 ]);
515 }).pipe(
516 Effect.provide(
517 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
518 getPullRequestDetail: () => Effect.succeed(openDetail),
519 getPullRequestBaseComparison: () => Effect.succeed({ behindBy: 0, viewerCanUpdate: true }),
520 listWorkflowRunsRequiringApproval: () =>
521 Effect.fail(
522 new GitHubPullRequestCli.GitHubWorkflowApprovalRefusedError({
523 command: "gh",
524 cwd: "/w",
525 number: 7,
526 reason: "head-not-unique",
527 observedCount: 2,
528 limit: 1_000,
529 }),
530 ),
531 getRepositoryAccess: () =>
532 Effect.succeed({
533 canWrite: true,
534 mergeCapabilities: { merge: true, squash: true, rebase: true },
535 }),
536 getViewerAccess: () =>
537 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
538 }),
539 ),
540 ),
541);
542​
543it.effect("propagates workflow discovery rate limits", () =>
544 Effect.gen(function* () {
545 const provider = yield* make;
546 const error = yield* provider
547 .getChangeRequest({
548 cwd: "/w",
549 repository: "acme/web",
550 host: "github.com",
551 number: 7,
552 })
553 .pipe(Effect.flip);
554​
555 expect(error.operation).toBe("getChangeRequest");
556 expect(error.reason).toBe("rate-limited");
557 }).pipe(
558 Effect.provide(
559 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
560 getPullRequestDetail: () => Effect.succeed(openDetail),
561 getPullRequestBaseComparison: () => Effect.succeed({ behindBy: 0, viewerCanUpdate: true }),
562 listWorkflowRunsRequiringApproval: () =>
563 Effect.fail(
564 new GitHubCli.GitHubCliRateLimitError({
565 command: "gh",
566 cwd: "/w",
567 cause: new Error("rate limited"),
568 }),
569 ),
570 getRepositoryAccess: () =>
571 Effect.succeed({
572 canWrite: true,
573 mergeCapabilities: { merge: true, squash: true, rebase: true },
574 }),
575 getViewerAccess: () =>
576 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
577 }),
578 ),
579 ),
580);
581​
582describe("getViewerPermissions", () => {
583 it.effect("checks fresh access without reading branch details for unrelated operations", () => {
584 let accessReads = 0;
585 return Effect.gen(function* () {
586 const provider = yield* make;
587 const permissions = yield* provider.getViewerPermissions({
588 cwd: "/w",
589 repository: "acme/web",
590 host: "github.com",
591 number: 7,
592 includeUpdateBranch: false,
593 });
594​
595 expect(accessReads).toBe(1);
596 expect(permissions.actions).toContain("merge");
597 expect(permissions.actions).not.toContain("update-branch");
598 }).pipe(
599 Effect.provide(
600 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
601 getPullRequestDetail: () => Effect.die("Unexpected detail read"),
602 getPullRequestBaseComparison: () => Effect.die("Unexpected comparison read"),
603 getViewerAccess: () =>
604 Effect.sync(() => {
605 accessReads++;
606 return { canWrite: true, canTriage: true, canUpdate: true, didAuthor: false };
607 }),
608 }),
609 ),
610 );
611 });
612​
613 const layerWithComparison = (
614 comparison: Effect.Effect<{
615 readonly behindBy: number | null;
616 readonly viewerCanUpdate: boolean;
617 }>,
618 ) =>
619 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
620 getPullRequestDetail: () => Effect.succeed(openDetail),
621 getPullRequestBaseComparison: () => comparison,
622 getViewerAccess: () =>
623 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
624 });
625​
626 it.effect("offers update-branch when the comparison grants it", () =>
627 Effect.gen(function* () {
628 const provider = yield* make;
629 const permissions = yield* provider.getViewerPermissions({
630 cwd: "/w",
631 repository: "acme/web",
632 host: "github.com",
633 number: 7,
634 });
635​
636 expect(permissions.actions).toContain("update-branch");
637 expect(permissions.updateMethods).toEqual(["merge", "rebase"]);
638 }).pipe(
639 Effect.provide(layerWithComparison(Effect.succeed({ behindBy: 3, viewerCanUpdate: true }))),
640 ),
641 );
642​
643 it.effect("uses the GraphQL reserve for manual permission checks", () => {
644 let viewerAllowReserve: boolean | undefined;
645 let comparisonAllowReserve: boolean | undefined;
646 return Effect.gen(function* () {
647 const provider = yield* make;
648 yield* provider.getViewerPermissions({
649 cwd: "/w",
650 repository: "acme/web",
651 host: "github.com",
652 number: 7,
653 });
654​
655 expect(viewerAllowReserve).toBe(true);
656 expect(comparisonAllowReserve).toBe(true);
657 }).pipe(
658 Effect.provide(
659 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
660 getPullRequestDetail: () => Effect.succeed(openDetail),
661 getPullRequestBaseComparison: (input) =>
662 Effect.sync(() => {
663 comparisonAllowReserve = input.allowReserve;
664 return { behindBy: 3, viewerCanUpdate: true };
665 }),
666 getViewerAccess: (input) =>
667 Effect.sync(() => {
668 viewerAllowReserve = input.allowReserve;
669 return { canWrite: true, canTriage: true, canUpdate: true, didAuthor: false };
670 }),
671 }),
672 ),
673 );
674 });
675​
676 it.effect("withholds update-branch when the comparison cannot be read", () =>
677 Effect.gen(function* () {
678 const provider = yield* make;
679 const permissions = yield* provider.getViewerPermissions({
680 cwd: "/w",
681 repository: "acme/web",
682 host: "github.com",
683 number: 7,
684 });
685​
686 expect(permissions.actions).not.toContain("update-branch");
687 expect(permissions.updateMethods).toBeUndefined();
688 // The rest of the answer survives a comparison nobody could make.
689 expect(permissions.actions).toContain("merge");
690 }).pipe(
691 Effect.provide(
692 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
693 getPullRequestDetail: () => Effect.succeed(openDetail),
694 getPullRequestBaseComparison: () =>
695 Effect.fail(
696 new GitHubPullRequestCli.GitHubPullRequestReadError({
697 command: "gh",
698 cwd: "/w",
699 operation: "getPullRequestBaseComparison",
700 cause: new Error("unreadable"),
701 }),
702 ),
703 getViewerAccess: () =>
704 Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
705 }),
706 ),
707 ),
708 );
709});
710​
711describe("getChangeRequest commits", () => {
712 const baseDetail = {
713 authorId: null,
714 number: 7,
715 title: "Pull request 7",
716 url: "https://github.com/acme/web/pull/7",
717 author: null,
718 headBranch: "feat/page",
719 baseBranch: "main",
720 state: "open" as const,
721 isDraft: false,
722 mergeability: "mergeable" as const,
723 additions: 1,
724 deletions: 1,
725 createdAt: "2026-07-01T00:00:00Z",
726 updatedAt: "2026-07-02T00:00:00Z",
727 reviewRequestLogins: [],
728 hasTeamReviewRequest: false,
729 checksState: null,
730 labels: [],
731 body: "",
732 changedFiles: 1,
733 mergedAt: null,
734 closedAt: null,
735 checks: [],
736 comments: [],
737 };
738​
739 const baseThreadComments = {
740 comments: [],
741 dismissalsByReviewId: new Map<string, string>(),
742 reviewThreads: [],
743 commentCount: 0,
744 truncated: false,
745 reactions: [],
746 reactionsById: new Map<string, ReadonlyArray<PullRequestReaction>>(),
747 reviewers: [],
748 avatarsByLogin: new Map<string, string>(),
749 commitStats: new Map<string, { readonly additions: number; readonly deletions: number }>(),
750 viewer: { canUpdate: true, didAuthor: false },
751 };
752​
753 const layerWith = (commits: GitHubReviewThreadComments["commits"]) =>
754 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
755 getPullRequestActivity: () =>
756 Effect.succeed({
757 author: baseDetail.author,
758 comments: baseDetail.comments,
759 commits: [
760 {
761 oid: "view-oldest",
762 messageHeadline: "gh pr view's oldest commit",
763 committedDate: "2026-01-01T00:00:00Z",
764 authors: [],
765 },
766 ],
767 }),
768 listReviewThreadComments: () => Effect.succeed({ ...baseThreadComments, commits }),
769 });
770​
771 it.effect("prefers the GraphQL commits, which are the newest, over the gh view list", () =>
772 Effect.gen(function* () {
773 const provider = yield* make;
774 const detail = yield* provider.getChangeRequestActivity({
775 cwd: "/w",
776 repository: "acme/web",
777 host: "github.com",
778 number: 7,
779 });
780​
781 expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest"]);
782 }).pipe(
783 Effect.provide(
784 layerWith([
785 {
786 oid: "graphql-newest",
787 messageHeadline: "the newest commit gh pr view drops",
788 committedDate: "2026-07-06T00:00:00Z",
789 authors: [],
790 },
791 ]),
792 ),
793 ),
794 );
795​
796 it.effect("falls back to the gh view list when the GraphQL read has no commits", () =>
797 Effect.gen(function* () {
798 const provider = yield* make;
799 const detail = yield* provider.getChangeRequestActivity({
800 cwd: "/w",
801 repository: "acme/web",
802 host: "github.com",
803 number: 7,
804 });
805​
806 expect(detail.commits.map((commit) => commit.oid)).toEqual(["view-oldest"]);
807 }).pipe(Effect.provide(layerWith([]))),
808 );
809});
810​
811describe("makeProvider", () => {
812 it.effect("registers under the enterprise kind and names it on a failure", () =>
813 Effect.gen(function* () {
814 const provider = yield* makeProvider("github-enterprise");
815 expect(provider.kind).toBe("github-enterprise");
816​
817 const error = yield* Effect.flip(provider.getViewer({ cwd: "/w", host: "ghe.example.com" }));
818 expect(error.provider).toBe("github-enterprise");
819 expect(error.reason).toBe("unauthenticated");
820 }).pipe(
821 Effect.provide(
822 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
823 getViewerLogin: () =>
824 Effect.fail(
825 new GitHubCli.GitHubCliAuthenticationError({ command: "gh", cwd: "/w", cause: null }),
826 ),
827 }),
828 ),
829 ),
830 );
831});
832​
833describe("getChangeRequestActivity dismissed reviews", () => {
834 const dismissedReview = (body: string) => ({
835 id: "PRR_1",
836 kind: "review" as const,
837 author: null,
838 body,
839 createdAt: "2026-07-03T00:00:00Z",
840 url: null,
841 path: null,
842 reviewState: "DISMISSED",
843 });
844 const threadComments: GitHubReviewThreadComments = {
845 comments: [],
846 dismissalsByReviewId: new Map([["PRR_1", "Dismissing prior approval to re-evaluate 9b66581"]]),
847 reviewThreads: [],
848 commentCount: 0,
849 truncated: false,
850 reactions: [],
851 reactionsById: new Map(),
852 reviewers: [],
853 avatarsByLogin: new Map(),
854 commitStats: new Map(),
855 commits: [],
856 viewer: { canUpdate: true, didAuthor: false },
857 };
858 const layerFor = (body: string) =>
859 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
860 getPullRequestActivity: () =>
861 Effect.succeed({ author: null, comments: [dismissedReview(body)], commits: [] }),
862 listReviewThreadComments: () => Effect.succeed(threadComments),
863 });
864 const readActivity = Effect.gen(function* () {
865 const provider = yield* make;
866 return yield* provider.getChangeRequestActivity({
867 cwd: "/w",
868 repository: "acme/web",
869 host: "github.com",
870 number: 7,
871 });
872 });
873​
874 it.effect("fills a marker-only dismissed review with the timeline's reason", () =>
875 // Macroscope's approvals carry only an HTML comment, which markdown renders as nothing —
876 // an empty-string check misses them and the card opens onto nothing.
877 readActivity.pipe(
878 Effect.map((activity) => {
879 expect(activity.comments[0]?.body).toBe("Dismissing prior approval to re-evaluate 9b66581");
880 }),
881 Effect.provide(layerFor("<!-- Macroscope (Approvability) review body marker -->")),
882 ),
883 );
884​
885 it.effect("keeps the words of a dismissed review that has its own", () =>
886 readActivity.pipe(
887 Effect.map((activity) => {
888 expect(activity.comments[0]?.body).toBe("These findings still stand.");
889 }),
890 Effect.provide(layerFor("These findings still stand.")),
891 ),
892 );
893});
894​
895describe("editing", () => {
896 const rewrites: Array<unknown> = [];
897​
898 it.effect("hands a rewrite to the CLI as the request named it", () =>
899 Effect.gen(function* () {
900 const provider = yield* make;
901​
902 expect(provider.capabilities.edit).toEqual({ changeRequest: true, comment: true });
903 yield* provider.updateChangeRequest!({
904 cwd: "/w",
905 repository: "acme/web",
906 host: "github.com",
907 number: 7,
908 title: "A better title",
909 });
910 yield* provider.updateComment!({
911 cwd: "/w",
912 repository: "acme/web",
913 host: "github.com",
914 number: 7,
915 commentId: "IC_1",
916 kind: "review-comment",
917 body: "Reworded.",
918 });
919​
920 expect(rewrites).toEqual([
921 {
922 cwd: "/w",
923 repository: "acme/web",
924 host: "github.com",
925 number: 7,
926 title: "A better title",
927 },
928 {
929 cwd: "/w",
930 repository: "acme/web",
931 host: "github.com",
932 number: 7,
933 commentId: "IC_1",
934 kind: "review-comment",
935 body: "Reworded.",
936 },
937 ]);
938 }).pipe(
939 Effect.provide(
940 Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
941 updatePullRequest: (input) => Effect.sync(() => void rewrites.push(input)),
942 updateComment: (input) => Effect.sync(() => void rewrites.push(input)),
943 }),
944 ),
945 ),
946 );
947});
948​
949describe("loginAvatarUrl", () => {
950 it("serves a user's picture from the host they belong to", () => {
951 expect(loginAvatarUrl("octocat", "github.com")).toBe("https://github.com/octocat.png?size=80");
952 expect(loginAvatarUrl("octocat", "ghe.example.com")).toBe(
953 "https://ghe.example.com/octocat.png?size=80",
954 );
955 });
956​
957 it("has nothing for an app, which names no page", () => {
958 // `dependabot[bot]` has a picture, but not at `/dependabot[bot].png` — a guess that 404s is
959 // worse than the initials it would replace.
960 expect(loginAvatarUrl("dependabot[bot]", "github.com")).toBeNull();
961 });
962​
963 it("refuses anything that is not a login, rather than building a URL out of it", () => {
964 for (const login of ["../../etc", "a b", "-leading", "x".repeat(40), ""]) {
965 expect(loginAvatarUrl(login, "github.com")).toBeNull();
966 }
967 });
968});