Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 sourceControlRepositorySelector,
5 detectSourceControlProviderFromRemoteUrl,
6 getChangeRequestTerminologyForKind,
7 isSshRemoteUrl,
8 resolveChangeRequestPresentation,
9} from "./sourceControl.ts";
10
11describe("source control presentation", () => {
12 it("uses merge request terminology for GitLab", () => {
13 expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({
14 shortLabel: "MR",
15 singular: "merge request",
16 });
17 });
18
19 it("uses pull request terminology for GitHub-compatible providers", () => {
20 expect(getChangeRequestTerminologyForKind("github")).toEqual({
21 shortLabel: "PR",
22 singular: "pull request",
23 });
24 expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({
25 shortLabel: "PR",
26 singular: "pull request",
27 });
28 expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({
29 shortLabel: "PR",
30 singular: "pull request",
31 });
32 });
33
34 it("falls back to generic change request copy for unknown providers", () => {
35 expect(
36 resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }),
37 ).toEqual(
38 expect.objectContaining({
39 shortName: "change request",
40 longName: "change request",
41 }),
42 );
43 });
44
45 it("presents github-enterprise with GitHub PR terminology and icon", () => {
46 const presentation = resolveChangeRequestPresentation({
47 kind: "github-enterprise",
48 name: "git.corp.com",
49 baseUrl: "https://git.corp.com",
50 });
51
52 expect(presentation.icon).toBe("github");
53 expect(presentation.shortName).toBe("PR");
54 expect(presentation.longName).toBe("pull request");
55 expect(presentation.providerName).toBe("GitHub Enterprise");
56 expect(presentation.checkoutCommandExample).toBe("gh pr checkout 123");
57 });
58});
59
60describe("detectSourceControlProviderFromRemoteUrl", () => {
61 it("detects common source control hosts", () => {
62 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
63 "github",
64 );
65 expect(
66 detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
67 ).toBe("gitlab");
68 expect(
69 detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
70 ).toBe("azure-devops");
71 expect(
72 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
73 ).toBe("bitbucket");
74 });
75
76 it("detects Forgejo and Gitea hosts while preserving HTTP origins", () => {
77 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
78 expect(detectSourceControlProviderFromRemoteUrl(`http://${host}:3000/team/repo.git`)).toEqual(
79 {
80 kind: "forgejo",
81 name: "Forgejo",
82 baseUrl: `http://${host}:3000`,
83 },
84 );
85 }
86 expect(getChangeRequestTerminologyForKind("forgejo")).toEqual({
87 shortLabel: "PR",
88 singular: "pull request",
89 });
90 });
91
92 it("detects Azure DevOps SSH remotes", () => {
93 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
94 expect(
95 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
96 ).toBe("azure-devops");
97 expect(
98 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
99 ?.kind,
100 ).toBe("azure-devops");
101 // Legacy visualstudio.com SSH host stays classified too.
102 expect(
103 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
104 ?.kind,
105 ).toBe("azure-devops");
106 });
107
108 it("preserves ports while classifying by hostname", () => {
109 expect(
110 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
111 ).toEqual({
112 kind: "gitlab",
113 name: "GitLab",
114 baseUrl: "https://gitlab.com:8443",
115 });
116 expect(
117 detectSourceControlProviderFromRemoteUrl(
118 "https://self-hosted.example.test:8443/group/repo.git",
119 ),
120 ).toEqual({
121 kind: "unknown",
122 name: "self-hosted.example.test:8443",
123 baseUrl: "https://self-hosted.example.test:8443",
124 });
125 });
126
127 it("classifies github.com as github", () => {
128 expect(detectSourceControlProviderFromRemoteUrl("https://github.com/owner/repo.git")).toEqual({
129 kind: "github",
130 name: "GitHub",
131 baseUrl: "https://github.com",
132 });
133 });
134
135 it("classifies a github.com endpoint served from a subdomain as github", () => {
136 // `ssh.github.com:443` is dotcom's own SSH endpoint for networks that block port 22, not an
137 // Enterprise install that happens to have `github` in its name.
138 expect(
139 detectSourceControlProviderFromRemoteUrl("git@ssh.github.com:443/owner/repo.git"),
140 ).toEqual({
141 kind: "github",
142 name: "GitHub",
143 baseUrl: "https://ssh.github.com",
144 });
145 });
146
147 it("classifies a ghe.com tenant as github-enterprise", () => {
148 expect(detectSourceControlProviderFromRemoteUrl("https://acme.ghe.com/owner/repo.git")).toEqual(
149 {
150 kind: "github-enterprise",
151 name: "acme.ghe.com",
152 baseUrl: "https://acme.ghe.com",
153 },
154 );
155 });
156
157 // Previously classified as kind "github" / name "GitHub Self-Hosted".
158 // Reclassification is intended; nothing persists the kind.
159 it("classifies a github-prefixed corporate host as github-enterprise", () => {
160 expect(detectSourceControlProviderFromRemoteUrl("git@github.acme.com:owner/repo.git")).toEqual({
161 kind: "github-enterprise",
162 name: "github.acme.com",
163 baseUrl: "https://github.acme.com",
164 });
165 });
166
167 it("leaves an arbitrary GHES hostname unknown for CLI refinement", () => {
168 expect(detectSourceControlProviderFromRemoteUrl("https://git.corp.com/owner/repo.git")).toEqual(
169 {
170 kind: "unknown",
171 name: "git.corp.com",
172 baseUrl: "https://git.corp.com",
173 },
174 );
175 });
176
177 it("still classifies gitlab hosts as gitlab", () => {
178 expect(
179 detectSourceControlProviderFromRemoteUrl("https://gitlab.acme.com/group/project.git")?.kind,
180 ).toBe("gitlab");
181 });
182
183 it("matches self-hosted providers by complete DNS labels", () => {
184 expect(
185 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
186 ).toBe("github-enterprise");
187 expect(
188 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
189 ).toBe("gitlab");
190 expect(
191 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
192 ?.kind,
193 ).toBe("bitbucket");
194 });
195
196 it("does not match provider names embedded in unrelated DNS labels", () => {
197 expect(
198 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
199 ?.kind,
200 ).toBe("unknown");
201 expect(
202 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
203 ?.kind,
204 ).toBe("unknown");
205 expect(
206 detectSourceControlProviderFromRemoteUrl(
207 "https://notbitbucket.example.com/workspace/repo.git",
208 )?.kind,
209 ).toBe("unknown");
210 });
211
212 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
213 expect(
214 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
215 ).toBe("gitlab");
216 expect(
217 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
218 ?.baseUrl,
219 ).toBe("https://gitlab.example.com");
220 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
221 "github",
222 );
223 expect(
224 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
225 ).toBe("bitbucket");
226 });
227});
228
229describe("isSshRemoteUrl", () => {
230 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
231 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
232 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
233 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
234 });
235
236 it("recognises ssh:// URLs with any case", () => {
237 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
238 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
239 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
240 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
241 });
242
243 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
244 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
245 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
246 expect(isSshRemoteUrl("")).toBe(false);
247 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
248 });
249});
250
251it("names an Azure DevOps repository by its own name, not its project path", () => {
252 // `az repos pr list --repository` takes a name and detects the organisation and project from
253 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
254 // reads as unavailable on the page.
255 const selector = sourceControlRepositorySelector({
256 provider: "azure-devops",
257 displayName: "contoso/payments/_git/checkout",
258 owner: "contoso",
259 name: "checkout",
260 });
261 expect(selector).toBe("checkout");
262});
263
264it("falls back to the path's last segment where an Azure identity has no name", () => {
265 const selector = sourceControlRepositorySelector({
266 provider: "azure-devops",
267 displayName: "contoso/payments/_git/checkout",
268 });
269 expect(selector).toBe("checkout");
270});
271
272it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
273 const selector = sourceControlRepositorySelector({
274 provider: "gitlab",
275 displayName: "group/subgroup/service",
276 owner: "group",
277 name: "service",
278 });
279 expect(selector).toBe("group/subgroup/service");
280});