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 hostReads = () => ({ pullRequests: true, threadPullRequests: true });
23const nativePullRequestDestination = (
24 url: string,
25 linkContext: typeof context | null,
26 linkProjects: typeof projects,
27 capabilities: Parameters<typeof destination>[3] = hostReads,
28) => destination(url, linkContext, linkProjects, capabilities);
29
30describe("native PR entry points", () => {
31 it("keeps a thread link in its own environment and handles renamed host path shapes", () => {
32 expect(
33 nativePullRequestDestination(
34 "https://github.com/org/repo/pull/12/files#diff",
35 context,
36 projects,
37 ),
38 ).toEqual({ ...context, host: "github.com", repository: "org/repo", number: 12 });
39 expect(
40 nativePullRequestDestination(
41 "https://git.example/group/sub/repo/-/merge_requests/9",
42 context,
43 projects,
44 ),
45 ).toMatchObject({ projectId: "gitlab", repository: "group/sub/repo" });
46 expect(
47 nativePullRequestDestination("http://forgejo.test:3000/org/repo/pulls/3", context, projects)
48 ?.host,
49 ).toBe("forgejo.test:3000");
50 });
51
52 it("opens a repository nobody has checked out under a project on its host, as web does", () => {
53 expect(
54 nativePullRequestDestination("https://github.com/someone/else/pull/12", context, projects),
55 ).toEqual({ ...context, host: "github.com", repository: "someone/else", number: 12 });
56 });
57
58 it("leaves links to the external opener when the environment cannot show them", () => {
59 const url = "https://github.com/org/repo/pull/12";
60 expect(nativePullRequestDestination(url, context, projects, () => undefined)).toBeNull();
61 expect(
62 nativePullRequestDestination(
63 "https://github.com/someone/else/pull/12",
64 context,
65 projects,
66 () => ({
67 pullRequests: true,
68 threadPullRequests: false,
69 }),
70 ),
71 ).toBeNull();
72 });
73
74 it("leaves issues, unknown sites, and unscoped links to the external opener", () => {
75 expect(
76 nativePullRequestDestination("https://github.com/org/repo/issues/12", context, projects),
77 ).toBeNull();
78 expect(
79 nativePullRequestDestination("https://example.com/org/repo/pull/12", context, projects),
80 ).toBeNull();
81 expect(
82 nativePullRequestDestination("https://github.com/org/repo/pull/12", null, projects),
83 ).toBeNull();
84 });
85});