The best way to control Claude Code, Codex and more from any computer or phone

main
ts 235 lines 9.74 KB
Raw
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3 import { EnvironmentId, ProjectId } from "@t3tools/contracts";
4
5 import {
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";
18 import type { EnvironmentProject } from "../state/models.ts";
19 import { isFilesystemBrowseQuery } from "../state/projects.ts";
20
21 describe("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(" https://github.com/owner/repo.git ")).toBe("repo");
146 });
147
148 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
149 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
150 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
151 "github.com/owner/repo",
152 );
153 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
154 "gitlab.com/group/sub/repo",
155 );
156 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
157 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
158 expect(
159 getCloneDirectoryPath(
160 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
161 false,
162 "Project/My Repo",
163 ),
164 ).toBe("My Repo");
165 expect(
166 getCloneDirectoryLeafName(
167 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
168 true,
169 "Project/My Repo",
170 ),
171 ).toBe("My%20Repo");
172 expect(
173 getCloneDestinationPath(
174 "C:\\Users\\me\\ghq",
175 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
176 ),
177 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
178 });
179
180 it("keeps a numeric repository name that sits on a path", () => {
181 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
182 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
183 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
184 });
185
186 it("resolves a native-picker folder like an in-app clone selection", () => {
187 const pick = (pickedPath: string, caseSensitive = true) =>
188 getCloneDestinationForPickedFolder({
189 pickedPath,
190 cloneDirectoryName: "repo",
191 caseSensitive,
192 });
193
194 expect(pick("/Users/me/code")).toBe("/Users/me/code/repo");
195 expect(pick("/Users/me/code/")).toBe("/Users/me/code/repo");
196 expect(pick("/Users/me/repo")).toBe("/Users/me/repo/");
197 expect(pick("/Users/me/Repo")).toBe("/Users/me/Repo/repo");
198 expect(pick("C:\\Projects\\Repo", false)).toBe("C:\\Projects\\Repo\\");
199 expect(pick("C:\\Projects", false)).toBe("C:\\Projects\\repo");
200 expect(pick("/")).toBe("/repo");
201 });
202
203 it("finds existing projects by normalized path in the target environment", () => {
204 const env = EnvironmentId.make("env");
205 const other = EnvironmentId.make("other");
206 const projects: EnvironmentProject[] = [
207 {
208 environmentId: other,
209 id: ProjectId.make("same-path-other-env"),
210 title: "Other",
211 workspaceRoot: "/repo",
212 createdAt: "2026-01-01T00:00:00.000Z",
213 updatedAt: "2026-01-01T00:00:00.000Z",
214 repositoryIdentity: null,
215 defaultModelSelection: null,
216 scripts: [],
217 },
218 {
219 environmentId: env,
220 id: ProjectId.make("project"),
221 title: "Repo",
222 workspaceRoot: "/repo/",
223 createdAt: "2026-01-01T00:00:00.000Z",
224 updatedAt: "2026-01-01T00:00:00.000Z",
225 repositoryIdentity: null,
226 defaultModelSelection: null,
227 scripts: [],
228 },
229 ];
230
231 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
232 "project",
233 );
234 });
235 });