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

definition.test.ts

312 lines12.4 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 "git@vs-ssh.visualstudio.com:v3/org/project/repo",
129 "https://org@dev.azure.com/org/project/_git/repo",
130 ]) {
131 expect(recognizeRemoteWith(definition, remote)?.kind, remote).toBe("azure-devops");
132 }
133 expect(recognizeRemoteWith(definition, "https://azure.example/o/p/_git/r")).toBeNull();
134 });
135});
136​
137describe("Azure DevOps repository keys", () => {
138 const registry = makeSourceControlDefinitionRegistry([definition]);
139​
140 it("gives a repository the same key over SSH as over HTTPS", () => {
141 for (const remote of [
142 "git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code",
143 "ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code",
144 "https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code",
145 ]) {
146 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
147 "dev.azure.com/t3tools/platform/_git/t3code",
148 );
149 }
150 });
151​
152 it.each([
153 "git@ssh.dev.azure.com:v3/acme/platform/tool.git",
154 "https://dev.azure.com/acme/platform/_git/tool.git",
155 ])("preserves a literal .git in an Azure repository name: %s", (remoteUrl) => {
156 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
157 expect(canonicalKey).toBe("dev.azure.com/acme/platform/_git/tool.git");
158 const identity: ChangeRequestProjectIdentity = {
159 canonicalKey,
160 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
161 };
162 const link = parse("https://dev.azure.com/acme/platform/_git/tool.git/pullrequest/7")!;
163 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
164 expect(
165 definition.isChangeRequestInRepository(identity, {
166 ...link,
167 repository: "acme/platform/_git/tool",
168 }),
169 ).toBe(false);
170 });
171​
172 it("matches a repository however its name is percent-encoded", () => {
173 const remoteUrl = "https://example@dev.azure.com/example/Sample%20(Beta)/_git/Sample%20(Beta)";
174 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
175 const identity: ChangeRequestProjectIdentity = {
176 canonicalKey,
177 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
178 };
179 for (const url of [
180 "https://dev.azure.com/example/Sample%20%28Beta%29/_git/Sample%20%28Beta%29/pullrequest/7",
181 "https://dev.azure.com/example/Sample (Beta)/_git/Sample (Beta)/pullrequest/7",
182 ]) {
183 const link = parse(url)!;
184 expect(link.repository, url).toBe("example/sample%20(beta)/_git/sample%20(beta)");
185 expect(definition.isChangeRequestInRepository(identity, link), url).toBe(true);
186 expect(
187 definition.isChangeRequestInRepository(
188 {
189 ...identity,
190 canonicalKey: "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
191 },
192 link,
193 ),
194 url,
195 ).toBe(true);
196 }
197 // Links stored before keys had one spelling still resolve to their project.
198 expect(
199 registry.canonicalRepositoryKey(
200 "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
201 ),
202 ).toBe(canonicalKey);
203 });
204​
205 it("still drops .git from other hosts' repository keys", () => {
206 expect(registry.repositoryKeyFromRemote("git@github.com:acme/tool.git")).toBe(
207 "github.com/acme/tool",
208 );
209 });
210​
211 it("keeps the organization in the host for the legacy visualstudio.com remotes", () => {
212 for (const remote of [
213 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
214 "https://T3Tools.visualstudio.com/Platform/_git/T3Code",
215 ]) {
216 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
217 "t3tools.visualstudio.com/platform/_git/t3code",
218 );
219 }
220 });
221​
222 it("compares every spelling on dev.azure.com", () => {
223 for (const key of [
224 "ssh.dev.azure.com/v3/org/project/repo",
225 "vs-ssh.visualstudio.com/v3/org/project/repo",
226 "org.visualstudio.com/project/_git/repo",
227 "org.visualstudio.com/defaultcollection/project/_git/repo",
228 "dev.azure.com/org/project/_git/repo",
229 ]) {
230 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/project/_git/repo");
231 expect(registry.findByRepositoryKey(key), key).toBe(definition);
232 }
233 expect(registry.canonicalRepositoryKey("tfs.example/c/p/_git/r")).toBe(
234 "tfs.example/c/p/_git/r",
235 );
236 expect(registry.findByRepositoryKey("tfs.example/c/p/_git/r")).toBeUndefined();
237 });
238});
239​
240describe("Azure DevOps repository selector", () => {
241 const registry = makeSourceControlDefinitionRegistry([definition]);
242​
243 it("names a repository by its own name, not its project path", () => {
244 // `az repos pr list --repository` takes a name and detects the organisation and project from
245 // the checkout; the recorded `org/project/_git/repo` path is refused.
246 expect(
247 registry.repositorySelector({
248 provider: SourceControlProviderKind.make("azure-devops"),
249 displayName: "contoso/payments/_git/checkout",
250 owner: "contoso",
251 name: "checkout",
252 }),
253 ).toBe("checkout");
254 });
255​
256 it("decodes a name the remote URL percent-encodes", () => {
257 expect(
258 registry.repositorySelector({
259 provider: SourceControlProviderKind.make("azure-devops"),
260 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
261 owner: "contoso",
262 name: "checkout%20(eu)",
263 }),
264 ).toBe("checkout (eu)");
265 });
266});
267​
268describe("Azure DevOps issue scope", () => {
269 it("places work items in the project rather than the repository", () => {
270 const kind = SourceControlProviderKind.make("azure-devops");
271 expect(
272 definition.issueScope?.({ provider: kind, displayName: "acme/Web%20App/_git/web" }),
273 ).toBe("acme/Web App");
274 expect(definition.issueScope?.({ provider: kind, displayName: "web" })).toBeNull();
275 // One segment before `_git` is a different thing on each kind of host, so it needs the host.
276 expect(definition.issueScope?.({ provider: kind, displayName: "acme/_git/web" })).toBeNull();
277 const scopeOn = (canonicalKey: string, displayName: string) =>
278 definition.issueScope?.({ provider: kind, canonicalKey, displayName });
279 expect(scopeOn("dev.azure.com/acme/_git/web", "acme/_git/Web")).toBe("acme/Web");
280 expect(scopeOn("acme.visualstudio.com/web/_git/web", "Web%20App/_git/web")).toBe("Web App");
281 expect(scopeOn("acme.visualstudio.com/_git/web", "_git/Web")).toBe("Web");
282 // Its work items are at `acme.visualstudio.com/Web/_workitems/...`, without the collection.
283 expect(
284 scopeOn("acme.visualstudio.com/defaultcollection/_git/web", "DefaultCollection/_git/Web"),
285 ).toBe("Web");
286 expect(scopeOn("dev.azure.com/_git/web", "_git/web")).toBeNull();
287 // A legacy host keeps the organization in its name, and so does its work item URL.
288 expect(
289 definition.parseIssueUrl?.(
290 new URL("https://acme.visualstudio.com/Web%20App/_workitems/edit/9"),
291 ),
292 ).toEqual({
293 host: "acme.visualstudio.com",
294 scope: "web app",
295 scopeAlias: "defaultcollection/web app",
296 key: "9",
297 });
298 expect(
299 definition.parseIssueUrl?.(
300 new URL("https://acme.visualstudio.com/DefaultCollection/Web%20App/_workitems/edit/9"),
301 ),
302 ).toEqual({
303 host: "acme.visualstudio.com",
304 scope: "defaultcollection/web app",
305 scopeAlias: "web app",
306 key: "9",
307 });
308 expect(
309 definition.parseIssueUrl?.(new URL("https://dev.azure.com/acme/_workitems/edit/9")),
310 ).toBeNull();
311 });
312});