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

definition.test.ts

97 lines3.5 KB

1import { recognizeRemoteWith } from "@t3tools/source-control-core/shared/definition";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { definition } from "./definition.ts";
5​
6const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
7​
8describe("GitLab change request URLs", () => {
9 it("reads a merge request on any host, nested groups and all", () => {
10 expect(parse("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42")).toEqual({
11 host: "gitlab.com",
12 repository: "t3tools/platform/t3code",
13 number: 42,
14 repositoryUrl: "https://gitlab.com/t3tools/platform/t3code",
15 });
16 expect(parse("https://code.acme.test/team/project/-/merge_requests/9")).toMatchObject({
17 host: "code.acme.test",
18 repository: "team/project",
19 number: 9,
20 });
21 });
22​
23 it("keeps pull-like segments inside nested repository paths", () => {
24 expect(
25 parse("https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42")?.repositoryUrl,
26 ).toBe("https://gitlab.example.test/group/pull/123/repo");
27 });
28​
29 it("leaves issues and single-segment paths alone", () => {
30 expect(parse("https://gitlab.com/t3tools/t3code/-/issues/12")).toBeNull();
31 expect(parse("https://gitlab.com/t3code/-/merge_requests/12")).toBeNull();
32 });
33​
34 it("recognizes only merge request URLs it can parse", () => {
35 for (const url of [
36 "https://gitlab.com/g/p?x=/-/merge_requests/5",
37 "https://gitlab.com/g/p#/-/merge_requests/5",
38 "https://gitlab.com?x=/g/p/-/merge_requests/5",
39 "https://gitlab.com#/g/p/-/merge_requests/5",
40 "https://gitlab.com/t3code/-/merge_requests/12",
41 "https://gitlab.com/g//-/merge_requests/5",
42 ]) {
43 expect(definition.isChangeRequestReference(url), url).toBe(false);
44 expect(parse(url), url).toBeNull();
45 }
46 for (const url of [
47 "https://code.acme.test/a/b/c/-/merge_requests/7?tab=diffs",
48 "http://git.internal/a/b/c/-/merge_requests/7#note_1",
49 "https://code.acme.test/a/b/c/-/merge_requests/7/diffs",
50 ]) {
51 expect(definition.isChangeRequestReference(url), url).toBe(true);
52 expect(parse(url), url).toMatchObject({ repository: "a/b/c", number: 7 });
53 }
54 });
55​
56 it("builds merge request URLs below the repository", () => {
57 expect(
58 definition.changeRequestUrl({
59 host: "gitlab.com",
60 repository: "group/sub/project",
61 number: 4,
62 }),
63 ).toBe("https://gitlab.com/group/sub/project/-/merge_requests/4");
64 expect(
65 definition.changeRequestUrl({
66 host: "gitlab.com",
67 repository: "group/sub/project",
68 number: 4,
69 webUrl: "https://gitlab.com/group/sub/project",
70 }),
71 ).toBe("https://gitlab.com/group/sub/project/-/merge_requests/4");
72 });
73});
74​
75describe("GitLab remotes", () => {
76 const recognize = (url: string) => recognizeRemoteWith(definition, url);
77​
78 it("keeps an HTTP port in the base URL and drops an SSH one", () => {
79 expect(recognize("https://gitlab.com:8443/group/repo.git")).toEqual({
80 kind: "gitlab",
81 name: "GitLab",
82 baseUrl: "https://gitlab.com:8443",
83 });
84 expect(recognize("ssh://git@gitlab.example.test:24/group/repo.git")).toEqual({
85 kind: "gitlab",
86 name: "GitLab Self-Hosted",
87 baseUrl: "https://gitlab.example.test",
88 });
89 expect(recognize("gitlab@gitlab.example.com:group/project.git")?.baseUrl).toBe(
90 "https://gitlab.example.com",
91 );
92 });
93​
94 it("matches complete DNS labels only", () => {
95 expect(recognize("https://notgitlab.example.com/group/repo.git")).toBeNull();
96 });
97});