Open History to see the last commit
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 findProjectOnChangeRequestHost,
12 projectChangeRequestUrl,
13 resolveChangeRequestLinkTarget,
14 resolvePullRequestPreviewTarget,
15} from "./changeRequestLinks.ts";
16
17describe("findProjectForChangeRequest", () => {
18 const project = (identity: Record<string, unknown>) =>
19 ({ id: "p1", repositoryIdentity: identity }) as never;
20
21 it("matches a nested GitLab group by the whole path below the host", () => {
22 const projects = [
23 project({
24 canonicalKey: "gitlab.com/t3tools/platform/t3code",
25 provider: SourceControlProviderKind.make("gitlab"),
26 displayName: "t3tools/platform/t3code",
27 owner: "t3tools",
28 name: "t3code",
29 }),
30 ];
31 expect(
32 findProjectForChangeRequest(projects, {
33 host: "gitlab.com",
34 repository: "t3tools/platform/t3code",
35 number: 42,
36 }),
37 ).toBe(projects[0]);
38 });
39
40 it("claims nothing for a lookalike host, which is what keeps a link a link", () => {
41 const projects = [
42 project({
43 canonicalKey: "github.com/pingdotgg/t3code",
44 provider: SourceControlProviderKind.make("github"),
45 owner: "pingdotgg",
46 name: "t3code",
47 }),
48 ];
49 expect(
50 findProjectForChangeRequest(projects, {
51 host: "github.com-evil.test",
52 repository: "pingdotgg/t3code",
53 number: 1,
54 }),
55 ).toBeUndefined();
56 });
57});
58
59describe("findProjectOnChangeRequestHost", () => {
60 const project = (id: string, identity: Record<string, unknown>) =>
61 ({ id, repositoryIdentity: identity }) as never;
62 const frontend = project("frontend", {
63 canonicalKey: "github.com/acme/frontend",
64 provider: "github",
65 owner: "acme",
66 name: "frontend",
67 });
68 const backend = project("backend", {
69 canonicalKey: "github.com/acme/backend",
70 provider: "github",
71 owner: "acme",
72 name: "backend",
73 });
74
75 it("prefers the project checked out from the link's own repository", () => {
76 expect(
77 findProjectOnChangeRequestHost([frontend, backend], {
78 host: "github.com",
79 repository: "acme/backend",
80 number: 7,
81 }),
82 ).toBe(backend);
83 });
84
85 it("lends any project on the host to a repository nobody has checked out", () => {
86 expect(
87 findProjectOnChangeRequestHost([frontend], {
88 host: "github.com",
89 repository: "acme/backend",
90 number: 7,
91 }),
92 ).toBe(frontend);
93 });
94});
95
96describe("resolveChangeRequestLinkTarget", () => {
97 const local = EnvironmentId.make("local");
98 const remote = EnvironmentId.make("remote");
99 const project = (id: string, environmentId: EnvironmentId, canonicalKey: string) => ({
100 id: ProjectId.make(id),
101 environmentId,
102 repositoryIdentity: {
103 canonicalKey,
104 provider: SourceControlProviderKind.make(
105 canonicalKey.startsWith("git.cafe") ? "gitcafe" : "github",
106 ),
107 displayName: canonicalKey.split("/").slice(1).join("/"),
108 locator: { source: "git-remote", remoteName: "origin", remoteUrl: `https://${canonicalKey}` },
109 } satisfies RepositoryIdentity,
110 });
111 const mirrored = project("mirrored", local, "git.cafe/acme/app");
112 const sibling = project("sibling", local, "github.com/acme/tools");
113 const elsewhere = project("elsewhere", remote, "github.com/acme/app");
114 const projects = [mirrored, sibling, elsewhere];
115 const hostReads = () => ({ pullRequests: true, threadPullRequests: true });
116 const url = "https://github.com/acme/app/pull/7";
117
118 it("opens a mirror's link in its own environment under a project on the link's host", () => {
119 expect(
120 resolveChangeRequestLinkTarget({
121 url,
122 projects,
123 capabilities: hostReads,
124 environmentId: local,
125 projectId: mirrored.id,
126 lendHostProject: true,
127 }),
128 ).toEqual({ project: sibling, host: "github.com", repository: "acme/app", number: 7 });
129 });
130
131 it("only borrows a host project for views that can show any repository on the host", () => {
132 const resolve = (capabilities: typeof hostReads, lendHostProject: boolean) =>
133 resolveChangeRequestLinkTarget({
134 url,
135 projects,
136 capabilities,
137 environmentId: local,
138 lendHostProject,
139 });
140 expect(resolve(hostReads, false)).toBeNull();
141 expect(resolve(() => ({ pullRequests: true, threadPullRequests: false }), true)).toBeNull();
142 });
143
144 it("finds the checked-out repository in any environment that reads change requests", () => {
145 expect(
146 resolveChangeRequestLinkTarget({
147 url,
148 projects,
149 capabilities: hostReads,
150 primaryEnvironmentId: local,
151 lendHostProject: false,
152 })?.project,
153 ).toBe(elsewhere);
154 expect(
155 resolveChangeRequestLinkTarget({
156 url,
157 projects,
158 capabilities: (environmentId) => (environmentId === remote ? undefined : hostReads()),
159 lendHostProject: false,
160 }),
161 ).toBeNull();
162 });
163});
164
165describe("resolvePullRequestPreviewTarget", () => {
166 const environmentId = EnvironmentId.make("env-1");
167 const project = {
168 id: ProjectId.make("project-1"),
169 environmentId,
170 repositoryIdentity: {
171 canonicalKey: "github.com/acme/web",
172 provider: SourceControlProviderKind.make("github"),
173 displayName: "acme/web",
174 locator: { source: "git-remote", remoteName: "origin", remoteUrl: "git@github.com:acme/web" },
175 } satisfies RepositoryIdentity,
176 };
177
178 it("previews a pull request only under the project that holds its repository", () => {
179 const target = (url: string) =>
180 resolvePullRequestPreviewTarget({
181 environmentId,
182 projects: [project],
183 pullRequestsEnabled: true,
184 url,
185 });
186 expect(target("https://github.com/acme/web/pull/7")).toEqual({
187 environmentId,
188 input: { projectId: project.id, host: "github.com", repository: "acme/web", number: 7 },
189 });
190 expect(target("https://github.com/acme/other/pull/7")).toBeNull();
191 });
192});
193
194describe("projectChangeRequestUrl", () => {
195 const identity = (provider: string): RepositoryIdentity => ({
196 canonicalKey: "github.com/acme/default",
197 provider: SourceControlProviderKind.make(provider),
198 locator: {
199 source: "git-remote",
200 remoteName: "origin",
201 remoteUrl: "git@github.com:acme/default",
202 },
203 });
204
205 it("builds the requested repository's URL on the project's host", () => {
206 expect(projectChangeRequestUrl(identity("github"), "acme/other", 42)).toBe(
207 "https://github.com/acme/other/pull/42",
208 );
209 });
210
211 it("builds the project's own Azure repository from its full path, not its short selector", () => {
212 const azure: RepositoryIdentity = {
213 canonicalKey: "dev.azure.com/org/project/_git/web",
214 provider: SourceControlProviderKind.make("azure-devops"),
215 owner: "org",
216 name: "web",
217 locator: {
218 source: "git-remote",
219 remoteName: "origin",
220 remoteUrl: "https://dev.azure.com/org/project/_git/web",
221 },
222 };
223 expect(projectChangeRequestUrl(azure, "web", 42)).toBe(
224 "https://dev.azure.com/org/project/_git/web/pullrequest/42",
225 );
226 });
227
228 it("builds nothing without a project host that knows its URL shape", () => {
229 expect(projectChangeRequestUrl(null, "acme/repository", 1)).toBeNull();
230 expect(projectChangeRequestUrl(identity("unknown"), "acme/repository", 1)).toBeNull();
231 const { provider: _provider, ...withoutProvider } = identity("github");
232 expect(projectChangeRequestUrl(withoutProvider, "acme/repository", 1)).toBeNull();
233 });
234});