Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openPullRequestLink.test.ts

545 lines18.3 KB

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";
14import { normalizeGitRemoteUrl } from "@t3tools/shared/git";
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 };
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.each([
175 ["http://forge.example:3000/git/team/repo/pulls/42/files", true],
176 ["http://forge.example:4000/git/team/repo/pulls/42", false],
177 ["http://forge.example/git/team/repo/pulls/42", false],
178 ])("matches Forgejo links by web authority: %s", (url, expected) => {
179 expect(
180 matchesLinkedPullRequestUrl(
181 { ...linkedPullRequest, url: "http://forge.example:3000/git/team/repo/pulls/42" },
182 url,
183 ),
184 ).toBe(expected);
185 });
186​
187 it("keeps other providers' existing port normalization", () => {
188 expect(
189 matchesLinkedPullRequestUrl(
190 linkedPullRequest,
191 "https://github.com:8443/pingdotgg/t3code/pull/42",
192 ),
193 ).toBe(true);
194 });
195​
196 it("keeps Forgejo and GitHub path shapes distinct on a GitHub-named host", () => {
197 expect(
198 matchesLinkedPullRequestUrl(
199 { ...linkedPullRequest, url: "https://github.internal/team/repo/pulls/42" },
200 "https://github.internal/team/repo/pull/42",
201 ),
202 ).toBe(false);
203 });
204​
205 it("rejects a different pull request or host", () => {
206 expect(
207 matchesLinkedPullRequestUrl(linkedPullRequest, "https://github.com/pingdotgg/t3code/pull/43"),
208 ).toBe(false);
209 expect(
210 matchesLinkedPullRequestUrl(
211 linkedPullRequest,
212 "https://github.example.com/pingdotgg/t3code/pull/42",
213 ),
214 ).toBe(false);
215 });
216});
217​
218describe("shouldOpenPullRequestExternally", () => {
219 it("uses the browser for command-click and control-click", () => {
220 expect(shouldOpenPullRequestExternally({ metaKey: true, ctrlKey: false })).toBe(true);
221 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: true })).toBe(true);
222 });
223​
224 it("keeps an unmodified click in the pull request view", () => {
225 expect(shouldOpenPullRequestExternally({ metaKey: false, ctrlKey: false })).toBe(false);
226 });
227});
228​
229describe("parseChangeRequestUrl", () => {
230 it("reads a GitHub pull request", () => {
231 expect(parseChangeRequestUrl("https://github.com/T3Tools/T3Code/pull/123")).toEqual({
232 host: "github.com",
233 repository: "t3tools/t3code",
234 number: 123,
235 });
236 });
237​
238 it("reads a pull request on a GitHub Enterprise host", () => {
239 expect(parseChangeRequestUrl("https://github.acme.test/platform/api/pull/7")).toEqual({
240 host: "github.acme.test",
241 repository: "platform/api",
242 number: 7,
243 });
244 });
245​
246 it("reads a GitLab merge request, nested groups and all", () => {
247 expect(
248 parseChangeRequestUrl("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42"),
249 ).toEqual({
250 host: "gitlab.com",
251 repository: "t3tools/platform/t3code",
252 number: 42,
253 });
254 });
255​
256 it("reads a merge request on a self-hosted GitLab named nothing like GitLab", () => {
257 expect(parseChangeRequestUrl("https://code.acme.test/team/project/-/merge_requests/9")).toEqual(
258 {
259 host: "code.acme.test",
260 repository: "team/project",
261 number: 9,
262 },
263 );
264 });
265​
266 it("reads a Bitbucket pull request", () => {
267 expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
268 host: "bitbucket.org",
269 repository: "workspace/repo",
270 number: 5,
271 });
272 });
273​
274 it("reads both Azure DevOps URL forms, keeping `_git` in the repository path", () => {
275 expect(
276 parseChangeRequestUrl("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17"),
277 ).toEqual({
278 host: "dev.azure.com",
279 repository: "acme/platform/_git/t3code",
280 number: 17,
281 });
282 expect(
283 parseChangeRequestUrl("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
284 ).toEqual({
285 host: "acme.visualstudio.com",
286 repository: "platform/_git/t3code",
287 number: 17,
288 });
289 });
290​
291 it("survives trailing segments, a trailing slash and a query string", () => {
292 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({
293 host: "github.com",
294 repository: "t3tools/t3code",
295 number: 123,
296 });
297 expect(
298 parseChangeRequestUrl("https://gitlab.com/team/project/-/merge_requests/42/diffs#note_1"),
299 ).toEqual({ host: "gitlab.com", repository: "team/project", number: 42 });
300 expect(
301 parseChangeRequestUrl("https://bitbucket.org/team/repo/pull-requests/5/commits"),
302 ).toEqual({ host: "bitbucket.org", repository: "team/repo", number: 5 });
303 expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/")).toEqual({
304 host: "github.com",
305 repository: "t3tools/t3code",
306 number: 123,
307 });
308 });
309​
310 it("claims nothing it cannot be sure of, so the link goes to the browser", () => {
311 for (const link of [
312 "https://github.com/t3tools/t3code/issues/123",
313 "https://github.com/t3tools/t3code/commit/0a1b2c3",
314 "https://github.com/t3tools/t3code",
315 "https://github.com/t3tools/t3code/pull/abc",
316 "https://gitlab.com/t3tools/t3code/-/snippets/12",
317 "https://gitlab.com/t3tools/t3code/-/issues/12",
318 // A path shape that means nothing off its own host.
319 "https://blog.example.test/2026/updates/pull/3",
320 // A lookalike is deliberately not fought here: `github.com.evil.test` reads as a GitHub
321 // Enterprise install and there is no way to tell it from one. It is `findProjectForChange
322 // Request` that refuses it, because no project in the workspace is checked out from it.
323 "javascript:alert(1)//github.com/t3tools/t3code/pull/1",
324 "not a url",
325 ]) {
326 expect(parseChangeRequestUrl(link), link).toBeNull();
327 }
328 });
329});
330​
331describe("findProjectOnChangeRequestHost", () => {
332 const project = (id: string, identity: Record<string, unknown>) =>
333 ({ id, repositoryIdentity: identity }) as never;
334 const frontend = project("frontend", {
335 canonicalKey: "github.com/acme/frontend",
336 provider: "github",
337 owner: "acme",
338 name: "frontend",
339 });
340 const backend = project("backend", {
341 canonicalKey: "github.com/acme/backend",
342 provider: "github",
343 owner: "acme",
344 name: "backend",
345 });
346​
347 it.each([
348 "ssh.dev.azure.com/v3/org-a/project/web",
349 "vs-ssh.visualstudio.com/v3/org-a/project/web",
350 "org-a.visualstudio.com/defaultcollection/project/_git/web",
351 "dev.azure.com/org-a/project/_git/web",
352 ])("matches Azure browser references against %s", (canonicalKey) => {
353 const checkout = project("azure", {
354 canonicalKey,
355 provider: "azure-devops",
356 displayName: canonicalKey.split("/").slice(1).join("/"),
357 });
358 const reference = { host: "dev.azure.com", repository: "org-a/project/_git/web", number: 42 };
359 expect(findProjectForChangeRequest([checkout], reference)).toBe(checkout);
360 expect(findProjectOnChangeRequestHost([checkout], reference)).toBe(checkout);
361 expect(
362 findProjectOnChangeRequestHost([checkout], {
363 ...reference,
364 repository: "org-b/project/_git/web",
365 }),
366 ).toBeUndefined();
367 expect(
368 findProjectOnChangeRequestHost([checkout], {
369 ...reference,
370 repository: "org-a/other-project/_git/web",
371 }),
372 ).toBeUndefined();
373 });
374​
375 it("prefers the project checked out from the link's own repository", () => {
376 expect(
377 findProjectOnChangeRequestHost([frontend, backend], {
378 host: "github.com",
379 repository: "acme/backend",
380 number: 7,
381 }),
382 ).toBe(backend);
383 });
384​
385 it("selects the Forgejo HTTP port for repository and host-level matches", () => {
386 const projects = [3000, 4000].map((port) =>
387 project(`forgejo-${port}`, {
388 canonicalKey: "forge.example/git/team/repo",
389 provider: "forgejo",
390 displayName: "git/team/repo",
391 locator: { remoteUrl: `http://forge.example:${port}/git/team/repo.git` },
392 }),
393 );
394 const reference = parseChangeRequestUrl("http://forge.example:4000/git/team/repo/pulls/42")!;
395 expect(findProjectForChangeRequest(projects, reference)).toBe(projects[1]);
396 expect(findProjectOnChangeRequestHost(projects, reference)).toBe(projects[1]);
397 expect(
398 findProjectOnChangeRequestHost(projects, { ...reference, repository: "git/team/other" }),
399 ).toBe(projects[1]);
400 expect(findProjectForChangeRequest([projects[0]!], reference)).toBeUndefined();
401 expect(findProjectOnChangeRequestHost([projects[0]!], reference)).toBeUndefined();
402 });
403​
404 it("lets tea resolve the web port for Forgejo SSH remotes", () => {
405 const checkout = project("forgejo-ssh", {
406 canonicalKey: "forge.example/git/team/repo",
407 provider: "forgejo",
408 displayName: "git/team/repo",
409 locator: { remoteUrl: "git@forge.example:git/team/repo.git" },
410 });
411 const reference = parseChangeRequestUrl("http://forge.example:4000/git/team/repo/pulls/42")!;
412 expect(findProjectForChangeRequest([checkout], reference)).toBe(checkout);
413 const aliased = project("forgejo-alias", {
414 canonicalKey: "ssh.forge.example/team/repo",
415 provider: "forgejo",
416 displayName: "team/repo",
417 locator: { remoteUrl: "git@ssh.forge.example:team/repo.git" },
418 webUrl: "http://forge.example:4000/git/team/repo",
419 });
420 expect(findProjectForChangeRequest([aliased], reference)).toBe(aliased);
421 expect(findProjectOnChangeRequestHost([aliased], reference)).toBe(aliased);
422 expect(
423 findProjectOnChangeRequestHost([aliased], { ...reference, repository: "git/team/other" }),
424 ).toBe(aliased);
425 for (const url of [
426 "http://other.example:4000/git/team/repo/pulls/42",
427 "http://forge.example:3000/git/team/repo/pulls/42",
428 ]) {
429 const other = parseChangeRequestUrl(url)!;
430 expect(findProjectForChangeRequest([aliased], other)).toBeUndefined();
431 expect(findProjectOnChangeRequestHost([aliased], other)).toBeUndefined();
432 }
433 const otherMount = parseChangeRequestUrl("http://forge.example:4000/other/team/repo/pulls/42")!;
434 expect(findProjectForChangeRequest([aliased], otherMount)).toBeUndefined();
435 expect(findProjectOnChangeRequestHost([aliased], otherMount)).toBeUndefined();
436 });
437​
438 it("lends any project on the host to a repository nobody has checked out", () => {
439 expect(
440 findProjectOnChangeRequestHost([frontend], {
441 host: "github.com",
442 repository: "acme/backend",
443 number: 7,
444 }),
445 ).toBe(frontend);
446 });
447​
448 it("finds nothing on a host nothing is checked out from", () => {
449 expect(
450 findProjectOnChangeRequestHost([frontend], {
451 host: "gitlab.com",
452 repository: "acme/backend",
453 number: 7,
454 }),
455 ).toBeUndefined();
456 });
457});
458​
459describe("findProjectForChangeRequest", () => {
460 const project = (identity: Record<string, unknown>) =>
461 ({ id: "p1", repositoryIdentity: identity }) as never;
462​
463 it("matches a nested GitLab group by the whole path below the host", () => {
464 // The server identifies a repository by `displayName`, which keeps every group segment; the
465 // two-segment owner/name form would look for `t3tools/t3code` and find nothing.
466 const projects = [
467 project({
468 canonicalKey: "gitlab.com/t3tools/platform/t3code",
469 provider: "gitlab",
470 displayName: "t3tools/platform/t3code",
471 owner: "t3tools",
472 name: "t3code",
473 }),
474 ];
475 expect(
476 findProjectForChangeRequest(projects, {
477 host: "gitlab.com",
478 repository: "t3tools/platform/t3code",
479 number: 42,
480 }),
481 ).toBe(projects[0]);
482 });
483​
484 it("keeps two hosts apart, so an Enterprise link does not open the public one", () => {
485 const projects = [
486 project({
487 canonicalKey: "github.com/pingdotgg/t3code",
488 provider: "github",
489 owner: "pingdotgg",
490 name: "t3code",
491 }),
492 ];
493 expect(
494 findProjectForChangeRequest(projects, {
495 host: "github.acme.test",
496 repository: "pingdotgg/t3code",
497 number: 1,
498 }),
499 ).toBeUndefined();
500 });
501​
502 it("matches an Azure repository cloned over SSH, whose remote shares no part with its URL", () => {
503 // Azure alone addresses one repository under two names: `ssh.dev.azure.com` and `v3/...` over
504 // SSH against `dev.azure.com` and `.../_git/...` everywhere a person sees it. The identity is
505 // recorded in the spelling a link arrives in, so both halves of this comparison line up.
506 //
507 // Derived from the SSH remote the way the server derives it rather than written out, so the
508 // day that normalization stops reaching the web spelling this fails here too.
509 const canonicalKey = normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code");
510 const projects = [
511 project({
512 canonicalKey,
513 provider: "azure-devops",
514 displayName: canonicalKey.split("/").slice(1).join("/"),
515 owner: "t3tools",
516 name: "t3code",
517 }),
518 ];
519 expect(
520 findProjectForChangeRequest(projects, {
521 host: "dev.azure.com",
522 repository: "t3tools/platform/_git/t3code",
523 number: 1,
524 }),
525 ).toBe(projects[0]);
526 });
527​
528 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
529 const projects = [
530 project({
531 canonicalKey: "github.com/pingdotgg/t3code",
532 provider: "github",
533 owner: "pingdotgg",
534 name: "t3code",
535 }),
536 ];
537 expect(
538 findProjectForChangeRequest(projects, {
539 host: "github.com-evil.test",
540 repository: "pingdotgg/t3code",
541 number: 1,
542 }),
543 ).toBeUndefined();
544 });
545});