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

pullRequest.test.ts

160 lines5.3 KB

1import { SourceControlProviderKind } from "./sourceControl.ts";
2import * as Exit from "effect/Exit";
3import * as Rpc from "effect/rpc/Rpc";
4import * as Schema from "effect/Schema";
5import { describe, expect, it } from "vite-plus/test";
6​
7import {
8 type PullRequestDetail,
9 PullRequestListResult,
10 resolvePullRequestAuthorFilter,
11} from "./pullRequest.ts";
12import { WS_METHODS, WsRpcGroup } from "./rpc.ts";
13​
14// Clients decode the detail inside the RPC's Exit, as the JSON serialization does.
15const decodeDetailExit = Schema.decodeUnknownSync(
16 Schema.toCodecJson(Rpc.exitSchema(WsRpcGroup.requests.get(WS_METHODS.pullRequestsDetail)!)),
17);
18const decodeDetail = (value: unknown): PullRequestDetail => {
19 const exit = decodeDetailExit({ _tag: "Success", value });
20 if (!Exit.isSuccess(exit)) throw new Error("expected a successful exit");
21 return exit.value as PullRequestDetail;
22};
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 /**
75 * The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
76 * with an optional value, for one — fails as an interrupted request rather than as a schema
77 * error.
78 */
79 it("round-trips through the JSON codec the RPC serializes with", () => {
80 const codec = Schema.toCodecJson(PullRequestListResult);
81​
82 const decoded = Schema.decodeSync(codec)(Schema.encodeUnknownSync(codec)(LIST_RESULT));
83​
84 expect(decoded).toStrictEqual(LIST_RESULT);
85 });
86});
87​
88describe("PullRequestCapabilities", () => {
89 const base = {
90 diff: true,
91 comment: true,
92 actions: [],
93 mergeMethods: [],
94 search: true,
95 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
96 reviewers: { request: true, listCandidates: true },
97 };
98​
99 it("loads a newer server's detail response, dropping unknown options in order", () => {
100 const options = {
101 actions: ["close", "archive", "merge"],
102 verdicts: ["request-changes", "block", "approve"],
103 updateMethods: ["rebase", "fast-forward", "merge"],
104 };
105 const detail = {
106 ...LIST_RESULT.entries[0],
107 workspaceRoot: "/workspace/t3code",
108 body: "",
109 changedFiles: 1,
110 mergedAt: null,
111 closedAt: null,
112 reviewers: [],
113 checks: [],
114 mergeCapabilities: { merge: true, squash: true, rebase: true },
115 capabilities: {
116 ...base,
117 actions: options.actions,
118 mergeMethods: ["squash", "semi-linear", "merge"],
119 updateMethods: options.updateMethods,
120 review: { ...base.review, verdicts: options.verdicts },
121 },
122 viewerPermissions: { ...options, comment: true, resolve: true, requestReviewers: true },
123 };
124 const decoded = decodeDetail(detail);
125​
126 expect(decoded.capabilities).toEqual({
127 ...base,
128 actions: ["close", "merge"],
129 mergeMethods: ["squash", "merge"],
130 updateMethods: ["rebase", "merge"],
131 review: { ...base.review, verdicts: ["request-changes", "approve"] },
132 });
133 expect(decoded.viewerPermissions).toEqual({
134 actions: ["close", "merge"],
135 verdicts: ["request-changes", "approve"],
136 updateMethods: ["rebase", "merge"],
137 comment: true,
138 resolve: true,
139 requestReviewers: true,
140 });
141 expect(decoded.title).toBe(detail.title);
142 });
143});
144​
145describe("resolvePullRequestAuthorFilter", () => {
146 it("reads me as the signed-in account on the host", () => {
147 expect(resolvePullRequestAuthorFilter("me", "bilal")).toBe("bilal");
148 expect(resolvePullRequestAuthorFilter(" @Me ", "bilal")).toBe("bilal");
149 });
150​
151 it("keeps me as a literal login when the host has no viewer", () => {
152 expect(resolvePullRequestAuthorFilter("me", null)).toBe("me");
153 expect(resolvePullRequestAuthorFilter("@me", " ")).toBe("@me");
154 });
155​
156 it("passes any other login through trimmed", () => {
157 expect(resolvePullRequestAuthorFilter(" ana ", "bilal")).toBe("ana");
158 expect(resolvePullRequestAuthorFilter("meredith", "bilal")).toBe("meredith");
159 });
160});