Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestEditing.test.ts

204 lines5.9 KB

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.ts";
15​
16type Subject = Pick<PullRequestDetail, "author" | "capabilities" | "viewer" | "viewerPermissions">;
17​
18function actor(login: string): PullRequestActor {
19 return { login, name: null, avatarUrl: null };
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 };
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 };
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 };
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 };
72​
73describe("canEditPullRequestChangeRequest", () => {
74 it("matches the author regardless of case", () => {
75 expect(canEditPullRequestChangeRequest(subject({ viewer: "OctoCat" }))).toBe(true);
76 });
77​
78 it("lets somebody who may merge rewrite a change request they did not write", () => {
79 expect(
80 canEditPullRequestChangeRequest(
81 subject({
82 author: actor("someone-else"),
83 viewerPermissions: permissions({ actions: ["merge"] }),
84 }),
85 ),
86 ).toBe(true);
87 });
88​
89 it("follows the host's own answer about the reader over authorship and merge access", () => {
90 expect(
91 canEditPullRequestChangeRequest(
92 subject({
93 author: actor("someone-else"),
94 viewerPermissions: permissions({ editChangeRequest: true }),
95 }),
96 ),
97 ).toBe(true);
98 expect(
99 canEditPullRequestChangeRequest(
100 subject({
101 viewerPermissions: permissions({ actions: ["merge"], editChangeRequest: false }),
102 }),
103 ),
104 ).toBe(false);
105 expect(
106 canEditPullRequestChangeRequest(
107 subject({
108 capabilities: capabilities({ changeRequest: false, comment: true }),
109 viewerPermissions: permissions({ editChangeRequest: true }),
110 }),
111 ),
112 ).toBe(false);
113 });
114​
115 it("refuses a reader who neither wrote it nor may merge it", () => {
116 expect(
117 canEditPullRequestChangeRequest(
118 subject({
119 author: actor("someone-else"),
120 viewerPermissions: permissions({ actions: ["close"] }),
121 }),
122 ),
123 ).toBe(false);
124 });
125​
126 it("refuses where the host cannot rewrite a change request at all", () => {
127 expect(
128 canEditPullRequestChangeRequest(
129 subject({ capabilities: capabilities({ changeRequest: false, comment: true }) }),
130 ),
131 ).toBe(false);
132 expect(canEditPullRequestChangeRequest(subject({ capabilities: capabilities() }))).toBe(false);
133 });
134​
135 it("still allows a merger the host could not name", () => {
136 expect(
137 canEditPullRequestChangeRequest(
138 subject({ viewer: undefined, viewerPermissions: permissions({ actions: ["merge"] }) }),
139 ),
140 ).toBe(true);
141 });
142});
143​
144describe("canEditPullRequestComment", () => {
145 it("lets the reader rewrite their own remark on a line", () => {
146 expect(canEditPullRequestComment(subject(), comment({ kind: "review-comment" }))).toBe(true);
147 });
148​
149 it("matches the author regardless of case", () => {
150 expect(
151 canEditPullRequestComment(
152 subject({ viewer: "OCTOCAT" }),
153 comment({ author: actor("octocat") }),
154 ),
155 ).toBe(true);
156 });
157​
158 it("refuses somebody else's remark", () => {
159 expect(canEditPullRequestComment(subject(), comment({ author: actor("someone-else") }))).toBe(
160 false,
161 );
162 });
163​
164 it("follows the host's own answer about a remark over authorship", () => {
165 expect(canEditPullRequestComment(subject(), comment({ canEdit: false }))).toBe(false);
166 expect(
167 canEditPullRequestComment(
168 subject(),
169 comment({ author: actor("someone-else"), canEdit: true }),
170 ),
171 ).toBe(true);
172 expect(
173 canEditPullRequestComment(
174 subject({ capabilities: capabilities({ changeRequest: true, comment: false }) }),
175 comment({ canEdit: true }),
176 ),
177 ).toBe(false);
178 });
179​
180 it("refuses a review's own summary", () => {
181 expect(canEditPullRequestComment(subject(), comment({ kind: "review" }))).toBe(false);
182 });
183});
184​
185describe("canResolvePullRequestThread", () => {
186 it("uses the reader's repository-wide permission where the host says nothing per thread", () => {
187 expect(canResolvePullRequestThread(subject(), {})).toBe(true);
188 expect(
189 canResolvePullRequestThread(
190 subject({ viewerPermissions: permissions({ resolve: false }) }),
191 {},
192 ),
193 ).toBe(false);
194 });
195​
196 it("only narrows the reader's permission with the host's answer for one thread", () => {
197 expect(canResolvePullRequestThread(subject(), { canResolve: false })).toBe(false);
198 expect(
199 canResolvePullRequestThread(subject({ viewerPermissions: permissions({ resolve: false }) }), {
200 canResolve: true,
201 }),
202 ).toBe(false);
203 });
204});