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