1import { beforeEach, describe, expect, it } from "vite-plus/test";
2import { ProjectId } from "@t3tools/contracts";
3
4import {
5 type PendingReviewComment,
6 pullRequestReviewKey,
7 usePullRequestReviewStore,
8} from "./pullRequestReviewStore";
9
10function comment(id: string, body = id): PendingReviewComment {
11 return { id, body, path: "src/app.ts", position: { kind: "added", newLine: 1 } };
12}
13
14describe("pull request review drafts", () => {
15 beforeEach(() => {
16 usePullRequestReviewStore.setState({ drafts: {}, summaries: {} });
17 });
18
19 it("removes only the line comments included in a submitted snapshot", () => {
20 const store = usePullRequestReviewStore.getState();
21 store.addComment("review-a", comment("submitted"));
22 const submittedIds =
23 usePullRequestReviewStore.getState().drafts["review-a"]?.map((entry) => entry.id) ?? [];
24
25 usePullRequestReviewStore.getState().addComment("review-a", comment("added-in-flight"));
26 usePullRequestReviewStore.getState().removeComments("review-a", submittedIds);
27
28 expect(usePullRequestReviewStore.getState().drafts["review-a"]).toEqual([
29 comment("added-in-flight"),
30 ]);
31 });
32
33 it("keeps summary bodies isolated by review key", () => {
34 const store = usePullRequestReviewStore.getState();
35 store.setSummary("review-a", "Summary A");
36 store.setSummary("review-b", "Summary B");
37 store.clearSummary("review-a", "Summary A");
38
39 expect(usePullRequestReviewStore.getState().summaries).toEqual({
40 "review-b": "Summary B",
41 });
42 });
43
44 it("keeps drafts on different hosts separate when a thread reviews the same repository and number", () => {
45 const reference = {
46 projectId: ProjectId.make("project-a"),
47 repository: "owner/repo",
48 number: 7,
49 };
50 const publicKey = pullRequestReviewKey({ ...reference, host: "github.com" });
51 const enterpriseKey = pullRequestReviewKey({ ...reference, host: "github.example.com" });
52 const store = usePullRequestReviewStore.getState();
53 store.addComment(publicKey, comment("public"));
54 store.setSummary(publicKey, "Public review");
55
56 expect(usePullRequestReviewStore.getState().drafts[enterpriseKey]).toBeUndefined();
57 expect(usePullRequestReviewStore.getState().summaries[enterpriseKey]).toBeUndefined();
58
59 store.addComment(enterpriseKey, comment("enterprise"));
60 store.setSummary(enterpriseKey, "Enterprise review");
61 store.clear(enterpriseKey);
62 store.clearSummary(enterpriseKey, "Enterprise review");
63
64 expect(usePullRequestReviewStore.getState().drafts[publicKey]).toEqual([comment("public")]);
65 expect(usePullRequestReviewStore.getState().summaries[publicKey]).toBe("Public review");
66 });
67
68 it("does not clear a summary revised while submission is in flight", () => {
69 const store = usePullRequestReviewStore.getState();
70 store.setSummary("review-a", "Submitted body");
71 usePullRequestReviewStore.getState().setSummary("review-a", "Revised body");
72 usePullRequestReviewStore.getState().clearSummary("review-a", "Submitted body");
73
74 expect(usePullRequestReviewStore.getState().summaries["review-a"]).toBe("Revised body");
75 });
76});