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