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