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 { PullRequestDetail, PullRequestListResult, pullRequestHostOf } from "./pullRequest.ts";
6
7const decodeDetail = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestDetail));
8
9const LIST_RESULT: PullRequestListResult = {
10 viewers: { "github.com": "bilal", "gitlab.com": "bilal.hassan" },
11 providers: [
12 {
13 host: "github.com",
14 kind: SourceControlProviderKind.make("github"),
15 searchesOnHost: true,
16 projectCount: 1,
17 configured: true,
18 detail: null,
19 },
20 {
21 host: "gitlab.com",
22 kind: SourceControlProviderKind.make("gitlab"),
23 searchesOnHost: true,
24 projectCount: 1,
25 configured: false,
26 detail: "glab is not installed.",
27 },
28 ],
29 entries: [
30 {
31 provider: SourceControlProviderKind.make("github"),
32 host: "github.com",
33 projectId: "project-1" as PullRequestListResult["entries"][number]["projectId"],
34 projectTitle: "t3code",
35 repository: "pingdotgg/t3code",
36 number: 1,
37 title: "Add a pull requests page",
38 url: "https://github.com/pingdotgg/t3code/pull/1",
39 author: { login: "octocat", name: null, avatarUrl: null },
40 headBranch: "feat/page",
41 baseBranch: "main",
42 state: "open",
43 isDraft: false,
44 mergeability: "mergeable",
45 additions: 1,
46 deletions: 0,
47 createdAt: "2026-07-01T00:00:00Z",
48 updatedAt: "2026-07-02T00:00:00Z",
49 viewerReviewRequested: false,
50 labels: [{ name: "backend", color: null }],
51 },
52 ],
53 errors: [],
54 truncated: false,
55 nextCursors: { "github.com pingdotgg/t3code": "2026-07-02T00:00:00Z|1|1" },
56};
57
58describe("PullRequestListResult", () => {
59 it("separates Forgejo HTTP ports while preserving other provider host identities", () => {
60 const identity = {
61 canonicalKey: "forge.example/team/repo",
62 locator: { remoteUrl: "http://forge.example:3000/team/repo.git" },
63 };
64 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("forgejo"))).toBe(
65 "forge.example:3000",
66 );
67 expect(pullRequestHostOf(identity, SourceControlProviderKind.make("gitlab"))).toBe(
68 "forge.example",
69 );
70 expect(
71 pullRequestHostOf(
72 { ...identity, locator: { remoteUrl: "ssh://git@forge.example:2222/team/repo.git" } },
73 SourceControlProviderKind.make("forgejo"),
74 ),
75 ).toBe("forge.example");
76 });
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", "fast-forward", "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});