Open History to see the last commit
1import {
2 SourceControlProviderKind,
3 type PullRequestDetail,
4 type PullRequestReaction,
5 type PullRequestReactionContent,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8
9import {
10 applyPendingPullRequestReactions,
11 canReactPullRequestComment,
12 retainPendingPullRequestReactions,
13} from "./pullRequestReactions.ts";
14
15function reaction(overrides: Partial<PullRequestReaction> = {}): PullRequestReaction {
16 return {
17 content: "heart",
18 count: 1,
19 actors: ["octocat"],
20 viewerHasReacted: false,
21 ...overrides,
22 };
23}
24
25describe("comment reaction permissions", () => {
26 it("excludes only GitCafe review summaries from comment reactions", () => {
27 const detail = {
28 provider: SourceControlProviderKind.make("gitcafe"),
29 capabilities: {
30 diff: true,
31 comment: true,
32 actions: [],
33 mergeMethods: [],
34 search: true,
35 reactions: true,
36 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
37 reviewers: { request: true, listCandidates: true },
38 },
39 } satisfies Pick<PullRequestDetail, "provider" | "capabilities">;
40
41 expect(canReactPullRequestComment(detail, { kind: "issue-comment" })).toBe(true);
42 expect(canReactPullRequestComment(detail, { kind: "review-comment" })).toBe(true);
43 expect(
44 canReactPullRequestComment(
45 { ...detail, provider: SourceControlProviderKind.make("github") },
46 { kind: "review" },
47 ),
48 ).toBe(true);
49 expect(canReactPullRequestComment(detail, { kind: "review" })).toBe(false);
50 });
51});
52
53describe("pending reactions", () => {
54 const pending = (entries: ReadonlyArray<readonly [PullRequestReactionContent, boolean]>) =>
55 new Map(entries.map(([content, reacted]) => [content, { reacted }] as const));
56
57 it("adds a reaction nobody had yet, in picker order", () => {
58 const applied = applyPendingPullRequestReactions(
59 [reaction({ content: "rocket", count: 2, actors: ["a", "b"] })],
60 pending([["thumbs-up", true]]),
61 );
62 expect(applied).toEqual([
63 { content: "thumbs-up", count: 1, actors: [], viewerHasReacted: true },
64 { content: "rocket", count: 2, actors: ["a", "b"], viewerHasReacted: false },
65 ]);
66 });
67
68 it("joins and leaves an existing reaction, and drops the pill nobody is left on", () => {
69 expect(
70 applyPendingPullRequestReactions(
71 [reaction({ count: 2, actors: ["a", "b"] })],
72 pending([["heart", true]]),
73 ),
74 ).toEqual([{ content: "heart", count: 3, actors: ["a", "b"], viewerHasReacted: true }]);
75 expect(
76 applyPendingPullRequestReactions(
77 [reaction({ count: 2, actors: ["a", "b"], viewerHasReacted: true })],
78 pending([["heart", false]]),
79 ),
80 ).toEqual([{ content: "heart", count: 1, actors: ["a", "b"], viewerHasReacted: false }]);
81 expect(
82 applyPendingPullRequestReactions(
83 [reaction({ count: 1, viewerHasReacted: true })],
84 pending([["heart", false]]),
85 ),
86 ).toEqual([]);
87 });
88
89 it("ignores a pending state the host has already caught up with", () => {
90 const reactions = [reaction({ count: 3, viewerHasReacted: true })];
91 expect(applyPendingPullRequestReactions(reactions, pending([["heart", true]]))).toEqual(
92 reactions,
93 );
94 expect(applyPendingPullRequestReactions([], pending([["heart", false]]))).toEqual([]);
95 });
96
97 it("keeps only unsaved toggles the host has not reported yet", () => {
98 const retained = retainPendingPullRequestReactions(
99 [reaction({ viewerHasReacted: true }), reaction({ content: "rocket" })],
100 new Map([
101 ["heart", { reacted: true }],
102 ["rocket", { reacted: true }],
103 ["eyes", { reacted: true, saved: true }],
104 ["laugh", { reacted: false }],
105 ]),
106 );
107 expect([...retained]).toEqual([["rocket", { reacted: true }]]);
108 });
109});