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