Open History to see the last commit
1import { EnvironmentId, ProjectId, SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { nativePullRequestDestination as destination } from "../../lib/pullRequestNavigation";
4
5const context = { environmentId: "remote", projectId: "project" };
6const project = (id: string, canonicalKey: string, provider: string, remoteUrl: string) => ({
7 id: ProjectId.make(id),
8 environmentId: EnvironmentId.make("remote"),
9 repositoryIdentity: {
10 canonicalKey,
11 locator: { source: "git-remote" as const, remoteName: "origin", remoteUrl },
12 displayName: canonicalKey.split("/").slice(1).join("/"),
13 provider: SourceControlProviderKind.make(provider),
14 },
15});
16const projects = [
17 project("project", "github.com/org/repo", "github", "git@github.com:org/repo.git"),
18 project("gitlab", "git.example/group/sub/repo", "gitlab", "git@git.example:group/sub/repo.git"),
19 project("forgejo", "forgejo.test/org/repo", "forgejo", "http://forgejo.test:3000/org/repo.git"),
20];
21
22const nativePullRequestDestination = (
23 url: string,
24 linkContext: typeof context | null,
25 linkProjects: typeof projects,
26 configLoaded: Parameters<typeof destination>[3] = () => true,
27) => destination(url, linkContext, linkProjects, configLoaded);
28
29describe("native PR entry points", () => {
30 it("keeps a thread link in its own environment and handles renamed host path shapes", () => {
31 expect(
32 nativePullRequestDestination(
33 "https://github.com/org/repo/pull/12/files#diff",
34 context,
35 projects,
36 ),
37 ).toEqual({ ...context, host: "github.com", repository: "org/repo", number: 12 });
38 expect(
39 nativePullRequestDestination(
40 "https://git.example/group/sub/repo/-/merge_requests/9",
41 context,
42 projects,
43 ),
44 ).toMatchObject({ projectId: "gitlab", repository: "group/sub/repo" });
45 expect(
46 nativePullRequestDestination("http://forgejo.test:3000/org/repo/pulls/3", context, projects)
47 ?.host,
48 ).toBe("forgejo.test:3000");
49 });
50
51 it("opens a repository nobody has checked out under a project on its host, as web does", () => {
52 expect(
53 nativePullRequestDestination("https://github.com/someone/else/pull/12", context, projects),
54 ).toEqual({ ...context, host: "github.com", repository: "someone/else", number: 12 });
55 });
56
57 it("leaves links to the external opener when the environment cannot show them", () => {
58 const url = "https://github.com/org/repo/pull/12";
59 expect(nativePullRequestDestination(url, context, projects, () => false)).toBeNull();
60 });
61
62 it("leaves issues, unknown sites, and unscoped links to the external opener", () => {
63 expect(
64 nativePullRequestDestination("https://github.com/org/repo/issues/12", context, projects),
65 ).toBeNull();
66 expect(
67 nativePullRequestDestination("https://example.com/org/repo/pull/12", context, projects),
68 ).toBeNull();
69 expect(
70 nativePullRequestDestination("https://github.com/org/repo/pull/12", null, projects),
71 ).toBeNull();
72 });
73});