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

pullRequest.test.ts

340 lines12.1 KB

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