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

pullRequest.test.ts

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