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