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