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

projects.test.ts

242 lines10.3 KB

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("uses HTTPS for repositories selected through a provider", () => {
117 expect(
118 getDefaultCloneUrl({
119 provider: SourceControlProviderKind.make("github"),
120 url: "https://github.com/imputnet/helium",
121 sshUrl: "git@github.com:imputnet/helium.git",
122 }),
123 ).toBe("https://github.com/imputnet/helium");
124 expect(
125 getDefaultCloneUrl({
126 provider: SourceControlProviderKind.make("forgejo"),
127 url: "https://forgejo.example.test:8443/owner/repo.git",
128 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
129 }),
130 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
131 });
132​
133 it("derives the clone folder name from any pasted clone URL", () => {
134 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
135 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
136 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
137 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
138 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
139 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
140 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
141 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
142 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
143 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
144 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
145 expect(getCloneDirectoryName("git@forge.example:team/spec#2026.git")).toBe("spec#2026");
146 expect(getCloneDirectoryName("forge.example:team#1/spec.git")).toBe("spec");
147 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
148 expect(getCloneDirectoryName("/srv/git/firmware#2.git")).toBe("firmware#2");
149 expect(getCloneDirectoryName("~/git/what?.git")).toBe("what?");
150 expect(getCloneDirectoryName("../firmware#2.git")).toBe("firmware#2");
151 expect(getCloneDirectoryName("C:\\src\\firmware#2.git")).toBe("firmware#2");
152 expect(getCloneDirectoryName("github.com:owner/repo.git?ref=main")).toBe("repo");
153 });
154​
155 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
156 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
157 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
158 "github.com/owner/repo",
159 );
160 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
161 "gitlab.com/group/sub/repo",
162 );
163 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
164 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
165 expect(
166 getCloneDirectoryPath(
167 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
168 false,
169 "Project/My Repo",
170 ),
171 ).toBe("My Repo");
172 expect(
173 getCloneDirectoryLeafName(
174 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
175 true,
176 "Project/My Repo",
177 ),
178 ).toBe("My%20Repo");
179 expect(
180 getCloneDestinationPath(
181 "C:\\Users\\me\\ghq",
182 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
183 ),
184 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
185 });
186​
187 it("keeps a numeric repository name that sits on a path", () => {
188 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
189 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
190 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
191 });
192​
193 it("resolves a native-picker folder like an in-app clone selection", () => {
194 const pick = (pickedPath: string, caseSensitive = true) =>
195 getCloneDestinationForPickedFolder({
196 pickedPath,
197 cloneDirectoryName: "repo",
198 caseSensitive,
199 });
200​
201 expect(pick("/Users/me/code")).toBe("/Users/me/code/repo");
202 expect(pick("/Users/me/code/")).toBe("/Users/me/code/repo");
203 expect(pick("/Users/me/repo")).toBe("/Users/me/repo/");
204 expect(pick("/Users/me/Repo")).toBe("/Users/me/Repo/repo");
205 expect(pick("C:\\Projects\\Repo", false)).toBe("C:\\Projects\\Repo\\");
206 expect(pick("C:\\Projects", false)).toBe("C:\\Projects\\repo");
207 expect(pick("/")).toBe("/repo");
208 });
209​
210 it("finds existing projects by normalized path in the target environment", () => {
211 const env = EnvironmentId.make("env");
212 const other = EnvironmentId.make("other");
213 const projects: EnvironmentProject[] = [
214 {
215 environmentId: other,
216 id: ProjectId.make("same-path-other-env"),
217 title: "Other",
218 workspaceRoot: "/repo",
219 createdAt: "2026-01-01T00:00:00.000Z",
220 updatedAt: "2026-01-01T00:00:00.000Z",
221 repositoryIdentity: null,
222 defaultModelSelection: null,
223 scripts: [],
224 },
225 {
226 environmentId: env,
227 id: ProjectId.make("project"),
228 title: "Repo",
229 workspaceRoot: "/repo/",
230 createdAt: "2026-01-01T00:00:00.000Z",
231 updatedAt: "2026-01-01T00:00:00.000Z",
232 repositoryIdentity: null,
233 defaultModelSelection: null,
234 scripts: [],
235 },
236 ];
237​
238 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
239 "project",
240 );
241 });
242});