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

definition.test.ts

108 lines4.1 KB

1import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { canonicalizePullRequestUrl, definition, parsePullRequestUrl } from "./definition.ts";
5​
6const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
7​
8describe("GitCafe change request URLs", () => {
9 it("selects the decoded repository of an escaped remote path", () => {
10 const select = (displayName: string) =>
11 definition.repositorySelector?.({ provider: definition.kind, displayName });
12 expect(select("%76ersecafe/ashlar")).toBe("versecafe/ashlar");
13 expect(select("versecafe/ashlar%2Egit")).toBe("versecafe/ashlar");
14 for (const path of ["team%2Fproject", "team/%E0%A4%A", "team/project%20"]) {
15 expect(select(path), path).toBeNull();
16 }
17 });
18​
19 it.each(["git.cafe", "staging.git.cafe"])("normalizes links for storage on %s", (host) => {
20 for (const repository of ["web", "pull", "pulls"]) {
21 for (const route of ["pull", "pulls"]) {
22 const url = `https://${host}/Acme/${repository}/${route}/42/files?w=1#diff`;
23 expect(parsePullRequestUrl(url)).toMatchObject({
24 host,
25 repository: `acme/${repository}`,
26 number: 42,
27 });
28 expect(definition.isChangeRequestReference(url)).toBe(true);
29 expect(canonicalizePullRequestUrl(url)).toBe(`https://${host}/acme/${repository}/pulls/42`);
30 }
31 }
32 });
33​
34 it("preserves other URLs and rejects invalid GitCafe references", () => {
35 for (const url of [
36 "https://github.com/team/repo/pull/7",
37 "https://forge.example/team/repo/pulls/7",
38 "https://not.git.cafe/team/repo/pull/7",
39 "https://git.cafe.evil.test/team/repo/pull/7",
40 "https://git.cafe/tfs/collection/project/_git/repo/pullrequest/7",
41 "https://git.cafe/team/repo/pull/0",
42 "https://git.cafe/team/repo/pulls/9007199254740992",
43 "https://staging.git.cafe/team/repo/pull/7suffix",
44 "ftp://git.cafe/team/repo/pull/7",
45 "not a url",
46 ]) {
47 expect(parsePullRequestUrl(url), url).toBeNull();
48 expect(definition.isChangeRequestReference(url), url).toBe(false);
49 expect(canonicalizePullRequestUrl(url), url).toBe(url);
50 }
51 });
52​
53 it("reads its canonical /pull/ path and the older /pulls/ path on its own hosts", () => {
54 for (const path of ["pull", "pulls"]) {
55 expect(parse(`https://git.cafe/PingDotGG/T3Code/${path}/23897`), path).toEqual({
56 host: "git.cafe",
57 repository: "pingdotgg/t3code",
58 number: 23897,
59 repositoryUrl: "https://git.cafe/PingDotGG/T3Code",
60 });
61 }
62 expect(parse("https://staging.git.cafe/team/repo/pull/7/files")).toMatchObject({
63 host: "staging.git.cafe",
64 repository: "team/repo",
65 number: 7,
66 });
67 });
68​
69 it("leaves other hosts and routes alone", () => {
70 for (const url of [
71 "https://github.com/team/repo/pull/7",
72 "https://forge.example/team/repo/pulls/7",
73 "https://git.cafe/pingdotgg/t3code/issues/23897",
74 "https://git.cafe/pingdotgg/t3code/pull/abc",
75 "https://git.cafe/pingdotgg/t3code",
76 ]) {
77 expect(parse(url), url).toBeNull();
78 }
79 });
80​
81 it("builds pull request URLs below the repository", () => {
82 expect(
83 definition.changeRequestUrl({ host: "git.cafe", repository: "team/repo", number: 8 }),
84 ).toBe("https://git.cafe/team/repo/pulls/8");
85 expect(
86 definition.changeRequestUrl({
87 host: "git.cafe",
88 repository: "team/repo",
89 number: 8,
90 webUrl: "https://git.cafe/Team/Repo/",
91 }),
92 ).toBe("https://git.cafe/Team/Repo/pulls/8");
93 });
94});
95​
96describe("GitCafe remotes", () => {
97 it("recognises its own hosts only, without a port", () => {
98 expect(recognizeRemoteWith(definition, "https://git.cafe:8443/a/b")).toEqual({
99 kind: "gitcafe",
100 name: "GitCafe",
101 baseUrl: "https://git.cafe",
102 });
103 expect(recognizeRemoteWith(definition, "git@staging.git.cafe:team/repo.git")?.kind).toBe(
104 "gitcafe",
105 );
106 expect(recognizeRemoteWith(definition, "https://cafe.example/a/b")).toBeNull();
107 });
108});