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