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