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 getDefaultCloneUrl,
13 githubIncompleteOwnerRepositoryMessage,
14 normalizePastedCloneUrl,
15} from "./projects.ts";
16import type { EnvironmentProject } from "../state/models.ts";
17
18describe("add project shared logic", () => {
19 it("asks for a repository name when GitHub input is only an owner path", () => {
20 const github = SourceControlProviderKind.make("github");
21 expect(githubIncompleteOwnerRepositoryMessage(github, "pingdotgg/")).toBe(
22 "Enter the full owner/repository name.",
23 );
24 expect(githubIncompleteOwnerRepositoryMessage(github, " pingdotgg// ")).toBe(
25 "Enter the full owner/repository name.",
26 );
27 expect(githubIncompleteOwnerRepositoryMessage(github, "mona-cat_octo/")).toBe(
28 "Enter the full owner/repository name.",
29 );
30
31 for (const input of [
32 "pingdotgg/t3code",
33 "mona-cat_octo/repo",
34 "pingdotgg",
35 "https://github.com/pingdotgg/t3code.git",
36 "git@github.com:pingdotgg/t3code.git",
37 ]) {
38 expect(githubIncompleteOwnerRepositoryMessage(github, input)).toBeNull();
39 }
40 expect(
41 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("gitlab"), "group/"),
42 ).toBeNull();
43 expect(githubIncompleteOwnerRepositoryMessage("url", "owner/")).toBeNull();
44 expect(
45 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("forgejo"), "owner/"),
46 ).toBeNull();
47 });
48
49 it("routes owner/repository shorthand to GitHub over HTTPS", () => {
50 expect(normalizePastedCloneUrl("imputnet/helium")).toBe(
51 "https://github.com/imputnet/helium.git",
52 );
53 expect(normalizePastedCloneUrl(" pingdotgg/t3code ")).toBe(
54 "https://github.com/pingdotgg/t3code.git",
55 );
56 });
57
58 it("keeps explicit clone URLs and local paths unchanged", () => {
59 expect(normalizePastedCloneUrl("https://gitlab.com/group/project.git")).toBe(
60 "https://gitlab.com/group/project.git",
61 );
62 expect(normalizePastedCloneUrl("git@github.com:owner/repo.git")).toBe(
63 "git@github.com:owner/repo.git",
64 );
65 expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
66 expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
67 for (const gitlabProject of [
68 "https://gitlab.com/acme/tools/src/branch/worker.git",
69 "https://scm.example.test/acme/tools/src/branch/worker.git",
70 "https://scm.example.test/acme/tools/src/branch/worker",
71 "https://scm.example.test/team/repo/src/branch/main/-/README.md",
72 "https://forgejo.example.test/a/b/src/branch/mount/team/repo.git",
73 ]) {
74 expect(normalizePastedCloneUrl(gitlabProject)).toBe(gitlabProject);
75 }
76 });
77
78 it("clones the repository when given a page URL copied from the browser", () => {
79 for (const [pageUrl, pageRoute, repository] of [
80 ["https://github.com/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
81 ["https://gitlab.com/gitlab-org/gitlab-runner/-/tree/main", "/-/", "gitlab-runner"],
82 ["https://git.cafe/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
83 ["https://gitlab.example.test/g/p/-/blob/main/src/branch/x", "/-/", "p"],
84 ["https://code.company.test/g/p/-/tree/main", "/-/", "p"],
85 ["https://github.com/pingdotgg/t3code/tree/release.git", "/tree/", "t3code"],
86 ] as const) {
87 const cloneUrl = normalizePastedCloneUrl(pageUrl);
88 expect(cloneUrl).not.toContain(pageRoute);
89 expect(getCloneDirectoryName(cloneUrl)).toBe(repository);
90 }
91 expect(
92 normalizePastedCloneUrl("https://alice:example@bitbucket.org/workspace/repo/src/master/"),
93 ).toBe("https://alice:example@bitbucket.org/workspace/repo");
94 });
95
96 it("uses HTTPS for repositories selected through a provider", () => {
97 expect(
98 getDefaultCloneUrl({
99 provider: SourceControlProviderKind.make("github"),
100 url: "https://github.com/imputnet/helium",
101 sshUrl: "git@github.com:imputnet/helium.git",
102 }),
103 ).toBe("https://github.com/imputnet/helium");
104 expect(
105 getDefaultCloneUrl({
106 provider: SourceControlProviderKind.make("forgejo"),
107 url: "https://forgejo.example.test:8443/owner/repo.git",
108 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
109 }),
110 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
111 });
112
113 it("derives the clone folder name from any pasted clone URL", () => {
114 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
115 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
116 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
117 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
118 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
119 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
120 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
121 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
122 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
123 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
124 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
125 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
126 });
127
128 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
129 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
130 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
131 "github.com/owner/repo",
132 );
133 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
134 "gitlab.com/group/sub/repo",
135 );
136 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
137 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
138 expect(
139 getCloneDirectoryPath(
140 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
141 false,
142 "Project/My Repo",
143 ),
144 ).toBe("My Repo");
145 expect(
146 getCloneDirectoryLeafName(
147 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
148 true,
149 "Project/My Repo",
150 ),
151 ).toBe("My%20Repo");
152 expect(
153 getCloneDestinationPath(
154 "C:\\Users\\me\\ghq",
155 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
156 ),
157 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
158 });
159
160 it("keeps a numeric repository name that sits on a path", () => {
161 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
162 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
163 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
164 });
165
166 it("resolves a native-picker folder like an in-app clone selection", () => {
167 const pick = (pickedPath: string, caseSensitive = true) =>
168 getCloneDestinationForPickedFolder({
169 pickedPath,
170 cloneDirectoryName: "repo",
171 caseSensitive,
172 });
173
174 expect(pick("/Users/me/code")).toBe("/Users/me/code/repo");
175 expect(pick("/Users/me/code/")).toBe("/Users/me/code/repo");
176 expect(pick("/Users/me/repo")).toBe("/Users/me/repo/");
177 expect(pick("/Users/me/Repo")).toBe("/Users/me/Repo/repo");
178 expect(pick("C:\\Projects\\Repo", false)).toBe("C:\\Projects\\Repo\\");
179 expect(pick("C:\\Projects", false)).toBe("C:\\Projects\\repo");
180 expect(pick("/")).toBe("/repo");
181 });
182
183 it("finds existing projects by normalized path in the target environment", () => {
184 const env = EnvironmentId.make("env");
185 const other = EnvironmentId.make("other");
186 const projects: EnvironmentProject[] = [
187 {
188 environmentId: other,
189 id: ProjectId.make("same-path-other-env"),
190 title: "Other",
191 workspaceRoot: "/repo",
192 createdAt: "2026-01-01T00:00:00.000Z",
193 updatedAt: "2026-01-01T00:00:00.000Z",
194 repositoryIdentity: null,
195 defaultModelSelection: null,
196 scripts: [],
197 },
198 {
199 environmentId: env,
200 id: ProjectId.make("project"),
201 title: "Repo",
202 workspaceRoot: "/repo/",
203 createdAt: "2026-01-01T00:00:00.000Z",
204 updatedAt: "2026-01-01T00:00:00.000Z",
205 repositoryIdentity: null,
206 defaultModelSelection: null,
207 scripts: [],
208 },
209 ];
210
211 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
212 "project",
213 );
214 });
215});