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

sourceControl.test.ts

206 lines7.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 sourceControlRepositorySelector,
5 detectSourceControlProviderFromHost,
6 detectSourceControlProviderFromRemoteUrl,
7 getChangeRequestTerminologyForKind,
8 isSshRemoteUrl,
9 resolveChangeRequestPresentation,
10} from "./sourceControl.ts";
11​
12describe("source control presentation", () => {
13 it("uses merge request terminology for GitLab", () => {
14 expect(getChangeRequestTerminologyForKind("gitlab")).toEqual({
15 shortLabel: "MR",
16 singular: "merge request",
17 });
18 });
19​
20 it("uses pull request terminology for GitHub-compatible providers", () => {
21 expect(getChangeRequestTerminologyForKind("github")).toEqual({
22 shortLabel: "PR",
23 singular: "pull request",
24 });
25 expect(getChangeRequestTerminologyForKind("azure-devops")).toEqual({
26 shortLabel: "PR",
27 singular: "pull request",
28 });
29 expect(getChangeRequestTerminologyForKind("bitbucket")).toEqual({
30 shortLabel: "PR",
31 singular: "pull request",
32 });
33 });
34​
35 it("falls back to generic change request copy for unknown providers", () => {
36 expect(
37 resolveChangeRequestPresentation({ kind: "unknown", name: "forge", baseUrl: "" }),
38 ).toEqual(
39 expect.objectContaining({
40 shortName: "change request",
41 longName: "change request",
42 }),
43 );
44 });
45});
46​
47describe("detectSourceControlProviderFromRemoteUrl", () => {
48 it("detects common source control hosts", () => {
49 expect(detectSourceControlProviderFromRemoteUrl("git@github.com:owner/repo.git")?.kind).toBe(
50 "github",
51 );
52 expect(
53 detectSourceControlProviderFromRemoteUrl("https://gitlab.com/group/repo.git")?.kind,
54 ).toBe("gitlab");
55 expect(
56 detectSourceControlProviderFromRemoteUrl("https://dev.azure.com/org/project/_git/repo")?.kind,
57 ).toBe("azure-devops");
58 expect(
59 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
60 ).toBe("bitbucket");
61 });
62​
63 it("detects Azure DevOps SSH remotes", () => {
64 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
65 expect(
66 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
67 ).toBe("azure-devops");
68 expect(
69 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
70 ?.kind,
71 ).toBe("azure-devops");
72 // Legacy visualstudio.com SSH host stays classified too.
73 expect(
74 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
75 ?.kind,
76 ).toBe("azure-devops");
77 });
78​
79 it("preserves ports while classifying by hostname", () => {
80 expect(
81 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
82 ).toEqual({
83 kind: "gitlab",
84 name: "GitLab",
85 baseUrl: "https://gitlab.com:8443",
86 });
87 expect(
88 detectSourceControlProviderFromRemoteUrl(
89 "https://self-hosted.example.test:8443/group/repo.git",
90 ),
91 ).toEqual({
92 kind: "unknown",
93 name: "self-hosted.example.test:8443",
94 baseUrl: "https://self-hosted.example.test:8443",
95 });
96 });
97​
98 it("matches self-hosted providers by complete DNS labels", () => {
99 expect(
100 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
101 ).toBe("github");
102 expect(
103 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
104 ).toBe("gitlab");
105 expect(
106 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
107 ?.kind,
108 ).toBe("bitbucket");
109 });
110​
111 it("does not match provider names embedded in unrelated DNS labels", () => {
112 expect(
113 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
114 ?.kind,
115 ).toBe("unknown");
116 expect(
117 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
118 ?.kind,
119 ).toBe("unknown");
120 expect(
121 detectSourceControlProviderFromRemoteUrl(
122 "https://notbitbucket.example.com/workspace/repo.git",
123 )?.kind,
124 ).toBe("unknown");
125 });
126​
127 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
128 expect(
129 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
130 ).toBe("gitlab");
131 expect(
132 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
133 ?.baseUrl,
134 ).toBe("https://gitlab.example.com");
135 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
136 "github",
137 );
138 expect(
139 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
140 ).toBe("bitbucket");
141 });
142});
143​
144describe("detectSourceControlProviderFromHost", () => {
145 it("classifies canonical and self-hosted hosts", () => {
146 expect(detectSourceControlProviderFromHost("github.com")?.kind).toBe("github");
147 expect(detectSourceControlProviderFromHost("gitlab.example.com")?.kind).toBe("gitlab");
148 });
149​
150 it("leaves SSH aliases unknown, because upstream resolves them first", () => {
151 expect(detectSourceControlProviderFromHost("github-personal")?.kind).toBe("unknown");
152 });
153});
154​
155describe("isSshRemoteUrl", () => {
156 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
157 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
158 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
159 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
160 });
161​
162 it("recognises ssh:// URLs with any case", () => {
163 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
164 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
165 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
166 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
167 });
168​
169 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
170 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
171 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
172 expect(isSshRemoteUrl("")).toBe(false);
173 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
174 });
175});
176​
177it("names an Azure DevOps repository by its own name, not its project path", () => {
178 // `az repos pr list --repository` takes a name and detects the organisation and project from
179 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
180 // reads as unavailable on the page.
181 const selector = sourceControlRepositorySelector({
182 provider: "azure-devops",
183 displayName: "contoso/payments/_git/checkout",
184 owner: "contoso",
185 name: "checkout",
186 });
187 expect(selector).toBe("checkout");
188});
189​
190it("falls back to the path's last segment where an Azure identity has no name", () => {
191 const selector = sourceControlRepositorySelector({
192 provider: "azure-devops",
193 displayName: "contoso/payments/_git/checkout",
194 });
195 expect(selector).toBe("checkout");
196});
197​
198it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
199 const selector = sourceControlRepositorySelector({
200 provider: "gitlab",
201 displayName: "group/subgroup/service",
202 owner: "group",
203 name: "service",
204 });
205 expect(selector).toBe("group/subgroup/service");
206});