Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 findProjectForChangeRequest,
5 openPullRequestLink,
6 parseChangeRequestUrl,
7 PullRequestLinkOpenError,
8} from "./openPullRequestLink";
9
10describe("openPullRequestLink", () => {
11 it("opens the requested pull request URL", async () => {
12 const openExternal = vi.fn(async () => undefined);
13 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123";
14
15 await openPullRequestLink({ openExternal }, targetUrl);
16
17 expect(openExternal).toHaveBeenCalledExactlyOnceWith(targetUrl);
18 });
19
20 it("reports bridge failures with a safe target origin", async () => {
21 const cause = new Error("desktop shell unavailable");
22 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123?token=secret";
23 const openExternal = vi.fn(async () => Promise.reject(cause));
24
25 const result = openPullRequestLink({ openExternal }, targetUrl);
26
27 await expect(result).rejects.toEqual(
28 new PullRequestLinkOpenError({
29 targetOrigin: "https://github.com",
30 cause,
31 }),
32 );
33 await expect(result).rejects.not.toHaveProperty("message", expect.stringContaining("secret"));
34 });
35});
36
37describe("parseChangeRequestUrl", () => {
38 it("reads a GitHub pull request", () => {
39 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
40 host: "github.com",
41 repository: "t3tools/t3code",
42 number: 123,
43 });
44 });
45
46 it("reads a pull request on a GitHub Enterprise host", () => {
47 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
48 host: "github.acme.test",
49 repository: "platform/api",
50 number: 7,
51 });
52 });
53
54 it("reads a GitLab merge request, nested groups and all", () => {
55 expect(
56 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
57 ).toEqual({
58 host: "gitlab.com",
59 repository: "t3tools/platform/t3code",
60 number: 42,
61 });
62 });
63
64 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
65 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
66 {
67 host: "code.acme.test",
68 repository: "team/project",
69 number: 9,
70 },
71 );
72 });
73
74 it("reads a Bitbucket pull request", () => {
75 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
76 host: "bitbucket.org",
77 repository: "workspace/repo",
78 number: 5,
79 });
80 });
81
82 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
83 expect(
84 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
85 ).toEqual({
86 host: "dev.azure.com",
87 repository: "acme/platform/_git/t3code",
88 number: 17,
89 });
90 expect(
91 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
92 ).toEqual({
93 host: "acme.visualstudio.com",
94 repository: "platform/_git/t3code",
95 number: 17,
96 });
97 });
98
99 it("survives trailing segments, a trailing slash and a query string", () => {
100 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
101 host: "github.com",
102 repository: "t3tools/t3code",
103 number: 123,
104 });
105 expect(
106 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
107 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
108 expect(
109 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
110 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
111 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
112 host: "github.com",
113 repository: "t3tools/t3code",
114 number: 123,
115 });
116 });
117
118 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
119 for (const link of [
120 "https://github.com/t3tools/t3code/issues/123",
121 "https://github.com/t3tools/t3code/commit/0a1b2c3",
122 "https://github.com/t3tools/t3code",
123 "https://github.com/t3tools/t3code/pull/abc",
124 "https://gitlab.com/t3tools/t3code/-/snippets/12",
125 "https://gitlab.com/t3tools/t3code/-/issues/12",
126 // A path shape that means nothing off its own host.
127 "https://blog.example.test/2026/updates/pull/3",
128 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
129 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
130 // Request` that refuses it, because no project in the workspace is checked out from it.
131 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
132 "not a url",
133 ]) {
134 expect(parseChangeRequestUrl(link), link).toBeNull();
135 }
136 });
137});
138
139describe("findProjectForChangeRequest", () => {
140 const project = (identity: Record<string, unknown>) =>
141 ({ id: "p1", repositoryIdentity: identity }) as never;
142
143 it("matches a nested GitLab group by the whole path below the host", () => {
144 // The server identifies a repository by `displayName`, which keeps every group segment; the
145 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
146 const projects = [
147 project({
148 canonicalKey: "gitlab.com/t3tools/platform/t3code",
149 provider: "gitlab",
150 displayName: "t3tools/platform/t3code",
151 owner: "t3tools",
152 name: "t3code",
153 }),
154 ];
155 expect(
156 findProjectForChangeRequest(projects, {
157 host: "gitlab.com",
158 repository: "t3tools/platform/t3code",
159 number: 42,
160 }),
161 ).toBe(projects[0]);
162 });
163
164 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
165 const projects = [
166 project({
167 canonicalKey: "github.com/pingdotgg/t3code",
168 provider: "github",
169 owner: "pingdotgg",
170 name: "t3code",
171 }),
172 ];
173 expect(
174 findProjectForChangeRequest(projects, {
175 host: "github.acme.test",
176 repository: "pingdotgg/t3code",
177 number: 1,
178 }),
179 ).toBeUndefined();
180 });
181
182 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
183 const projects = [
184 project({
185 canonicalKey: "github.com/pingdotgg/t3code",
186 provider: "github",
187 owner: "pingdotgg",
188 name: "t3code",
189 }),
190 ];
191 expect(
192 findProjectForChangeRequest(projects, {
193 host: "github.com-evil.test",
194 repository: "pingdotgg/t3code",
195 number: 1,
196 }),
197 ).toBeUndefined();
198 });
199});