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

GitHubPullRequestProvider.test.ts

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