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