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

definition.test.ts

352 lines14.2 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import type { ChangeRequestProjectIdentity } from "@t3tools/source-control-core/shared/definition";
3import {
4 makeSourceControlDefinitionRegistry,
5 recognizeRemoteWith,
6} from "@t3tools/source-control-core/shared/definition";
7import { describe, expect, it } from "vite-plus/test";
8​
9import { definition } from "./definition.ts";
10​
11const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
12​
13describe("Azure DevOps change request URLs", () => {
14 it("reads both hosted URL forms", () => {
15 expect(parse("https://dev.azure.com/acme/platform/_git/t3code/pullrequest/17")).toEqual({
16 host: "dev.azure.com",
17 repository: "acme/platform/_git/t3code",
18 number: 17,
19 repositoryUrl: "https://dev.azure.com/acme/platform/_git/t3code",
20 });
21 expect(
22 parse("https://acme.visualstudio.com/platform/_git/t3code/pullrequest/17"),
23 ).toMatchObject({
24 host: "acme.visualstudio.com",
25 repository: "platform/_git/t3code",
26 number: 17,
27 });
28 });
29​
30 it("keeps Azure DevOps Server ports in the authority, on any hostname", () => {
31 expect(
32 parse("https://GIT.CAFE:8443/tfs/Collection/Project/_git/Repo/pullrequest/123?view=files"),
33 ).toEqual({
34 host: "git.cafe",
35 authority: "git.cafe:8443",
36 repository: "tfs/collection/project/_git/repo",
37 number: 123,
38 repositoryUrl: "https://git.cafe:8443/tfs/Collection/Project/_git/Repo",
39 });
40 expect(parse("https://scm.example.test/tfs/c/p/_git/r/pullrequest/1")).not.toHaveProperty(
41 "authority",
42 );
43 });
44​
45 it("claims nothing without the _git route or a valid number", () => {
46 for (const url of [
47 "https://scm.example.test/tfs/Collection/Project/_git/Repo",
48 "https://scm.example.test/tfs/Collection/Project/pullrequest/123",
49 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/abc",
50 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/0",
51 "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/9007199254740992",
52 ]) {
53 expect(parse(url), url).toBeNull();
54 }
55 });
56​
57 it("takes a pull request URL that leaves the project out as a checkout reference", () => {
58 for (const url of [
59 "https://dev.azure.com/acme/_git/web/pullrequest/17",
60 "https://acme.visualstudio.com/_git/web/pullrequest/17",
61 ]) {
62 expect(definition.isChangeRequestReference(url), url).toBe(true);
63 }
64 expect(
65 definition.isChangeRequestReference("https://dev.azure.com/_git/web/pullrequest/17"),
66 ).toBe(false);
67 });
68​
69 it.each([
70 ["ssh.dev.azure.com", "v3/org/project/web"],
71 ["org.visualstudio.com", "defaultcollection/project/_git/web"],
72 ["dev.azure.com", "org/project/_git/web"],
73 ])("builds a canonical browser URL from the remote %s/%s", (host, repository) => {
74 expect(definition.changeRequestUrl({ host, repository, number: 42 })).toBe(
75 "https://dev.azure.com/org/project/_git/web/pullrequest/42",
76 );
77 });
78​
79 it("keeps an Azure DevOps Server's HTTP origin and builds below a web URL", () => {
80 expect(
81 definition.changeRequestUrl({
82 host: "scm.example.test",
83 repository: "tfs/collection/project/_git/repo",
84 number: 124,
85 remoteUrl: "http://dummy:secret@scm.example.test:8080/tfs/Collection/Project/_git/Repo",
86 }),
87 ).toBe("http://scm.example.test:8080/tfs/collection/project/_git/repo/pullrequest/124");
88 expect(
89 definition.changeRequestUrl({
90 host: "dev.azure.com",
91 repository: "acme/project/_git/web",
92 number: 43,
93 webUrl: "https://dev.azure.com/acme/project/_git/web",
94 }),
95 ).toBe("https://dev.azure.com/acme/project/_git/web/pullrequest/43");
96 });
97​
98 it.each([
99 ["ssh.dev.azure.com/v3/org-a/project/web", "git@ssh.dev.azure.com:v3/org-a/project/web"],
100 [
101 "vs-ssh.visualstudio.com/v3/org-a/project/web",
102 "git@vs-ssh.visualstudio.com:v3/org-a/project/web",
103 ],
104 [
105 "org-a.visualstudio.com/defaultcollection/project/_git/web",
106 "https://org-a.visualstudio.com/DefaultCollection/Project/_git/Web",
107 ],
108 ["dev.azure.com/org-a/project/_git/web", "https://org-a@dev.azure.com/org-a/Project/_git/Web"],
109 ])("matches browser references against the checkout %s", (canonicalKey, remoteUrl) => {
110 const identity: ChangeRequestProjectIdentity = {
111 canonicalKey,
112 displayName: canonicalKey.split("/").slice(1).join("/"),
113 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
114 };
115 const link = parse("https://dev.azure.com/org-a/project/_git/web/pullrequest/42")!;
116 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
117 for (const repository of ["org-b/project/_git/web", "org-a/other-project/_git/web"]) {
118 expect(definition.isChangeRequestInRepository(identity, { ...link, repository })).toBe(false);
119 }
120 });
121});
122​
123describe("Azure DevOps remotes", () => {
124 it("recognises the SSH clone hosts as well as the web ones", () => {
125 for (const remote of [
126 "git@ssh.dev.azure.com:v3/org/project/repo",
127 "ssh://git@ssh.dev.azure.com:22/v3/org/project/repo",
128 "ssh.dev.azure.com:v3/Org/My Project/My Repo",
129 "git@vs-ssh.visualstudio.com:v3/org/project/repo",
130 "https://org@dev.azure.com/org/project/_git/repo",
131 ]) {
132 expect(recognizeRemoteWith(definition, remote)?.kind, remote).toBe("azure-devops");
133 }
134 expect(recognizeRemoteWith(definition, "https://azure.example/o/p/_git/r")).toBeNull();
135 });
136});
137​
138describe("Azure DevOps repository keys", () => {
139 const registry = makeSourceControlDefinitionRegistry([definition]);
140​
141 it("gives a repository the same key over SSH as over HTTPS", () => {
142 for (const remote of [
143 "git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code",
144 "ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code",
145 "https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code",
146 ]) {
147 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
148 "dev.azure.com/t3tools/platform/_git/t3code",
149 );
150 }
151 });
152​
153 it("gives a project with spaces in its name the same key over SSH as over HTTPS", () => {
154 for (const remote of [
155 "git@ssh.dev.azure.com:v3/Org/My Project/My Repo",
156 "ssh.dev.azure.com:v3/Org/My Project/My Repo",
157 "https://dev.azure.com/Org/My%20Project/_git/My%20Repo",
158 ]) {
159 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
160 "dev.azure.com/org/my%20project/_git/my%20repo",
161 );
162 }
163 });
164​
165 it.each([
166 "git@ssh.dev.azure.com:v3/acme/platform/tool.git",
167 "https://dev.azure.com/acme/platform/_git/tool.git",
168 ])("preserves a literal .git in an Azure repository name: %s", (remoteUrl) => {
169 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
170 expect(canonicalKey).toBe("dev.azure.com/acme/platform/_git/tool.git");
171 const identity: ChangeRequestProjectIdentity = {
172 canonicalKey,
173 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
174 };
175 const link = parse("https://dev.azure.com/acme/platform/_git/tool.git/pullrequest/7")!;
176 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
177 expect(
178 definition.isChangeRequestInRepository(identity, {
179 ...link,
180 repository: "acme/platform/_git/tool",
181 }),
182 ).toBe(false);
183 });
184​
185 it("matches a repository however its name is percent-encoded", () => {
186 const remoteUrl = "https://example@dev.azure.com/example/Sample%20(Beta)/_git/Sample%20(Beta)";
187 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
188 const identity: ChangeRequestProjectIdentity = {
189 canonicalKey,
190 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
191 };
192 for (const url of [
193 "https://dev.azure.com/example/Sample%20%28Beta%29/_git/Sample%20%28Beta%29/pullrequest/7",
194 "https://dev.azure.com/example/Sample (Beta)/_git/Sample (Beta)/pullrequest/7",
195 ]) {
196 const link = parse(url)!;
197 expect(link.repository, url).toBe("example/sample%20(beta)/_git/sample%20(beta)");
198 expect(definition.isChangeRequestInRepository(identity, link), url).toBe(true);
199 expect(
200 definition.isChangeRequestInRepository(
201 {
202 ...identity,
203 canonicalKey: "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
204 },
205 link,
206 ),
207 url,
208 ).toBe(true);
209 }
210 // Links stored before keys had one spelling still resolve to their project.
211 expect(
212 registry.canonicalRepositoryKey(
213 "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
214 ),
215 ).toBe(canonicalKey);
216 });
217​
218 it("still drops .git from other hosts' repository keys", () => {
219 expect(registry.repositoryKeyFromRemote("git@github.com:acme/tool.git")).toBe(
220 "github.com/acme/tool",
221 );
222 });
223​
224 it("keeps the organization in the host for the legacy visualstudio.com remotes", () => {
225 for (const remote of [
226 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
227 "https://T3Tools.visualstudio.com/Platform/_git/T3Code",
228 ]) {
229 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
230 "t3tools.visualstudio.com/platform/_git/t3code",
231 );
232 }
233 });
234​
235 it("compares every spelling on dev.azure.com", () => {
236 for (const key of [
237 "ssh.dev.azure.com/v3/org/project/repo",
238 "vs-ssh.visualstudio.com/v3/org/project/repo",
239 "org.visualstudio.com/project/_git/repo",
240 "org.visualstudio.com/defaultcollection/project/_git/repo",
241 "dev.azure.com/org/project/_git/repo",
242 ]) {
243 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/project/_git/repo");
244 expect(registry.findByRepositoryKey(key), key).toBe(definition);
245 }
246 // A repository named like its project may leave the project out of its URL.
247 for (const key of [
248 "dev.azure.com/org/_git/repo",
249 "dev.azure.com/org/defaultcollection/_git/repo",
250 "org.visualstudio.com/_git/repo",
251 "org.visualstudio.com/defaultcollection/_git/repo",
252 ]) {
253 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/repo/_git/repo");
254 }
255 expect(registry.findByRepositoryKey("tfs/foo.visualstudio.com/_git/repo")).toBeUndefined();
256 expect(registry.canonicalRepositoryKey("tfs.example/c/p/_git/r")).toBe(
257 "tfs.example/c/p/_git/r",
258 );
259 expect(registry.findByRepositoryKey("tfs.example/c/p/_git/r")).toBeUndefined();
260 });
261​
262 it("matches a shorthand pull request URL against an SSH checkout and builds its canonical URL", () => {
263 const remoteUrl = "git@ssh.dev.azure.com:v3/org/repo/repo";
264 const identity: ChangeRequestProjectIdentity = {
265 canonicalKey: registry.repositoryKeyFromRemote(remoteUrl),
266 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
267 };
268 const link = parse("https://org.visualstudio.com/DefaultCollection/_git/repo/pullrequest/7")!;
269 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
270 expect(
271 definition.changeRequestUrl({
272 host: "org.visualstudio.com",
273 repository: "defaultcollection/_git/repo",
274 number: 7,
275 }),
276 ).toBe("https://dev.azure.com/org/repo/_git/repo/pullrequest/7");
277 });
278});
279​
280describe("Azure DevOps repository selector", () => {
281 const registry = makeSourceControlDefinitionRegistry([definition]);
282​
283 it("names a repository by its own name, not its project path", () => {
284 // `az repos pr list --repository` takes a name and detects the organisation and project from
285 // the checkout; the recorded `org/project/_git/repo` path is refused.
286 expect(
287 registry.repositorySelector({
288 provider: SourceControlProviderKind.make("azure-devops"),
289 displayName: "contoso/payments/_git/checkout",
290 owner: "contoso",
291 name: "checkout",
292 }),
293 ).toBe("checkout");
294 });
295​
296 it("decodes a name the remote URL percent-encodes", () => {
297 expect(
298 registry.repositorySelector({
299 provider: SourceControlProviderKind.make("azure-devops"),
300 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
301 owner: "contoso",
302 name: "checkout%20(eu)",
303 }),
304 ).toBe("checkout (eu)");
305 });
306});
307​
308describe("Azure DevOps issue scope", () => {
309 it("places work items in the project rather than the repository", () => {
310 const kind = SourceControlProviderKind.make("azure-devops");
311 expect(
312 definition.issueScope?.({ provider: kind, displayName: "acme/Web%20App/_git/web" }),
313 ).toBe("acme/Web App");
314 expect(definition.issueScope?.({ provider: kind, displayName: "web" })).toBeNull();
315 // One segment before `_git` is a different thing on each kind of host, so it needs the host.
316 expect(definition.issueScope?.({ provider: kind, displayName: "acme/_git/web" })).toBeNull();
317 const scopeOn = (canonicalKey: string, displayName: string) =>
318 definition.issueScope?.({ provider: kind, canonicalKey, displayName });
319 expect(scopeOn("dev.azure.com/acme/_git/web", "acme/_git/Web")).toBe("acme/Web");
320 expect(scopeOn("acme.visualstudio.com/web/_git/web", "Web%20App/_git/web")).toBe("Web App");
321 expect(scopeOn("acme.visualstudio.com/_git/web", "_git/Web")).toBe("Web");
322 // Its work items are at `acme.visualstudio.com/Web/_workitems/...`, without the collection.
323 expect(
324 scopeOn("acme.visualstudio.com/defaultcollection/_git/web", "DefaultCollection/_git/Web"),
325 ).toBe("Web");
326 expect(scopeOn("dev.azure.com/_git/web", "_git/web")).toBeNull();
327 // A legacy host keeps the organization in its name, and so does its work item URL.
328 expect(
329 definition.parseIssueUrl?.(
330 new URL("https://acme.visualstudio.com/Web%20App/_workitems/edit/9"),
331 ),
332 ).toEqual({
333 host: "acme.visualstudio.com",
334 scope: "web app",
335 scopeAlias: "defaultcollection/web app",
336 key: "9",
337 });
338 expect(
339 definition.parseIssueUrl?.(
340 new URL("https://acme.visualstudio.com/DefaultCollection/Web%20App/_workitems/edit/9"),
341 ),
342 ).toEqual({
343 host: "acme.visualstudio.com",
344 scope: "defaultcollection/web app",
345 scopeAlias: "web app",
346 key: "9",
347 });
348 expect(
349 definition.parseIssueUrl?.(new URL("https://dev.azure.com/acme/_workitems/edit/9")),
350 ).toBeNull();
351 });
352});