Open History to see the last commit
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.each(["git.cafe", "staging.git.cafe"])("normalizes links for storage on %s", (host) => {
10 for (const repository of ["web", "pull", "pulls"]) {
11 for (const route of ["pull", "pulls"]) {
12 const url = `https://${host}/Acme/${repository}/${route}/42/files?w=1#diff`;
13 expect(parsePullRequestUrl(url)).toMatchObject({
14 host,
15 repository: `acme/${repository}`,
16 number: 42,
17 });
18 expect(definition.isChangeRequestReference(url)).toBe(true);
19 expect(canonicalizePullRequestUrl(url)).toBe(`https://${host}/acme/${repository}/pulls/42`);
20 }
21 }
22 });
23
24 it("preserves other URLs and rejects invalid GitCafe references", () => {
25 for (const url of [
26 "https://github.com/team/repo/pull/7",
27 "https://forge.example/team/repo/pulls/7",
28 "https://not.git.cafe/team/repo/pull/7",
29 "https://git.cafe.evil.test/team/repo/pull/7",
30 "https://git.cafe/tfs/collection/project/_git/repo/pullrequest/7",
31 "https://git.cafe/team/repo/pull/0",
32 "https://git.cafe/team/repo/pulls/9007199254740992",
33 "https://staging.git.cafe/team/repo/pull/7suffix",
34 "ftp://git.cafe/team/repo/pull/7",
35 "not a url",
36 ]) {
37 expect(parsePullRequestUrl(url), url).toBeNull();
38 expect(definition.isChangeRequestReference(url), url).toBe(false);
39 expect(canonicalizePullRequestUrl(url), url).toBe(url);
40 }
41 });
42
43 it("reads its canonical /pull/ path and the older /pulls/ path on its own hosts", () => {
44 for (const path of ["pull", "pulls"]) {
45 expect(parse(`https://git.cafe/PingDotGG/T3Code/${path}/23897`), path).toEqual({
46 host: "git.cafe",
47 repository: "pingdotgg/t3code",
48 number: 23897,
49 repositoryUrl: "https://git.cafe/PingDotGG/T3Code",
50 });
51 }
52 expect(parse("https://staging.git.cafe/team/repo/pull/7/files")).toMatchObject({
53 host: "staging.git.cafe",
54 repository: "team/repo",
55 number: 7,
56 });
57 });
58
59 it("leaves other hosts and routes alone", () => {
60 for (const url of [
61 "https://github.com/team/repo/pull/7",
62 "https://forge.example/team/repo/pulls/7",
63 "https://git.cafe/pingdotgg/t3code/issues/23897",
64 "https://git.cafe/pingdotgg/t3code/pull/abc",
65 "https://git.cafe/pingdotgg/t3code",
66 ]) {
67 expect(parse(url), url).toBeNull();
68 }
69 });
70
71 it("builds pull request URLs below the repository", () => {
72 expect(
73 definition.changeRequestUrl({ host: "git.cafe", repository: "team/repo", number: 8 }),
74 ).toBe("https://git.cafe/team/repo/pulls/8");
75 expect(
76 definition.changeRequestUrl({
77 host: "git.cafe",
78 repository: "team/repo",
79 number: 8,
80 webUrl: "https://git.cafe/Team/Repo/",
81 }),
82 ).toBe("https://git.cafe/Team/Repo/pulls/8");
83 });
84});
85
86describe("GitCafe remotes", () => {
87 it("recognises its own hosts only, without a port", () => {
88 expect(recognizeRemoteWith(definition, "https://git.cafe:8443/a/b")).toEqual({
89 kind: "gitcafe",
90 name: "GitCafe",
91 baseUrl: "https://git.cafe",
92 });
93 expect(recognizeRemoteWith(definition, "git@staging.git.cafe:team/repo.git")?.kind).toBe(
94 "gitcafe",
95 );
96 expect(recognizeRemoteWith(definition, "https://cafe.example/a/b")).toBeNull();
97 });
98});