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

changeRequestLinks.test.ts

126 lines4.0 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 type RepositoryIdentity,
5 SourceControlProviderKind,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8​
9import {
10 findProjectForChangeRequest,
11 projectChangeRequestUrl,
12 resolvePullRequestPreviewTarget,
13} from "./changeRequestLinks.ts";
14​
15describe("findProjectForChangeRequest", () => {
16 const project = (identity: Record<string, unknown>) =>
17 ({ id: "p1", repositoryIdentity: identity }) as never;
18​
19 it("matches a nested GitLab group by the whole path below the host", () => {
20 const projects = [
21 project({
22 canonicalKey: "gitlab.com/t3tools/platform/t3code",
23 provider: SourceControlProviderKind.make("gitlab"),
24 displayName: "t3tools/platform/t3code",
25 owner: "t3tools",
26 name: "t3code",
27 }),
28 ];
29 expect(
30 findProjectForChangeRequest(projects, {
31 host: "gitlab.com",
32 repository: "t3tools/platform/t3code",
33 number: 42,
34 }),
35 ).toBe(projects[0]);
36 });
37​
38 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
39 const projects = [
40 project({
41 canonicalKey: "github.com/pingdotgg/t3code",
42 provider: SourceControlProviderKind.make("github"),
43 owner: "pingdotgg",
44 name: "t3code",
45 }),
46 ];
47 expect(
48 findProjectForChangeRequest(projects, {
49 host: "github.com-evil.test",
50 repository: "pingdotgg/t3code",
51 number: 1,
52 }),
53 ).toBeUndefined();
54 });
55});
56​
57describe("resolvePullRequestPreviewTarget", () => {
58 const environmentId = EnvironmentId.make("env-1");
59 const project = {
60 id: ProjectId.make("project-1"),
61 environmentId,
62 repositoryIdentity: {
63 canonicalKey: "github.com/acme/web",
64 provider: SourceControlProviderKind.make("github"),
65 displayName: "acme/web",
66 locator: { source: "git-remote", remoteName: "origin", remoteUrl: "git@github.com:acme/web" },
67 } satisfies RepositoryIdentity,
68 };
69​
70 it("previews a pull request only under the project that holds its repository", () => {
71 const target = (url: string) =>
72 resolvePullRequestPreviewTarget({
73 environmentId,
74 projects: [project],
75 pullRequestsEnabled: true,
76 url,
77 });
78 expect(target("https://github.com/acme/web/pull/7")).toEqual({
79 environmentId,
80 input: { projectId: project.id, host: "github.com", repository: "acme/web", number: 7 },
81 });
82 expect(target("https://github.com/acme/other/pull/7")).toBeNull();
83 });
84});
85​
86describe("projectChangeRequestUrl", () => {
87 const identity = (provider: string): RepositoryIdentity => ({
88 canonicalKey: "github.com/acme/default",
89 provider: SourceControlProviderKind.make(provider),
90 locator: {
91 source: "git-remote",
92 remoteName: "origin",
93 remoteUrl: "git@github.com:acme/default",
94 },
95 });
96​
97 it("builds the requested repository's URL on the project's host", () => {
98 expect(projectChangeRequestUrl(identity("github"), "acme/other", 42)).toBe(
99 "https://github.com/acme/other/pull/42",
100 );
101 });
102​
103 it("builds the project's own Azure repository from its full path, not its short selector", () => {
104 const azure: RepositoryIdentity = {
105 canonicalKey: "dev.azure.com/org/project/_git/web",
106 provider: SourceControlProviderKind.make("azure-devops"),
107 owner: "org",
108 name: "web",
109 locator: {
110 source: "git-remote",
111 remoteName: "origin",
112 remoteUrl: "https://dev.azure.com/org/project/_git/web",
113 },
114 };
115 expect(projectChangeRequestUrl(azure, "web", 42)).toBe(
116 "https://dev.azure.com/org/project/_git/web/pullrequest/42",
117 );
118 });
119​
120 it("builds nothing without a project host that knows its URL shape", () => {
121 expect(projectChangeRequestUrl(null, "acme/repository", 1)).toBeNull();
122 expect(projectChangeRequestUrl(identity("unknown"), "acme/repository", 1)).toBeNull();
123 const { provider: _provider, ...withoutProvider } = identity("github");
124 expect(projectChangeRequestUrl(withoutProvider, "acme/repository", 1)).toBeNull();
125 });
126});