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

sourceControl.test.ts

372 lines13.2 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 sourceControlRepositorySelector,
6 sourceControlHostOverridesKey,
7 detectSourceControlProviderFromRemoteUrl,
8 getChangeRequestTerminologyForKind,
9 isSshRemoteUrl,
10 resolveChangeRequestPresentation,
11} from "./sourceControl.ts";
12​
13describe("source control presentation", () => {
14 it("uses merge request terminology for GitLab", () => {
15 expect(getChangeRequestTerminologyForKind(SourceControlProviderKind.make("gitlab"))).toEqual({
16 shortLabel: "MR",
17 singular: "merge request",
18 });
19 });
20​
21 it("uses pull request terminology for GitHub-compatible providers", () => {
22 expect(getChangeRequestTerminologyForKind(SourceControlProviderKind.make("github"))).toEqual({
23 shortLabel: "PR",
24 singular: "pull request",
25 });
26 expect(
27 getChangeRequestTerminologyForKind(SourceControlProviderKind.make("azure-devops")),
28 ).toEqual({
29 shortLabel: "PR",
30 singular: "pull request",
31 });
32 expect(getChangeRequestTerminologyForKind(SourceControlProviderKind.make("bitbucket"))).toEqual(
33 {
34 shortLabel: "PR",
35 singular: "pull request",
36 },
37 );
38 });
39​
40 it("falls back to generic change request copy for unknown providers", () => {
41 expect(
42 resolveChangeRequestPresentation({
43 kind: SourceControlProviderKind.make("unknown"),
44 name: "forge",
45 baseUrl: "",
46 }),
47 ).toEqual(
48 expect.objectContaining({
49 shortName: "change request",
50 longName: "change request",
51 }),
52 );
53 });
54});
55​
56describe("detectSourceControlProviderFromRemoteUrl", () => {
57 it("uses an explicit mapping before hostname heuristics", () => {
58 expect(
59 detectSourceControlProviderFromRemoteUrl("git@github.example.test:team/repo.git", {
60 "github.example.test": { kind: "forgejo" },
61 }),
62 ).toEqual({ kind: "forgejo", name: "Forgejo", baseUrl: "https://github.example.test" });
63 expect(
64 detectSourceControlProviderFromRemoteUrl("git@code.aopc.cloud:team/repo.git", {
65 "code.aopc.cloud": { kind: "forgejo" },
66 })?.kind,
67 ).toBe("forgejo");
68 expect(
69 detectSourceControlProviderFromRemoteUrl("git@github.com:team/repo.git", {
70 "code.aopc.cloud": { kind: "forgejo" },
71 })?.kind,
72 ).toBe("github");
73 });
74​
75 it("matches hostnames case-insensitively and never matches subdomains or suffixes", () => {
76 const mappings = { "CODE.Example.TEST": { kind: "forgejo" as const } };
77 expect(
78 detectSourceControlProviderFromRemoteUrl("git@Code.EXAMPLE.test:team/repo.git", mappings)
79 ?.kind,
80 ).toBe("forgejo");
81 for (const host of [
82 "sub.code.example.test",
83 "code.example.test.evil.test",
84 "othercode.example.test",
85 ]) {
86 expect(
87 detectSourceControlProviderFromRemoteUrl(`https://${host}/team/repo.git`, mappings)?.kind,
88 ).toBe("unknown");
89 }
90 });
91​
92 it("prefers an exact port and falls back to a hostname-only mapping", () => {
93 const mappings = {
94 "code.example.test": { kind: "forgejo" as const },
95 "CODE.EXAMPLE.TEST:8443": { kind: "gitlab" as const },
96 "code.example.test:443": { kind: "github" as const },
97 };
98 expect(
99 detectSourceControlProviderFromRemoteUrl(
100 "https://code.example.test:8443/team/repo.git",
101 mappings,
102 )?.kind,
103 ).toBe("gitlab");
104 expect(
105 detectSourceControlProviderFromRemoteUrl("https://code.example.test/team/repo.git", mappings)
106 ?.kind,
107 ).toBe("github");
108 expect(
109 detectSourceControlProviderFromRemoteUrl(
110 "https://code.example.test:3000/team/repo.git",
111 mappings,
112 ),
113 ).toEqual({ kind: "forgejo", name: "Forgejo", baseUrl: "https://code.example.test:3000" });
114 expect(
115 detectSourceControlProviderFromRemoteUrl("https://code.example.test:3000/team/repo.git", {
116 "code.example.test:8443": { kind: "forgejo" },
117 })?.kind,
118 ).toBe("unknown");
119 });
120​
121 it("matches SSH ports without using them as the web port, and supports explicit base URLs", () => {
122 expect(
123 detectSourceControlProviderFromRemoteUrl("ssh://git@code.example.test:2222/team/repo.git", {
124 "code.example.test:2222": { kind: "forgejo" },
125 })?.baseUrl,
126 ).toBe("https://code.example.test");
127 expect(
128 detectSourceControlProviderFromRemoteUrl("git@code.example.test:team/repo.git", {
129 "code.example.test": { kind: "forgejo", baseUrl: "http://web.example.test:3000/forgejo/" },
130 })?.baseUrl,
131 ).toBe("http://web.example.test:3000/forgejo");
132 expect(
133 detectSourceControlProviderFromRemoteUrl("http://code.example.test:3000/team/repo.git", {
134 "code.example.test": { kind: "forgejo" },
135 })?.baseUrl,
136 ).toBe("http://code.example.test:3000");
137 });
138​
139 it("detects common source control hosts", () => {
140 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
141 "github",
142 );
143 expect(
144 detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
145 ).toBe("gitlab");
146 expect(
147 detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
148 ).toBe("azure-devops");
149 expect(
150 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
151 ).toBe("bitbucket");
152 });
153​
154 it("detects Forgejo and Gitea hosts while preserving HTTP origins", () => {
155 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
156 expect(detectSourceControlProviderFromRemoteUrl(`http://${host}:3000/team/repo.git`)).toEqual(
157 {
158 kind: "forgejo",
159 name: "Forgejo",
160 baseUrl: `http://${host}:3000`,
161 },
162 );
163 }
164 expect(getChangeRequestTerminologyForKind(SourceControlProviderKind.make("forgejo"))).toEqual({
165 shortLabel: "PR",
166 singular: "pull request",
167 });
168 });
169​
170 it("detects Azure DevOps SSH remotes", () => {
171 expect(
172 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
173 ).toBe("azure-devops");
174 expect(
175 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
176 ?.kind,
177 ).toBe("azure-devops");
178 expect(
179 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
180 ?.kind,
181 ).toBe("azure-devops");
182 });
183​
184 it("preserves ports while classifying by hostname", () => {
185 expect(
186 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
187 ).toEqual({
188 kind: "gitlab",
189 name: "GitLab",
190 baseUrl: "https://gitlab.com:8443",
191 });
192 expect(
193 detectSourceControlProviderFromRemoteUrl(
194 "https://self-hosted.example.test:8443/group/repo.git",
195 ),
196 ).toEqual({
197 kind: "unknown",
198 name: "self-hosted.example.test:8443",
199 baseUrl: "https://self-hosted.example.test:8443",
200 });
201 });
202​
203 it("does not reuse SSH ports for HTTPS provider URLs", () => {
204 expect(
205 detectSourceControlProviderFromRemoteUrl("ssh://git@gitlab.example.test:24/group/repo.git"),
206 ).toEqual({
207 kind: "gitlab",
208 name: "GitLab Self-Hosted",
209 baseUrl: "https://gitlab.example.test",
210 });
211 expect(
212 detectSourceControlProviderFromRemoteUrl("ssh://git@code.example.test:24/team/project.git"),
213 ).toEqual({
214 kind: "unknown",
215 name: "code.example.test",
216 baseUrl: "https://code.example.test",
217 });
218 });
219​
220 it("matches self-hosted providers by complete DNS labels", () => {
221 expect(
222 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
223 ).toBe("github");
224 expect(
225 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
226 ).toBe("gitlab");
227 expect(
228 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
229 ?.kind,
230 ).toBe("bitbucket");
231 });
232​
233 it("detects GitHub Enterprise Cloud data residency hosts on ghe.com", () => {
234 expect(detectSourceControlProviderFromRemoteUrl("https://acme.ghe.com/owner/repo.git")).toEqual(
235 {
236 kind: "github",
237 name: "GitHub Self-Hosted",
238 baseUrl: "https://acme.ghe.com",
239 },
240 );
241 expect(detectSourceControlProviderFromRemoteUrl("git@acme.ghe.com:owner/repo.git")?.kind).toBe(
242 "github",
243 );
244 expect(detectSourceControlProviderFromRemoteUrl("https://ghe.com/owner/repo.git")?.kind).toBe(
245 "unknown",
246 );
247 });
248​
249 it("does not match provider names embedded in unrelated DNS labels", () => {
250 expect(
251 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
252 ?.kind,
253 ).toBe("unknown");
254 expect(
255 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
256 ?.kind,
257 ).toBe("unknown");
258 expect(
259 detectSourceControlProviderFromRemoteUrl(
260 "https://notbitbucket.example.com/workspace/repo.git",
261 )?.kind,
262 ).toBe("unknown");
263 });
264​
265 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
266 expect(
267 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
268 ).toBe("gitlab");
269 expect(
270 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
271 ?.baseUrl,
272 ).toBe("https://gitlab.example.com");
273 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
274 "github",
275 );
276 expect(
277 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
278 ).toBe("bitbucket");
279 });
280});
281​
282describe("isSshRemoteUrl", () => {
283 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
284 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
285 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
286 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
287 });
288​
289 it("recognises ssh:// URLs with any case", () => {
290 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
291 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
292 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
293 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
294 });
295​
296 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
297 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
298 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
299 expect(isSshRemoteUrl("")).toBe(false);
300 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
301 });
302});
303​
304it("names an Azure DevOps repository by its own name, not its project path", () => {
305 // `az repos pr list --repository` takes a name and detects the organisation and project from
306 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
307 // reads as unavailable on the page.
308 const selector = sourceControlRepositorySelector({
309 provider: "azure-devops",
310 displayName: "contoso/payments/_git/checkout",
311 owner: "contoso",
312 name: "checkout",
313 });
314 expect(selector).toBe("checkout");
315});
316​
317it("falls back to the path's last segment where an Azure identity has no name", () => {
318 const selector = sourceControlRepositorySelector({
319 provider: "azure-devops",
320 displayName: "contoso/payments/_git/checkout",
321 });
322 expect(selector).toBe("checkout");
323});
324​
325it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
326 const selector = sourceControlRepositorySelector({
327 provider: "gitlab",
328 displayName: "group/subgroup/service",
329 owner: "group",
330 name: "service",
331 });
332 expect(selector).toBe("group/subgroup/service");
333});
334​
335it("puts owner and name back together for an identity recorded before displayName", () => {
336 const selector = sourceControlRepositorySelector({
337 provider: "github",
338 owner: "t3tools",
339 name: "t3code",
340 });
341 expect(selector).toBe("t3tools/t3code");
342});
343​
344it("names nothing for a project with no remote to name it by", () => {
345 expect(sourceControlRepositorySelector(null)).toBeNull();
346 expect(sourceControlRepositorySelector({ provider: "github" })).toBeNull();
347});
348​
349it("compares host mappings independently of entry order, hostname case, and URL spelling", () => {
350 const original = sourceControlHostOverridesKey({
351 "CODE.EXAMPLE.TEST": { kind: "forgejo", baseUrl: "https://WEB.example.test:443/Forge/" },
352 "other.example.test": { kind: "gitlab" },
353 });
354 expect(
355 sourceControlHostOverridesKey({
356 "other.example.test": { kind: "gitlab" },
357 "code.example.test": { baseUrl: "https://web.example.test/Forge", kind: "forgejo" },
358 }),
359 ).toBe(original);
360 for (const mapping of [
361 { kind: "github" as const, baseUrl: "https://web.example.test/Forge" },
362 { kind: "forgejo" as const, baseUrl: "https://web.example.test/forge" },
363 { kind: "forgejo" as const },
364 ]) {
365 expect(
366 sourceControlHostOverridesKey({
367 "code.example.test": mapping,
368 "other.example.test": { kind: "gitlab" },
369 }),
370 ).not.toBe(original);
371 }
372});