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 canResolvePullRequestThread,
14} from "./pullRequestEditing.logic";
15
16type Subject = Pick<PullRequestDetail, "author" | "capabilities" | "viewer" | "viewerPermissions">;
17
18function actor(login: string): PullRequestActor {
19 return { login, name: null, avatarUrl: null };
20}
21
22function capabilities(edit?: PullRequestCapabilities["edit"]): PullRequestCapabilities {
23 return {
24 diff: true,
25 comment: true,
26 actions: [],
27 mergeMethods: [],
28 search: true,
29 reactions: true,
30 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
31 reviewers: { request: true, listCandidates: true },
32 ...(edit === undefined ? {} : { edit }),
33 };
34}
35
36function permissions(
37 overrides: Partial<PullRequestViewerPermissions> = {},
38): PullRequestViewerPermissions {
39 return {
40 actions: [],
41 comment: true,
42 resolve: true,
43 verdicts: [],
44 requestReviewers: true,
45 ...overrides,
46 };
47}
48
49function subject(overrides: Partial<Subject> = {}): Subject {
50 return {
51 author: actor("octocat"),
52 capabilities: capabilities({ changeRequest: true, comment: true }),
53 viewer: "octocat",
54 viewerPermissions: permissions(),
55 ...overrides,
56 };
57}
58
59function comment(overrides: Partial<PullRequestComment> = {}): PullRequestComment {
60 return {
61 id: "c1",
62 kind: "issue-comment",
63 author: actor("octocat"),
64 body: "words",
65 createdAt: "2026-01-01T00:00:00.000Z",
66 url: null,
67 path: null,
68 reviewState: null,
69 ...overrides,
70 };
71}
72
73describe("canEditPullRequestChangeRequest", () => {
74 it("lets the author rewrite their own change request", () => {
75 expect(canEditPullRequestChangeRequest(subject())).toBe(true);
76 });
77
78 it("matches the author regardless of case", () => {
79 expect(canEditPullRequestChangeRequest(subject({ viewer: "OctoCat" }))).toBe(true);
80 });
81
82 it("lets somebody who may merge rewrite a change request they did not write", () => {
83 expect(
84 canEditPullRequestChangeRequest(
85 subject({
86 author: actor("someone-else"),
87 viewerPermissions: permissions({ actions: ["merge"] }),
88 }),
89 ),
90 ).toBe(true);
91 });
92
93 it("follows the host's own answer about the reader over authorship and merge access", () => {
94 expect(
95 canEditPullRequestChangeRequest(
96 subject({
97 author: actor("someone-else"),
98 viewerPermissions: permissions({ editChangeRequest: true }),
99 }),
100 ),
101 ).toBe(true);
102 expect(
103 canEditPullRequestChangeRequest(
104 subject({
105 viewerPermissions: permissions({ actions: ["merge"], editChangeRequest: false }),
106 }),
107 ),
108 ).toBe(false);
109 expect(
110 canEditPullRequestChangeRequest(
111 subject({
112 capabilities: capabilities({ changeRequest: false, comment: true }),
113 viewerPermissions: permissions({ editChangeRequest: true }),
114 }),
115 ),
116 ).toBe(false);
117 });
118
119 it("refuses a reader who neither wrote it nor may merge it", () => {
120 expect(
121 canEditPullRequestChangeRequest(
122 subject({
123 author: actor("someone-else"),
124 viewerPermissions: permissions({ actions: ["close"] }),
125 }),
126 ),
127 ).toBe(false);
128 });
129
130 it("refuses where the host cannot rewrite a change request at all", () => {
131 expect(
132 canEditPullRequestChangeRequest(
133 subject({ capabilities: capabilities({ changeRequest: false, comment: true }) }),
134 ),
135 ).toBe(false);
136 expect(canEditPullRequestChangeRequest(subject({ capabilities: capabilities() }))).toBe(false);
137 });
138
139 it("refuses where the host did not say who the reader is", () => {
140 expect(canEditPullRequestChangeRequest(subject({ viewer: undefined }))).toBe(false);
141 });
142
143 it("still allows a merger the host could not name", () => {
144 expect(
145 canEditPullRequestChangeRequest(
146 subject({ viewer: undefined, viewerPermissions: permissions({ actions: ["merge"] }) }),
147 ),
148 ).toBe(true);
149 });
150});
151
152describe("canEditPullRequestComment", () => {
153 it("lets the reader rewrite their own conversation comment", () => {
154 expect(canEditPullRequestComment(subject(), comment())).toBe(true);
155 });
156
157 it("lets the reader rewrite their own remark on a line", () => {
158 expect(canEditPullRequestComment(subject(), comment({ kind: "review-comment" }))).toBe(true);
159 });
160
161 it("matches the author regardless of case", () => {
162 expect(
163 canEditPullRequestComment(
164 subject({ viewer: "OCTOCAT" }),
165 comment({ author: actor("octocat") }),
166 ),
167 ).toBe(true);
168 });
169
170 it("refuses somebody else's remark", () => {
171 expect(canEditPullRequestComment(subject(), comment({ author: actor("someone-else") }))).toBe(
172 false,
173 );
174 });
175
176 it("follows the host's own answer about a remark over authorship", () => {
177 expect(canEditPullRequestComment(subject(), comment({ canEdit: false }))).toBe(false);
178 expect(
179 canEditPullRequestComment(
180 subject(),
181 comment({ author: actor("someone-else"), canEdit: true }),
182 ),
183 ).toBe(true);
184 expect(
185 canEditPullRequestComment(
186 subject({ capabilities: capabilities({ changeRequest: true, comment: false }) }),
187 comment({ canEdit: true }),
188 ),
189 ).toBe(false);
190 });
191
192 it("refuses a remark the host attributes to nobody", () => {
193 expect(canEditPullRequestComment(subject(), comment({ author: null }))).toBe(false);
194 });
195
196 it("refuses a review's own summary", () => {
197 expect(canEditPullRequestComment(subject(), comment({ kind: "review" }))).toBe(false);
198 });
199
200 it("refuses where the host cannot rewrite a remark at all", () => {
201 expect(
202 canEditPullRequestComment(
203 subject({ capabilities: capabilities({ changeRequest: true, comment: false }) }),
204 comment(),
205 ),
206 ).toBe(false);
207 expect(canEditPullRequestComment(subject({ capabilities: capabilities() }), comment())).toBe(
208 false,
209 );
210 });
211
212 it("refuses where the host did not say who the reader is", () => {
213 expect(canEditPullRequestComment(subject({ viewer: undefined }), comment())).toBe(false);
214 });
215});
216
217describe("canResolvePullRequestThread", () => {
218 it("uses the reader's repository-wide permission where the host says nothing per thread", () => {
219 expect(canResolvePullRequestThread(subject(), {})).toBe(true);
220 expect(
221 canResolvePullRequestThread(
222 subject({ viewerPermissions: permissions({ resolve: false }) }),
223 {},
224 ),
225 ).toBe(false);
226 });
227
228 it("only narrows the reader's permission with the host's answer for one thread", () => {
229 expect(canResolvePullRequestThread(subject(), { canResolve: false })).toBe(false);
230 expect(
231 canResolvePullRequestThread(subject({ viewerPermissions: permissions({ resolve: false }) }), {
232 canResolve: true,
233 }),
234 ).toBe(false);
235 });
236});