The best way to control Claude Code, Codex and more from any computer or phone

main
ts 300 lines 12 KB
Raw
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import type { ChangeRequestProjectIdentity } from "@t3tools/source-control-core/shared/definition";
3 import {
4 makeSourceControlDefinitionRegistry,
5 recognizeRemoteWith,
6 } from "@t3tools/source-control-core/shared/definition";
7 import { describe, expect, it } from "vite-plus/test";
8
9 import { definition } from "./definition.ts";
10
11 const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
12
13 describe("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
111 describe("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
125 describe("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("matches a repository however its name is percent-encoded", () => {
161 const remoteUrl = "https://example@dev.azure.com/example/Sample%20(Beta)/_git/Sample%20(Beta)";
162 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
163 const identity: ChangeRequestProjectIdentity = {
164 canonicalKey,
165 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
166 };
167 for (const url of [
168 "https://dev.azure.com/example/Sample%20%28Beta%29/_git/Sample%20%28Beta%29/pullrequest/7",
169 "https://dev.azure.com/example/Sample (Beta)/_git/Sample (Beta)/pullrequest/7",
170 ]) {
171 const link = parse(url)!;
172 expect(link.repository, url).toBe("example/sample%20(beta)/_git/sample%20(beta)");
173 expect(definition.isChangeRequestInRepository(identity, link), url).toBe(true);
174 expect(
175 definition.isChangeRequestInRepository(
176 {
177 ...identity,
178 canonicalKey: "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
179 },
180 link,
181 ),
182 url,
183 ).toBe(true);
184 }
185 // Links stored before keys had one spelling still resolve to their project.
186 expect(
187 registry.canonicalRepositoryKey(
188 "dev.azure.com/example/sample%20%28beta%29/_git/sample%20%28beta%29",
189 ),
190 ).toBe(canonicalKey);
191 });
192
193 it("still drops .git from other hosts' repository keys", () => {
194 expect(registry.repositoryKeyFromRemote("git@github.com:acme/tool.git")).toBe(
195 "github.com/acme/tool",
196 );
197 });
198
199 it("keeps the organization in the host for the legacy visualstudio.com remotes", () => {
200 for (const remote of [
201 "T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code",
202 "https://T3Tools.visualstudio.com/Platform/_git/T3Code",
203 ]) {
204 expect(registry.repositoryKeyFromRemote(remote), remote).toBe(
205 "t3tools.visualstudio.com/platform/_git/t3code",
206 );
207 }
208 });
209
210 it("compares every spelling on dev.azure.com", () => {
211 for (const key of [
212 "ssh.dev.azure.com/v3/org/project/repo",
213 "vs-ssh.visualstudio.com/v3/org/project/repo",
214 "org.visualstudio.com/project/_git/repo",
215 "org.visualstudio.com/defaultcollection/project/_git/repo",
216 "dev.azure.com/org/project/_git/repo",
217 ]) {
218 expect(registry.canonicalRepositoryKey(key), key).toBe("dev.azure.com/org/project/_git/repo");
219 expect(registry.findByRepositoryKey(key), key).toBe(definition);
220 }
221 expect(registry.canonicalRepositoryKey("tfs.example/c/p/_git/r")).toBe(
222 "tfs.example/c/p/_git/r",
223 );
224 expect(registry.findByRepositoryKey("tfs.example/c/p/_git/r")).toBeUndefined();
225 });
226 });
227
228 describe("Azure DevOps repository selector", () => {
229 const registry = makeSourceControlDefinitionRegistry([definition]);
230
231 it("names a repository by its own name, not its project path", () => {
232 // `az repos pr list --repository` takes a name and detects the organisation and project from
233 // the checkout; the recorded `org/project/_git/repo` path is refused.
234 expect(
235 registry.repositorySelector({
236 provider: SourceControlProviderKind.make("azure-devops"),
237 displayName: "contoso/payments/_git/checkout",
238 owner: "contoso",
239 name: "checkout",
240 }),
241 ).toBe("checkout");
242 });
243
244 it("decodes a name the remote URL percent-encodes", () => {
245 expect(
246 registry.repositorySelector({
247 provider: SourceControlProviderKind.make("azure-devops"),
248 displayName: "contoso/payments%20(eu)/_git/checkout%20(eu)",
249 owner: "contoso",
250 name: "checkout%20(eu)",
251 }),
252 ).toBe("checkout (eu)");
253 });
254 });
255
256 describe("Azure DevOps issue scope", () => {
257 it("places work items in the project rather than the repository", () => {
258 const kind = SourceControlProviderKind.make("azure-devops");
259 expect(
260 definition.issueScope?.({ provider: kind, displayName: "acme/Web%20App/_git/web" }),
261 ).toBe("acme/Web App");
262 expect(definition.issueScope?.({ provider: kind, displayName: "web" })).toBeNull();
263 // One segment before `_git` is a different thing on each kind of host, so it needs the host.
264 expect(definition.issueScope?.({ provider: kind, displayName: "acme/_git/web" })).toBeNull();
265 const scopeOn = (canonicalKey: string, displayName: string) =>
266 definition.issueScope?.({ provider: kind, canonicalKey, displayName });
267 expect(scopeOn("dev.azure.com/acme/_git/web", "acme/_git/Web")).toBe("acme/Web");
268 expect(scopeOn("acme.visualstudio.com/web/_git/web", "Web%20App/_git/web")).toBe("Web App");
269 expect(scopeOn("acme.visualstudio.com/_git/web", "_git/Web")).toBe("Web");
270 // Its work items are at `acme.visualstudio.com/Web/_workitems/...`, without the collection.
271 expect(
272 scopeOn("acme.visualstudio.com/defaultcollection/_git/web", "DefaultCollection/_git/Web"),
273 ).toBe("Web");
274 expect(scopeOn("dev.azure.com/_git/web", "_git/web")).toBeNull();
275 // A legacy host keeps the organization in its name, and so does its work item URL.
276 expect(
277 definition.parseIssueUrl?.(
278 new URL("https://acme.visualstudio.com/Web%20App/_workitems/edit/9"),
279 ),
280 ).toEqual({
281 host: "acme.visualstudio.com",
282 scope: "web app",
283 scopeAlias: "defaultcollection/web app",
284 key: "9",
285 });
286 expect(
287 definition.parseIssueUrl?.(
288 new URL("https://acme.visualstudio.com/DefaultCollection/Web%20App/_workitems/edit/9"),
289 ),
290 ).toEqual({
291 host: "acme.visualstudio.com",
292 scope: "defaultcollection/web app",
293 scopeAlias: "web app",
294 key: "9",
295 });
296 expect(
297 definition.parseIssueUrl?.(new URL("https://dev.azure.com/acme/_workitems/edit/9")),
298 ).toBeNull();
299 });
300 });