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