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 getCloneDirectoryName,
8 getDefaultCloneUrl,
9 normalizePastedCloneUrl,
10} from "./projects.ts";
11import type { EnvironmentProject } from "../state/models.ts";
12
13describe("add project shared logic", () => {
14 it("routes owner/repository shorthand to GitHub over HTTPS", () => {
15 expect(normalizePastedCloneUrl("imputnet/helium")).toBe(
16 "https://github.com/imputnet/helium.git",
17 );
18 expect(normalizePastedCloneUrl(" pingdotgg/t3code ")).toBe(
19 "https://github.com/pingdotgg/t3code.git",
20 );
21 });
22
23 it("keeps explicit clone URLs and local paths unchanged", () => {
24 expect(normalizePastedCloneUrl("https://gitlab.com/group/project.git")).toBe(
25 "https://gitlab.com/group/project.git",
26 );
27 expect(normalizePastedCloneUrl("git@github.com:owner/repo.git")).toBe(
28 "git@github.com:owner/repo.git",
29 );
30 expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
31 expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
32 });
33
34 it("uses HTTPS for repositories selected through a provider", () => {
35 expect(
36 getDefaultCloneUrl({
37 provider: SourceControlProviderKind.make("github"),
38 url: "https://github.com/imputnet/helium",
39 sshUrl: "git@github.com:imputnet/helium.git",
40 }),
41 ).toBe("https://github.com/imputnet/helium");
42 expect(
43 getDefaultCloneUrl({
44 provider: SourceControlProviderKind.make("forgejo"),
45 url: "https://forgejo.example.test:8443/owner/repo.git",
46 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
47 }),
48 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
49 });
50
51 it("derives the clone folder name from any pasted clone URL", () => {
52 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
53 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
54 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
55 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
56 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
57 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
58 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
59 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
60 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
61 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
62 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
63 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
64 });
65
66 it("keeps a numeric repository name that sits on a path", () => {
67 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
68 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
69 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
70 });
71
72 it("finds existing projects by normalized path in the target environment", () => {
73 const env = EnvironmentId.make("env");
74 const other = EnvironmentId.make("other");
75 const projects: EnvironmentProject[] = [
76 {
77 environmentId: other,
78 id: ProjectId.make("same-path-other-env"),
79 title: "Other",
80 workspaceRoot: "/repo",
81 createdAt: "2026-01-01T00:00:00.000Z",
82 updatedAt: "2026-01-01T00:00:00.000Z",
83 repositoryIdentity: null,
84 defaultModelSelection: null,
85 scripts: [],
86 },
87 {
88 environmentId: env,
89 id: ProjectId.make("project"),
90 title: "Repo",
91 workspaceRoot: "/repo/",
92 createdAt: "2026-01-01T00:00:00.000Z",
93 updatedAt: "2026-01-01T00:00:00.000Z",
94 repositoryIdentity: null,
95 defaultModelSelection: null,
96 scripts: [],
97 },
98 ];
99
100 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
101 "project",
102 );
103 });
104});