Open History to see the last commit
1import type {
2 PullRequestActor,
3 PullRequestCapabilities,
4 PullRequestComment,
5 PullRequestDetail,
6 PullRequestViewerPermissions,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9
10import {
11 canEditPullRequestChangeRequest,
12 canEditPullRequestComment,
13} from "./pullRequestEditing.logic";
14
15type Subject = Pick<PullRequestDetail, "author" | "capabilities" | "viewer" | "viewerPermissions">;
16
17function actor(login: string): PullRequestActor {
18 return { login, name: null, avatarUrl: null };
19}
20
21function capabilities(edit?: PullRequestCapabilities["edit"]): PullRequestCapabilities {
22 return {
23 diff: true,
24 comment: true,
25 actions: [],
26 mergeMethods: [],
27 search: true,
28 reactions: true,
29 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
30 reviewers: { request: true, listCandidates: true },
31 ...(edit === undefined ? {} : { edit }),
32 };
33}
34
35function permissions(
36 overrides: Partial<PullRequestViewerPermissions> = {},
37): PullRequestViewerPermissions {
38 return {
39 actions: [],
40 comment: true,
41 resolve: true,
42 verdicts: [],
43 requestReviewers: true,
44 ...overrides,
45 };
46}
47
48function subject(overrides: Partial<Subject> = {}): Subject {
49 return {
50 author: actor("octocat"),
51 capabilities: capabilities({ changeRequest: true, comment: true }),
52 viewer: "octocat",
53 viewerPermissions: permissions(),
54 ...overrides,
55 };
56}
57
58function comment(overrides: Partial<PullRequestComment> = {}): PullRequestComment {
59 return {
60 id: "c1",
61 kind: "issue-comment",
62 author: actor("octocat"),
63 body: "words",
64 createdAt: "2026-01-01T00:00:00.000Z",
65 url: null,
66 path: null,
67 reviewState: null,
68 ...overrides,
69 };
70}
71
72describe("canEditPullRequestChangeRequest", () => {
73 it("lets the author rewrite their own change request", () => {
74 expect(canEditPullRequestChangeRequest(subject())).toBe(true);
75 });
76
77 it("matches the author regardless of case", () => {
78 expect(canEditPullRequestChangeRequest(subject({ viewer: "OctoCat" }))).toBe(true);
79 });
80
81 it("lets somebody who may merge rewrite a change request they did not write", () => {
82 expect(
83 canEditPullRequestChangeRequest(
84 subject({
85 author: actor("someone-else"),
86 viewerPermissions: permissions({ actions: ["merge"] }),
87 }),
88 ),
89 ).toBe(true);
90 });
91
92 it("refuses a reader who neither wrote it nor may merge it", () => {
93 expect(
94 canEditPullRequestChangeRequest(
95 subject({
96 author: actor("someone-else"),
97 viewerPermissions: permissions({ actions: ["close"] }),
98 }),
99 ),
100 ).toBe(false);
101 });
102
103 it("refuses where the host cannot rewrite a change request at all", () => {
104 expect(
105 canEditPullRequestChangeRequest(
106 subject({ capabilities: capabilities({ changeRequest: false, comment: true }) }),
107 ),
108 ).toBe(false);
109 expect(canEditPullRequestChangeRequest(subject({ capabilities: capabilities() }))).toBe(false);
110 });
111
112 it("refuses where the host did not say who the reader is", () => {
113 expect(canEditPullRequestChangeRequest(subject({ viewer: undefined }))).toBe(false);
114 });
115
116 it("still allows a merger the host could not name", () => {
117 expect(
118 canEditPullRequestChangeRequest(
119 subject({ viewer: undefined, viewerPermissions: permissions({ actions: ["merge"] }) }),
120 ),
121 ).toBe(true);
122 });
123});
124
125describe("canEditPullRequestComment", () => {
126 it("lets the reader rewrite their own conversation comment", () => {
127 expect(canEditPullRequestComment(subject(), comment())).toBe(true);
128 });
129
130 it("lets the reader rewrite their own remark on a line", () => {
131 expect(canEditPullRequestComment(subject(), comment({ kind: "review-comment" }))).toBe(true);
132 });
133
134 it("matches the author regardless of case", () => {
135 expect(
136 canEditPullRequestComment(
137 subject({ viewer: "OCTOCAT" }),
138 comment({ author: actor("octocat") }),
139 ),
140 ).toBe(true);
141 });
142
143 it("refuses somebody else's remark", () => {
144 expect(canEditPullRequestComment(subject(), comment({ author: actor("someone-else") }))).toBe(
145 false,
146 );
147 });
148
149 it("refuses a remark the host attributes to nobody", () => {
150 expect(canEditPullRequestComment(subject(), comment({ author: null }))).toBe(false);
151 });
152
153 it("refuses a review's own summary", () => {
154 expect(canEditPullRequestComment(subject(), comment({ kind: "review" }))).toBe(false);
155 });
156
157 it("refuses where the host cannot rewrite a remark at all", () => {
158 expect(
159 canEditPullRequestComment(
160 subject({ capabilities: capabilities({ changeRequest: true, comment: false }) }),
161 comment(),
162 ),
163 ).toBe(false);
164 expect(canEditPullRequestComment(subject({ capabilities: capabilities() }), comment())).toBe(
165 false,
166 );
167 });
168
169 it("refuses where the host did not say who the reader is", () => {
170 expect(canEditPullRequestComment(subject({ viewer: undefined }), comment())).toBe(false);
171 });
172});