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

projects.test.ts

277 lines10.0 KB

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 expect(
80 getDefaultCloneUrl({
81 provider: "forgejo",
82 url: "https://forgejo.example.test:8443/owner/repo.git",
83 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
84 }),
85 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
86 });
87​
88 it("preserves existing clone transport behavior for other providers", () => {
89 expect(
90 getDefaultCloneUrl({
91 provider: "gitlab",
92 url: "https://gitlab.com/group/project.git",
93 sshUrl: "git@gitlab.com:group/project.git",
94 }),
95 ).toBe("git@gitlab.com:group/project.git");
96 });
97​
98 it("derives the clone folder name from any pasted clone URL", () => {
99 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
100 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
101 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
102 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
103 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
104 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
105 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
106 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
107 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
108 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
109 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
110 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
111 });
112​
113 it("keeps a numeric repository name that sits on a path", () => {
114 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
115 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
116 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
117 });
118​
119 it("proposes no clone folder for a link that names no repository", () => {
120 expect(getCloneDirectoryName("https://github.com/")).toBe("");
121 expect(getCloneDirectoryName("https://github.com")).toBe("");
122 expect(getCloneDirectoryName("git@github.com:")).toBe("");
123 expect(getCloneDirectoryName("ssh://git@github.com:22")).toBe("");
124 expect(getCloneDirectoryName("https://")).toBe("");
125 });
126​
127 it("proposes the clone destination inside the selected directory", () => {
128 expect(getCloneDestinationPath("~/Projects/", "repo")).toBe("~/Projects/repo");
129 expect(getCloneDestinationPath("~/Projects", "repo")).toBe("~/Projects/repo");
130 expect(getCloneDestinationPath("C:\\work\\", "repo")).toBe("C:\\work\\repo");
131 expect(getCloneDestinationPath("~/Projects/", null)).toBe("~/Projects/");
132 expect(getCloneDestinationPath("~/Projects/", "")).toBe("~/Projects/");
133 });
134​
135 it("keeps pinned clone destinations anchored to the browsed directory", () => {
136 expect(
137 getCloneDestinationBrowsePath({
138 browseDirectoryPath: "~/Projects/",
139 selectedDirectoryName: "work",
140 cloneDirectoryName: "repo",
141 caseSensitive: true,
142 }),
143 ).toBe("~/Projects/work/repo");
144 expect(
145 getCloneDestinationBrowsePath({
146 browseDirectoryPath: "~/Projects/",
147 selectedDirectoryName: "repo",
148 cloneDirectoryName: "repo",
149 caseSensitive: true,
150 }),
151 ).toBe("~/Projects/repo/");
152 expect(
153 getCloneDestinationBrowsePath({
154 browseDirectoryPath: "C:\\Projects\\",
155 selectedDirectoryName: "Repo",
156 cloneDirectoryName: "repo",
157 caseSensitive: false,
158 }),
159 ).toBe("C:\\Projects\\Repo\\");
160 });
161​
162 it("rejects unsupported windows paths on non-windows environments", () => {
163 expect(
164 resolveAddProjectPath({
165 rawPath: "C:\\repo",
166 platform: "MacIntel",
167 currentProjectCwd: null,
168 }),
169 ).toEqual({
170 ok: false,
171 error: "Windows-style paths are only supported on Windows environments.",
172 });
173 });
174​
175 it("resolves relative paths from the active project cwd", () => {
176 expect(
177 resolveAddProjectPath({
178 rawPath: "../next",
179 platform: "Linux",
180 currentProjectCwd: "/work/current",
181 }),
182 ).toEqual({ ok: true, path: "/work/next" });
183 });
184​
185 it("marks authenticated source control providers as ready", () => {
186 const discovery: SourceControlDiscoveryResult = {
187 versionControlSystems: [],
188 sourceControlProviders: [
189 {
190 kind: "github",
191 label: "GitHub",
192 status: "available",
193 installHint: "Install gh",
194 version: Option.some("1.0.0"),
195 detail: Option.none(),
196 auth: {
197 status: "authenticated",
198 account: Option.some("octo"),
199 host: Option.some("github.com"),
200 detail: Option.none(),
201 },
202 },
203 {
204 kind: "gitlab",
205 label: "GitLab",
206 status: "available",
207 installHint: "Install glab",
208 version: Option.some("1.0.0"),
209 detail: Option.none(),
210 auth: {
211 status: "unauthenticated",
212 account: Option.none(),
213 host: Option.none(),
214 detail: Option.some("Run glab auth login"),
215 },
216 },
217 ],
218 };
219​
220 const readiness = buildAddProjectRemoteSourceReadiness(discovery);
221 expect(readiness.url.ready).toBe(true);
222 expect(readiness.github.ready).toBe(true);
223 expect(readiness.gitlab).toEqual({ ready: false, hint: "Run glab auth login" });
224 expect(sortAddProjectProviderSources(readiness)[0]).toBe("github");
225 });
226​
227 it("finds existing projects by normalized path in the target environment", () => {
228 const env = EnvironmentId.make("env");
229 const other = EnvironmentId.make("other");
230 const projects: EnvironmentProject[] = [
231 {
232 environmentId: other,
233 id: ProjectId.make("same-path-other-env"),
234 title: "Other",
235 workspaceRoot: "/repo",
236 createdAt: "2026-01-01T00:00:00.000Z",
237 updatedAt: "2026-01-01T00:00:00.000Z",
238 repositoryIdentity: null,
239 defaultModelSelection: null,
240 scripts: [],
241 },
242 {
243 environmentId: env,
244 id: ProjectId.make("project"),
245 title: "Repo",
246 workspaceRoot: "/repo/",
247 createdAt: "2026-01-01T00:00:00.000Z",
248 updatedAt: "2026-01-01T00:00:00.000Z",
249 repositoryIdentity: null,
250 defaultModelSelection: null,
251 scripts: [],
252 },
253 ];
254​
255 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
256 "project",
257 );
258 });
259​
260 it("builds the V2 project.create mutation", () => {
261 expect(
262 buildProjectCreateCommand({
263 commandId: CommandId.make("command"),
264 projectId: ProjectId.make("project"),
265 workspaceRoot: "/work/repo",
266 }),
267 ).toMatchObject({
268 type: "project.create",
269 commandId: "command",
270 projectId: "project",
271 title: "repo",
272 workspaceRoot: "/work/repo",
273 createWorkspaceRootIfMissing: true,
274 defaultModelSelection: null,
275 });
276 });
277});