Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 PullRequestListInput,
6 PullRequestListResult,
7 PullRequestReviewerRequestInput,
8} from "./pullRequest.ts";
9
10const decodeListResult = Schema.decodeUnknownSync(PullRequestListResult);
11const decodeListInput = Schema.decodeUnknownSync(PullRequestListInput);
12const decodeReviewerRequest = Schema.decodeUnknownSync(PullRequestReviewerRequestInput);
13
14const LIST_RESULT: PullRequestListResult = {
15 viewers: { "github.com": "bilal", "gitlab.com": "bilal.hassan" },
16 providers: [
17 {
18 host: "github.com",
19 kind: "github",
20 searchesOnHost: true,
21 projectCount: 1,
22 configured: true,
23 detail: null,
24 },
25 {
26 host: "gitlab.com",
27 kind: "gitlab",
28 searchesOnHost: true,
29 projectCount: 1,
30 configured: false,
31 detail: "glab is not installed.",
32 },
33 ],
34 entries: [
35 {
36 provider: "github",
37 host: "github.com",
38 projectId: "project-1" as PullRequestListResult["entries"][number]["projectId"],
39 projectTitle: "t3code",
40 repository: "pingdotgg/t3code",
41 number: 1,
42 title: "Add a pull requests page",
43 url: "https://github.com/pingdotgg/t3code/pull/1",
44 author: { login: "octocat", name: null, avatarUrl: null },
45 headBranch: "feat/page",
46 baseBranch: "main",
47 state: "open",
48 isDraft: false,
49 mergeability: "mergeable",
50 additions: 1,
51 deletions: 0,
52 createdAt: "2026-07-01T00:00:00Z",
53 updatedAt: "2026-07-02T00:00:00Z",
54 viewerReviewRequested: false,
55 labels: [{ name: "backend", color: null }],
56 },
57 ],
58 errors: [],
59 truncated: false,
60 nextCursors: { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|1|1" },
61};
62
63describe("PullRequestListResult", () => {
64 /**
65 * The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
66 * with an optional value, for one — fails as an interrupted request rather than as a schema
67 * error. Building it here turns that into a test failure instead.
68 */
69 it("round-trips through the JSON codec the RPC serializes with", () => {
70 const codec = Schema.toCodecJson(PullRequestListResult);
71
72 const decoded = Schema.decodeUnknownSync(codec)(Schema.encodeUnknownSync(codec)(LIST_RESULT));
73
74 expect(decoded).toStrictEqual(LIST_RESULT);
75 });
76
77 it("keys a viewer by host, so two hosts of one kind stay separate accounts", () => {
78 const decoded = decodeListResult({
79 ...LIST_RESULT,
80 viewers: { "github.com": "bilal", "github.acme.dev": "b.hassan" },
81 });
82
83 expect(decoded.viewers["github.com"]).toBe("bilal");
84 expect(decoded.viewers["github.acme.dev"]).toBe("b.hassan");
85 });
86});
87
88describe("PullRequestListInput", () => {
89 it("trims a search, so what is sent is what was typed", () => {
90 expect(decodeListInput({ state: "open", query: " page " }).query).toBe("page");
91 });
92
93 it("has no search when none was asked for", () => {
94 expect(decodeListInput({ state: "open" }).query).toBeUndefined();
95 });
96
97 it("bounds a search, because it travels into a command and a query string", () => {
98 expect(decodeListInput({ state: "open", query: "p".repeat(200) }).query).toHaveLength(200);
99 expect(() => decodeListInput({ state: "open", query: "p".repeat(201) })).toThrow();
100 });
101
102 it("takes back the continuation a result handed out, keyed the way it arrived", () => {
103 const cursors = { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|99|1,2" };
104
105 expect(decodeListInput({ state: "open", cursors }).cursors).toStrictEqual(cursors);
106 });
107
108 it("has no continuation when the listing is being read from the top", () => {
109 expect(decodeListInput({ state: "open" }).cursors).toBeUndefined();
110 });
111
112 it("bounds a continuation, because it comes back from the page and goes into a filter", () => {
113 const long = (length: number) => ({ "github.com acme/web": "c".repeat(length) });
114 expect(decodeListInput({ state: "open", cursors: long(4096) })).toBeDefined();
115 expect(() => decodeListInput({ state: "open", cursors: long(4097) })).toThrow();
116 });
117});
118
119describe("PullRequestReviewerRequestInput", () => {
120 const ref = { projectId: "p1", repository: "acme/web", number: 1 };
121 const reviewer = { id: "octocat", kind: "user" };
122
123 it("carries the same shape whichever direction the request goes", () => {
124 // One operation, turned around: `requested` is the whole difference between asking somebody
125 // for a review and taking the request back.
126 for (const requested of [true, false]) {
127 expect(decodeReviewerRequest({ ...ref, reviewers: [reviewer], requested }).requested).toBe(
128 requested,
129 );
130 }
131 });
132
133 it("refuses a request that names nobody, which no host would do anything with", () => {
134 expect(() => decodeReviewerRequest({ ...ref, reviewers: [], requested: true })).toThrow();
135 });
136
137 it("bounds the reviewers, because they travel into a body the page composed", () => {
138 const many = (count: number) =>
139 Array.from({ length: count }, (_, index) => ({ id: `user${index}`, kind: "user" }));
140 expect(
141 decodeReviewerRequest({ ...ref, reviewers: many(25), requested: true }).reviewers,
142 ).toHaveLength(25);
143 expect(() => decodeReviewerRequest({ ...ref, reviewers: many(26), requested: true })).toThrow();
144 });
145
146 it("keeps a team apart from a person, which is a different thing to ask", () => {
147 expect(
148 decodeReviewerRequest({
149 ...ref,
150 reviewers: [reviewer, { id: "web-platform", kind: "team" }],
151 requested: true,
152 }).reviewers.map((entry) => entry.kind),
153 ).toEqual(["user", "team"]);
154 });
155});