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