Open History to see the last commit
1import { SourceControlProviderKind } from "./sourceControl.ts";
2import * as Schema from "effect/Schema";
3import { describe, expect, it } from "vite-plus/test";
4
5import {
6 PullRequestDetail,
7 PullRequestListResult,
8 pullRequestHostOf,
9 resolvePullRequestAuthorFilter,
10} from "./pullRequest.ts";
11
12const decodeDetail = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestDetail));
13
14const LIST_RESULT: PullRequestListResult = {
15 viewers: { "github.com": "bilal", "gitlab.com": "bilal.hassan" },
16 providers: [
17 {
18 host: "github.com",
19 kind: SourceControlProviderKind.make("github"),
20 searchesOnHost: true,
21 projectCount: 1,
22 configured: true,
23 detail: null,
24 },
25 {
26 host: "gitlab.com",
27 kind: SourceControlProviderKind.make("gitlab"),
28 searchesOnHost: true,
29 projectCount: 1,
30 configured: false,
31 detail: "glab is not installed.",
32 },
33 ],
34 entries: [
35 {
36 provider: SourceControlProviderKind.make("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 it("separates Forgejo HTTP ports while preserving other provider host identities", () => {
65 const identity = {
66 canonicalKey: "forge.example/team/repo",
67 locator: { remoteUrl: "http://forge.example:3000/team/repo.git" },
68 };
69 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("forgejo"))).toBe(
70 "forge.example:3000",
71 );
72 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("gitlab"))).toBe(
73 "forge.example",
74 );
75 expect(
76 pullRequestHostOf(
77 { ...identity, locator: { remoteUrl: "ssh://git@forge.example:2222/team/repo.git" } },
78 SourceControlProviderKind.make("forgejo"),
79 ),
80 ).toBe("forge.example");
81 });
82 /**
83 * The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
84 * with an optional value, for one — fails as an interrupted request rather than as a schema
85 * error.
86 */
87 it("round-trips through the JSON codec the RPC serializes with", () => {
88 const codec = Schema.toCodecJson(PullRequestListResult);
89
90 const decoded = Schema.decodeSync(codec)(Schema.encodeUnknownSync(codec)(LIST_RESULT));
91
92 expect(decoded).toStrictEqual(LIST_RESULT);
93 });
94});
95
96describe("PullRequestCapabilities", () => {
97 const base = {
98 diff: true,
99 comment: true,
100 actions: [],
101 mergeMethods: [],
102 search: true,
103 review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
104 reviewers: { request: true, listCandidates: true },
105 };
106
107 it("loads a newer server's detail response, dropping unknown options in order", () => {
108 const options = {
109 actions: ["close", "archive", "merge"],
110 verdicts: ["request-changes", "block", "approve"],
111 updateMethods: ["rebase", "fast-forward", "merge"],
112 };
113 const detail = {
114 ...LIST_RESULT.entries[0],
115 workspaceRoot: "/workspace/t3code",
116 body: "",
117 changedFiles: 1,
118 mergedAt: null,
119 closedAt: null,
120 reviewers: [],
121 checks: [],
122 mergeCapabilities: { merge: true, squash: true, rebase: true },
123 capabilities: {
124 ...base,
125 actions: options.actions,
126 mergeMethods: ["squash", "fast-forward", "merge"],
127 updateMethods: options.updateMethods,
128 review: { ...base.review, verdicts: options.verdicts },
129 },
130 viewerPermissions: { ...options, comment: true, resolve: true, requestReviewers: true },
131 };
132 const decoded = decodeDetail(detail);
133
134 expect(decoded.capabilities).toEqual({
135 ...base,
136 actions: ["close", "merge"],
137 mergeMethods: ["squash", "merge"],
138 updateMethods: ["rebase", "merge"],
139 review: { ...base.review, verdicts: ["request-changes", "approve"] },
140 });
141 expect(decoded.viewerPermissions).toEqual({
142 actions: ["close", "merge"],
143 verdicts: ["request-changes", "approve"],
144 updateMethods: ["rebase", "merge"],
145 comment: true,
146 resolve: true,
147 requestReviewers: true,
148 });
149 expect(decoded.title).toBe(detail.title);
150 });
151});
152
153describe("resolvePullRequestAuthorFilter", () => {
154 it("reads me as the signed-in account on the host", () => {
155 expect(resolvePullRequestAuthorFilter("me", "bilal")).toBe("bilal");
156 expect(resolvePullRequestAuthorFilter(" @Me ", "bilal")).toBe("bilal");
157 });
158
159 it("keeps me as a literal login when the host has no viewer", () => {
160 expect(resolvePullRequestAuthorFilter("me", null)).toBe("me");
161 expect(resolvePullRequestAuthorFilter("@me", " ")).toBe("@me");
162 });
163
164 it("passes any other login through trimmed", () => {
165 expect(resolvePullRequestAuthorFilter(" ana ", "bilal")).toBe("ana");
166 expect(resolvePullRequestAuthorFilter("meredith", "bilal")).toBe("meredith");
167 });
168});