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

pullRequest.test.ts

211 lines7.0 KB

1import { SourceControlProviderKind } from "./sourceControl.ts";
2import * as Effect from "effect/Effect";
3import * as Exit from "effect/Exit";
4import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
5import * as Rpc from "effect/rpc/Rpc";
6import * as Schema from "effect/Schema";
7import { describe, expect, it } from "@effect/vitest";
8​
9import {
10 type PullRequestDetail,
11 PullRequestDiffInput,
12 PullRequestDiffResult,
13 PullRequestListResult,
14 PullRequestOperationError,
15 resolvePullRequestAuthorFilter,
16} from "./pullRequest.ts";
17import { WS_METHODS, WsRpcGroup } from "./rpc.ts";
18​
19// Clients decode the detail inside the RPC's Exit, as the JSON serialization does.
20const decodeDetailExit = Schema.decodeUnknownSync(
21 Schema.toCodecJson(Rpc.exitSchema(WsRpcGroup.requests.get(WS_METHODS.pullRequestsDetail)!)),
22);
23const decodeDetail = (value: unknown): PullRequestDetail => {
24 const exit = decodeDetailExit({ _tag: "Success", value });
25 if (!Exit.isSuccess(exit)) throw new Error("expected a successful exit");
26 return exit.value as PullRequestDetail;
27};
28​
29const LIST_RESULT: PullRequestListResult = {
30 viewers: { "github.com": "bilal", "gitlab.com": "bilal.hassan" },
31 providers: [
32 {
33 host: "github.com",
34 kind: SourceControlProviderKind.make("github"),
35 searchesOnHost: true,
36 projectCount: 1,
37 configured: true,
38 detail: null,
39 },
40 {
41 host: "gitlab.com",
42 kind: SourceControlProviderKind.make("gitlab"),
43 searchesOnHost: true,
44 projectCount: 1,
45 configured: false,
46 detail: "glab is not installed.",
47 },
48 ],
49 entries: [
50 {
51 provider: SourceControlProviderKind.make("github"),
52 host: "github.com",
53 projectId: "project-1" as PullRequestListResult["entries"][number]["projectId"],
54 projectTitle: "t3code",
55 repository: "pingdotgg/t3code",
56 number: 1,
57 title: "Add a pull requests page",
58 url: "https://github.com/pingdotgg/t3code/pull/1",
59 author: { login: "octocat", name: null, avatarUrl: null },
60 headBranch: "feat/page",
61 baseBranch: "main",
62 state: "open",
63 isDraft: false,
64 mergeability: "mergeable",
65 additions: 1,
66 deletions: 0,
67 createdAt: "2026-07-01T00:00:00Z",
68 updatedAt: "2026-07-02T00:00:00Z",
69 viewerReviewRequested: false,
70 labels: [{ name: "backend", color: null }],
71 },
72 ],
73 errors: [],
74 truncated: false,
75 nextCursors: { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|1|1" },
76};
77​
78describe("PullRequestListResult", () => {
79 /**
80 * The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
81 * with an optional value, for one — fails as an interrupted request rather than as a schema
82 * error.
83 */
84 it("round-trips through the JSON codec the RPC serializes with", () => {
85 const codec = Schema.toCodecJson(PullRequestListResult);
86​
87 const decoded = Schema.decodeSync(codec)(Schema.encodeUnknownSync(codec)(LIST_RESULT));
88​
89 expect(decoded).toStrictEqual(LIST_RESULT);
90 });
91});
92​
93const decodePullRequestDiffInput = Schema.decodeSync(PullRequestDiffInput);
94​
95describe("PullRequestDiffResult", () => {
96 it("keeps omitted file names that differ only by a trailing space", () => {
97 const codec = Schema.toCodecJson(PullRequestDiffResult);
98 const result = {
99 patch: "",
100 truncated: true,
101 nextCursor: null,
102 omittedFileStats: [
103 { path: "report.txt", additions: 1, deletions: 0 },
104 { path: "report.txt ", additions: 2, deletions: 0 },
105 ],
106 };
107​
108 expect(Schema.decodeSync(codec)(Schema.encodeUnknownSync(codec)(result))).toStrictEqual(result);
109 });
110​
111 it("reads back a withheld file whose name ends in a space", () => {
112 const input = decodePullRequestDiffInput({
113 projectId: "project-1",
114 repository: "owner/repo",
115 number: 1,
116 path: "report.txt ",
117 });
118​
119 expect(input.path).toBe("report.txt ");
120 });
121});
122​
123describe("PullRequestCapabilities", () => {
124 const base = {
125 diff: true,
126 comment: true,
127 actions: [],
128 mergeMethods: [],
129 search: true,
130 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
131 reviewers: { request: true, listCandidates: true },
132 };
133​
134 it("loads a newer server's detail response, dropping unknown options in order", () => {
135 const options = {
136 actions: ["close", "archive", "merge"],
137 verdicts: ["request-changes", "block", "approve"],
138 updateMethods: ["rebase", "fast-forward", "merge"],
139 };
140 const detail = {
141 ...LIST_RESULT.entries[0],
142 workspaceRoot: "/workspace/t3code",
143 body: "",
144 changedFiles: 1,
145 mergedAt: null,
146 closedAt: null,
147 reviewers: [],
148 checks: [],
149 mergeCapabilities: { merge: true, squash: true, rebase: true },
150 capabilities: {
151 ...base,
152 actions: options.actions,
153 mergeMethods: ["squash", "semi-linear", "merge"],
154 updateMethods: options.updateMethods,
155 review: { ...base.review, verdicts: options.verdicts },
156 },
157 viewerPermissions: { ...options, comment: true, resolve: true, requestReviewers: true },
158 };
159 const decoded = decodeDetail(detail);
160​
161 expect(decoded.capabilities).toEqual({
162 ...base,
163 actions: ["close", "merge"],
164 mergeMethods: ["squash", "merge"],
165 updateMethods: ["rebase", "merge"],
166 review: { ...base.review, verdicts: ["request-changes", "approve"] },
167 });
168 expect(decoded.viewerPermissions).toEqual({
169 actions: ["close", "merge"],
170 verdicts: ["request-changes", "approve"],
171 updateMethods: ["rebase", "merge"],
172 comment: true,
173 resolve: true,
174 requestReviewers: true,
175 });
176 expect(decoded.title).toBe(detail.title);
177 });
178});
179​
180describe("resolvePullRequestAuthorFilter", () => {
181 it("reads me as the signed-in account on the host", () => {
182 expect(resolvePullRequestAuthorFilter("me", "bilal")).toBe("bilal");
183 expect(resolvePullRequestAuthorFilter(" @Me ", "bilal")).toBe("bilal");
184 });
185​
186 it("keeps me as a literal login when the host has no viewer", () => {
187 expect(resolvePullRequestAuthorFilter("me", null)).toBe("me");
188 expect(resolvePullRequestAuthorFilter("@me", " ")).toBe("@me");
189 });
190​
191 it("passes any other login through trimmed", () => {
192 expect(resolvePullRequestAuthorFilter(" ana ", "bilal")).toBe("ana");
193 expect(resolvePullRequestAuthorFilter("meredith", "bilal")).toBe("meredith");
194 });
195});
196​
197describe("PullRequestOperationError", () => {
198 // Cloudflare swaps an origin 502 for its own body without CORS headers, so a
199 // client behind a T3 Connect tunnel sees a transport error instead of this error.
200 it.effect("answers with a status a Cloudflare tunnel passes through", () =>
201 Effect.gen(function* () {
202 const error = new PullRequestOperationError({
203 operation: "diff",
204 detail: "gitlab requests to gitlab.com are paused until the rate limit resets.",
205 });
206 const response = yield* error[HttpServerRespondable.symbol]();
207 expect(response.status).not.toBe(502);
208 expect(PullRequestOperationError.ast.annotations?.httpApiStatus).not.toBe(502);
209 }),
210 );
211});