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

sourceControl.test.ts

194 lines7.0 KB

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