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

git.test.ts

402 lines14.7 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 applyGitStatusStreamEvent,
7 formatGeneratedBranchName,
8 buildTemporaryWorktreeBranchName,
9 flattenTemporaryWorktreeBranchName,
10 isTemporaryWorktreeBranch,
11 normalizeGitRemoteUrl,
12 parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
13 parseOriginUrlFromGitConfig,
14 resolveAutoFeatureBranchName,
15 sanitizeFeatureBranchName,
16 WORKTREE_BRANCH_PREFIX,
17} from "./git.ts";
18​
19describe("normalizeGitRemoteUrl", () => {
20 it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
21 expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
22 "github.com/t3tools/t3code",
23 );
24 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
25 "github.com/t3tools/t3code",
26 );
27 expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
28 "github.com/t3tools/t3code",
29 );
30 });
31​
32 it("preserves nested group paths for providers like GitLab", () => {
33 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
34 "gitlab.com/t3tools/platform/t3code",
35 );
36 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
37 "gitlab.com/t3tools/platform/t3code",
38 );
39 });
40​
41 it("drops explicit ports from URL-shaped remotes", () => {
42 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
43 "gitlab.company.com/team/project",
44 );
45 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
46 "gitlab.company.com/team/project",
47 );
48 });
49​
50 it("normalizes SCP-like remotes with non-git SSH users", () => {
51 expect(normalizeGitRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(
52 "gitlab.example.com/group/project",
53 );
54 expect(normalizeGitRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(
55 "bitbucket.org/workspace/repo",
56 );
57 expect(normalizeGitRemoteUrl("code.example.test:team/repo.git")).toBe(
58 "code.example.test/team/repo",
59 );
60 expect(normalizeGitRemoteUrl("git@code.example.test:/team/repo.git")).toBe(
61 "code.example.test/team/repo",
62 );
63 expect(normalizeGitRemoteUrl("C:/repos/project")).toBe("c:/repos/project");
64 expect(normalizeGitRemoteUrl("git@x:team/project.git")).toBe("x/team/project");
65 });
66​
67 it("gives an Azure DevOps repository the same key over SSH as over HTTPS", () => {
68 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code")).toBe(
69 "dev.azure.com/t3tools/platform/_git/t3code",
70 );
71 expect(normalizeGitRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code")).toBe(
72 "dev.azure.com/t3tools/platform/_git/t3code",
73 );
74 expect(
75 normalizeGitRemoteUrl("https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code"),
76 ).toBe("dev.azure.com/t3tools/platform/_git/t3code");
77 });
78​
79 it("puts the organization back in the host on the name dev.azure.com replaced", () => {
80 expect(
81 normalizeGitRemoteUrl("T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code"),
82 ).toBe("t3tools.visualstudio.com/platform/_git/t3code");
83 expect(normalizeGitRemoteUrl("https://T3Tools.visualstudio.com/Platform/_git/T3Code")).toBe(
84 "t3tools.visualstudio.com/platform/_git/t3code",
85 );
86 });
87​
88 it("leaves an Azure SSH host it cannot read as the path it was given", () => {
89 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v4/T3Tools/Platform/T3Code")).toBe(
90 "ssh.dev.azure.com/v4/t3tools/platform/t3code",
91 );
92 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/T3Code")).toBe(
93 "ssh.dev.azure.com/v3/t3tools/t3code",
94 );
95 });
96});
97​
98describe("parseOriginUrlFromGitConfig", () => {
99 it("reads the origin url and ignores other remotes", () => {
100 const config = [
101 "[core]",
102 "\trepositoryformatversion = 0",
103 '[remote "upstream"]',
104 "\turl = https://github.com/other/repo.git",
105 '[remote "origin"]',
106 "\turl = git@github.com:pingdotgg/t3code.git",
107 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
108 '[branch "main"]',
109 "\tremote = origin",
110 ].join("\n");
111 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
112 });
113​
114 it("strips inline comments and quotes from the url value", () => {
115 expect(
116 parseOriginUrlFromGitConfig(
117 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
118 ),
119 ).toBe("https://github.com/acme/repo.git");
120 expect(
121 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
122 ).toBe("git@github.com:acme/repo.git");
123 });
124​
125 it("accepts legacy dotted headers, header comments, and line continuations", () => {
126 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
127 "git@github.com:a/b.git",
128 );
129 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
130 "git@github.com:a/b.git",
131 );
132 expect(
133 parseOriginUrlFromGitConfig(
134 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
135 ),
136 ).toBe("git@github.com:a/b.git");
137 expect(
138 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
139 ).toBe("git@github.com:a/b.git");
140 expect(
141 parseOriginUrlFromGitConfig(
142 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
143 ),
144 ).toBe("https://github.com/acme/repo.git");
145 });
146​
147 it("falls back to the first remote when there is no origin", () => {
148 const config = [
149 '[remote "upstream"]',
150 "\turl = https://github.com/acme/repo.git",
151 '[remote "fork"]',
152 "\turl = https://github.com/me/repo.git",
153 ].join("\n");
154 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
155 });
156​
157 it("returns null when there is no remote section", () => {
158 expect(parseOriginUrlFromGitConfig("[core]\n\tbare = false\n")).toBeNull();
159 expect(parseOriginUrlFromGitConfig("")).toBeNull();
160 });
161});
162​
163describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
164 it("extracts the owner and repository from common GitHub remote shapes", () => {
165 expect(
166 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
167 ).toBe("T3Tools/T3Code");
168 expect(
169 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
170 ).toBe("T3Tools/T3Code");
171 expect(
172 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://github.com/T3Tools/T3Code.git"),
173 ).toBe("T3Tools/T3Code");
174 });
175});
176​
177describe("isTemporaryWorktreeBranch", () => {
178 it("matches the generated temporary worktree refName format", () => {
179 expect(
180 isTemporaryWorktreeBranch(
181 buildTemporaryWorktreeBranchName((byteLength) => {
182 expect(byteLength).toBe(4);
183 return "DEADBEEF";
184 }),
185 ),
186 ).toBe(true);
187 });
188​
189 it("matches generated temporary worktree refs", () => {
190 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
191 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
192 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
193 });
194​
195 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
196 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
197 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
198 );
199 });
200​
201 it("matches legacy t3code temporary worktree refs", () => {
202 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
203 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
204 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
205 expect(
206 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
207 ).toBe(false);
208 });
209​
210 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
211 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
212 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
213 });
214​
215 it("matches the flat fallback used when a plain t3 branch exists", () => {
216 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
217 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
218 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
219 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
220 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
221 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
222 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
223 );
224 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
225 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
226 );
227 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
228 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
229 );
230 });
231​
232 it("rejects non-temporary refName names", () => {
233 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
234 expect(isTemporaryWorktreeBranch("main")).toBe(false);
235 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
236 });
237});
238​
239describe("applyGitStatusStreamEvent", () => {
240 it("treats a remote-only update as a repository when local state is missing", () => {
241 const remote: VcsStatusRemoteResult = {
242 hasUpstream: true,
243 aheadCount: 2,
244 behindCount: 1,
245 pr: null,
246 };
247​
248 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
249 isRepo: true,
250 hasPrimaryRemote: false,
251 isDefaultRef: false,
252 refName: null,
253 hasWorkingTreeChanges: false,
254 workingTree: { files: [], insertions: 0, deletions: 0 },
255 hasUpstream: true,
256 aheadCount: 2,
257 behindCount: 1,
258 pr: null,
259 });
260 });
261​
262 it("preserves local-only fields when applying a remote update", () => {
263 const current: VcsStatusResult = {
264 isRepo: true,
265 sourceControlProvider: {
266 kind: SourceControlProviderKind.make("github"),
267 name: "GitHub",
268 baseUrl: "https://github.com",
269 },
270 hasPrimaryRemote: true,
271 isDefaultRef: false,
272 refName: "feature/demo",
273 hasWorkingTreeChanges: true,
274 workingTree: {
275 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
276 insertions: 1,
277 deletions: 0,
278 },
279 hasUpstream: false,
280 aheadCount: 0,
281 behindCount: 0,
282 pr: null,
283 };
284​
285 const remote: VcsStatusRemoteResult = {
286 hasUpstream: true,
287 aheadCount: 2,
288 behindCount: 1,
289 pr: null,
290 };
291​
292 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
293 ...current,
294 hasUpstream: true,
295 aheadCount: 2,
296 behindCount: 1,
297 pr: null,
298 });
299 });
300});
301​
302describe("sanitizeFeatureBranchName", () => {
303 it.each(["feature", "fix", "team/jules"])("preserves the %s namespace", (namespace) => {
304 expect(sanitizeFeatureBranchName(`${namespace}/refine-toolbar`)).toBe(
305 `${namespace}/refine-toolbar`,
306 );
307 });
308​
309 it("sanitizes an explicit namespace before preserving it", () => {
310 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
311 "fix/calendar-recruitment-filter",
312 );
313 });
314​
315 it.each([
316 ["refine toolbar", "feature/refine-toolbar"],
317 ["", "feature/update"],
318 [" /?. / ", "feature/update"],
319 ["fix///", "feature/fix"],
320 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
321 expect(sanitizeFeatureBranchName(input)).toBe(expected);
322 });
323​
324 it("keeps the existing length limit for namespaced branches", () => {
325 expect(sanitizeFeatureBranchName(`fix/${"x".repeat(80)}`)).toBe(`fix/${"x".repeat(60)}`);
326 });
327});
328​
329describe("resolveAutoFeatureBranchName", () => {
330 it("resolves case-insensitive collisions within the generated namespace", () => {
331 expect(
332 resolveAutoFeatureBranchName(
333 ["FIX/refine-toolbar", "fix/refine-toolbar-2"],
334 "fix/refine-toolbar",
335 ),
336 ).toBe("fix/refine-toolbar-3");
337 });
338​
339 it("keeps the fallback when no preferred branch is supplied", () => {
340 expect(resolveAutoFeatureBranchName(["feature/update"])).toBe("feature/update-2");
341 });
342​
343 it.each([
344 ["fix/name", ["fix"], "fix-2/name"],
345 ["team/jules/fix/name", ["team/jules"], "team/jules-2/fix/name"],
346 ["fix/name", ["fix/name/child"], "fix/name-2"],
347 ["FIX/name", ["FIX", "FIX-2"], "fix-3/name"],
348 ["fix/name", ["fixes", "fix/name-extra/child"], "fix/name"],
349 [undefined, ["feature", "feature-2"], "feature-3/update"],
350 ])("resolves %s against namespace blockers %j", (preferredBranch, existingNames, expected) => {
351 expect(resolveAutoFeatureBranchName(existingNames, preferredBranch)).toBe(expected);
352 });
353});
354​
355describe("formatGeneratedBranchName", () => {
356 it.each(["t3code", "t3code/"])("joins static prefix %s with one slash", (prefix) => {
357 expect(
358 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
359 ).toBe("t3code/add-search");
360 });
361 it("supports an empty prefix and preserves user prefix casing", () => {
362 expect(
363 formatGeneratedBranchName("Add Search", { mode: "static", prefix: "", instructions: "" }),
364 ).toBe("add-search");
365 expect(
366 formatGeneratedBranchName("Add Search", {
367 mode: "static",
368 prefix: "Team/Julius/",
369 instructions: "",
370 }),
371 ).toBe("Team/Julius/add-search");
372 });
373 it.each([
374 ["release..candidate", "release-candidate/add-search"],
375 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
376 ["-team//feature@{new}", "team/feature-new/add-search"],
377 [" /?. / ", "add-search"],
378 ])("normalizes invalid static prefix %s", (prefix, expected) => {
379 expect(
380 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
381 ).toBe(expected);
382 });
383 it("uses the model's semantic prefix without the stored static prefix", () => {
384 expect(
385 formatGeneratedBranchName("feat/Add Search", {
386 mode: "semantic",
387 prefix: "t3code",
388 instructions: "",
389 }),
390 ).toBe("feat/add-search");
391 });
392 it("preserves the full custom name, including case, dots and length", () => {
393 const branch = `Julius/ABC-123/release.v2-${"x".repeat(70)}`;
394 expect(
395 formatGeneratedBranchName(` ${branch} `, {
396 mode: "custom",
397 prefix: "ignored",
398 instructions: "",
399 }),
400 ).toBe(branch);
401 });
402});