Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 sourceControlRepositorySelector,
6 detectSourceControlProviderFromRemoteUrl,
7 getChangeRequestTerminologyForKind,
8} from "./sourceControl.ts";
9
10describe("detectSourceControlProviderFromRemoteUrl", () => {
11 it("detects Forgejo and Gitea hosts while preserving HTTP origins", () => {
12 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
13 expect(detectSourceControlProviderFromRemoteUrl(`http://${host}:3000/team/repo.git`)).toEqual(
14 {
15 kind: "forgejo",
16 name: "Forgejo",
17 baseUrl: `http://${host}:3000`,
18 },
19 );
20 }
21 expect(getChangeRequestTerminologyForKind(SourceControlProviderKind.make("forgejo"))).toEqual({
22 shortLabel: "PR",
23 singular: "pull request",
24 });
25 });
26
27 it("detects Azure DevOps SSH remotes", () => {
28 expect(
29 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
30 ).toBe("azure-devops");
31 expect(
32 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
33 ?.kind,
34 ).toBe("azure-devops");
35 expect(
36 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
37 ?.kind,
38 ).toBe("azure-devops");
39 });
40
41 it("preserves ports while classifying by hostname", () => {
42 expect(
43 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
44 ).toEqual({
45 kind: "gitlab",
46 name: "GitLab",
47 baseUrl: "https://gitlab.com:8443",
48 });
49 expect(
50 detectSourceControlProviderFromRemoteUrl(
51 "https://self-hosted.example.test:8443/group/repo.git",
52 ),
53 ).toEqual({
54 kind: "unknown",
55 name: "self-hosted.example.test:8443",
56 baseUrl: "https://self-hosted.example.test:8443",
57 });
58 });
59
60 it("does not reuse SSH ports for HTTPS provider URLs", () => {
61 expect(
62 detectSourceControlProviderFromRemoteUrl("ssh://git@gitlab.example.test:24/group/repo.git"),
63 ).toEqual({
64 kind: "gitlab",
65 name: "GitLab Self-Hosted",
66 baseUrl: "https://gitlab.example.test",
67 });
68 expect(
69 detectSourceControlProviderFromRemoteUrl("ssh://git@code.example.test:24/team/project.git"),
70 ).toEqual({
71 kind: "unknown",
72 name: "code.example.test",
73 baseUrl: "https://code.example.test",
74 });
75 });
76
77 it("matches self-hosted providers by complete DNS labels", () => {
78 expect(
79 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
80 ).toBe("github");
81 expect(
82 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
83 ).toBe("gitlab");
84 expect(
85 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
86 ?.kind,
87 ).toBe("bitbucket");
88 });
89
90 it("detects GitHub Enterprise Cloud data residency hosts on ghe.com", () => {
91 expect(detectSourceControlProviderFromRemoteUrl("https://acme.ghe.com/owner/repo.git")).toEqual(
92 {
93 kind: "github",
94 name: "GitHub Self-Hosted",
95 baseUrl: "https://acme.ghe.com",
96 },
97 );
98 expect(detectSourceControlProviderFromRemoteUrl("git@acme.ghe.com:owner/repo.git")?.kind).toBe(
99 "github",
100 );
101 expect(detectSourceControlProviderFromRemoteUrl("https://ghe.com/owner/repo.git")?.kind).toBe(
102 "unknown",
103 );
104 });
105
106 it("does not match provider names embedded in unrelated DNS labels", () => {
107 expect(
108 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
109 ?.kind,
110 ).toBe("unknown");
111 expect(
112 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
113 ?.kind,
114 ).toBe("unknown");
115 expect(
116 detectSourceControlProviderFromRemoteUrl(
117 "https://notbitbucket.example.com/workspace/repo.git",
118 )?.kind,
119 ).toBe("unknown");
120 });
121
122 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
123 expect(
124 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
125 ).toBe("gitlab");
126 expect(
127 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
128 ?.baseUrl,
129 ).toBe("https://gitlab.example.com");
130 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
131 "github",
132 );
133 expect(
134 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
135 ).toBe("bitbucket");
136 });
137});
138
139it("names an Azure DevOps repository by its own name, not its project path", () => {
140 // `az repos pr list --repository` takes a name and detects the organisation and project from
141 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
142 // reads as unavailable on the page.
143 const selector = sourceControlRepositorySelector({
144 provider: "azure-devops",
145 displayName: "contoso/payments/_git/checkout",
146 owner: "contoso",
147 name: "checkout",
148 });
149 expect(selector).toBe("checkout");
150});
151
152it("decodes an Azure DevOps repository name that the remote URL percent-encodes", () => {
153 // A remote spells a space as `%20`, and `az repos pr list --repository` reads that literally.
154 const selector = sourceControlRepositorySelector({
155 provider: "azure-devops",
156 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
157 owner: "contoso",
158 name: "checkout%20(eu)",
159 });
160 expect(selector).toBe("checkout (eu)");
161});
162
163it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
164 const selector = sourceControlRepositorySelector({
165 provider: "gitlab",
166 displayName: "group/subgroup/service",
167 owner: "group",
168 name: "service",
169 });
170 expect(selector).toBe("group/subgroup/service");
171});