Open History to see the last commit
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("preserves encoded Azure repository components and same-protocol URLs", () => {
82 const ssh = "git@ssh.dev.azure.com:v3/Fabrikam/Project%20One/fab%20repo";
83 const https = "https://dev.azure.com/Fabrikam/Project%20One/_git/fab%20repo";
84 expect(cloneUrlForProtocol(ssh, "https")).toBe(https);
85 expect(cloneUrlForProtocol(https, "ssh")).toBe(ssh);
86 expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);
87 expect(cloneUrlForProtocol(https, "https")).toBe(https);
88 });
89
90 it("refuses Azure rewrites when the project is missing or a custom port is set", () => {
91 expect(cloneUrlForProtocol("git@ssh.dev.azure.com:v3/Fabrikam/fab_repo", "https")).toBeNull();
92 expect(cloneUrlForProtocol("https://dev.azure.com/Fabrikam/_git/fab_repo", "ssh")).toBeNull();
93 expect(
94 cloneUrlForProtocol("git@ssh.dev.azure.com:v3/Fabrikam?token/Project1/fab_repo", "https"),
95 ).toBeNull();
96 expect(
97 cloneUrlForProtocol("https://dev.azure.com/Fabrikam/Project1/_git/fab_repo#main", "ssh"),
98 ).toBeNull();
99 expect(
100 cloneUrlForProtocol(
101 "ssh://git@ssh.dev.azure.com:2222/v3/Fabrikam/Project1/fab_repo",
102 "https",
103 ),
104 ).toBeNull();
105 expect(
106 cloneUrlForProtocol("https://dev.azure.com:8443/Fabrikam/Project1/_git/fab_repo", "ssh"),
107 ).toBeNull();
108 });
109
110 it("switches between scp-style SSH and HTTPS", () => {
111 expect(cloneUrlForProtocol("git@github.com:pingdotgg/t3code.git", "https")).toBe(
112 "https://github.com/pingdotgg/t3code.git",
113 );
114 expect(cloneUrlForProtocol("https://github.com/pingdotgg/t3code.git", "ssh")).toBe(
115 "git@github.com:pingdotgg/t3code.git",
116 );
117 expect(cloneUrlForProtocol("ssh://git@gitlab.com/group/sub/repo.git", "https")).toBe(
118 "https://gitlab.com/group/sub/repo.git",
119 );
120 expect(cloneUrlForProtocol("git@[2001:db8::1]:org/repo.git", "https")).toBe(
121 "https://[2001:db8::1]/org/repo.git",
122 );
123 expect(cloneUrlForProtocol("[2001:db8::1]:org/repo.git", "https")).toBe(
124 "https://[2001:db8::1]/org/repo.git",
125 );
126 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]/org/repo.git", "https")).toBe(
127 "https://[2001:db8::1]/org/repo.git",
128 );
129 expect(cloneUrlForProtocol("https://[2001:db8::1]/org/repo.git", "ssh")).toBe(
130 "git@[2001:db8::1]:org/repo.git",
131 );
132 for (const [literal, encoded] of [
133 ["my repo.git", "my%20repo.git"],
134 ["repo%20name.git", "repo%2520name.git"],
135 ["repo#1?.git", "repo%231%3F.git"],
136 ]) {
137 const scp = `git@host.example:team/${literal}`;
138 const https = `https://host.example/team/${encoded}`;
139 expect(cloneUrlForProtocol(https, "ssh")).toBe(scp);
140 expect(cloneUrlForProtocol(scp, "https")).toBe(https);
141 expect(cloneUrlForProtocol(`ssh://git@host.example/team/${encoded}`, "https")).toBe(https);
142 }
143 });
144
145 it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
146 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "https")).toBe(
147 "https://token@github.com/a/b.git",
148 );
149 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "ssh")).toBe(
150 "git@github.com:a/b.git",
151 );
152 });
153
154 it("refuses rewrites that would guess wrong", () => {
155 expect(cloneUrlForProtocol("https://host.example/team/repo%ZZ.git", "ssh")).toBeNull();
156 expect(cloneUrlForProtocol("ssh://git@host.example:2222/a/b.git", "https")).toBeNull();
157 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "ssh")).toBeNull();
158 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
159 "https://host.example:8443/a/b.git",
160 );
161 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "https")).toBeNull();
162 expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "ssh")).toBe(
163 "ssh://git@[2001:db8::1]:2222/a/b.git",
164 );
165 expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "ssh")).toBeNull();
166 expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "https")).toBe(
167 "https://[2001:db8::1]:8443/a/b.git",
168 );
169 expect(cloneUrlForProtocol("git@host.example:/srv/repo.git", "https")).toBeNull();
170 expect(cloneUrlForProtocol("git@[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
171 expect(cloneUrlForProtocol("[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
172 expect(cloneUrlForProtocol("/srv/git/repo.git", "https")).toBeNull();
173 expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
174 });
175});
176
177describe("cloneUrlRepositoryPath", () => {
178 it("rejects Windows device names and trailing dots or spaces in every folder", () => {
179 for (const segment of [
180 "CON",
181 "nul.txt",
182 "COM1",
183 "LPT0",
184 "com¹",
185 "CONIN$",
186 "conout$",
187 "aux .txt",
188 "repo.",
189 "repo ",
190 ]) {
191 expect(cloneUrlRepositoryPath(`https://github.com/${segment}/repo.git`)).toBeNull();
192 expect(cloneUrlRepositoryPath(`https://${segment}/owner/repo.git`)).toBeNull();
193 expect(cloneUrlRepositoryPath(`https://github.com/owner/${segment}.git`)).toBeNull();
194 }
195 expect(cloneUrlRepositoryPath("https://github.com/acme/repo.git")).toBe("github.com/acme/repo");
196 expect(cloneUrlRepositoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git")).toBe(
197 "gitlab.com/group/sub/repo",
198 );
199 expect(cloneUrlRepositoryPath("C:/repos/project.git")).toBeNull();
200 });
201});