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

projects.test.ts

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