Skip to content
Source
Sign inSign up

pullRequestNavigation.test.ts

79 lines3.1 KB

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({
38 ...context,
39 host: "github.com",
40 repository: "org/repo",
41 number: 12,
42 navigationUrl: "https://github.com/org/repo/pull/12/files#diff",
43 });
44 expect(
45 nativePullRequestDestination(
46 "https://git.example/group/sub/repo/-/merge_requests/9",
47 context,
48 projects,
49 ),
50 ).toMatchObject({ projectId: "gitlab", repository: "group/sub/repo" });
51 expect(
52 nativePullRequestDestination("http://forgejo.test:3000/org/repo/pulls/3", context, projects)
53 ?.host,
54 ).toBe("forgejo.test:3000");
55 });
56​
57 it("opens a repository nobody has checked out under a project on its host, as web does", () => {
58 expect(
59 nativePullRequestDestination("https://github.com/someone/else/pull/12", context, projects),
60 ).toEqual({ ...context, host: "github.com", repository: "someone/else", number: 12 });
61 });
62​
63 it("leaves links to the external opener when the environment cannot show them", () => {
64 const url = "https://github.com/org/repo/pull/12";
65 expect(nativePullRequestDestination(url, context, projects, () => false)).toBeNull();
66 });
67​
68 it("leaves issues, unknown sites, and unscoped links to the external opener", () => {
69 expect(
70 nativePullRequestDestination("https://github.com/org/repo/issues/12", context, projects),
71 ).toBeNull();
72 expect(
73 nativePullRequestDestination("https://example.com/org/repo/pull/12", context, projects),
74 ).toBeNull();
75 expect(
76 nativePullRequestDestination("https://github.com/org/repo/pull/12", null, projects),
77 ).toBeNull();
78 });
79});