Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sourceControl.test.ts

264 lines9.2 KB

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("detects GitHub Enterprise Cloud data residency hosts on ghe.com", () => {
151 expect(detectSourceControlProviderFromRemoteUrl("https://acme.ghe.com/owner/repo.git")).toEqual(
152 {
153 kind: "github",
154 name: "GitHub Self-Hosted",
155 baseUrl: "https://acme.ghe.com",
156 },
157 );
158 expect(detectSourceControlProviderFromRemoteUrl("git@acme.ghe.com:owner/repo.git")?.kind).toBe(
159 "github",
160 );
161 expect(detectSourceControlProviderFromRemoteUrl("https://ghe.com/owner/repo.git")?.kind).toBe(
162 "unknown",
163 );
164 });
165​
166 it("does not match provider names embedded in unrelated DNS labels", () => {
167 expect(
168 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
169 ?.kind,
170 ).toBe("unknown");
171 expect(
172 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
173 ?.kind,
174 ).toBe("unknown");
175 expect(
176 detectSourceControlProviderFromRemoteUrl(
177 "https://notbitbucket.example.com/workspace/repo.git",
178 )?.kind,
179 ).toBe("unknown");
180 });
181​
182 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
183 expect(
184 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
185 ).toBe("gitlab");
186 expect(
187 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
188 ?.baseUrl,
189 ).toBe("https://gitlab.example.com");
190 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
191 "github",
192 );
193 expect(
194 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
195 ).toBe("bitbucket");
196 });
197});
198​
199describe("isSshRemoteUrl", () => {
200 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
201 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
202 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
203 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
204 });
205​
206 it("recognises ssh:// URLs with any case", () => {
207 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
208 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
209 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
210 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
211 });
212​
213 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
214 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
215 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
216 expect(isSshRemoteUrl("")).toBe(false);
217 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
218 });
219});
220​
221it("names an Azure DevOps repository by its own name, not its project path", () => {
222 // `az repos pr list --repository` takes a name and detects the organisation and project from
223 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
224 // reads as unavailable on the page.
225 const selector = sourceControlRepositorySelector({
226 provider: "azure-devops",
227 displayName: "contoso/payments/_git/checkout",
228 owner: "contoso",
229 name: "checkout",
230 });
231 expect(selector).toBe("checkout");
232});
233​
234it("falls back to the path's last segment where an Azure identity has no name", () => {
235 const selector = sourceControlRepositorySelector({
236 provider: "azure-devops",
237 displayName: "contoso/payments/_git/checkout",
238 });
239 expect(selector).toBe("checkout");
240});
241​
242it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
243 const selector = sourceControlRepositorySelector({
244 provider: "gitlab",
245 displayName: "group/subgroup/service",
246 owner: "group",
247 name: "service",
248 });
249 expect(selector).toBe("group/subgroup/service");
250});
251​
252it("puts owner and name back together for an identity recorded before displayName", () => {
253 const selector = sourceControlRepositorySelector({
254 provider: "github",
255 owner: "t3tools",
256 name: "t3code",
257 });
258 expect(selector).toBe("t3tools/t3code");
259});
260​
261it("names nothing for a project with no remote to name it by", () => {
262 expect(sourceControlRepositorySelector(null)).toBeNull();
263 expect(sourceControlRepositorySelector({ provider: "github" })).toBeNull();
264});