Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 EnvironmentId,
4 ProjectId,
5 CommandId,
6 SourceControlDiscoveryResult,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9
10import {
11 buildAddProjectRemoteSourceReadiness,
12 buildProjectCreateCommand,
13 canCreateProjectInEnvironment,
14 findExistingAddProject,
15 getAddProjectInitialQuery,
16 getCloneDestinationBrowsePath,
17 getCloneDestinationPath,
18 getCloneDirectoryName,
19 getDefaultCloneUrl,
20 normalizePastedCloneUrl,
21 resolveAddProjectPath,
22 sortAddProjectProviderSources,
23} from "./projects.ts";
24import type { EnvironmentProject } from "../state/models.ts";
25
26describe("add project shared logic", () => {
27 it("only allows project creation in connected environments", () => {
28 expect(canCreateProjectInEnvironment("connected")).toBe(true);
29 expect(canCreateProjectInEnvironment("available")).toBe(false);
30 expect(canCreateProjectInEnvironment("offline")).toBe(false);
31 expect(canCreateProjectInEnvironment("connecting")).toBe(false);
32 expect(canCreateProjectInEnvironment("reconnecting")).toBe(false);
33 expect(canCreateProjectInEnvironment("error")).toBe(false);
34 });
35
36 it("resolves initial browse paths from settings", () => {
37 expect(getAddProjectInitialQuery("")).toBe("~/");
38 expect(getAddProjectInitialQuery("/work")).toBe("/work/");
39 expect(getAddProjectInitialQuery("C:\\work")).toBe("C:\\work\\");
40 });
41
42 it("derives the clone folder name from the repository name with owner", () => {
43 expect(getCloneDirectoryName("owner/repo")).toBe("repo");
44 expect(getCloneDirectoryName("org/project/repo")).toBe("repo");
45 expect(getCloneDirectoryName("repo")).toBe("repo");
46 expect(getCloneDirectoryName("owner/repo/")).toBe("repo");
47 expect(getCloneDirectoryName("")).toBe("");
48 expect(getCloneDirectoryName(null)).toBe("");
49 });
50
51 it("routes owner/repository shorthand to GitHub over HTTPS", () => {
52 expect(normalizePastedCloneUrl("imputnet/helium")).toBe(
53 "https://github.com/imputnet/helium.git",
54 );
55 expect(normalizePastedCloneUrl(" pingdotgg/t3code ")).toBe(
56 "https://github.com/pingdotgg/t3code.git",
57 );
58 });
59
60 it("keeps explicit clone URLs and local paths unchanged", () => {
61 expect(normalizePastedCloneUrl("https://gitlab.com/group/project.git")).toBe(
62 "https://gitlab.com/group/project.git",
63 );
64 expect(normalizePastedCloneUrl("git@github.com:owner/repo.git")).toBe(
65 "git@github.com:owner/repo.git",
66 );
67 expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
68 expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
69 });
70
71 it("uses HTTPS for repositories selected through a provider", () => {
72 expect(
73 getDefaultCloneUrl({
74 provider: "github",
75 url: "https://github.com/imputnet/helium",
76 sshUrl: "git@github.com:imputnet/helium.git",
77 }),
78 ).toBe("https://github.com/imputnet/helium");
79 });
80
81 it("preserves existing clone transport behavior for other providers", () => {
82 expect(
83 getDefaultCloneUrl({
84 provider: "gitlab",
85 url: "https://gitlab.com/group/project.git",
86 sshUrl: "git@gitlab.com:group/project.git",
87 }),
88 ).toBe("git@gitlab.com:group/project.git");
89 });
90
91 it("derives the clone folder name from any pasted clone URL", () => {
92 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
93 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
94 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
95 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
96 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
97 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
98 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
99 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
100 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
101 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
102 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
103 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
104 });
105
106 it("keeps a numeric repository name that sits on a path", () => {
107 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
108 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
109 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
110 });
111
112 it("proposes no clone folder for a link that names no repository", () => {
113 expect(getCloneDirectoryName("https://github.com/")).toBe("");
114 expect(getCloneDirectoryName("https://github.com")).toBe("");
115 expect(getCloneDirectoryName("git@github.com:")).toBe("");
116 expect(getCloneDirectoryName("ssh://git@github.com:22")).toBe("");
117 expect(getCloneDirectoryName("https://")).toBe("");
118 });
119
120 it("proposes the clone destination inside the selected directory", () => {
121 expect(getCloneDestinationPath("~/Projects/", "repo")).toBe("~/Projects/repo");
122 expect(getCloneDestinationPath("~/Projects", "repo")).toBe("~/Projects/repo");
123 expect(getCloneDestinationPath("C:\\work\\", "repo")).toBe("C:\\work\\repo");
124 expect(getCloneDestinationPath("~/Projects/", null)).toBe("~/Projects/");
125 expect(getCloneDestinationPath("~/Projects/", "")).toBe("~/Projects/");
126 });
127
128 it("keeps pinned clone destinations anchored to the browsed directory", () => {
129 expect(
130 getCloneDestinationBrowsePath({
131 browseDirectoryPath: "~/Projects/",
132 selectedDirectoryName: "work",
133 cloneDirectoryName: "repo",
134 caseSensitive: true,
135 }),
136 ).toBe("~/Projects/work/repo");
137 expect(
138 getCloneDestinationBrowsePath({
139 browseDirectoryPath: "~/Projects/",
140 selectedDirectoryName: "repo",
141 cloneDirectoryName: "repo",
142 caseSensitive: true,
143 }),
144 ).toBe("~/Projects/repo/");
145 expect(
146 getCloneDestinationBrowsePath({
147 browseDirectoryPath: "C:\\Projects\\",
148 selectedDirectoryName: "Repo",
149 cloneDirectoryName: "repo",
150 caseSensitive: false,
151 }),
152 ).toBe("C:\\Projects\\Repo\\");
153 });
154
155 it("rejects unsupported windows paths on non-windows environments", () => {
156 expect(
157 resolveAddProjectPath({
158 rawPath: "C:\\repo",
159 platform: "MacIntel",
160 currentProjectCwd: null,
161 }),
162 ).toEqual({
163 ok: false,
164 error: "Windows-style paths are only supported on Windows environments.",
165 });
166 });
167
168 it("resolves relative paths from the active project cwd", () => {
169 expect(
170 resolveAddProjectPath({
171 rawPath: "../next",
172 platform: "Linux",
173 currentProjectCwd: "/work/current",
174 }),
175 ).toEqual({ ok: true, path: "/work/next" });
176 });
177
178 it("marks authenticated source control providers as ready", () => {
179 const discovery: SourceControlDiscoveryResult = {
180 versionControlSystems: [],
181 sourceControlProviders: [
182 {
183 kind: "github",
184 label: "GitHub",
185 status: "available",
186 installHint: "Install gh",
187 version: Option.some("1.0.0"),
188 detail: Option.none(),
189 auth: {
190 status: "authenticated",
191 account: Option.some("octo"),
192 host: Option.some("github.com"),
193 detail: Option.none(),
194 },
195 },
196 {
197 kind: "gitlab",
198 label: "GitLab",
199 status: "available",
200 installHint: "Install glab",
201 version: Option.some("1.0.0"),
202 detail: Option.none(),
203 auth: {
204 status: "unauthenticated",
205 account: Option.none(),
206 host: Option.none(),
207 detail: Option.some("Run glab auth login"),
208 },
209 },
210 ],
211 };
212
213 const readiness = buildAddProjectRemoteSourceReadiness(discovery);
214 expect(readiness.url.ready).toBe(true);
215 expect(readiness.github.ready).toBe(true);
216 expect(readiness.gitlab).toEqual({ ready: false, hint: "Run glab auth login" });
217 expect(sortAddProjectProviderSources(readiness)[0]).toBe("github");
218 });
219
220 it("finds existing projects by normalized path in the target environment", () => {
221 const env = EnvironmentId.make("env");
222 const other = EnvironmentId.make("other");
223 const projects: EnvironmentProject[] = [
224 {
225 environmentId: other,
226 id: ProjectId.make("same-path-other-env"),
227 title: "Other",
228 workspaceRoot: "/repo",
229 createdAt: "2026-01-01T00:00:00.000Z",
230 updatedAt: "2026-01-01T00:00:00.000Z",
231 repositoryIdentity: null,
232 defaultModelSelection: null,
233 scripts: [],
234 },
235 {
236 environmentId: env,
237 id: ProjectId.make("project"),
238 title: "Repo",
239 workspaceRoot: "/repo/",
240 createdAt: "2026-01-01T00:00:00.000Z",
241 updatedAt: "2026-01-01T00:00:00.000Z",
242 repositoryIdentity: null,
243 defaultModelSelection: null,
244 scripts: [],
245 },
246 ];
247
248 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
249 "project",
250 );
251 });
252
253 it("builds the existing project.create command shape", () => {
254 expect(
255 buildProjectCreateCommand({
256 commandId: CommandId.make("command"),
257 projectId: ProjectId.make("project"),
258 workspaceRoot: "/work/repo",
259 createdAt: "2026-01-01T00:00:00.000Z",
260 }),
261 ).toMatchObject({
262 type: "project.create",
263 commandId: "command",
264 projectId: "project",
265 title: "repo",
266 workspaceRoot: "/work/repo",
267 createWorkspaceRootIfMissing: true,
268 defaultModelSelection: null,
269 });
270 });
271});