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

definition.test.ts

195 lines7.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("keeps the organization in the host for the legacy visualstudio.com remotes", () => {
141 for (const remote of [
142 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
143 "https://T3Tools.visualstudio.com/Platform/_git/T3Code",
144 ]) {
145 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
146 "t3tools.visualstudio.com/platform/_git/t3code",
147 );
148 }
149 });
150​
151 it("compares every spelling on dev.azure.com", () => {
152 for (const key of [
153 "ssh.dev.azure.com/v3/org/project/repo",
154 "vs-ssh.visualstudio.com/v3/org/project/repo",
155 "org.visualstudio.com/project/_git/repo",
156 "org.visualstudio.com/defaultcollection/project/_git/repo",
157 "dev.azure.com/org/project/_git/repo",
158 ]) {
159 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/project/_git/repo");
160 expect(registry.findByRepositoryKey(key), key).toBe(definition);
161 }
162 expect(registry.canonicalRepositoryKey("tfs.example/c/p/_git/r")).toBe(
163 "tfs.example/c/p/_git/r",
164 );
165 expect(registry.findByRepositoryKey("tfs.example/c/p/_git/r")).toBeUndefined();
166 });
167});
168​
169describe("Azure DevOps repository selector", () => {
170 const registry = makeSourceControlDefinitionRegistry([definition]);
171​
172 it("names a repository by its own name, not its project path", () => {
173 // `az repos pr list --repository` takes a name and detects the organisation and project from
174 // the checkout; the recorded `org/project/_git/repo` path is refused.
175 expect(
176 registry.repositorySelector({
177 provider: SourceControlProviderKind.make("azure-devops"),
178 displayName: "contoso/payments/_git/checkout",
179 owner: "contoso",
180 name: "checkout",
181 }),
182 ).toBe("checkout");
183 });
184​
185 it("decodes a name the remote URL percent-encodes", () => {
186 expect(
187 registry.repositorySelector({
188 provider: SourceControlProviderKind.make("azure-devops"),
189 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
190 owner: "contoso",
191 name: "checkout%20(eu)",
192 }),
193 ).toBe("checkout (eu)");
194 });
195});