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