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

cloneUrl.test.ts

260 lines11.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { cloneUrlForProtocol, cloneUrlProtocol, cloneUrlRepositoryPath } from "./cloneUrl.ts";
4​
5describe("cloneUrlForProtocol", () => {
6 it.each([
7 ["ordinary", "user:password"],
8 ["an embedded at sign", "user:pa@rt"],
9 ["multiple at signs", "user:pa@rt@word"],
10 ["encoded", "us%40er:pa%40ss%3Aword"],
11 ])("separates Azure hosts from credentials (%s)", (_label, userinfo) => {
12 const https = `https://${userinfo}@dev.azure.com/org/Project%20One/_git/repo%40name`;
13 const ssh = `ssh://${userinfo}@ssh.dev.azure.com/v3/org/Project%20One/repo%40name`;
14 expect(cloneUrlProtocol(https)).toBe("https");
15 expect(cloneUrlProtocol(ssh)).toBe("ssh");
16 expect(cloneUrlForProtocol(https, "ssh")).toBe(
17 "git@ssh.dev.azure.com:v3/org/Project%20One/repo%40name",
18 );
19 expect(cloneUrlForProtocol(ssh, "https")).toBe(
20 "https://dev.azure.com/org/Project%20One/_git/repo%40name",
21 );
22 expect(cloneUrlForProtocol(https, "https")).toBe(https);
23 expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);
24 });
25​
26 it.each([
27 ["https", "ssh", "dev.azure.com", "org/project/_git/repo"],
28 ["ssh", "https", "ssh.dev.azure.com", "v3/org/project/repo"],
29 ["https", "ssh", "host.example", "org/repo.git"],
30 ["ssh", "https", "host.example", "org/repo.git"],
31 ] as const)(
32 "refuses credential-bearing %s URLs with custom ports on %s switches",
33 (from, to, host, path) => {
34 const url = `${from}://user:pa@rt@${host}:8443/${path}`;
35 expect(cloneUrlForProtocol(url, to)).toBeNull();
36 expect(cloneUrlForProtocol(url, from)).toBe(url);
37 },
38 );
39​
40 it("drops all userinfo on generic hosts without consuming at signs in paths", () => {
41 expect(cloneUrlForProtocol("https://user:pa@rt@github.com/org/repo@branch.git", "ssh")).toBe(
42 "git@github.com:org/repo@branch.git",
43 );
44 expect(cloneUrlForProtocol("ssh://user:pa@rt@github.com/org/repo@branch.git", "https")).toBe(
45 "https://github.com/org/repo@branch.git",
46 );
47 expect(cloneUrlForProtocol("https://github.com/org/repo@branch.git", "ssh")).toBe(
48 "git@github.com:org/repo@branch.git",
49 );
50 });
51​
52 it.each(["https://user:pa@rt@/org/repo", "ssh://user:pa@rt@/org/repo"])(
53 "refuses a credential-bearing URL without a hostname (%s)",
54 (url) => {
55 expect(cloneUrlProtocol(url)).toBeNull();
56 expect(cloneUrlForProtocol(url, "ssh")).toBeNull();
57 expect(cloneUrlForProtocol(url, "https")).toBeNull();
58 },
59 );
60​
61 it.each([
62 "git@ssh.dev.azure.com:v3/Fabrikam/Project1/fab_repo",
63 "ssh://git@ssh.dev.azure.com/v3/Fabrikam/Project1/fab_repo",
64 "git@SSH.DEV.AZURE.COM:v3/Fabrikam/Project1/fab_repo",
65 ])("uses Azure's HTTPS host and repository path for %s", (url) => {
66 expect(cloneUrlForProtocol(url, "https")).toBe(
67 "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",
68 );
69 });
70​
71 it.each([
72 "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",
73 "https://Fabrikam@dev.azure.com/Fabrikam/Project1/_git/fab_repo",
74 "https://DEV.AZURE.COM/Fabrikam/Project1/_git/fab_repo",
75 ])("uses Azure's SSH host and repository path for %s", (url) => {
76 expect(cloneUrlForProtocol(url, "ssh")).toBe(
77 "git@ssh.dev.azure.com:v3/Fabrikam/Project1/fab_repo",
78 );
79 });
80​
81 it("switches Bitbucket Cloud between its SSH and HTTPS hosts", () => {
82 expect(cloneUrlForProtocol("https://bitbucket.org/workspace/repo.git", "ssh")).toBe(
83 "git@ssh.bitbucket.org:workspace/repo.git",
84 );
85 expect(cloneUrlForProtocol("git@ssh.bitbucket.org:workspace/repo.git", "https")).toBe(
86 "https://bitbucket.org/workspace/repo.git",
87 );
88 });
89​
90 it("preserves encoded Azure repository components and same-protocol URLs", () => {
91 const ssh = "git@ssh.dev.azure.com:v3/Fabrikam/Project%20One/fab%20repo";
92 const https = "https://dev.azure.com/Fabrikam/Project%20One/_git/fab%20repo";
93 expect(cloneUrlForProtocol(ssh, "https")).toBe(https);
94 expect(cloneUrlForProtocol(https, "ssh")).toBe(ssh);
95 expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);
96 expect(cloneUrlForProtocol(https, "https")).toBe(https);
97 });
98​
99 it("refuses Azure rewrites when the project is missing or a custom port is set", () => {
100 expect(cloneUrlForProtocol("git@ssh.dev.azure.com:v3/Fabrikam/fab_repo", "https")).toBeNull();
101 expect(cloneUrlForProtocol("https://dev.azure.com/Fabrikam/_git/fab_repo", "ssh")).toBeNull();
102 expect(
103 cloneUrlForProtocol("git@ssh.dev.azure.com:v3/Fabrikam?token/Project1/fab_repo", "https"),
104 ).toBeNull();
105 expect(
106 cloneUrlForProtocol("https://dev.azure.com/Fabrikam/Project1/_git/fab_repo#main", "ssh"),
107 ).toBeNull();
108 expect(
109 cloneUrlForProtocol(
110 "ssh://git@ssh.dev.azure.com:2222/v3/Fabrikam/Project1/fab_repo",
111 "https",
112 ),
113 ).toBeNull();
114 expect(
115 cloneUrlForProtocol("https://dev.azure.com:8443/Fabrikam/Project1/_git/fab_repo", "ssh"),
116 ).toBeNull();
117 });
118​
119 it("switches between scp-style SSH and HTTPS", () => {
120 expect(cloneUrlForProtocol("git@github.com:pingdotgg/t3code.git", "https")).toBe(
121 "https://github.com/pingdotgg/t3code.git",
122 );
123 expect(cloneUrlForProtocol("https://github.com/pingdotgg/t3code.git", "ssh")).toBe(
124 "git@github.com:pingdotgg/t3code.git",
125 );
126 expect(cloneUrlForProtocol("ssh://git@gitlab.com/group/sub/repo.git", "https")).toBe(
127 "https://gitlab.com/group/sub/repo.git",
128 );
129 expect(cloneUrlForProtocol("git@[2001:db8::1]:org/repo.git", "https")).toBe(
130 "https://[2001:db8::1]/org/repo.git",
131 );
132 expect(cloneUrlForProtocol("[2001:db8::1]:org/repo.git", "https")).toBe(
133 "https://[2001:db8::1]/org/repo.git",
134 );
135 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]/org/repo.git", "https")).toBe(
136 "https://[2001:db8::1]/org/repo.git",
137 );
138 expect(cloneUrlForProtocol("https://[2001:db8::1]/org/repo.git", "ssh")).toBe(
139 "git@[2001:db8::1]:org/repo.git",
140 );
141 for (const [literal, encoded] of [
142 ["my repo.git", "my%20repo.git"],
143 ["repo%20name.git", "repo%2520name.git"],
144 ["repo#1?.git", "repo%231%3F.git"],
145 ]) {
146 const scp = `git@host.example:team/${literal}`;
147 const https = `https://host.example/team/${encoded}`;
148 expect(cloneUrlForProtocol(https, "ssh")).toBe(scp);
149 expect(cloneUrlForProtocol(scp, "https")).toBe(https);
150 expect(cloneUrlForProtocol(`ssh://git@host.example/team/${encoded}`, "https")).toBe(https);
151 }
152 expect(cloneUrlForProtocol("ssh://git@host.example/team/repo%20name#1?.git", "https")).toBe(
153 "https://host.example/team/repo%20name%231%3F.git",
154 );
155 });
156​
157 it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
158 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "https")).toBe(
159 "https://token@github.com/a/b.git",
160 );
161 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "ssh")).toBe(
162 "git@github.com:a/b.git",
163 );
164 });
165​
166 it("refuses rewrites that would guess wrong", () => {
167 for (const url of [
168 "https://host.example/team/repo.git#readme",
169 "https://host.example/team/repo.git?view=code",
170 "https://host.example?ref=/team/repo.git",
171 "https://host.example#ref/team/repo.git",
172 ]) {
173 expect(cloneUrlForProtocol(url, "ssh")).toBeNull();
174 expect(cloneUrlForProtocol(url, "https")).toBe(url);
175 }
176 expect(cloneUrlForProtocol("https://host.example/team/repo%ZZ.git", "ssh")).toBeNull();
177 expect(cloneUrlForProtocol("ssh://git@host.example:2222/a/b.git", "https")).toBeNull();
178 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "ssh")).toBeNull();
179 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
180 "https://host.example:8443/a/b.git",
181 );
182 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "https")).toBeNull();
183 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "ssh")).toBe(
184 "ssh://git@[2001:db8::1]:2222/a/b.git",
185 );
186 expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "ssh")).toBeNull();
187 expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "https")).toBe(
188 "https://[2001:db8::1]:8443/a/b.git",
189 );
190 expect(cloneUrlForProtocol("git@host.example:/srv/repo.git", "https")).toBeNull();
191 expect(cloneUrlForProtocol("git@[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
192 expect(cloneUrlForProtocol("[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
193 expect(cloneUrlForProtocol("/srv/git/repo.git", "https")).toBeNull();
194 expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
195 });
196});
197​
198describe("cloneUrlRepositoryPath", () => {
199 it("derives portable clone folders from URI and SCP paths", () => {
200 for (const segment of [
201 "CON",
202 "nul.txt",
203 "COM1",
204 "LPT0",
205 "com¹",
206 "CONIN$",
207 "conout$",
208 "aux .txt",
209 "repo.",
210 "repo ",
211 ]) {
212 expect(cloneUrlRepositoryPath(`https://github.com/${segment}/repo.git`)).toBeNull();
213 expect(cloneUrlRepositoryPath(`https://${segment}/owner/repo.git`)).toBeNull();
214 expect(cloneUrlRepositoryPath(`https://github.com/owner/${segment}.git`)).toBeNull();
215 }
216 expect(cloneUrlRepositoryPath("https://github.com/acme/repo.git")).toBe("github.com/acme/repo");
217 expect(cloneUrlRepositoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git")).toBe(
218 "gitlab.com/group/sub/repo",
219 );
220 expect(cloneUrlRepositoryPath("C:/repos/project.git")).toBeNull();
221 for (const prefix of ["https://host.example/", "ssh://git@host.example/"]) {
222 expect(cloneUrlRepositoryPath(`${prefix}Research%20Tools/r%C3%A9sum%C3%A9.git`)).toBe(
223 "host.example/Research Tools/résumé",
224 );
225 expect(cloneUrlRepositoryPath(`${prefix}team/repo%2520name.git`)).toBe(
226 "host.example/team/repo%20name",
227 );
228 expect(cloneUrlRepositoryPath(`${prefix}team/%2e%2e/repo.git`)).toBeNull();
229 expect(cloneUrlRepositoryPath(`${prefix}team/repo%20.git`)).toBeNull();
230 expect(cloneUrlRepositoryPath(`${prefix}team/bad%FF.git`)).toBeNull();
231 }
232 expect(cloneUrlRepositoryPath("git@forge.example:team#1/spec#2026.git")).toBe(
233 "forge.example/team#1/spec#2026",
234 );
235 expect(cloneUrlRepositoryPath("ssh://git@forge.example/team#1/spec#2026.git")).toBe(
236 "forge.example/team#1/spec#2026",
237 );
238 expect(
239 cloneUrlRepositoryPath("ssh://git@forge.example:2222/team#1/spec#2026.git?ref=main"),
240 ).toBe("forge.example/team#1/spec#2026");
241 expect(cloneUrlRepositoryPath("ssh://git@forge.example/team/spec#100%.git")).toBe(
242 "forge.example/team/spec#100%",
243 );
244 expect(cloneUrlRepositoryPath("https://forge.example/team/spec.git#readme")).toBe(
245 "forge.example/team/spec",
246 );
247 expect(cloneUrlRepositoryPath("git@host.example:team/repo%20name.git")).toBe(
248 "host.example/team/repo%20name",
249 );
250 });
251​
252 it("derives ghq folders for absolute SCP paths", () => {
253 expect(cloneUrlRepositoryPath("git@forge.example:/team/repo.git")).toBe(
254 "forge.example/team/repo",
255 );
256 expect(cloneUrlRepositoryPath("forge.example:/team/repo.git")).toBe("forge.example/team/repo");
257 expect(cloneUrlRepositoryPath("git@forge.example:/")).toBeNull();
258 expect(cloneUrlRepositoryPath("git@forge.example:/team/../repo.git")).toBeNull();
259 });
260});