Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { EnvironmentId, ProjectId } from "@t3tools/contracts";
4
5import {
6 findExistingAddProject,
7 getCloneDestinationForPickedFolder,
8 getCloneDestinationPath,
9 getCloneDirectoryLeafName,
10 getCloneDirectoryName,
11 getCloneDirectoryPath,
12 getAddProjectInitialQuery,
13 getDefaultCloneUrl,
14 githubIncompleteOwnerRepositoryMessage,
15 normalizePastedCloneUrl,
16 sortAddProjectProviderSources,
17} from "./projects.ts";
18import type { EnvironmentProject } from "../state/models.ts";
19import { isFilesystemBrowseQuery } from "../state/projects.ts";
20
21describe("add project shared logic", () => {
22 it("starts the Add Project picker on a browsable path for a relative base directory", () => {
23 expect(isFilesystemBrowseQuery(getAddProjectInitialQuery("Desktop/dev"))).toBe(true);
24 expect(getAddProjectInitialQuery("Desktop/dev")).toBe("~/Desktop/dev/");
25 expect(getAddProjectInitialQuery("/srv/code")).toBe("/srv/code/");
26 expect(getAddProjectInitialQuery("~")).toBe("~/");
27 expect(getAddProjectInitialQuery("./work")).toBe("./work/");
28 expect(getAddProjectInitialQuery("C:\\dev")).toBe("C:\\dev\\");
29 });
30
31 it("offers only GitHub until other hosts are turned on", () => {
32 const ready = () => ({ ready: true, hint: null });
33 expect(sortAddProjectProviderSources(ready, {})).toEqual(["github"]);
34 expect(sortAddProjectProviderSources(ready, { gitlab: true, github: false })).toEqual([
35 "gitlab",
36 ]);
37 });
38
39 it("asks for a repository name when GitHub input is only an owner path", () => {
40 const github = SourceControlProviderKind.make("github");
41 expect(githubIncompleteOwnerRepositoryMessage(github, "pingdotgg/")).toBe(
42 "Enter the full owner/repository name.",
43 );
44 expect(githubIncompleteOwnerRepositoryMessage(github, " pingdotgg// ")).toBe(
45 "Enter the full owner/repository name.",
46 );
47 expect(githubIncompleteOwnerRepositoryMessage(github, "mona-cat_octo/")).toBe(
48 "Enter the full owner/repository name.",
49 );
50
51 for (const input of [
52 "pingdotgg/t3code",
53 "mona-cat_octo/repo",
54 "pingdotgg",
55 "https://github.com/pingdotgg/t3code.git",
56 "git@github.com:pingdotgg/t3code.git",
57 ]) {
58 expect(githubIncompleteOwnerRepositoryMessage(github, input)).toBeNull();
59 }
60 expect(
61 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("gitlab"), "group/"),
62 ).toBeNull();
63 expect(githubIncompleteOwnerRepositoryMessage("url", "owner/")).toBeNull();
64 expect(
65 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("forgejo"), "owner/"),
66 ).toBeNull();
67 });
68
69 it("routes owner/repository shorthand to GitHub over HTTPS", () => {
70 expect(normalizePastedCloneUrl("imputnet/helium")).toBe(
71 "https://github.com/imputnet/helium.git",
72 );
73 expect(normalizePastedCloneUrl(" pingdotgg/t3code ")).toBe(
74 "https://github.com/pingdotgg/t3code.git",
75 );
76 });
77
78 it("keeps explicit clone URLs and local paths unchanged", () => {
79 expect(normalizePastedCloneUrl("https://gitlab.com/group/project.git")).toBe(
80 "https://gitlab.com/group/project.git",
81 );
82 expect(normalizePastedCloneUrl("git@github.com:owner/repo.git")).toBe(
83 "git@github.com:owner/repo.git",
84 );
85 expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
86 expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
87 for (const gitlabProject of [
88 "https://gitlab.com/acme/tools/src/branch/worker.git",
89 "https://scm.example.test/acme/tools/src/branch/worker.git",
90 "https://scm.example.test/acme/tools/src/branch/worker",
91 "https://scm.example.test/team/repo/src/branch/main/-/README.md",
92 "https://forgejo.example.test/a/b/src/branch/mount/team/repo.git",
93 ]) {
94 expect(normalizePastedCloneUrl(gitlabProject)).toBe(gitlabProject);
95 }
96 });
97
98 it("clones the repository when given a page URL copied from the browser", () => {
99 for (const [pageUrl, pageRoute, repository] of [
100 ["https://github.com/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
101 ["https://gitlab.com/gitlab-org/gitlab-runner/-/tree/main", "/-/", "gitlab-runner"],
102 ["https://git.cafe/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
103 ["https://gitlab.example.test/g/p/-/blob/main/src/branch/x", "/-/", "p"],
104 ["https://code.company.test/g/p/-/tree/main", "/-/", "p"],
105 ["https://github.com/pingdotgg/t3code/tree/release.git", "/tree/", "t3code"],
106 ] as const) {
107 const cloneUrl = normalizePastedCloneUrl(pageUrl);
108 expect(cloneUrl).not.toContain(pageRoute);
109 expect(getCloneDirectoryName(cloneUrl)).toBe(repository);
110 }
111 expect(
112 normalizePastedCloneUrl("https://alice:example@bitbucket.org/workspace/repo/src/master/"),
113 ).toBe("https://alice:example@bitbucket.org/workspace/repo");
114 });
115
116 it("clones a TanStack Source repository from a page URL with either owner spelling", () => {
117 for (const owner of ["@TanStack", "%40TanStack"]) {
118 for (const page of ["tree/main", "blob/main/README.md"]) {
119 expect(normalizePastedCloneUrl(`https://source.tanstack.com/${owner}/router/${page}`)).toBe(
120 "https://source.tanstack.com/@TanStack/router",
121 );
122 }
123 }
124 });
125
126 it("uses HTTPS for repositories selected through a provider", () => {
127 expect(
128 getDefaultCloneUrl({
129 provider: SourceControlProviderKind.make("github"),
130 url: "https://github.com/imputnet/helium",
131 sshUrl: "git@github.com:imputnet/helium.git",
132 }),
133 ).toBe("https://github.com/imputnet/helium");
134 expect(
135 getDefaultCloneUrl({
136 provider: SourceControlProviderKind.make("forgejo"),
137 url: "https://forgejo.example.test:8443/owner/repo.git",
138 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
139 }),
140 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
141 });
142
143 it("derives the clone folder name from any pasted clone URL", () => {
144 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
145 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
146 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
147 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
148 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
149 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
150 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
151 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
152 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
153 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
154 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
155 expect(getCloneDirectoryName("git@forge.example:team/spec#2026.git")).toBe("spec#2026");
156 expect(getCloneDirectoryName("forge.example:team#1/spec.git")).toBe("spec");
157 expect(getCloneDirectoryName("ssh://git@forge.example/team#1/spec#2026.git")).toBe("spec#2026");
158 expect(
159 getCloneDirectoryName("ssh://git@forge.example:2222/team#1/spec#2026.git?ref=main"),
160 ).toBe("spec#2026");
161 expect(getCloneDirectoryName("ssh://git@forge.example/repos#2026/..")).toBe("");
162 expect(getCloneDirectoryName("ssh://git@forge.example/repos#2026/...git")).toBe("");
163 expect(getCloneDirectoryPath("ssh://git@forge.example/repos#2026/..", true)).toBe("");
164 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
165 expect(getCloneDirectoryName("/srv/git/firmware#2.git")).toBe("firmware#2");
166 expect(getCloneDirectoryName("~/git/what?.git")).toBe("what?");
167 expect(getCloneDirectoryName("../firmware#2.git")).toBe("firmware#2");
168 expect(getCloneDirectoryName("C:\\src\\firmware#2.git")).toBe("firmware#2");
169 expect(getCloneDirectoryName("github.com:owner/repo.git?ref=main")).toBe("repo");
170 });
171
172 it("keeps the clone folder for a file URL at the filesystem root", () => {
173 expect(getCloneDirectoryName("file:///repo.git")).toBe("repo");
174 expect(getCloneDirectoryName("https://github.com")).toBe("");
175 });
176
177 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
178 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
179 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
180 "github.com/owner/repo",
181 );
182 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
183 "gitlab.com/group/sub/repo",
184 );
185 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
186 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
187 expect(
188 getCloneDirectoryPath(
189 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
190 false,
191 "Project/My Repo",
192 ),
193 ).toBe("My Repo");
194 expect(
195 getCloneDirectoryLeafName(
196 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
197 true,
198 "Project/My Repo",
199 ),
200 ).toBe("My%20Repo");
201 expect(
202 getCloneDestinationPath(
203 "C:\\Users\\me\\ghq",
204 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
205 ),
206 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
207 });
208
209 it("keeps a literal ? in an SCP parent folder when naming the clone folder", () => {
210 for (const url of [
211 "git@forge.example:team?lab/repo.git",
212 "forge.example:team?lab/repo.git",
213 "DOMAIN\\user@forge.example:team?lab/repo.git",
214 "git@forge.example:team?region=us/repo.git",
215 "git@forge.example:team?region=us/repo.git?ref=feature/main",
216 "git@forge.example:group/team?region=us/repo",
217 "git@forge.example:org/team?region=us/repo?ref=feature/main",
218 ]) {
219 expect(getCloneDirectoryPath(url, false)).toBe("repo");
220 expect(getCloneDirectoryPath(url, true)).toBe("repo");
221 }
222 for (const [url, path] of [
223 ["git@forge.example:team/what?.git", "forge.example/team/what"],
224 ["git@forge.example:team/what?.git/", "forge.example/team/what"],
225 ["forge.example:team/repo.git?ref=feature/main", "forge.example/team/repo"],
226 ["forge.example:team/repo.git?ref=feature/topic.git", "forge.example/team/repo"],
227 ["forge.example:team/repo.git?custom=feature/topic.git", "forge.example/team/repo"],
228 ["forge.example:team/repo?ref=feature/topic.git", "forge.example/team/repo"],
229 ["forge.example:team/repo?ref=feature/main", "forge.example/team/repo"],
230 ["forge.example:team/repo/?custom=feature/main", "forge.example/team/repo"],
231 ["forge.example:team/repo.git/?view=feature/topic.git", "forge.example/team/repo"],
232 ] as const) {
233 expect(getCloneDirectoryPath(url, false)).toBe(path.split("/").at(-1));
234 expect(getCloneDirectoryPath(url, true)).toBe(path);
235 }
236 });
237
238 it("keeps a numeric repository name that sits on a path", () => {
239 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
240 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
241 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
242 });
243
244 it("resolves a native-picker folder like an in-app clone selection", () => {
245 const pick = (pickedPath: string, caseSensitive = true) =>
246 getCloneDestinationForPickedFolder({
247 pickedPath,
248 cloneDirectoryName: "repo",
249 caseSensitive,
250 });
251
252 expect(pick("/Users/me/code")).toBe("/Users/me/code/repo");
253 expect(pick("/Users/me/code/")).toBe("/Users/me/code/repo");
254 expect(pick("/Users/me/repo")).toBe("/Users/me/repo/");
255 expect(pick("/Users/me/Repo")).toBe("/Users/me/Repo/repo");
256 expect(pick("C:\\Projects\\Repo", false)).toBe("C:\\Projects\\Repo\\");
257 expect(pick("C:\\Projects", false)).toBe("C:\\Projects\\repo");
258 expect(pick("/")).toBe("/repo");
259 });
260
261 it("finds existing projects by normalized path in the target environment", () => {
262 const env = EnvironmentId.make("env");
263 const other = EnvironmentId.make("other");
264 const projects: EnvironmentProject[] = [
265 {
266 environmentId: other,
267 id: ProjectId.make("same-path-other-env"),
268 title: "Other",
269 workspaceRoot: "/repo",
270 createdAt: "2026-01-01T00:00:00.000Z",
271 updatedAt: "2026-01-01T00:00:00.000Z",
272 repositoryIdentity: null,
273 defaultModelSelection: null,
274 scripts: [],
275 },
276 {
277 environmentId: env,
278 id: ProjectId.make("project"),
279 title: "Repo",
280 workspaceRoot: "/repo/",
281 createdAt: "2026-01-01T00:00:00.000Z",
282 updatedAt: "2026-01-01T00:00:00.000Z",
283 repositoryIdentity: null,
284 defaultModelSelection: null,
285 scripts: [],
286 },
287 ];
288
289 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
290 "project",
291 );
292 });
293});