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

pullRequest.test.ts

258 lines9.1 KB

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