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

definition.test.ts

221 lines8.6 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.each([
58 ["ssh.dev.azure.com", "v3/org/project/web"],
59 ["org.visualstudio.com", "defaultcollection/project/_git/web"],
60 ["dev.azure.com", "org/project/_git/web"],
61 ])("builds a canonical browser URL from the remote %s/%s", (host, repository) => {
62 expect(definition.changeRequestUrl({ host, repository, number: 42 })).toBe(
63 "https://dev.azure.com/org/project/_git/web/pullrequest/42",
64 );
65 });
66​
67 it("keeps an Azure DevOps Server's HTTP origin and builds below a web URL", () => {
68 expect(
69 definition.changeRequestUrl({
70 host: "scm.example.test",
71 repository: "tfs/collection/project/_git/repo",
72 number: 124,
73 remoteUrl: "http://dummy:secret@scm.example.test:8080/tfs/Collection/Project/_git/Repo",
74 }),
75 ).toBe("http://scm.example.test:8080/tfs/collection/project/_git/repo/pullrequest/124");
76 expect(
77 definition.changeRequestUrl({
78 host: "dev.azure.com",
79 repository: "acme/project/_git/web",
80 number: 43,
81 webUrl: "https://dev.azure.com/acme/project/_git/web",
82 }),
83 ).toBe("https://dev.azure.com/acme/project/_git/web/pullrequest/43");
84 });
85​
86 it.each([
87 ["ssh.dev.azure.com/v3/org-a/project/web", "git@ssh.dev.azure.com:v3/org-a/project/web"],
88 [
89 "vs-ssh.visualstudio.com/v3/org-a/project/web",
90 "git@vs-ssh.visualstudio.com:v3/org-a/project/web",
91 ],
92 [
93 "org-a.visualstudio.com/defaultcollection/project/_git/web",
94 "https://org-a.visualstudio.com/DefaultCollection/Project/_git/Web",
95 ],
96 ["dev.azure.com/org-a/project/_git/web", "https://org-a@dev.azure.com/org-a/Project/_git/Web"],
97 ])("matches browser references against the checkout %s", (canonicalKey, remoteUrl) => {
98 const identity: ChangeRequestProjectIdentity = {
99 canonicalKey,
100 displayName: canonicalKey.split("/").slice(1).join("/"),
101 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
102 };
103 const link = parse("https://dev.azure.com/org-a/project/_git/web/pullrequest/42")!;
104 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
105 for (const repository of ["org-b/project/_git/web", "org-a/other-project/_git/web"]) {
106 expect(definition.isChangeRequestInRepository(identity, { ...link, repository })).toBe(false);
107 }
108 });
109});
110​
111describe("Azure DevOps remotes", () => {
112 it("recognises the SSH clone hosts as well as the web ones", () => {
113 for (const remote of [
114 "git@ssh.dev.azure.com:v3/org/project/repo",
115 "ssh://git@ssh.dev.azure.com:22/v3/org/project/repo",
116 "git@vs-ssh.visualstudio.com:v3/org/project/repo",
117 "https://org@dev.azure.com/org/project/_git/repo",
118 ]) {
119 expect(recognizeRemoteWith(definition, remote)?.kind, remote).toBe("azure-devops");
120 }
121 expect(recognizeRemoteWith(definition, "https://azure.example/o/p/_git/r")).toBeNull();
122 });
123});
124​
125describe("Azure DevOps repository keys", () => {
126 const registry = makeSourceControlDefinitionRegistry([definition]);
127​
128 it("gives a repository the same key over SSH as over HTTPS", () => {
129 for (const remote of [
130 "git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code",
131 "ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code",
132 "https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code",
133 ]) {
134 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
135 "dev.azure.com/t3tools/platform/_git/t3code",
136 );
137 }
138 });
139​
140 it.each([
141 "git@ssh.dev.azure.com:v3/acme/platform/tool.git",
142 "https://dev.azure.com/acme/platform/_git/tool.git",
143 ])("preserves a literal .git in an Azure repository name: %s", (remoteUrl) => {
144 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
145 expect(canonicalKey).toBe("dev.azure.com/acme/platform/_git/tool.git");
146 const identity: ChangeRequestProjectIdentity = {
147 canonicalKey,
148 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
149 };
150 const link = parse("https://dev.azure.com/acme/platform/_git/tool.git/pullrequest/7")!;
151 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
152 expect(
153 definition.isChangeRequestInRepository(identity, {
154 ...link,
155 repository: "acme/platform/_git/tool",
156 }),
157 ).toBe(false);
158 });
159​
160 it("still drops .git from other hosts' repository keys", () => {
161 expect(registry.repositoryKeyFromRemote("git@github.com:acme/tool.git")).toBe(
162 "github.com/acme/tool",
163 );
164 });
165​
166 it("keeps the organization in the host for the legacy visualstudio.com remotes", () => {
167 for (const remote of [
168 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
169 "https://T3Tools.visualstudio.com/Platform/_git/T3Code",
170 ]) {
171 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
172 "t3tools.visualstudio.com/platform/_git/t3code",
173 );
174 }
175 });
176​
177 it("compares every spelling on dev.azure.com", () => {
178 for (const key of [
179 "ssh.dev.azure.com/v3/org/project/repo",
180 "vs-ssh.visualstudio.com/v3/org/project/repo",
181 "org.visualstudio.com/project/_git/repo",
182 "org.visualstudio.com/defaultcollection/project/_git/repo",
183 "dev.azure.com/org/project/_git/repo",
184 ]) {
185 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/project/_git/repo");
186 expect(registry.findByRepositoryKey(key), key).toBe(definition);
187 }
188 expect(registry.canonicalRepositoryKey("tfs.example/c/p/_git/r")).toBe(
189 "tfs.example/c/p/_git/r",
190 );
191 expect(registry.findByRepositoryKey("tfs.example/c/p/_git/r")).toBeUndefined();
192 });
193});
194​
195describe("Azure DevOps repository selector", () => {
196 const registry = makeSourceControlDefinitionRegistry([definition]);
197​
198 it("names a repository by its own name, not its project path", () => {
199 // `az repos pr list --repository` takes a name and detects the organisation and project from
200 // the checkout; the recorded `org/project/_git/repo` path is refused.
201 expect(
202 registry.repositorySelector({
203 provider: SourceControlProviderKind.make("azure-devops"),
204 displayName: "contoso/payments/_git/checkout",
205 owner: "contoso",
206 name: "checkout",
207 }),
208 ).toBe("checkout");
209 });
210​
211 it("decodes a name the remote URL percent-encodes", () => {
212 expect(
213 registry.repositorySelector({
214 provider: SourceControlProviderKind.make("azure-devops"),
215 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
216 owner: "contoso",
217 name: "checkout%20(eu)",
218 }),
219 ).toBe("checkout (eu)");
220 });
221});