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
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 Forgejo and Gitea hosts while preserving HTTP origins", () => {
63 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
64 expect(detectSourceControlProviderFromRemoteUrl(`http://${host}:3000/team/repo.git`)).toEqual(
65 {
66 kind: "forgejo",
67 name: "Forgejo",
68 baseUrl: `http://${host}:3000`,
69 },
70 );
71 }
72 expect(getChangeRequestTerminologyForKind("forgejo")).toEqual({
73 shortLabel: "PR",
74 singular: "pull request",
75 });
76 });
77
78 it("detects Azure DevOps SSH remotes", () => {
79 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
80 expect(
81 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
82 ).toBe("azure-devops");
83 expect(
84 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
85 ?.kind,
86 ).toBe("azure-devops");
87 // Legacy visualstudio.com SSH host stays classified too.
88 expect(
89 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
90 ?.kind,
91 ).toBe("azure-devops");
92 });
93
94 it("preserves ports while classifying by hostname", () => {
95 expect(
96 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
97 ).toEqual({
98 kind: "gitlab",
99 name: "GitLab",
100 baseUrl: "https://gitlab.com:8443",
101 });
102 expect(
103 detectSourceControlProviderFromRemoteUrl(
104 "https://self-hosted.example.test:8443/group/repo.git",
105 ),
106 ).toEqual({
107 kind: "unknown",
108 name: "self-hosted.example.test:8443",
109 baseUrl: "https://self-hosted.example.test:8443",
110 });
111 });
112
113 it("does not reuse SSH ports for HTTPS provider URLs", () => {
114 expect(
115 detectSourceControlProviderFromRemoteUrl("ssh://git@gitlab.example.test:24/group/repo.git"),
116 ).toEqual({
117 kind: "gitlab",
118 name: "GitLab Self-Hosted",
119 baseUrl: "https://gitlab.example.test",
120 });
121 expect(
122 detectSourceControlProviderFromRemoteUrl("ssh://git@code.example.test:24/team/project.git"),
123 ).toEqual({
124 kind: "unknown",
125 name: "code.example.test",
126 baseUrl: "https://code.example.test",
127 });
128 });
129
130 it("matches self-hosted providers by complete DNS labels", () => {
131 expect(
132 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
133 ).toBe("github");
134 expect(
135 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
136 ).toBe("gitlab");
137 expect(
138 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
139 ?.kind,
140 ).toBe("bitbucket");
141 });
142
143 it("does not match provider names embedded in unrelated DNS labels", () => {
144 expect(
145 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
146 ?.kind,
147 ).toBe("unknown");
148 expect(
149 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
150 ?.kind,
151 ).toBe("unknown");
152 expect(
153 detectSourceControlProviderFromRemoteUrl(
154 "https://notbitbucket.example.com/workspace/repo.git",
155 )?.kind,
156 ).toBe("unknown");
157 });
158
159 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
160 expect(
161 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
162 ).toBe("gitlab");
163 expect(
164 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
165 ?.baseUrl,
166 ).toBe("https://gitlab.example.com");
167 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
168 "github",
169 );
170 expect(
171 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
172 ).toBe("bitbucket");
173 });
174});
175
176describe("isSshRemoteUrl", () => {
177 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
178 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
179 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
180 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
181 });
182
183 it("recognises ssh:// URLs with any case", () => {
184 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
185 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
186 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
187 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
188 });
189
190 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
191 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
192 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
193 expect(isSshRemoteUrl("")).toBe(false);
194 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
195 });
196});
197
198it("names an Azure DevOps repository by its own name, not its project path", () => {
199 // `az repos pr list --repository` takes a name and detects the organisation and project from
200 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
201 // reads as unavailable on the page.
202 const selector = sourceControlRepositorySelector({
203 provider: "azure-devops",
204 displayName: "contoso/payments/_git/checkout",
205 owner: "contoso",
206 name: "checkout",
207 });
208 expect(selector).toBe("checkout");
209});
210
211it("falls back to the path's last segment where an Azure identity has no name", () => {
212 const selector = sourceControlRepositorySelector({
213 provider: "azure-devops",
214 displayName: "contoso/payments/_git/checkout",
215 });
216 expect(selector).toBe("checkout");
217});
218
219it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
220 const selector = sourceControlRepositorySelector({
221 provider: "gitlab",
222 displayName: "group/subgroup/service",
223 owner: "group",
224 name: "service",
225 });
226 expect(selector).toBe("group/subgroup/service");
227});
228
229it("puts owner and name back together for an identity recorded before displayName", () => {
230 const selector = sourceControlRepositorySelector({
231 provider: "github",
232 owner: "t3tools",
233 name: "t3code",
234 });
235 expect(selector).toBe("t3tools/t3code");
236});
237
238it("names nothing for a project with no remote to name it by", () => {
239 expect(sourceControlRepositorySelector(null)).toBeNull();
240 expect(sourceControlRepositorySelector({ provider: "github" })).toBeNull();
241});