Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import {
4 changeRequestRepositoryUrl,
5 findProjectForChangeRequest,
6 gitHubPullRequestBrowserUrl,
7 matchesLinkedPullRequestUrl,
8 openPullRequestLink,
9 parseChangeRequestUrl,
10 pullRequestCandidateUrlFromReferenceAutolink,
11 PullRequestLinkOpenError,
12 shouldOpenPullRequestExternally,
13} from "./openPullRequestLink";
14import { ProjectId, type RepositoryIdentity } from "@t3tools/contracts";
15
16function repositoryIdentity(
17 provider: string,
18 canonicalKey: string,
19 remoteUrl: string,
20): RepositoryIdentity {
21 return {
22 canonicalKey,
23 provider,
24 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
25 };
26}
27
28describe("gitHubPullRequestBrowserUrl", () => {
29 it("uses the requested GitHub repository instead of the project's default repository", () => {
30 const identity = repositoryIdentity(
31 "github",
32 "github.com/acme/default",
33 "https://github.com/acme/default.git",
34 );
35
36 expect(gitHubPullRequestBrowserUrl(identity, "acme/other", 42)).toBe(
37 "https://github.com/acme/other/pull/42",
38 );
39 });
40
41 it("preserves a custom GitHub HTTP origin without its credentials", () => {
42 const identity = repositoryIdentity(
43 "github",
44 "github.acme.test/team/default",
45 "http://token@github.acme.test:8443/team/default.git",
46 );
47
48 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 7)).toBe(
49 "http://github.acme.test:8443/platform/api/pull/7",
50 );
51 });
52
53 it.each([
54 {
55 name: "SSH",
56 remoteUrl: "git@github.acme.test:team/default.git",
57 },
58 {
59 name: "git protocol",
60 remoteUrl: "git://github.acme.test/team/default.git",
61 },
62 ])("uses the normalized host for a $name remote", ({ remoteUrl }) => {
63 const identity = repositoryIdentity("github", "github.acme.test/team/default", remoteUrl);
64
65 expect(gitHubPullRequestBrowserUrl(identity, "platform/api", 9)).toBe(
66 "https://github.acme.test/platform/api/pull/9",
67 );
68 });
69
70 it("returns null for missing or invalid GitHub data", () => {
71 expect(gitHubPullRequestBrowserUrl(null, "acme/repository", 1)).toBeNull();
72 expect(
73 gitHubPullRequestBrowserUrl(
74 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
75 "acme",
76 1,
77 ),
78 ).toBeNull();
79 expect(
80 gitHubPullRequestBrowserUrl(
81 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
82 "../repository",
83 1,
84 ),
85 ).toBeNull();
86 expect(
87 gitHubPullRequestBrowserUrl(
88 repositoryIdentity("github", "github.com/acme/repository", "https://github.com/a/b"),
89 "acme/repository",
90 0,
91 ),
92 ).toBeNull();
93 expect(
94 gitHubPullRequestBrowserUrl(
95 repositoryIdentity("github", "bad host/acme/repository", "not a remote"),
96 "acme/repository",
97 1,
98 ),
99 ).toBeNull();
100 });
101
102 it.each(["gitlab", "bitbucket", "azure-devops", "unknown"])(
103 "does not build a fallback for %s",
104 (provider) => {
105 expect(
106 gitHubPullRequestBrowserUrl(
107 repositoryIdentity(provider, "github.com/acme/repository", "https://github.com/a/b"),
108 "acme/repository",
109 1,
110 ),
111 ).toBeNull();
112 },
113 );
114});
115
116describe("changeRequestRepositoryUrl", () => {
117 it("preserves repository path casing", () => {
118 expect(
119 changeRequestRepositoryUrl(
120 "https://gitlab.example.test/Team/Platform/Repo/-/merge_requests/42/diffs#note_1",
121 ),
122 ).toBe("https://gitlab.example.test/Team/Platform/Repo");
123 });
124
125 it("keeps pull-like segments inside nested GitLab repository paths", () => {
126 expect(
127 changeRequestRepositoryUrl(
128 "https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42",
129 ),
130 ).toBe("https://gitlab.example.test/group/pull/123/repo");
131 });
132});
133
134describe("pullRequestCandidateUrlFromReferenceAutolink", () => {
135 it("turns GitHub's shared issue route into a pull request candidate", () => {
136 expect(
137 pullRequestCandidateUrlFromReferenceAutolink(
138 "https://github.com/pingdotgg/t3code/issues/8600#issuecomment-1",
139 ),
140 ).toBe("https://github.com/pingdotgg/t3code/pull/8600#issuecomment-1");
141 });
142
143 it("does not reinterpret other issue hosts or malformed references", () => {
144 expect(
145 pullRequestCandidateUrlFromReferenceAutolink(
146 "https://gitlab.com/pingdotgg/t3code/-/issues/8600",
147 ),
148 ).toBeNull();
149 expect(
150 pullRequestCandidateUrlFromReferenceAutolink(
151 "https://github.com/pingdotgg/t3code/issues/not-a-number",
152 ),
153 ).toBeNull();
154 });
155});
156
157describe("matchesLinkedPullRequestUrl", () => {
158 const linkedPullRequest = {
159 projectId: ProjectId.make("project-1"),
160 repository: "pingdotgg/t3code",
161 number: 42,
162 url: "https://github.com/pingdotgg/t3code/pull/42",
163 };
164
165 it("matches the same pull request without looking up its project", () => {
166 expect(
167 matchesLinkedPullRequestUrl(
168 linkedPullRequest,
169 "https://github.com/PingDotGG/T3Code/pull/42/files",
170 ),
171 ).toBe(true);
172 });
173
174 it("rejects a different pull request or host", () => {
175 expect(
176 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
177 ).toBe(false);
178 expect(
179 matchesLinkedPullRequestUrl(
180 linkedPullRequest,
181 "https://github.example.com/pingdotgg/t3code/pull/42",
182 ),
183 ).toBe(false);
184 });
185});
186
187describe("openPullRequestLink", () => {
188 it("opens the requested pull request URL", async () => {
189 const openExternal = vi.fn(async () => undefined);
190 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123";
191
192 await openPullRequestLink({ openExternal }, targetUrl);
193
194 expect(openExternal).toHaveBeenCalledExactlyOnceWith(targetUrl);
195 });
196
197 it("reports bridge failures with a safe target origin", async () => {
198 const cause = new Error("desktop shell unavailable");
199 const targetUrl = "https://github.com/pingdotgg/t3code/pull/123?token=secret";
200 const openExternal = vi.fn(async () => Promise.reject(cause));
201
202 const result = openPullRequestLink({ openExternal }, targetUrl);
203
204 await expect(result).rejects.toEqual(
205 new PullRequestLinkOpenError({
206 targetOrigin: "https://github.com",
207 cause,
208 }),
209 );
210 await expect(result).rejects.not.toHaveProperty("message", expect.stringContaining("secret"));
211 });
212});
213
214describe("shouldOpenPullRequestExternally", () => {
215 it("uses the browser for command-click and control-click", () => {
216 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
217 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
218 });
219
220 it("keeps an unmodified click in the pull request view", () => {
221 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
222 });
223});
224
225describe("parseChangeRequestUrl", () => {
226 it("reads a GitHub pull request", () => {
227 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
228 host: "github.com",
229 repository: "t3tools/t3code",
230 number: 123,
231 });
232 });
233
234 it("reads a pull request on a GitHub Enterprise host", () => {
235 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
236 host: "github.acme.test",
237 repository: "platform/api",
238 number: 7,
239 });
240 });
241
242 it("reads a GitLab merge request, nested groups and all", () => {
243 expect(
244 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
245 ).toEqual({
246 host: "gitlab.com",
247 repository: "t3tools/platform/t3code",
248 number: 42,
249 });
250 });
251
252 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
253 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
254 {
255 host: "code.acme.test",
256 repository: "team/project",
257 number: 9,
258 },
259 );
260 });
261
262 it("reads a Bitbucket pull request", () => {
263 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
264 host: "bitbucket.org",
265 repository: "workspace/repo",
266 number: 5,
267 });
268 });
269
270 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
271 expect(
272 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
273 ).toEqual({
274 host: "dev.azure.com",
275 repository: "acme/platform/_git/t3code",
276 number: 17,
277 });
278 expect(
279 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
280 ).toEqual({
281 host: "acme.visualstudio.com",
282 repository: "platform/_git/t3code",
283 number: 17,
284 });
285 });
286
287 it("survives trailing segments, a trailing slash and a query string", () => {
288 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
289 host: "github.com",
290 repository: "t3tools/t3code",
291 number: 123,
292 });
293 expect(
294 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
295 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
296 expect(
297 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
298 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
299 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
300 host: "github.com",
301 repository: "t3tools/t3code",
302 number: 123,
303 });
304 });
305
306 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
307 for (const link of [
308 "https://github.com/t3tools/t3code/issues/123",
309 "https://github.com/t3tools/t3code/commit/0a1b2c3",
310 "https://github.com/t3tools/t3code",
311 "https://github.com/t3tools/t3code/pull/abc",
312 "https://gitlab.com/t3tools/t3code/-/snippets/12",
313 "https://gitlab.com/t3tools/t3code/-/issues/12",
314 // A path shape that means nothing off its own host.
315 "https://blog.example.test/2026/updates/pull/3",
316 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
317 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
318 // Request` that refuses it, because no project in the workspace is checked out from it.
319 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
320 "not a url",
321 ]) {
322 expect(parseChangeRequestUrl(link), link).toBeNull();
323 }
324 });
325});
326
327describe("findProjectForChangeRequest", () => {
328 const project = (identity: Record<string, unknown>) =>
329 ({ id: "p1", repositoryIdentity: identity }) as never;
330
331 it("matches a nested GitLab group by the whole path below the host", () => {
332 // The server identifies a repository by `displayName`, which keeps every group segment; the
333 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
334 const projects = [
335 project({
336 canonicalKey: "gitlab.com/t3tools/platform/t3code",
337 provider: "gitlab",
338 displayName: "t3tools/platform/t3code",
339 owner: "t3tools",
340 name: "t3code",
341 }),
342 ];
343 expect(
344 findProjectForChangeRequest(projects, {
345 host: "gitlab.com",
346 repository: "t3tools/platform/t3code",
347 number: 42,
348 }),
349 ).toBe(projects[0]);
350 });
351
352 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
353 const projects = [
354 project({
355 canonicalKey: "github.com/pingdotgg/t3code",
356 provider: "github",
357 owner: "pingdotgg",
358 name: "t3code",
359 }),
360 ];
361 expect(
362 findProjectForChangeRequest(projects, {
363 host: "github.acme.test",
364 repository: "pingdotgg/t3code",
365 number: 1,
366 }),
367 ).toBeUndefined();
368 });
369
370 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
371 const projects = [
372 project({
373 canonicalKey: "github.com/pingdotgg/t3code",
374 provider: "github",
375 owner: "pingdotgg",
376 name: "t3code",
377 }),
378 ];
379 expect(
380 findProjectForChangeRequest(projects, {
381 host: "github.com-evil.test",
382 repository: "pingdotgg/t3code",
383 number: 1,
384 }),
385 ).toBeUndefined();
386 });
387});