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

sourceControl.test.ts

162 lines5.8 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 detectSourceControlProviderFromRemoteUrl,
5 getChangeRequestTerminologyForKind,
6 isSshRemoteUrl,
7 resolveChangeRequestPresentation,
8} from "./sourceControl.ts";
9​
10describe("source control presentation", () => {
11 it("uses merge request terminology for GitLab", () => {
12 expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({
13 shortLabel: "MR",
14 singular: "merge request",
15 });
16 });
17​
18 it("uses pull request terminology for GitHub-compatible providers", () => {
19 expect(getChangeRequestTerminologyForKind("github")).toEqual({
20 shortLabel: "PR",
21 singular: "pull request",
22 });
23 expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({
24 shortLabel: "PR",
25 singular: "pull request",
26 });
27 expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({
28 shortLabel: "PR",
29 singular: "pull request",
30 });
31 });
32​
33 it("falls back to generic change request copy for unknown providers", () => {
34 expect(
35 resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }),
36 ).toEqual(
37 expect.objectContaining({
38 shortName: "change request",
39 longName: "change request",
40 }),
41 );
42 });
43});
44​
45describe("detectSourceControlProviderFromRemoteUrl", () => {
46 it("detects common source control hosts", () => {
47 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
48 "github",
49 );
50 expect(
51 detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
52 ).toBe("gitlab");
53 expect(
54 detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
55 ).toBe("azure-devops");
56 expect(
57 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
58 ).toBe("bitbucket");
59 });
60​
61 it("detects Azure DevOps SSH remotes", () => {
62 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
63 expect(
64 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
65 ).toBe("azure-devops");
66 expect(
67 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
68 ?.kind,
69 ).toBe("azure-devops");
70 // Legacy visualstudio.com SSH host stays classified too.
71 expect(
72 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
73 ?.kind,
74 ).toBe("azure-devops");
75 });
76​
77 it("preserves ports while classifying by hostname", () => {
78 expect(
79 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
80 ).toEqual({
81 kind: "gitlab",
82 name: "GitLab",
83 baseUrl: "https://gitlab.com:8443",
84 });
85 expect(
86 detectSourceControlProviderFromRemoteUrl(
87 "https://self-hosted.example.test:8443/group/repo.git",
88 ),
89 ).toEqual({
90 kind: "unknown",
91 name: "self-hosted.example.test:8443",
92 baseUrl: "https://self-hosted.example.test:8443",
93 });
94 });
95​
96 it("matches self-hosted providers by complete DNS labels", () => {
97 expect(
98 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
99 ).toBe("github");
100 expect(
101 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
102 ).toBe("gitlab");
103 expect(
104 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
105 ?.kind,
106 ).toBe("bitbucket");
107 });
108​
109 it("does not match provider names embedded in unrelated DNS labels", () => {
110 expect(
111 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
112 ?.kind,
113 ).toBe("unknown");
114 expect(
115 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
116 ?.kind,
117 ).toBe("unknown");
118 expect(
119 detectSourceControlProviderFromRemoteUrl(
120 "https://notbitbucket.example.com/workspace/repo.git",
121 )?.kind,
122 ).toBe("unknown");
123 });
124​
125 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
126 expect(
127 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
128 ).toBe("gitlab");
129 expect(
130 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
131 ?.baseUrl,
132 ).toBe("https://gitlab.example.com");
133 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
134 "github",
135 );
136 expect(
137 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
138 ).toBe("bitbucket");
139 });
140});
141​
142describe("isSshRemoteUrl", () => {
143 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
144 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
145 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
146 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
147 });
148​
149 it("recognises ssh:// URLs with any case", () => {
150 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
151 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
152 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
153 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
154 });
155​
156 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
157 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
158 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
159 expect(isSshRemoteUrl("")).toBe(false);
160 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
161 });
162});