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

projects.test.ts

197 lines8.1 KB

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 for (const gitlabProject of [
67 "https://gitlab.com/acme/tools/src/branch/worker.git",
68 "https://scm.example.test/acme/tools/src/branch/worker.git",
69 "https://scm.example.test/acme/tools/src/branch/worker",
70 "https://scm.example.test/team/repo/src/branch/main/-/README.md",
71 "https://forgejo.example.test/a/b/src/branch/mount/team/repo.git",
72 ]) {
73 expect(normalizePastedCloneUrl(gitlabProject)).toBe(gitlabProject);
74 }
75 });
76​
77 it("clones the repository when given a page URL copied from the browser", () => {
78 for (const [pageUrl, pageRoute, repository] of [
79 ["https://github.com/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
80 ["https://gitlab.com/gitlab-org/gitlab-runner/-/tree/main", "/-/", "gitlab-runner"],
81 ["https://git.cafe/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
82 ["https://gitlab.example.test/g/p/-/blob/main/src/branch/x", "/-/", "p"],
83 ["https://code.company.test/g/p/-/tree/main", "/-/", "p"],
84 ["https://github.com/pingdotgg/t3code/tree/release.git", "/tree/", "t3code"],
85 ] as const) {
86 const cloneUrl = normalizePastedCloneUrl(pageUrl);
87 expect(cloneUrl).not.toContain(pageRoute);
88 expect(getCloneDirectoryName(cloneUrl)).toBe(repository);
89 }
90 expect(
91 normalizePastedCloneUrl("https://alice:example@bitbucket.org/workspace/repo/src/master/"),
92 ).toBe("https://alice:example@bitbucket.org/workspace/repo");
93 });
94​
95 it("uses HTTPS for repositories selected through a provider", () => {
96 expect(
97 getDefaultCloneUrl({
98 provider: SourceControlProviderKind.make("github"),
99 url: "https://github.com/imputnet/helium",
100 sshUrl: "git@github.com:imputnet/helium.git",
101 }),
102 ).toBe("https://github.com/imputnet/helium");
103 expect(
104 getDefaultCloneUrl({
105 provider: SourceControlProviderKind.make("forgejo"),
106 url: "https://forgejo.example.test:8443/owner/repo.git",
107 sshUrl: "ssh://git@forgejo.example.test:2222/owner/repo.git",
108 }),
109 ).toBe("https://forgejo.example.test:8443/owner/repo.git");
110 });
111​
112 it("derives the clone folder name from any pasted clone URL", () => {
113 expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
114 expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
115 expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
116 expect(getCloneDirectoryName("git@github.com:owner/repo.git")).toBe("repo");
117 expect(getCloneDirectoryName("ssh://git@github.com:22/owner/repo.git")).toBe("repo");
118 expect(getCloneDirectoryName("https://user@bitbucket.org/owner/repo.git")).toBe("repo");
119 expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
120 expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
121 expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
122 expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
123 expect(getCloneDirectoryName("git@github.com:repo.git")).toBe("repo");
124 expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
125 });
126​
127 it("lays clone folders out like ghq only when asked, and only when the URL is safe", () => {
128 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", false)).toBe("repo");
129 expect(getCloneDirectoryPath("git@github.com:owner/repo.git", true)).toBe(
130 "github.com/owner/repo",
131 );
132 expect(getCloneDirectoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git", true)).toBe(
133 "gitlab.com/group/sub/repo",
134 );
135 expect(getCloneDirectoryPath("https://github.com/owner/../../repo", true)).toBe("repo");
136 expect(getCloneDirectoryPath("/srv/git/repo.git", true)).toBe("repo");
137 expect(
138 getCloneDirectoryPath(
139 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
140 false,
141 "Project/My Repo",
142 ),
143 ).toBe("My Repo");
144 expect(
145 getCloneDirectoryLeafName(
146 "git@ssh.dev.azure.com:v3/org/Project/My%20Repo",
147 true,
148 "Project/My Repo",
149 ),
150 ).toBe("My%20Repo");
151 expect(
152 getCloneDestinationPath(
153 "C:\\Users\\me\\ghq",
154 getCloneDirectoryPath("git@github.com:owner/repo.git", true),
155 ),
156 ).toBe("C:\\Users\\me\\ghq\\github.com\\owner\\repo");
157 });
158​
159 it("keeps a numeric repository name that sits on a path", () => {
160 expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
161 expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
162 expect(getCloneDirectoryName("git@github.com:acme/123.git")).toBe("123");
163 });
164​
165 it("finds existing projects by normalized path in the target environment", () => {
166 const env = EnvironmentId.make("env");
167 const other = EnvironmentId.make("other");
168 const projects: EnvironmentProject[] = [
169 {
170 environmentId: other,
171 id: ProjectId.make("same-path-other-env"),
172 title: "Other",
173 workspaceRoot: "/repo",
174 createdAt: "2026-01-01T00:00:00.000Z",
175 updatedAt: "2026-01-01T00:00:00.000Z",
176 repositoryIdentity: null,
177 defaultModelSelection: null,
178 scripts: [],
179 },
180 {
181 environmentId: env,
182 id: ProjectId.make("project"),
183 title: "Repo",
184 workspaceRoot: "/repo/",
185 createdAt: "2026-01-01T00:00:00.000Z",
186 updatedAt: "2026-01-01T00:00:00.000Z",
187 repositoryIdentity: null,
188 defaultModelSelection: null,
189 scripts: [],
190 },
191 ];
192​
193 expect(findExistingAddProject({ projects, environmentId: env, path: "/repo" })?.id).toBe(
194 "project",
195 );
196 });
197});