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

definition.test.ts

220 lines8.0 KB

1import {
2 makeSourceControlDefinitionRegistry,
3 recognizeRemoteWith,
4} from "@t3tools/source-control-core/shared/definition";
5import { describe, expect, it } from "vite-plus/test";
6​
7import { definition } from "./definition.ts";
8​
9const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
10​
11describe("GitLab change request URLs", () => {
12 it("reads a merge request on any host, nested groups and all", () => {
13 expect(parse("https://gitlab.com/t3tools/platform/t3code/-/merge_requests/42")).toEqual({
14 host: "gitlab.com",
15 repository: "t3tools/platform/t3code",
16 number: 42,
17 repositoryUrl: "https://gitlab.com/t3tools/platform/t3code",
18 });
19 expect(parse("https://code.acme.test/team/project/-/merge_requests/9")).toMatchObject({
20 host: "code.acme.test",
21 repository: "team/project",
22 number: 9,
23 });
24 });
25​
26 it("keeps pull-like segments inside nested repository paths", () => {
27 expect(
28 parse("https://gitlab.example.test/group/pull/123/repo/-/merge_requests/42")?.repositoryUrl,
29 ).toBe("https://gitlab.example.test/group/pull/123/repo");
30 });
31​
32 it("leaves issues and single-segment paths alone", () => {
33 expect(parse("https://gitlab.com/t3tools/t3code/-/issues/12")).toBeNull();
34 expect(parse("https://gitlab.com/t3code/-/merge_requests/12")).toBeNull();
35 });
36​
37 it("recognizes only merge request URLs it can parse", () => {
38 for (const url of [
39 "https://gitlab.com/g/p?x=/-/merge_requests/5",
40 "https://gitlab.com/g/p#/-/merge_requests/5",
41 "https://gitlab.com?x=/g/p/-/merge_requests/5",
42 "https://gitlab.com#/g/p/-/merge_requests/5",
43 "https://gitlab.com/t3code/-/merge_requests/12",
44 "https://gitlab.com/g//-/merge_requests/5",
45 ]) {
46 expect(definition.isChangeRequestReference(url), url).toBe(false);
47 expect(parse(url), url).toBeNull();
48 }
49 for (const url of [
50 "https://code.acme.test/a/b/c/-/merge_requests/7?tab=diffs",
51 "http://git.internal/a/b/c/-/merge_requests/7#note_1",
52 "https://code.acme.test/a/b/c/-/merge_requests/7/diffs",
53 ]) {
54 expect(definition.isChangeRequestReference(url), url).toBe(true);
55 expect(parse(url), url).toMatchObject({ repository: "a/b/c", number: 7 });
56 }
57 });
58​
59 it("builds merge request URLs below the repository", () => {
60 expect(
61 definition.changeRequestUrl({
62 host: "gitlab.com",
63 repository: "group/sub/project",
64 number: 4,
65 }),
66 ).toBe("https://gitlab.com/group/sub/project/-/merge_requests/4");
67 expect(
68 definition.changeRequestUrl({
69 host: "gitlab.internal",
70 repository: "team/app",
71 number: 5,
72 remoteUrl: "http://gitlab.internal:8080/team/app.git",
73 }),
74 ).toBe("http://gitlab.internal:8080/team/app/-/merge_requests/5");
75 expect(
76 definition.changeRequestUrl({
77 host: "gitlab.internal",
78 repository: "team/app",
79 number: 5,
80 remoteUrl: "git@gitlab.internal:team/app.git",
81 }),
82 ).toBe("https://gitlab.internal/team/app/-/merge_requests/5");
83 expect(
84 definition.changeRequestUrl({
85 host: "gitlab.com",
86 repository: "group/sub/project",
87 number: 4,
88 webUrl: "https://gitlab.com/group/sub/project",
89 }),
90 ).toBe("https://gitlab.com/group/sub/project/-/merge_requests/4");
91 });
92​
93 it("links authors, bots included, to their profile on the repository's host", () => {
94 expect(definition.authorProfileUrl("TonybynMp4", "https://gitlab.com/group/sub/project")).toBe(
95 "https://gitlab.com/TonybynMp4",
96 );
97 expect(
98 definition.authorProfileUrl(
99 "project_278964_bot_89f97e2c",
100 "http://gitlab.internal:8080/team/app/-/merge_requests/5",
101 ),
102 ).toBe("http://gitlab.internal:8080/project_278964_bot_89f97e2c");
103 });
104});
105​
106describe("GitLab CLI commands", () => {
107 it("recognizes shell merges and closes, including glab's `accept` alias", () => {
108 for (const command of ["glab mr merge 7 --yes", "glab mr accept 7 --yes", "glab mr close 7"]) {
109 expect(definition.changeRequestCloseCommand.test(command), command).toBe(true);
110 }
111 expect(definition.changeRequestCloseCommand.test("glab mr view 7")).toBe(false);
112 });
113​
114 it("recognizes shell creates, including glab's `new` alias", () => {
115 for (const command of ["glab mr create --fill --yes", "glab mr new --fill --yes"]) {
116 expect(definition.changeRequestCreateCommand.test(command), command).toBe(true);
117 }
118 expect(definition.changeRequestCreateCommand.test("glab mr view 7")).toBe(false);
119 });
120});
121​
122describe("GitLab remotes", () => {
123 const recognize = (url: string) => recognizeRemoteWith(definition, url);
124​
125 it("keeps an HTTP port in the base URL and drops an SSH one", () => {
126 expect(recognize("https://gitlab.com:8443/group/repo.git")).toEqual({
127 kind: "gitlab",
128 name: "GitLab",
129 baseUrl: "https://gitlab.com:8443",
130 });
131 expect(recognize("ssh://git@gitlab.example.test:24/group/repo.git")).toEqual({
132 kind: "gitlab",
133 name: "GitLab Self-Hosted",
134 baseUrl: "https://gitlab.example.test",
135 });
136 expect(recognize("gitlab@gitlab.example.com:group/project.git")?.baseUrl).toBe(
137 "https://gitlab.example.com",
138 );
139 });
140​
141 it("matches complete DNS labels only", () => {
142 expect(recognize("https://notgitlab.example.com/group/repo.git")).toBeNull();
143 });
144​
145 it.each([
146 "ssh://git@altssh.gitlab.com:443/group/project.git",
147 "git@altssh.gitlab.com:group/project.git",
148 ])("recognizes GitLab.com's port 443 SSH host in %s", (url) => {
149 expect(recognize(url)).toEqual({
150 kind: "gitlab",
151 name: "GitLab",
152 baseUrl: "https://gitlab.com",
153 });
154 });
155​
156 it("reads a self-hosted altssh host as its own server", () => {
157 expect(recognize("ssh://git@altssh.gitlab.example.com:443/group/project.git")).toEqual({
158 kind: "gitlab",
159 name: "GitLab Self-Hosted",
160 baseUrl: "https://altssh.gitlab.example.com",
161 });
162 });
163});
164​
165describe("GitLab repository keys", () => {
166 const registry = makeSourceControlDefinitionRegistry([definition]);
167​
168 it.each([
169 "git@gitlab.com:Group/Sub/Project.git",
170 "ssh://git@altssh.gitlab.com:443/Group/Sub/Project.git",
171 "git@altssh.gitlab.com:Group/Sub/Project.git",
172 "https://gitlab.com/Group/Sub/Project.git",
173 ])("names %s on gitlab.com, where its merge requests live", (remoteUrl) => {
174 const canonicalKey = registry.repositoryKeyFromRemote(remoteUrl);
175 expect(canonicalKey).toBe("gitlab.com/group/sub/project");
176 const link = parse("https://gitlab.com/group/sub/project/-/merge_requests/5")!;
177 expect(
178 definition.isChangeRequestInRepository(
179 {
180 canonicalKey,
181 displayName: "group/sub/project",
182 locator: { source: "git-remote", remoteName: "origin", remoteUrl },
183 },
184 link,
185 ),
186 ).toBe(true);
187 expect(registry.changeRequestHost({ canonicalKey }, "gitlab")).toBe("gitlab.com");
188 });
189​
190 it("matches merge requests for a key saved under altssh.gitlab.com", () => {
191 const canonicalKey = "altssh.gitlab.com/group/sub/project";
192 expect(registry.canonicalRepositoryKey(canonicalKey)).toBe("gitlab.com/group/sub/project");
193 const identity = {
194 canonicalKey,
195 displayName: "group/sub/project",
196 locator: {
197 source: "git-remote" as const,
198 remoteName: "origin",
199 remoteUrl: "ssh://git@altssh.gitlab.com:443/group/sub/project.git",
200 },
201 };
202 const link = parse("https://gitlab.com/group/sub/project/-/merge_requests/5")!;
203 expect(definition.isChangeRequestInRepository(identity, link)).toBe(true);
204 expect(definition.canReadChangeRequestOnHost(identity, link)).toBe(true);
205 });
206​
207 it.each([
208 [
209 "ssh://git@altssh.gitlab.example.com:443/team/project.git",
210 "altssh.gitlab.example.com/team/project",
211 ],
212 [
213 "git@altssh.gitlab.com.evil.example:team/project.git",
214 "altssh.gitlab.com.evil.example/team/project",
215 ],
216 ])("leaves the key of %s alone", (remoteUrl, key) => {
217 expect(registry.repositoryKeyFromRemote(remoteUrl)).toBe(key);
218 expect(registry.canonicalRepositoryKey(key)).toBe(key);
219 });
220});