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

GitHubPullRequestProvider.test.ts

1,014 lines32.5 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 * as TestClock from "effect/testing/TestClock";
6import type { PullRequestReaction } from "@t3tools/contracts";
7​
8import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts";
9import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
10import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
11import type {
12 GitHubPullRequestCore,
13 GitHubPullRequestSearchItem,
14} from "./gitHubPullRequestJson.ts";
15import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts";
16import type { GitHubReviewThreadComments } from "./gitHubPullRequestJson.ts";
17​
18const coreFields = {
19 maintainerCanModify: true,
20 canEditMaintainerEdits: false,
21 reviewRequestTeams: [],
22 checksTruncated: false,
23 comparison: { behindBy: 0, viewerCanUpdate: true },
24 viewerAccess: {
25 canWrite: true,
26 canTriage: true,
27 canUpdate: true,
28 didAuthor: false,
29 mergeCapabilities: { merge: true, squash: true, rebase: true },
30 autoMergeAllowed: true,
31 },
32};
33​
34it.effect("maps credential verification failures without relabeling operation failures", () =>
35 Effect.gen(function* () {
36 let verificationFails = true;
37 let operations = 0;
38 const provider = yield* make.pipe(
39 Effect.provide(
40 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
41 revalidateChecks: (_input, read) => read,
42 withVerifiedCredential: (_input, use) =>
43 verificationFails
44 ? Effect.fail(
45 new GitHubPullRequestApi.GitHubViewerLoginUnavailableError({
46 cwd: "/w",
47 }),
48 )
49 : use({ accountId: "123", viewer: "viewer", credentialFingerprint: "fingerprint" }),
50 }),
51 ),
52 );
53 const verify = provider.withVerifiedCredential;
54 if (verify === undefined)
55 return yield* Effect.die("credential verification was not implemented");
56 const input = { cwd: "/w", host: "github.com" };
57 const operation = () =>
58 Effect.sync(() => {
59 operations++;
60 }).pipe(Effect.andThen(Effect.fail("operation-failed")));
61 expect(yield* verify(input, operation).pipe(Effect.flip)).toMatchObject({
62 _tag: "PullRequestProviderError",
63 operation: "routeIdentity",
64 });
65 expect(operations).toBe(0);
66 verificationFails = false;
67 expect(yield* verify(input, operation).pipe(Effect.flip)).toBe("operation-failed");
68 expect(operations).toBe(1);
69 }),
70);
71​
72it.effect.each([
73 { status: 403, githubErrorTypes: [], reason: "forbidden" },
74 { status: 503, githubErrorTypes: [], reason: "failed" },
75 { status: 200, githubErrorTypes: ["FORBIDDEN"], reason: "forbidden" },
76 { status: 200, githubErrorTypes: ["INTERNAL"], reason: "failed" },
77 { status: 200, githubErrorTypes: ["FORBIDDEN", "NOT_FOUND"], reason: "forbidden" },
78 { status: 200, githubErrorTypes: ["FORBIDDEN", "INTERNAL"], reason: "failed" },
79 { status: 200, githubErrorTypes: ["FORBIDDEN", null], reason: "failed" },
80])("classifies GitHub response $status as $reason", ({ status, githubErrorTypes, reason }) =>
81 Effect.gen(function* () {
82 const provider = yield* make.pipe(
83 Effect.provide(
84 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
85 revalidateChecks: (_input, read) => read,
86 getPullRequestDetail: () =>
87 Effect.fail(
88 new GitHubRequestPolicy.GitHubApiResponseError({
89 host: "github.com",
90 operation: "detail",
91 status,
92 githubErrorTypes,
93 }),
94 ),
95 }),
96 ),
97 );
98 const error = yield* provider
99 .getChangeRequest({
100 cwd: "/w",
101 repository: "acme/web",
102 host: "github.com",
103 number: 7,
104 })
105 .pipe(Effect.flip);
106 expect(error.reason).toBe(reason);
107 }),
108);
109​
110it.effect("advertises standalone branch deletion for source repository writers", () =>
111 Effect.gen(function* () {
112 let action: string | undefined;
113 const provider = yield* make.pipe(
114 Effect.provide(
115 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
116 runPullRequestAction: (input) =>
117 Effect.sync(() => {
118 action = input.action;
119 }),
120 }),
121 ),
122 );
123 expect(provider.capabilities.deleteBranch).toBe(true);
124 expect(provider.capabilities.actions).toContain("delete-branch");
125 yield* provider.runAction({
126 cwd: "/w",
127 repository: "acme/web",
128 host: "github.com",
129 number: 7,
130 action: "delete-branch",
131 });
132 expect(action).toBe("delete-branch");
133 const access = {
134 canWrite: false,
135 canTriage: false,
136 canUpdate: false,
137 didAuthor: false,
138 canDeleteBranch: true,
139 autoMergeAllowed: true,
140 };
141 expect(gitHubViewerPermissions(access).actions).toContain("delete-branch");
142 expect(
143 gitHubViewerPermissions({
144 ...access,
145 canWrite: true,
146 didAuthor: true,
147 canDeleteBranch: false,
148 }).actions,
149 ).not.toContain("delete-branch");
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 canDeleteBranch: true,
159 canTriage: true,
160 canUpdate: true,
161 didAuthor: false,
162 autoMergeAllowed: true,
163 }),
164 ).toEqual({
165 actions: [
166 "delete-branch",
167 "merge",
168 "enable-auto-merge",
169 "disable-auto-merge",
170 "revert",
171 "approve-workflows",
172 "ready",
173 "draft",
174 "close",
175 "reopen",
176 ],
177 comment: true,
178 resolve: true,
179 stackRebase: true,
180 verdicts: ["comment", "approve", "request-changes"],
181 requestReviewers: true,
182 labels: true,
183 });
184 });
185​
186 it("leaves a passer-by on a repository they can only read nothing but the review", () => {
187 expect(
188 gitHubViewerPermissions({
189 canWrite: false,
190 canTriage: false,
191 canUpdate: false,
192 didAuthor: false,
193 autoMergeAllowed: true,
194 }),
195 ).toEqual({
196 actions: [],
197 comment: true,
198 resolve: false,
199 verdicts: ["comment", "approve", "request-changes"],
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 autoMergeAllowed: true,
212 });
213 expect(permissions.labels).toBe(true);
214 expect(permissions.requestReviewers).toBe(false);
215 expect(permissions.actions).toEqual([]);
216 });
217​
218 it("keeps an author's own pull request theirs to close, with read access and no more", () => {
219 expect(
220 gitHubViewerPermissions({
221 canWrite: false,
222 canTriage: false,
223 canUpdate: true,
224 didAuthor: true,
225 autoMergeAllowed: true,
226 }),
227 ).toEqual({
228 actions: ["ready", "draft", "close", "reopen"],
229 comment: true,
230 resolve: true,
231 verdicts: ["comment"],
232 requestReviewers: false,
233 labels: false,
234 });
235 });
236​
237 it.effect("uses the permissions carried by the core read without a second request", () =>
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 maintainerEdits: false,
249 actions: ["ready", "draft", "close", "reopen"],
250 comment: true,
251 resolve: false,
252 verdicts: ["comment", "approve", "request-changes"],
253 requestReviewers: false,
254 labels: false,
255 });
256 expect(detail.workflowApprovalsRequired).toBeUndefined();
257 expect(detail.checks).toContainEqual({
258 name: "Workflow approval status",
259 status: "action-required",
260 description: "GitHub could not determine whether workflows are awaiting approval.",
261 url: null,
262 placeholder: true,
263 });
264 for (const fingerprint of ["broad", "restricted", "broad"]) {
265 const scoped = yield* provider
266 .getChangeRequest({
267 cwd: "/w",
268 repository: "acme/web",
269 host: "github.com",
270 number: 7,
271 })
272 .pipe(
273 Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, {
274 host: "github.com",
275 token: Redacted.make("credential"),
276 credentialFingerprint: fingerprint,
277 }),
278 );
279 expect(scoped.mergeCapabilities.squash).toBe(fingerprint !== "restricted");
280 }
281 }).pipe(
282 Effect.provide(
283 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
284 revalidateChecks: (_input, read) => read,
285 getPullRequestDetail: () =>
286 Effect.succeed({
287 ...coreFields,
288 comparison: null,
289 viewerAccess: { ...coreFields.viewerAccess, canWrite: false, canTriage: false },
290 number: 7,
291 title: "Pull request 7",
292 url: "https://github.com/acme/web/pull/7",
293 author: null,
294 isCrossRepository: true,
295 headRepositoryOwner: null,
296 headBranch: "feat/page",
297 baseBranch: "main",
298 state: "open" as const,
299 isDraft: false,
300 mergeability: "mergeable" as const,
301 reviewDecision: null,
302 additions: 1,
303 deletions: 1,
304 createdAt: "2026-07-01T00:00:00Z",
305 updatedAt: "2026-07-02T00:00:00Z",
306 reviewRequestLogins: [],
307 hasTeamReviewRequest: false,
308 checksState: null,
309 labels: [],
310 body: "",
311 changedFiles: 1,
312 mergedAt: null,
313 closedAt: null,
314 checks: [],
315 comments: [],
316 commits: [],
317 }).pipe(
318 Effect.flatMap((detail) =>
319 GitHubRequestPolicy.PinnedGitHubCredential.pipe(
320 Effect.map((credential) => ({
321 ...detail,
322 viewerAccess: {
323 ...detail.viewerAccess,
324 mergeCapabilities: {
325 ...detail.viewerAccess.mergeCapabilities,
326 squash: credential?.credentialFingerprint !== "restricted",
327 },
328 },
329 })),
330 ),
331 ),
332 ),
333 }),
334 ),
335 ),
336 );
337​
338 it.effect("keeps fork workflows awaiting approval out of the passing state", () =>
339 Effect.gen(function* () {
340 const provider = yield* make;
341 const detail = yield* provider.getChangeRequest({
342 cwd: "/w",
343 repository: "acme/web",
344 host: "github.com",
345 number: 7,
346 });
347​
348 const readChecks = provider.getChangeRequestChecks;
349 if (readChecks === undefined) return yield* Effect.die("checks read missing");
350 expect(
351 yield* readChecks({ cwd: "/w", repository: "acme/web", host: "github.com", number: 7 }),
352 ).toEqual({ state: detail.state, checks: detail.checks });
353 expect(detail.workflowApprovalsRequired).toBe(1);
354 expect(detail.checks).toEqual([
355 {
356 name: "manual gate",
357 status: "action-required",
358 description: null,
359 url: "https://example.com/manual-gate",
360 },
361 {
362 name: "build",
363 status: "success",
364 description: null,
365 url: null,
366 },
367 {
368 name: "contributor tests",
369 status: "action-required",
370 description: "A maintainer must approve this workflow before it can run.",
371 url: "https://github.com/acme/web/actions/runs/123",
372 },
373 ]);
374 }).pipe(
375 Effect.provide(
376 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
377 revalidateChecks: (_input, read) => read,
378 getPullRequestDetail: () =>
379 Effect.succeed({
380 ...coreFields,
381 number: 7,
382 title: "Pull request 7",
383 url: "https://github.com/acme/web/pull/7",
384 author: null,
385 isCrossRepository: true,
386 headRepositoryOwner: "octocat",
387 headSha: "abc123",
388 headBranch: "feat/page",
389 baseBranch: "main",
390 state: "open",
391 isDraft: false,
392 mergeability: "mergeable",
393 reviewDecision: null,
394 additions: 1,
395 deletions: 1,
396 createdAt: "2026-07-01T00:00:00Z",
397 updatedAt: "2026-07-02T00:00:00Z",
398 reviewRequestLogins: [],
399 hasTeamReviewRequest: false,
400 checksState: "passing",
401 labels: [],
402 body: "",
403 changedFiles: 1,
404 mergedAt: null,
405 closedAt: null,
406 checks: [
407 {
408 name: "manual gate",
409 status: "action-required",
410 description: null,
411 url: "https://example.com/manual-gate",
412 },
413 { name: "build", status: "success", description: null, url: null },
414 ],
415 comments: [],
416 commits: [],
417 }),
418 listUnfinishedWorkflowRuns: () => Effect.succeed([]),
419 listWorkflowRunsRequiringApproval: () =>
420 Effect.succeed([
421 {
422 id: 123,
423 name: "contributor tests",
424 url: "https://github.com/acme/web/actions/runs/123",
425 },
426 ]),
427 getViewerAccess: () =>
428 Effect.succeed({
429 canWrite: true,
430 canTriage: true,
431 canUpdate: true,
432 didAuthor: false,
433 mergeCapabilities: { merge: true, squash: true, rebase: true },
434 autoMergeAllowed: true,
435 }),
436 }),
437 ),
438 ),
439 );
440});
441​
442const openDetail = {
443 ...coreFields,
444 number: 7,
445 title: "Pull request 7",
446 url: "https://github.com/acme/web/pull/7",
447 author: null,
448 isCrossRepository: true,
449 headRepositoryOwner: "acme",
450 headSha: "abc123",
451 headBranch: "feat/page",
452 baseBranch: "main",
453 state: "open" as const,
454 isDraft: false,
455 mergeability: "mergeable" as const,
456 reviewDecision: null,
457 additions: 1,
458 deletions: 1,
459 createdAt: "2026-07-01T00:00:00Z",
460 updatedAt: "2026-07-02T00:00:00Z",
461 reviewRequestLogins: [],
462 hasTeamReviewRequest: false,
463 checksState: null,
464 labels: [],
465 body: "",
466 changedFiles: 1,
467 mergedAt: null,
468 closedAt: null,
469 checks: [],
470 comments: [],
471 commits: [],
472};
473​
474const sameRepositoryChecks = [
475 { name: "CodeRabbit", status: "skipped" as const, description: null, url: null },
476 {
477 name: "lint",
478 status: "pending" as const,
479 description: null,
480 url: "https://github.com/acme/web/actions/runs/201/job/1",
481 },
482];
483​
484it.effect("adds unstarted same-repository workflow runs as pending, not as approvals", () =>
485 Effect.gen(function* () {
486 const provider = yield* make;
487 const detail = yield* provider.getChangeRequest({
488 cwd: "/w",
489 repository: "acme/web",
490 host: "github.com",
491 number: 7,
492 });
493​
494 expect(detail.workflowApprovalsRequired).toBe(0);
495 expect(detail.checks).toEqual([
496 ...sameRepositoryChecks,
497 {
498 name: "CI",
499 status: "pending",
500 description: "This workflow run has not finished.",
501 url: "https://github.com/acme/web/actions/runs/202",
502 },
503 ]);
504 }).pipe(
505 Effect.provide(
506 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
507 revalidateChecks: (_input, read) => read,
508 getPullRequestDetail: () =>
509 Effect.succeed({ ...openDetail, isCrossRepository: false, checks: sameRepositoryChecks }),
510 // Run 201 is represented by its running job; run 202 is queued before any job exists.
511 listUnfinishedWorkflowRuns: () =>
512 Effect.succeed([
513 { id: 201, name: "Lint", url: "https://github.com/acme/web/actions/runs/201" },
514 { id: 202, name: "CI", url: "https://github.com/acme/web/actions/runs/202" },
515 ]),
516 listWorkflowRunsRequiringApproval: () =>
517 Effect.die("same-repository pull requests must not probe fork workflow approvals"),
518 getViewerAccess: () =>
519 Effect.succeed({
520 canWrite: true,
521 canTriage: true,
522 canUpdate: true,
523 didAuthor: false,
524 mergeCapabilities: { merge: true, squash: true, rebase: true },
525 autoMergeAllowed: true,
526 }),
527 }),
528 ),
529 ),
530);
531​
532it.effect("keeps an unsafe workflow approval scope visible as unknown", () =>
533 Effect.gen(function* () {
534 const provider = yield* make;
535 const detail = yield* provider.getChangeRequest({
536 cwd: "/w",
537 repository: "acme/web",
538 host: "github.com",
539 number: 7,
540 });
541​
542 expect(detail.workflowApprovalsRequired).toBeUndefined();
543 expect(detail.checks).toEqual([
544 {
545 name: "Workflow approval status",
546 status: "action-required",
547 description: "GitHub could not determine whether workflows are awaiting approval.",
548 url: null,
549 placeholder: true,
550 },
551 ]);
552 }).pipe(
553 Effect.provide(
554 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
555 revalidateChecks: (_input, read) => read,
556 getPullRequestDetail: () => Effect.succeed(openDetail),
557 listUnfinishedWorkflowRuns: () => Effect.succeed([]),
558 listWorkflowRunsRequiringApproval: () =>
559 Effect.fail(
560 new GitHubPullRequestApi.GitHubWorkflowApprovalRefusedError({
561 cwd: "/w",
562 number: 7,
563 reason: "head-not-unique",
564 observedCount: 2,
565 limit: 1_000,
566 }),
567 ),
568 getViewerAccess: () =>
569 Effect.succeed({
570 canWrite: true,
571 canTriage: true,
572 canUpdate: true,
573 didAuthor: false,
574 mergeCapabilities: { merge: true, squash: true, rebase: true },
575 autoMergeAllowed: true,
576 }),
577 }),
578 ),
579 ),
580);
581​
582describe("getViewerPermissions", () => {
583 const access = {
584 canWrite: true,
585 canTriage: true,
586 canUpdate: true,
587 didAuthor: false,
588 mergeCapabilities: { merge: true, squash: true, rebase: true },
589 autoMergeAllowed: true,
590 };
591​
592 const layerWithDetail = (
593 detail: Effect.Effect<GitHubPullRequestCore, GitHubPullRequestApi.GitHubPullRequestApiError>,
594 onDetail: (allowReserve: boolean) => void = () => {},
595 ) =>
596 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
597 revalidateChecks: (_input, read) => read,
598 getPullRequestDetail: () =>
599 SourceControlRateLimit.Interactive.pipe(
600 Effect.tap((allowReserve) => Effect.sync(() => onDetail(allowReserve))),
601 Effect.flatMap(() => detail),
602 ),
603 getViewerAccess: () => Effect.die("Unexpected access read"),
604 });
605​
606 it.effect("answers access and update-branch from the one detail read", () =>
607 Effect.gen(function* () {
608 const provider = yield* make;
609 const permissions = yield* provider.getViewerPermissions({
610 cwd: "/w",
611 repository: "acme/web",
612 host: "github.com",
613 number: 7,
614 });
615​
616 expect(permissions.actions).toContain("merge");
617 expect(permissions.actions).toContain("update-branch");
618 expect(permissions.updateMethods).toEqual(["merge", "rebase"]);
619 }).pipe(
620 Effect.provide(
621 layerWithDetail(
622 Effect.succeed({ ...openDetail, comparison: { behindBy: 3, viewerCanUpdate: true } }),
623 ),
624 ),
625 ),
626 );
627​
628 it.effect("still answers from the light access read when the detail read fails", () =>
629 Effect.gen(function* () {
630 const provider = yield* make;
631 const permissions = yield* provider.getViewerPermissions({
632 cwd: "/w",
633 repository: "acme/web",
634 host: "github.com",
635 number: 7,
636 });
637 expect(permissions.actions).toContain("merge");
638 expect(permissions.actions).not.toContain("update-branch");
639 }).pipe(
640 Effect.provide(
641 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
642 revalidateChecks: (_input, read) => read,
643 getPullRequestDetail: () =>
644 Effect.fail(
645 new GitHubPullRequestApi.GitHubPullRequestReadError({
646 cwd: "/w",
647 operation: "getPullRequestDetail",
648 cause: new Error("head changed"),
649 }),
650 ),
651 getViewerAccess: () => Effect.succeed(access),
652 }),
653 ),
654 ),
655 );
656​
657 it.effect("withholds update-branch where GitHub could not compare the branch", () =>
658 Effect.gen(function* () {
659 const provider = yield* make;
660 const permissions = yield* provider.getViewerPermissions({
661 cwd: "/w",
662 repository: "acme/web",
663 host: "github.com",
664 number: 7,
665 });
666​
667 expect(permissions.actions).not.toContain("update-branch");
668 expect(permissions.updateMethods).toBeUndefined();
669 expect(permissions.actions).toContain("merge");
670 }).pipe(Effect.provide(layerWithDetail(Effect.succeed({ ...openDetail, comparison: null })))),
671 );
672});
673​
674describe("getChangeRequest commits", () => {
675 const baseDetail = {
676 number: 7,
677 title: "Pull request 7",
678 url: "https://github.com/acme/web/pull/7",
679 author: null,
680 headBranch: "feat/page",
681 baseBranch: "main",
682 state: "open" as const,
683 isDraft: false,
684 mergeability: "mergeable" as const,
685 additions: 1,
686 deletions: 1,
687 createdAt: "2026-07-01T00:00:00Z",
688 updatedAt: "2026-07-02T00:00:00Z",
689 reviewRequestLogins: [],
690 hasTeamReviewRequest: false,
691 checksState: null,
692 labels: [],
693 body: "",
694 changedFiles: 1,
695 mergedAt: null,
696 closedAt: null,
697 checks: [],
698 comments: [],
699 };
700​
701 const baseThreadComments = {
702 comments: [],
703 dismissalsByReviewId: new Map<string, string>(),
704 reviewThreads: [],
705 commentCount: 0,
706 truncated: false,
707 reviewThreadsTruncated: false,
708 reactions: [],
709 reactionsById: new Map<string, ReadonlyArray<PullRequestReaction>>(),
710 editedAtById: new Map<string, string>(),
711 reviewers: [],
712 avatarsByLogin: new Map<string, string>(),
713 botLogins: new Set<string>(),
714 viewer: { canUpdate: true, didAuthor: false },
715 };
716​
717 const layerWith = (commits: GitHubReviewThreadComments["commits"]) =>
718 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
719 revalidateChecks: (_input, read) => read,
720 getPullRequestActivity: () =>
721 Effect.succeed({
722 author: baseDetail.author,
723 comments: baseDetail.comments,
724 commits: [
725 {
726 oid: "graphql-newest",
727 messageHeadline: "Activity metadata",
728 committedDate: "2026-07-06T00:00:00Z",
729 additions: 18,
730 deletions: 7,
731 authors: [],
732 },
733 {
734 oid: "view-oldest",
735 messageHeadline: "gh pr view's oldest commit",
736 committedDate: "2026-01-01T00:00:00Z",
737 authors: [],
738 },
739 ],
740 }),
741 listReviewThreadComments: () => Effect.succeed({ ...baseThreadComments, commits }),
742 });
743​
744 it.effect("prefers the GraphQL commits, which are the newest, over the gh view list", () =>
745 Effect.gen(function* () {
746 const provider = yield* make;
747 const detail = yield* provider.getChangeRequestActivity({
748 cwd: "/w",
749 repository: "acme/web",
750 host: "github.com",
751 number: 7,
752 });
753​
754 expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest"]);
755 expect(detail.commits[0]).toMatchObject({
756 messageHeadline: "the newest commit gh pr view drops",
757 additions: 18,
758 deletions: 7,
759 });
760 }).pipe(
761 Effect.provide(
762 layerWith([
763 {
764 oid: "graphql-newest",
765 messageHeadline: "the newest commit gh pr view drops",
766 committedDate: "2026-07-06T00:00:00Z",
767 authors: [],
768 },
769 ]),
770 ),
771 ),
772 );
773​
774 it.effect("falls back to the gh view list when the GraphQL read has no commits", () =>
775 Effect.gen(function* () {
776 const provider = yield* make;
777 const detail = yield* provider.getChangeRequestActivity({
778 cwd: "/w",
779 repository: "acme/web",
780 host: "github.com",
781 number: 7,
782 });
783​
784 expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest", "view-oldest"]);
785 }).pipe(Effect.provide(layerWith([]))),
786 );
787});
788​
789describe("getChangeRequestActivity dismissed reviews", () => {
790 const dismissedReview = (body: string) => ({
791 id: "PRR_1",
792 kind: "review" as const,
793 author: { login: "macroscopeapp", name: null, avatarUrl: null },
794 body,
795 createdAt: "2026-07-03T00:00:00Z",
796 url: null,
797 path: null,
798 reviewState: "DISMISSED",
799 });
800 const threadComments: GitHubReviewThreadComments = {
801 comments: [],
802 dismissalsByReviewId: new Map([["PRR_1", "Dismissing prior approval to re-evaluate 9b66581"]]),
803 reviewThreads: [],
804 commentCount: 0,
805 truncated: false,
806 reviewThreadsTruncated: false,
807 reactions: [],
808 reactionsById: new Map(),
809 editedAtById: new Map([["PRR_1", "2026-07-04T00:00:00Z"]]),
810 reviewers: [],
811 avatarsByLogin: new Map(),
812 botLogins: new Set(["macroscopeapp"]),
813 commits: [],
814 viewer: { canUpdate: true, didAuthor: false },
815 };
816 const layerFor = (body: string) =>
817 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
818 revalidateChecks: (_input, read) => read,
819 getPullRequestActivity: () =>
820 Effect.succeed({ author: null, comments: [dismissedReview(body)], commits: [] }),
821 listReviewThreadComments: () => Effect.succeed(threadComments),
822 });
823 const readActivity = Effect.gen(function* () {
824 const provider = yield* make;
825 return yield* provider.getChangeRequestActivity({
826 cwd: "/w",
827 repository: "acme/web",
828 host: "github.com",
829 number: 7,
830 });
831 });
832​
833 it.effect("fills a marker-only dismissed review with the timeline's reason", () =>
834 readActivity.pipe(
835 Effect.map((activity) => {
836 expect(activity.comments[0]?.body).toBe("Dismissing prior approval to re-evaluate 9b66581");
837 expect(activity.comments[0]?.author?.isBot).toBe(true);
838 expect(activity.comments[0]?.editedAt).toBe("2026-07-04T00:00:00Z");
839 }),
840 Effect.provide(layerFor("<!-- Macroscope (Approvability) review body marker -->")),
841 ),
842 );
843});
844​
845describe("loginAvatarUrl", () => {
846 it("refuses anything that is not a login, rather than building a URL out of it", () => {
847 for (const login of ["../../etc", "a b", "-leading", "x".repeat(40), ""]) {
848 expect(loginAvatarUrl(login, "github.com")).toBeNull();
849 }
850 });
851});
852​
853describe("listChangeRequestsAcross", () => {
854 const searched = (repository: string): GitHubPullRequestSearchItem => ({
855 repository,
856 number: 1,
857 title: "Change",
858 url: `https://github.com/${repository}/pull/1`,
859 author: null,
860 headBranch: "feature",
861 baseBranch: "main",
862 state: "open" as const,
863 isDraft: false,
864 mergeability: "mergeable" as const,
865 additions: 0,
866 deletions: 0,
867 createdAt: "2026-07-01T00:00:00Z",
868 updatedAt: "2026-07-02T00:00:00Z",
869 reviewRequestLogins: [],
870 hasTeamReviewRequest: false,
871 labels: [],
872 reviewDecision: null,
873 checksState: null,
874 });
875 const across = {
876 cwd: "/w",
877 host: "github.com",
878 repositories: ["acme/web", "acme/docs"],
879 state: "open" as const,
880 involvement: "all" as const,
881 viewer: "viewer",
882 limit: 50,
883 };
884 type Search = Parameters<
885 GitHubPullRequestApi.GitHubPullRequestApi["Service"]["searchPullRequests"]
886 >[0];
887 // Pages answer in turn; a one-repository check for any pull request answers from `covered`.
888 const providerAnswering = (input: {
889 readonly pages: Array<GitHubPullRequestApi.GitHubPullRequestSearchBatch>;
890 readonly covered?: (repository: string) => boolean;
891 readonly checks?: Array<string>;
892 }) =>
893 make.pipe(
894 Effect.provide(
895 Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
896 searchPullRequests: (search: Search) => {
897 if (search.state !== "all") return Effect.succeed(input.pages.shift()!);
898 const repository = search.repositories[0]!;
899 input.checks?.push(repository);
900 return input.covered === undefined
901 ? Effect.fail(
902 new GitHubPullRequestApi.GitHubViewerLoginUnavailableError({ cwd: search.cwd }),
903 )
904 : Effect.succeed({
905 items: input.covered(repository) ? [searched(repository)] : [],
906 truncated: false,
907 });
908 },
909 }),
910 ),
911 Effect.map((provider) => provider.listChangeRequestsAcross!),
912 );
913​
914 it.effect("believes silence about a repository search covers, and checks it once", () =>
915 Effect.gen(function* () {
916 const checks: Array<string> = [];
917 const read = yield* providerAnswering({
918 pages: [
919 { items: [searched("acme/web")], truncated: false },
920 { items: [searched("acme/web")], truncated: false },
921 ],
922 covered: () => true,
923 checks,
924 });
925 expect((yield* read(across)).unresolved).toEqual([]);
926 expect((yield* read(across)).unresolved).toEqual([]);
927 expect(checks).toEqual(["acme/docs"]);
928 }),
929 );
930​
931 it.effect("leaves a repository search does not cover to be read on its own", () =>
932 Effect.gen(function* () {
933 const read = yield* providerAnswering({
934 pages: [{ items: [searched("acme/web")], truncated: false }],
935 covered: (repository) => repository !== "acme/docs",
936 });
937 expect((yield* read(across)).unresolved).toEqual(["acme/docs"]);
938 }),
939 );
940​
941 it.effect("does not check a repository a page had rows for", () =>
942 Effect.gen(function* () {
943 const checks: Array<string> = [];
944 const read = yield* providerAnswering({
945 pages: [
946 { items: [searched("acme/web"), searched("acme/docs")], truncated: false },
947 { items: [searched("acme/web")], truncated: false },
948 ],
949 covered: () => true,
950 checks,
951 });
952 yield* read(across);
953 expect((yield* read(across)).unresolved).toEqual([]);
954 expect(checks).toEqual([]);
955 }),
956 );
957​
958 it.effect("vouches for no silent repository in a truncated first slice", () =>
959 Effect.gen(function* () {
960 const read = yield* providerAnswering({
961 pages: [{ items: [searched("acme/web")], truncated: true }],
962 covered: () => true,
963 });
964 expect((yield* read(across)).unresolved).toEqual(["acme/docs"]);
965 }),
966 );
967​
968 it.effect("past the first slice, believes a covered repository and not an uncovered one", () =>
969 Effect.gen(function* () {
970 const read = yield* providerAnswering({
971 pages: [
972 { items: [searched("acme/web")], truncated: true },
973 { items: [searched("acme/web")], truncated: true },
974 ],
975 covered: (repository) => repository !== "acme/renamed",
976 });
977 const later = { ...across, cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 1 } };
978 expect((yield* read(later)).unresolved).toEqual([]);
979 expect(
980 (yield* read({ ...later, repositories: ["acme/web", "acme/renamed"] })).unresolved,
981 ).toEqual(["acme/renamed"]);
982 }),
983 );
984​
985 it.effect("asks again after a failed check, and after coverage has had time to change", () =>
986 Effect.gen(function* () {
987 const checks: Array<string> = [];
988 const failing = yield* providerAnswering({
989 pages: [
990 { items: [searched("acme/web")], truncated: false },
991 { items: [searched("acme/web")], truncated: false },
992 ],
993 checks,
994 });
995 expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]);
996 expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]);
997 expect(checks).toEqual(["acme/docs", "acme/docs"]);
998​
999 checks.length = 0;
1000 const read = yield* providerAnswering({
1001 pages: [
1002 { items: [searched("acme/web")], truncated: false },
1003 { items: [searched("acme/web")], truncated: false },
1004 ],
1005 covered: () => true,
1006 checks,
1007 });
1008 yield* read(across);
1009 yield* TestClock.adjust("61 minutes");
1010 yield* read(across);
1011 expect(checks).toEqual(["acme/docs", "acme/docs"]);
1012 }),
1013 );
1014});