Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { EnvironmentId, ProjectId } from "@t3tools/contracts";
4
5import {
6 findExistingAddProject,
7 getCloneDestinationPath,
8 getCloneDirectoryLeafName,
9 getCloneDirectoryName,
10 getCloneDirectoryPath,
11 getDefaultCloneUrl,
12 githubIncompleteOwnerRepositoryMessage,
13 normalizePastedCloneUrl,
14} from "./projects.ts";
15import type { EnvironmentProject } from "../state/models.ts";
16
17describe("add project shared logic", () => {
18 it("asks for a repository name when GitHub input is only an owner path", () => {
19 const github = SourceControlProviderKind.make("github");
20 expect(githubIncompleteOwnerRepositoryMessage(github, "pingdotgg/")).toBe(
21 "Enter the full owner/repository name.",
22 );
23 expect(githubIncompleteOwnerRepositoryMessage(github, " pingdotgg// ")).toBe(
24 "Enter the full owner/repository name.",
25 );
26 expect(githubIncompleteOwnerRepositoryMessage(github, "mona-cat_octo/")).toBe(
27 "Enter the full owner/repository name.",
28 );
29
30 for (const input of [
31 "pingdotgg/t3code",
32 "mona-cat_octo/repo",
33 "pingdotgg",
34 "https://github.com/pingdotgg/t3code.git",
35 "git@github.com:pingdotgg/t3code.git",
36 ]) {
37 expect(githubIncompleteOwnerRepositoryMessage(github, input)).toBeNull();
38 }
39 expect(
40 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("gitlab"), "group/"),
41 ).toBeNull();
42 expect(githubIncompleteOwnerRepositoryMessage("url", "owner/")).toBeNull();
43 expect(
44 githubIncompleteOwnerRepositoryMessage(SourceControlProviderKind.make("forgejo"), "owner/"),
45 ).toBeNull();
46 });
47
48 it("routes owner/repository shorthand to GitHub over HTTPS", () => {
49 expect(normalizePastedCloneUrl("imputnet/helium")).toBe(
50 "https://github.com/imputnet/helium.git",
51 );
52 expect(normalizePastedCloneUrl(" pingdotgg/t3code ")).toBe(
53 "https://github.com/pingdotgg/t3code.git",
54 );
55 });
56
57 it("keeps explicit clone URLs and local paths unchanged", () => {
58 expect(normalizePastedCloneUrl("https://gitlab.com/group/project.git")).toBe(
59 "https://gitlab.com/group/project.git",
60 );
61 expect(normalizePastedCloneUrl("git@github.com:owner/repo.git")).toBe(
62 "git@github.com:owner/repo.git",
63 );
64 expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
65 expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
66 });
67
68 it("uses HTTPS for repositories selected through a provider", () => {
69 expect(
70 getDefaultCloneUrl({
71 provider: SourceControlProviderKind.make("github"),
72 url: "https://github.com/imputnet/helium",
73 sshUrl: "git@github.com:imputnet/helium.git",
74 }),
75 ).toBe("https://github.com/imputnet/helium");
76 expect(
77 getDefaultCloneUrl({
78 provider: SourceControlProviderKind.make("forgejo"),
79 url: "https://forgejo.example.test:8443/owner/repo.git",
80 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
81 }),
82 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
83 });
84
85 it("derives the clone folder name from any pasted clone URL", () => {
86 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
87 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
88 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
89 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
90 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
91 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
92 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
93 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
94 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
95 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
96 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
97 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
98 });
99
100 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
101 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
102 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
103 "github.com/owner/repo",
104 );
105 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
106 "gitlab.com/group/sub/repo",
107 );
108 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
109 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
110 expect(
111 getCloneDirectoryPath(
112 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
113 false,
114 "Project/My Repo",
115 ),
116 ).toBe("My Repo");
117 expect(
118 getCloneDirectoryLeafName(
119 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
120 true,
121 "Project/My Repo",
122 ),
123 ).toBe("My%20Repo");
124 expect(
125 getCloneDestinationPath(
126 "C:\\Users\\me\\ghq",
127 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
128 ),
129 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
130 });
131
132 it("keeps a numeric repository name that sits on a path", () => {
133 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
134 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
135 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
136 });
137
138 it("finds existing projects by normalized path in the target environment", () => {
139 const env = EnvironmentId.make("env");
140 const other = EnvironmentId.make("other");
141 const projects: EnvironmentProject[] = [
142 {
143 environmentId: other,
144 id: ProjectId.make("same-path-other-env"),
145 title: "Other",
146 workspaceRoot: "/repo",
147 createdAt: "2026-01-01T00:00:00.000Z",
148 updatedAt: "2026-01-01T00:00:00.000Z",
149 repositoryIdentity: null,
150 defaultModelSelection: null,
151 scripts: [],
152 },
153 {
154 environmentId: env,
155 id: ProjectId.make("project"),
156 title: "Repo",
157 workspaceRoot: "/repo/",
158 createdAt: "2026-01-01T00:00:00.000Z",
159 updatedAt: "2026-01-01T00:00:00.000Z",
160 repositoryIdentity: null,
161 defaultModelSelection: null,
162 scripts: [],
163 },
164 ];
165
166 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
167 "project",
168 );
169 });
170});