Open History to see the last commit
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 Forgejo and Gitea hosts while preserving HTTP origins", () => {
63 for (const host of ["codeberg.org", "forgejo.example.test", "gitea.example.test"]) {
64 expect(detectSourceControlProviderFromRemoteUrl(`http://${host}:3000/team/repo.git`)).toEqual(
65 {
66 kind: "forgejo",
67 name: "Forgejo",
68 baseUrl: `http://${host}:3000`,
69 },
70 );
71 }
72 expect(getChangeRequestTerminologyForKind("forgejo")).toEqual({
73 shortLabel: "PR",
74 singular: "pull request",
75 });
76 });
77
78 it("detects Azure DevOps SSH remotes", () => {
79 // The default Azure DevOps SSH clone URL uses the ssh.dev.azure.com host.
80 expect(
81 detectSourceControlProviderFromRemoteUrl("git@ssh.dev.azure.com:v3/org/project/repo")?.kind,
82 ).toBe("azure-devops");
83 expect(
84 detectSourceControlProviderFromRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/org/project/repo")
85 ?.kind,
86 ).toBe("azure-devops");
87 // Legacy visualstudio.com SSH host stays classified too.
88 expect(
89 detectSourceControlProviderFromRemoteUrl("git@vs-ssh.visualstudio.com:v3/org/project/repo")
90 ?.kind,
91 ).toBe("azure-devops");
92 });
93
94 it("preserves ports while classifying by hostname", () => {
95 expect(
96 detectSourceControlProviderFromRemoteUrl("https://gitlab.com:8443/group/repo.git"),
97 ).toEqual({
98 kind: "gitlab",
99 name: "GitLab",
100 baseUrl: "https://gitlab.com:8443",
101 });
102 expect(
103 detectSourceControlProviderFromRemoteUrl(
104 "https://self-hosted.example.test:8443/group/repo.git",
105 ),
106 ).toEqual({
107 kind: "unknown",
108 name: "self-hosted.example.test:8443",
109 baseUrl: "https://self-hosted.example.test:8443",
110 });
111 });
112
113 it("matches self-hosted providers by complete DNS labels", () => {
114 expect(
115 detectSourceControlProviderFromRemoteUrl("https://github.example.com/owner/repo.git")?.kind,
116 ).toBe("github");
117 expect(
118 detectSourceControlProviderFromRemoteUrl("https://gitlab.example.com/group/repo.git")?.kind,
119 ).toBe("gitlab");
120 expect(
121 detectSourceControlProviderFromRemoteUrl("https://bitbucket.example.com/workspace/repo.git")
122 ?.kind,
123 ).toBe("bitbucket");
124 });
125
126 it("does not match provider names embedded in unrelated DNS labels", () => {
127 expect(
128 detectSourceControlProviderFromRemoteUrl("https://notgithub.example.com/owner/repo.git")
129 ?.kind,
130 ).toBe("unknown");
131 expect(
132 detectSourceControlProviderFromRemoteUrl("https://notgitlab.example.com/group/repo.git")
133 ?.kind,
134 ).toBe("unknown");
135 expect(
136 detectSourceControlProviderFromRemoteUrl(
137 "https://notbitbucket.example.com/workspace/repo.git",
138 )?.kind,
139 ).toBe("unknown");
140 });
141
142 it("detects SSH remotes with non-git SSH users (e.g. gitlab@, deploy@)", () => {
143 expect(
144 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")?.kind,
145 ).toBe("gitlab");
146 expect(
147 detectSourceControlProviderFromRemoteUrl("gitlab@gitlab.example.com:group/project.git")
148 ?.baseUrl,
149 ).toBe("https://gitlab.example.com");
150 expect(detectSourceControlProviderFromRemoteUrl("deploy@github.com:owner/repo.git")?.kind).toBe(
151 "github",
152 );
153 expect(
154 detectSourceControlProviderFromRemoteUrl("git@bitbucket.org:workspace/repo.git")?.kind,
155 ).toBe("bitbucket");
156 });
157});
158
159describe("isSshRemoteUrl", () => {
160 it("recognises SCP-like SSH URLs with any SSH user prefix", () => {
161 expect(isSshRemoteUrl("git@github.com:owner/repo.git")).toBe(true);
162 expect(isSshRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(true);
163 expect(isSshRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(true);
164 });
165
166 it("recognises ssh:// URLs with any case", () => {
167 expect(isSshRemoteUrl("ssh://git@gitlab.example.com/group/project.git")).toBe(true);
168 expect(isSshRemoteUrl("ssh://git@gitlab.example.com:22/group/project.git")).toBe(true);
169 expect(isSshRemoteUrl("SSH://git@gitlab.example.com/group/project.git")).toBe(true);
170 expect(isSshRemoteUrl("SsH://git@gitlab.example.com/group/project.git")).toBe(true);
171 });
172
173 it("returns false for HTTPS, local paths, and SCP-like paths without a colon", () => {
174 expect(isSshRemoteUrl("https://gitlab.example.com/group/project.git")).toBe(false);
175 expect(isSshRemoteUrl("/home/user/repos/project")).toBe(false);
176 expect(isSshRemoteUrl("")).toBe(false);
177 expect(isSshRemoteUrl("deploy@github.com/project/repo")).toBe(false);
178 });
179});
180
181it("names an Azure DevOps repository by its own name, not its project path", () => {
182 // `az repos pr list --repository` takes a name and detects the organisation and project from
183 // the checkout; the recorded `org/project/_git/repo` path is refused, and the repository then
184 // reads as unavailable on the page.
185 const selector = sourceControlRepositorySelector({
186 provider: "azure-devops",
187 displayName: "contoso/payments/_git/checkout",
188 owner: "contoso",
189 name: "checkout",
190 });
191 expect(selector).toBe("checkout");
192});
193
194it("falls back to the path's last segment where an Azure identity has no name", () => {
195 const selector = sourceControlRepositorySelector({
196 provider: "azure-devops",
197 displayName: "contoso/payments/_git/checkout",
198 });
199 expect(selector).toBe("checkout");
200});
201
202it("keeps a GitLab identity's whole path, because a nested group is part of the name", () => {
203 const selector = sourceControlRepositorySelector({
204 provider: "gitlab",
205 displayName: "group/subgroup/service",
206 owner: "group",
207 name: "service",
208 });
209 expect(selector).toBe("group/subgroup/service");
210});