Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 detectSourceControlProviderFromRemoteUrl,
5 getChangeRequestTerminologyForKind,
6 isSshRemoteUrl,
7 resolveChangeRequestPresentation,
8 rewriteGitRemoteUrlHost,
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
46describe("detectSourceControlProviderFromRemoteUrl", () => {
47 it("detects common source control hosts", () => {
48 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
49 "github",
50 );
51 expect(
52 detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
53 ).toBe("gitlab");
54 expect(
55 detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
56 ).toBe("azure-devops");
57 expect(
58 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
59 ).toBe("bitbucket");
60 });
61
62 it("detects Azure DevOps SSH remotes", () => {
63 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
64 expect(
65 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
66 ).toBe("azure-devops");
67 expect(
68 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
69 ?.kind,
70 ).toBe("azure-devops");
71 // Legacy visualstudio.com SSH host stays classified too.
72 expect(
73 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
74 ?.kind,
75 ).toBe("azure-devops");
76 });
77
78 it("preserves ports while classifying by hostname", () => {
79 expect(
80 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
81 ).toEqual({
82 kind: "gitlab",
83 name: "GitLab",
84 baseUrl: "https://gitlab.com:8443",
85 });
86 expect(
87 detectSourceControlProviderFromRemoteUrl(
88 "https://self-hosted.example.test:8443/group/repo.git",
89 ),
90 ).toEqual({
91 kind: "unknown",
92 name: "self-hosted.example.test:8443",
93 baseUrl: "https://self-hosted.example.test:8443",
94 });
95 });
96
97 it("matches self-hosted providers by complete DNS labels", () => {
98 expect(
99 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
100 ).toBe("github");
101 expect(
102 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
103 ).toBe("gitlab");
104 expect(
105 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
106 ?.kind,
107 ).toBe("bitbucket");
108 });
109
110 it("does not match provider names embedded in unrelated DNS labels", () => {
111 expect(
112 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
113 ?.kind,
114 ).toBe("unknown");
115 expect(
116 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
117 ?.kind,
118 ).toBe("unknown");
119 expect(
120 detectSourceControlProviderFromRemoteUrl(
121 "https://notbitbucket.example.com/workspace/repo.git",
122 )?.kind,
123 ).toBe("unknown");
124 });
125
126 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
127 expect(
128 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
129 ).toBe("gitlab");
130 expect(
131 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
132 ?.baseUrl,
133 ).toBe("https://gitlab.example.com");
134 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
135 "github",
136 );
137 expect(
138 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
139 ).toBe("bitbucket");
140 });
141
142 it("does not treat undotted SSH aliases as enterprise hosts", () => {
143 expect(detectSourceControlProviderFromRemoteUrl("git@github-personal:owner/repo.git")).toEqual({
144 kind: "unknown",
145 name: "github-personal",
146 baseUrl: "https://github-personal",
147 });
148 expect(detectSourceControlProviderFromRemoteUrl("git@github:owner/repo.git")?.kind).toBe(
149 "unknown",
150 );
151 expect(
152 detectSourceControlProviderFromRemoteUrl("git@gitlab-work:group/project.git")?.kind,
153 ).toBe("unknown");
154 expect(
155 detectSourceControlProviderFromRemoteUrl("git@bitbucket-work:workspace/repo.git")?.kind,
156 ).toBe("unknown");
157 });
158
159 it("still classifies public and dotted enterprise hosts", () => {
160 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")).toEqual({
161 kind: "github",
162 name: "GitHub",
163 baseUrl: "https://github.com",
164 });
165 expect(
166 detectSourceControlProviderFromRemoteUrl("https://github.mycorp.com/owner/repo.git"),
167 ).toEqual({
168 kind: "github",
169 name: "GitHub Self-Hosted",
170 baseUrl: "https://github.mycorp.com",
171 });
172 expect(detectSourceControlProviderFromRemoteUrl("git@gitlab.com:group/project.git")?.kind).toBe(
173 "gitlab",
174 );
175 expect(
176 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
177 ).toBe("bitbucket");
178 });
179});
180
181describe("rewriteGitRemoteUrlHost", () => {
182 it("rewrites SCP-like SSH aliases and leaves HTTPS remotes alone", () => {
183 expect(rewriteGitRemoteUrlHost("git@github-personal:o/r.git", "github.com")).toBe(
184 "git@github.com:o/r.git",
185 );
186 expect(rewriteGitRemoteUrlHost("deploy@github-personal:Owner/Repo.git", "github.com")).toBe(
187 "deploy@github.com:Owner/Repo.git",
188 );
189 expect(rewriteGitRemoteUrlHost("ssh://git@github-personal/o/r.git", "github.com")).toBe(
190 "ssh://git@github.com/o/r.git",
191 );
192 expect(rewriteGitRemoteUrlHost("https://github.com/o/r.git", "example.com")).toBe(
193 "https://github.com/o/r.git",
194 );
195 });
196});
197
198describe("isSshRemoteUrl", () => {
199 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
200 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
201 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
202 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
203 });
204
205 it("recognises ssh:// URLs with any case", () => {
206 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
207 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
208 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
209 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
210 });
211
212 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
213 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
214 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
215 expect(isSshRemoteUrl("")).toBe(false);
216 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
217 });
218});