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

pullRequest.test.ts

425 lines14.9 KB

1import { SourceControlProviderKind } from "./sourceControl.ts";
2import * as Schema from "effect/Schema";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 PullRequestActionInput,
7 PullRequestCapabilities,
8 PullRequestDetail,
9 PullRequestFilesViewedResult,
10 PullRequestListInput,
11 PullRequestListResult,
12 PullRequestReviewerRequestInput,
13 PullRequestSetFilesViewedInput,
14 PullRequestSubmitReviewInput,
15 pullRequestHostOf,
16 resolvePullRequestAuthorFilter,
17} from "./pullRequest.ts";
18​
19const decodeListResult = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestListResult));
20const decodeDetail = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestDetail));
21const decodeListInput = Schema.decodeUnknownSync(PullRequestListInput);
22const decodeReviewerRequest = Schema.decodeUnknownSync(PullRequestReviewerRequestInput);
23const decodeAction = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestActionInput));
24const decodeReview = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestSubmitReviewInput));
25const decodeSetFilesViewed = Schema.decodeUnknownSync(PullRequestSetFilesViewedInput);
26const decodeFilesViewed = Schema.decodeUnknownSync(PullRequestFilesViewedResult);
27​
28const LIST_RESULT: PullRequestListResult = {
29 viewers: { "github.com": "bilal", "gitlab.com": "bilal.hassan" },
30 providers: [
31 {
32 host: "github.com",
33 kind: SourceControlProviderKind.make("github"),
34 searchesOnHost: true,
35 projectCount: 1,
36 configured: true,
37 detail: null,
38 },
39 {
40 host: "gitlab.com",
41 kind: SourceControlProviderKind.make("gitlab"),
42 searchesOnHost: true,
43 projectCount: 1,
44 configured: false,
45 detail: "glab is not installed.",
46 },
47 ],
48 entries: [
49 {
50 provider: SourceControlProviderKind.make("github"),
51 host: "github.com",
52 projectId: "project-1" as PullRequestListResult["entries"][number]["projectId"],
53 projectTitle: "t3code",
54 repository: "pingdotgg/t3code",
55 number: 1,
56 title: "Add a pull requests page",
57 url: "https://github.com/pingdotgg/t3code/pull/1",
58 author: { login: "octocat", name: null, avatarUrl: null },
59 headBranch: "feat/page",
60 baseBranch: "main",
61 state: "open",
62 isDraft: false,
63 mergeability: "mergeable",
64 additions: 1,
65 deletions: 0,
66 createdAt: "2026-07-01T00:00:00Z",
67 updatedAt: "2026-07-02T00:00:00Z",
68 viewerReviewRequested: false,
69 labels: [{ name: "backend", color: null }],
70 },
71 ],
72 errors: [],
73 truncated: false,
74 nextCursors: { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|1|1" },
75};
76​
77describe("PullRequestListResult", () => {
78 it("separates Forgejo HTTP ports while preserving other provider host identities", () => {
79 const identity = {
80 canonicalKey: "forge.example/team/repo",
81 locator: { remoteUrl: "http://forge.example:3000/team/repo.git" },
82 };
83 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("forgejo"))).toBe(
84 "forge.example:3000",
85 );
86 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("gitlab"))).toBe(
87 "forge.example",
88 );
89 expect(
90 pullRequestHostOf(
91 { ...identity, locator: { remoteUrl: "ssh://git@forge.example:2222/team/repo.git" } },
92 SourceControlProviderKind.make("forgejo"),
93 ),
94 ).toBe("forge.example");
95 });
96 /**
97 * The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
98 * with an optional value, for one — fails as an interrupted request rather than as a schema
99 * error.
100 */
101 it("round-trips through the JSON codec the RPC serializes with", () => {
102 const codec = Schema.toCodecJson(PullRequestListResult);
103​
104 const decoded = Schema.decodeSync(codec)(Schema.encodeUnknownSync(codec)(LIST_RESULT));
105​
106 expect(decoded).toStrictEqual(LIST_RESULT);
107 });
108​
109 it("loads a newer server's list response, dropping unknown provider actions in order", () => {
110 const decoded = decodeListResult({
111 ...LIST_RESULT,
112 providers: [{ ...LIST_RESULT.providers[0], actions: ["close", "archive", "merge"] }],
113 });
114​
115 expect(decoded).toEqual({
116 ...LIST_RESULT,
117 providers: [{ ...LIST_RESULT.providers[0], actions: ["close", "merge"] }],
118 });
119 });
120​
121 it("keys a viewer by host, so two hosts of one kind stay separate accounts", () => {
122 const decoded = decodeListResult({
123 ...LIST_RESULT,
124 viewers: { "github.com": "bilal", "github.acme.dev": "b.hassan" },
125 });
126​
127 expect(decoded.viewers["github.com"]).toBe("bilal");
128 expect(decoded.viewers["github.acme.dev"]).toBe("b.hassan");
129 });
130});
131​
132describe("PullRequestListInput", () => {
133 it("trims a search, so what is sent is what was typed", () => {
134 expect(decodeListInput({ state: "open", query: " page " }).query).toBe("page");
135 });
136​
137 it("has no search when none was asked for", () => {
138 expect(decodeListInput({ state: "open" }).query).toBeUndefined();
139 });
140​
141 it("bounds a search, because it travels into a command and a query string", () => {
142 expect(decodeListInput({ state: "open", query: "p".repeat(200) }).query).toHaveLength(200);
143 expect(() => decodeListInput({ state: "open", query: "p".repeat(201) })).toThrow();
144 });
145​
146 it("takes back the continuation a result handed out, keyed the way it arrived", () => {
147 const cursors = { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|99|1,2" };
148​
149 expect(decodeListInput({ state: "open", cursors }).cursors).toStrictEqual(cursors);
150 });
151​
152 it("has no continuation when the listing is being read from the top", () => {
153 expect(decodeListInput({ state: "open" }).cursors).toBeUndefined();
154 });
155​
156 it("bounds a continuation, because it comes back from the page and goes into a filter", () => {
157 const long = (length: number) => ({ "github.com acme/web": "c".repeat(length) });
158 expect(decodeListInput({ state: "open", cursors: long(4096) })).toBeDefined();
159 expect(() => decodeListInput({ state: "open", cursors: long(4097) })).toThrow();
160 });
161});
162​
163describe("PullRequestReviewerRequestInput", () => {
164 const ref = { projectId: "p1", repository: "acme/web", number: 1 };
165 const reviewer = { id: "octocat", kind: "user" };
166​
167 it("carries the same shape whichever direction the request goes", () => {
168 for (const requested of [true, false]) {
169 expect(decodeReviewerRequest({ ...ref, reviewers: [reviewer], requested }).requested).toBe(
170 requested,
171 );
172 }
173 });
174​
175 it("refuses a request that names nobody, which no host would do anything with", () => {
176 expect(() => decodeReviewerRequest({ ...ref, reviewers: [], requested: true })).toThrow();
177 });
178​
179 it("bounds the reviewers, because they travel into a body the page composed", () => {
180 const many = (count: number) =>
181 Array.from({ length: count }, (_, index) => ({ id: `user${index}`, kind: "user" }));
182 expect(
183 decodeReviewerRequest({ ...ref, reviewers: many(25), requested: true }).reviewers,
184 ).toHaveLength(25);
185 expect(() => decodeReviewerRequest({ ...ref, reviewers: many(26), requested: true })).toThrow();
186 });
187​
188 it("keeps a team apart from a person, which is a different thing to ask", () => {
189 expect(
190 decodeReviewerRequest({
191 ...ref,
192 reviewers: [reviewer, { id: "web-platform", kind: "team" }],
193 requested: true,
194 }).reviewers.map((entry) => entry.kind),
195 ).toEqual(["user", "team"]);
196 });
197});
198​
199describe("updating a branch that has fallen behind its base", () => {
200 const ref = { projectId: "project-1", repository: "acme/web", number: 7 };
201​
202 it("carries the way the branch should be brought up to date", () => {
203 expect(decodeAction({ ...ref, action: "update-branch", updateMethod: "rebase" })).toMatchObject(
204 {
205 action: "update-branch",
206 updateMethod: "rebase",
207 },
208 );
209 });
210​
211 it("takes the action without a method, which is the host's own default", () => {
212 expect(decodeAction({ ...ref, action: "update-branch" }).updateMethod).toBeUndefined();
213 });
214​
215 it("refuses a way no host offers", () => {
216 expect(() =>
217 decodeAction({ ...ref, action: "update-branch", updateMethod: "squash" }),
218 ).toThrow();
219 });
220});
221​
222describe("leaving a merge for the host to make once it is ready", () => {
223 const ref = { projectId: "project-1", repository: "acme/web", number: 7 };
224​
225 it("carries the strategy the deferred merge should use, as merging now does", () => {
226 expect(
227 decodeAction({ ...ref, action: "enable-auto-merge", mergeMethod: "squash" }),
228 ).toMatchObject({ action: "enable-auto-merge", mergeMethod: "squash" });
229 });
230​
231 it("takes the arming back without a strategy, because there is nothing to choose", () => {
232 expect(decodeAction({ ...ref, action: "disable-auto-merge" }).mergeMethod).toBeUndefined();
233 });
234});
235​
236describe("reverting a merged pull request", () => {
237 it("carries a standalone branch deletion action", () => {
238 expect(
239 decodeAction({
240 projectId: "project-1",
241 repository: "acme/web",
242 number: 7,
243 action: "delete-branch",
244 }).action,
245 ).toBe("delete-branch");
246 });
247 it("carries the revert action without merge options", () => {
248 const action = decodeAction({
249 projectId: "project-1",
250 repository: "acme/web",
251 number: 7,
252 action: "revert",
253 });
254​
255 expect(action.action).toBe("revert");
256 expect(action.mergeMethod).toBeUndefined();
257 });
258});
259​
260describe("approving fork workflows", () => {
261 it("carries workflow approval as its own action", () => {
262 const action = decodeAction({
263 projectId: "project-1",
264 repository: "acme/web",
265 number: 7,
266 action: "approve-workflows",
267 });
268​
269 expect(action.action).toBe("approve-workflows");
270 });
271});
272​
273describe("PullRequestCapabilities", () => {
274 const decodeCapabilities = Schema.decodeUnknownSync(PullRequestCapabilities);
275 const base = {
276 diff: true,
277 comment: true,
278 actions: [],
279 mergeMethods: [],
280 search: true,
281 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
282 reviewers: { request: true, listCandidates: true },
283 };
284​
285 it("decodes a server that says nothing about reactions as a server with none", () => {
286 expect(decodeCapabilities(base).reactions).toBeUndefined();
287 });
288​
289 it("only offers branch deletion when the server advertises it", () => {
290 expect(decodeCapabilities(base).deleteBranch).toBeUndefined();
291 expect(decodeCapabilities({ ...base, deleteBranch: true }).deleteBranch).toBe(true);
292 });
293​
294 it("loads a newer server's detail response, dropping unknown options in order", () => {
295 const options = {
296 actions: ["close", "archive", "merge"],
297 verdicts: ["request-changes", "block", "approve"],
298 updateMethods: ["rebase", "fast-forward", "merge"],
299 };
300 const detail = {
301 ...LIST_RESULT.entries[0],
302 workspaceRoot: "/workspace/t3code",
303 body: "",
304 changedFiles: 1,
305 mergedAt: null,
306 closedAt: null,
307 reviewers: [],
308 checks: [],
309 mergeCapabilities: { merge: true, squash: true, rebase: true },
310 capabilities: {
311 ...base,
312 actions: options.actions,
313 mergeMethods: ["squash", "fast-forward", "merge"],
314 updateMethods: options.updateMethods,
315 review: { ...base.review, verdicts: options.verdicts },
316 },
317 viewerPermissions: { ...options, comment: true, resolve: true, requestReviewers: true },
318 };
319 const decoded = decodeDetail(detail);
320​
321 expect(decoded.capabilities).toEqual({
322 ...base,
323 actions: ["close", "merge"],
324 mergeMethods: ["squash", "merge"],
325 updateMethods: ["rebase", "merge"],
326 review: { ...base.review, verdicts: ["request-changes", "approve"] },
327 });
328 expect(decoded.viewerPermissions).toEqual({
329 actions: ["close", "merge"],
330 verdicts: ["request-changes", "approve"],
331 updateMethods: ["rebase", "merge"],
332 comment: true,
333 resolve: true,
334 requestReviewers: true,
335 });
336 expect(decoded.title).toBe(detail.title);
337 });
338});
339​
340describe("pull request mutation requests", () => {
341 it("still rejects unknown actions, merge methods, and review verdicts on the wire", () => {
342 const ref = { projectId: "project-1", repository: "acme/web", number: 7 };
343​
344 expect(() => decodeAction({ ...ref, action: "archive" })).toThrow();
345 expect(() => decodeAction({ ...ref, action: "merge", mergeMethod: "fast-forward" })).toThrow();
346 const review = { ...ref, body: "", comments: [], verdict: "approve" };
347 expect(decodeReview(review).verdict).toBe("approve");
348 expect(() => decodeReview({ ...review, verdict: "block" })).toThrow();
349 });
350});
351​
352describe("naming the reader as the author to narrow by", () => {
353 it("reads me as whoever is signed in, however it is written", () => {
354 expect(resolvePullRequestAuthorFilter("me", "octocat")).toBe("octocat");
355 expect(resolvePullRequestAuthorFilter("@me", "octocat")).toBe("octocat");
356 expect(resolvePullRequestAuthorFilter(" ME ", "octocat")).toBe("octocat");
357 });
358​
359 it("leaves any other name as it was typed", () => {
360 expect(resolvePullRequestAuthorFilter(" mercedes ", "octocat")).toBe("mercedes");
361 expect(resolvePullRequestAuthorFilter("me-too", "octocat")).toBe("me-too");
362 });
363​
364 it("stands as typed where the host has not said who the reader is", () => {
365 expect(resolvePullRequestAuthorFilter("me", null)).toBe("me");
366 expect(resolvePullRequestAuthorFilter("me", " ")).toBe("me");
367 });
368});
369​
370describe("naming the file a tick belongs to", () => {
371 it("keeps the spaces around a path being ticked", () => {
372 expect(
373 decodeSetFilesViewed({
374 projectId: "p1",
375 repository: "group/project",
376 number: 7,
377 files: [{ path: "docs/readme.md ", viewed: true }],
378 }).files,
379 ).toEqual([{ path: "docs/readme.md ", viewed: true }]);
380 });
381​
382 it("keeps the spaces around a path being reported back", () => {
383 expect(
384 decodeFilesViewed({
385 files: [{ path: " leading.md", state: "viewed" }],
386 truncated: false,
387 }).files,
388 ).toEqual([{ path: " leading.md", state: "viewed" }]);
389 });
390​
391 it("still refuses a path that is nothing at all", () => {
392 expect(() =>
393 decodeSetFilesViewed({
394 projectId: "p1",
395 repository: "group/project",
396 number: 7,
397 files: [{ path: "", viewed: true }],
398 }),
399 ).toThrow();
400 });
401​
402 it("refuses a batch larger than a reader can press", () => {
403 const press = (path: string) => ({ path, viewed: true });
404 const batch = (count: number) => ({
405 projectId: "p1",
406 repository: "group/project",
407 number: 7,
408 files: Array.from({ length: count }, (_, at) => press(`src/f${at}.ts`)),
409 });
410​
411 expect(() => decodeSetFilesViewed(batch(500))).not.toThrow();
412 expect(() => decodeSetFilesViewed(batch(501))).toThrow();
413 });
414​
415 it("refuses a path far longer than any real one", () => {
416 expect(() =>
417 decodeSetFilesViewed({
418 projectId: "p1",
419 repository: "group/project",
420 number: 7,
421 files: [{ path: `src/${"a".repeat(4096)}.ts`, viewed: true }],
422 }),
423 ).toThrow();
424 });
425});