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 isBareGitConfig,
11 isTemporaryWorktreeBranch,
12 normalizeGitRemoteUrl,
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 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git?view=compact")).toBe(
31 "github.com/t3tools/t3code",
32 );
33 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git#section")).toBe(
34 "github.com/t3tools/t3code",
35 );
36 expect(normalizeGitRemoteUrl("https://code.example.test/team/%C3%84pp.git")).toBe(
37 normalizeGitRemoteUrl("git@code.example.test:team/Äpp.git"),
38 );
39 expect(normalizeGitRemoteUrl("ssh://git@code.example.test/owner/repo.git.git")).toBe(
40 normalizeGitRemoteUrl("git@code.example.test:owner/repo.git.git"),
41 );
42 });
43
44 it.each([
45 "git@[2001:db8::1]:team/repo.git",
46 "[2001:db8::1]:team/repo.git",
47 "git@[2001:DB8:0::1]:team/repo.git",
48 ])("canonicalizes an IPv6 SCP remote like its SSH URL: %s", (remote) => {
49 expect(normalizeGitRemoteUrl(remote)).toBe(
50 normalizeGitRemoteUrl("ssh://git@[2001:db8::1]/team/repo.git"),
51 );
52 });
53
54 it("preserves nested group paths for providers like GitLab", () => {
55 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
56 "gitlab.com/t3tools/platform/t3code",
57 );
58 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
59 "gitlab.com/t3tools/platform/t3code",
60 );
61 });
62
63 it("drops explicit ports from URL-shaped remotes", () => {
64 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
65 "gitlab.company.com/team/project",
66 );
67 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
68 "gitlab.company.com/team/project",
69 );
70 });
71
72 it("normalizes SCP-like remotes with non-git SSH users", () => {
73 expect(normalizeGitRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(
74 "gitlab.example.com/group/project",
75 );
76 expect(normalizeGitRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(
77 "bitbucket.org/workspace/repo",
78 );
79 expect(normalizeGitRemoteUrl("code.example.test:team/repo.git")).toBe(
80 "code.example.test/team/repo",
81 );
82 expect(normalizeGitRemoteUrl("git@code.example.test:/team/repo.git")).toBe(
83 "code.example.test/team/repo",
84 );
85 expect(normalizeGitRemoteUrl("C:/repos/project")).toBe("c:/repos/project");
86 expect(normalizeGitRemoteUrl("git@x:team/project.git")).toBe("x/team/project");
87 });
88
89 it("keys a one-segment URL remote by what names the repository", () => {
90 expect(normalizeGitRemoteUrl("https://deploy:s3cr3ttok@git.example.com/Repo.git")).toBe(
91 "https://git.example.com/repo",
92 );
93 expect(normalizeGitRemoteUrl("https://0123abcd@git.example.com/repo.git?key=0123abcd#x")).toBe(
94 "https://git.example.com/repo",
95 );
96 expect(normalizeGitRemoteUrl("ftp://deploy:s3cr3ttok@git.example.com/team/repo.git")).toBe(
97 "ftp://git.example.com/team/repo",
98 );
99 expect(normalizeGitRemoteUrl("ssh://git:s3cr3ttok@git.example.com:2222/repo.git")).toBe(
100 "ssh://git@git.example.com:2222/repo",
101 );
102 expect(normalizeGitRemoteUrl("ssh://git@git.example.com/repo.git")).toBe(
103 "ssh://git@git.example.com/repo",
104 );
105 expect(normalizeGitRemoteUrl("https://git.example.com/repo.git")).toBe(
106 "https://git.example.com/repo",
107 );
108 expect(normalizeGitRemoteUrl("/srv/git/Repo.git")).toBe("/srv/git/repo");
109 });
110});
111
112describe("isBareGitConfig", () => {
113 it("reads Git's numeric booleans without accepting invalid or overflowing values", () => {
114 for (const value of [
115 "true",
116 "yes",
117 "on",
118 "1",
119 "2",
120 "-1",
121 "+1",
122 "01",
123 "0x10",
124 "1k",
125 "1m",
126 "1g",
127 "2147483647",
128 "-2147483648",
129 ]) {
130 expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(true);
131 }
132 for (const value of [
133 "false",
134 "no",
135 "off",
136 "0",
137 "-0",
138 "00",
139 "0x0",
140 "0k",
141 "08",
142 "1.5",
143 "2no",
144 "2g",
145 "2147483648",
146 "-2147483649",
147 ]) {
148 expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(false);
149 }
150 expect(isBareGitConfig("[core]\nbare = 2\nbare = 0\n")).toBe(false);
151 expect(isBareGitConfig("[core]\nbare = false\nbare = -2\n")).toBe(true);
152 });
153
154 it("reads continued bare values after comments and at end of input", () => {
155 const config = ["[core] bare = false # ignored backslash \\", 'bare = "tr\\', 'ue"'].join("\n");
156
157 expect(isBareGitConfig(config)).toBe(true);
158 expect(isBareGitConfig("[core]\nbare = tr\\\nue\\")).toBe(true);
159 expect(isBareGitConfig('[core]\nbare = "true\\')).toBe(false);
160 expect(isBareGitConfig('[core]\nbare = "true\\\n')).toBe(false);
161 });
162});
163
164describe("parseOriginUrlFromGitConfig", () => {
165 it("reads the origin url and ignores other remotes", () => {
166 const config = [
167 "[core]",
168 "\trepositoryformatversion = 0",
169 '[remote "upstream"]',
170 "\turl = https://github.com/other/repo.git",
171 '[remote "origin"]',
172 "\turl = git@github.com:pingdotgg/t3code.git",
173 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
174 '[branch "main"]',
175 "\tremote = origin",
176 ].join("\n");
177 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
178 });
179
180 it("strips inline comments and quotes from the url value", () => {
181 expect(
182 parseOriginUrlFromGitConfig(
183 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
184 ),
185 ).toBe("https://github.com/acme/repo.git");
186 expect(
187 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
188 ).toBe("git@github.com:acme/repo.git");
189 for (const newline of ["\n", "\r\n"]) {
190 for (const comment of ["#", ";"]) {
191 const config = [
192 '[remote "upstream"]',
193 `url = https://github.com/other/repo.git ${comment} ignored backslash \\`,
194 '[remote "origin"]',
195 `url = https://github.com/acme/repo.git ${comment} ignored backslash \\`,
196 `${comment} ignored backslash \\`,
197 '[remote "fork"]',
198 "url = https://github.com/me/repo.git",
199 ].join(newline);
200 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
201 }
202 }
203 });
204
205 it("accepts legacy dotted headers, header comments, and line continuations", () => {
206 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
207 "git@github.com:a/b.git",
208 );
209 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
210 "git@github.com:a/b.git",
211 );
212 expect(
213 parseOriginUrlFromGitConfig(
214 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
215 ),
216 ).toBe("git@github.com:a/b.git");
217 expect(
218 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
219 ).toBe("git@github.com:a/b.git");
220 expect(
221 parseOriginUrlFromGitConfig(
222 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
223 ),
224 ).toBe("https://github.com/acme/\t\trepo.git");
225 });
226
227 it("reads settings on the remote header line", () => {
228 const upstream = '\n[remote "upstream"]\n\turl = https://github.com/other/source.git\n';
229 expect(
230 parseOriginUrlFromGitConfig(`[remote "origin"] url = git@github.com:a/b.git${upstream}`),
231 ).toBe("git@github.com:a/b.git");
232 expect(parseOriginUrlFromGitConfig("[remote.Origin]url=git@github.com:a/b.git\n")).toBe(
233 "git@github.com:a/b.git",
234 );
235 expect(
236 parseOriginUrlFromGitConfig(
237 `[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/*\n\turl = git@github.com:a/b.git${upstream}`,
238 ),
239 ).toBe("git@github.com:a/b.git");
240 });
241
242 it("falls back to the first remote when there is no origin", () => {
243 const config = [
244 '[remote "upstream"]',
245 "\turl = https://github.com/acme/repo.git",
246 '[remote "fork"]',
247 "\turl = https://github.com/me/repo.git",
248 ].join("\n");
249 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
250 });
251});
252
253describe("isTemporaryWorktreeBranch", () => {
254 it("matches generated temporary worktree refs", () => {
255 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
256 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
257 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
258 });
259
260 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
261 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
262 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
263 );
264 });
265
266 it("matches legacy t3code temporary worktree refs", () => {
267 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
268 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
269 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
270 expect(
271 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
272 ).toBe(false);
273 });
274
275 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
276 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
277 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
278 });
279
280 it("matches the flat fallback used when a plain t3 branch exists", () => {
281 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
282 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
283 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
284 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
285 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
286 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
287 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
288 );
289 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
290 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
291 );
292 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
293 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
294 );
295 });
296
297 it("rejects non-temporary refName names", () => {
298 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
299 expect(isTemporaryWorktreeBranch("main")).toBe(false);
300 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
301 });
302});
303
304describe("applyGitStatusStreamEvent", () => {
305 it("treats a remote-only update as a repository when local state is missing", () => {
306 const remote: VcsStatusRemoteResult = {
307 hasUpstream: true,
308 aheadCount: 2,
309 behindCount: 1,
310 pr: null,
311 };
312
313 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
314 isRepo: true,
315 hasPrimaryRemote: false,
316 isDefaultRef: false,
317 refName: null,
318 hasWorkingTreeChanges: false,
319 workingTree: { files: [], insertions: 0, deletions: 0 },
320 hasUpstream: true,
321 aheadCount: 2,
322 behindCount: 1,
323 pr: null,
324 });
325 });
326
327 it("preserves local-only fields when applying a remote update", () => {
328 const current: VcsStatusResult = {
329 isRepo: true,
330 sourceControlProvider: {
331 kind: SourceControlProviderKind.make("github"),
332 name: "GitHub",
333 baseUrl: "https://github.com",
334 },
335 hasPrimaryRemote: true,
336 isDefaultRef: false,
337 refName: "feature/demo",
338 hasWorkingTreeChanges: true,
339 workingTree: {
340 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
341 insertions: 1,
342 deletions: 0,
343 },
344 hasUpstream: false,
345 aheadCount: 0,
346 behindCount: 0,
347 pr: null,
348 };
349
350 const remote: VcsStatusRemoteResult = {
351 hasUpstream: true,
352 aheadCount: 2,
353 behindCount: 1,
354 pr: null,
355 };
356
357 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
358 ...current,
359 hasUpstream: true,
360 aheadCount: 2,
361 behindCount: 1,
362 pr: null,
363 });
364 });
365});
366
367describe("sanitizeFeatureBranchName", () => {
368 it("sanitizes an explicit namespace before preserving it", () => {
369 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
370 "fix/calendar-recruitment-filter",
371 );
372 });
373
374 // regression: #15615 generated branch namespaces are preserved
375 it.each([
376 ["refine toolbar", "feature/refine-toolbar"],
377 [" /?. / ", "feature/update"],
378 ["fix///", "feature/fix"],
379 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
380 expect(sanitizeFeatureBranchName(input)).toBe(expected);
381 });
382
383 it("keeps accented letters as their plain letters", () => {
384 expect(sanitizeFeatureBranchName("fix/Añadir validación del café")).toBe(
385 "fix/anadir-validacion-del-cafe",
386 );
387 });
388
389 it("keeps words with letters that have no Unicode decomposition whole", () => {
390 expect(sanitizeFeatureBranchName("Größe der Straße korrigieren")).toBe(
391 "feature/grosse-der-strasse-korrigieren",
392 );
393 expect(sanitizeFeatureBranchName("fix/Đặt tên")).toBe("fix/dat-ten");
394 expect(sanitizeFeatureBranchName("Napraw błąd w żółtym przycisku")).toBe(
395 "feature/napraw-blad-w-zoltym-przycisku",
396 );
397 });
398});
399
400describe("resolveAutoFeatureBranchName", () => {
401 it("resolves case-insensitive collisions within the generated namespace", () => {
402 expect(
403 resolveAutoFeatureBranchName(
404 ["FIX/refine-toolbar", "fix/refine-toolbar-2"],
405 "fix/refine-toolbar",
406 ),
407 ).toBe("fix/refine-toolbar-3");
408 });
409
410 // regression: #15615 generated branch namespaces are preserved
411 it.each([
412 ["fix/name", ["fix"], "fix-2/name"],
413 ["fix/name", ["fix/name/child"], "fix/name-2"],
414 ["fix/name", ["fixes", "fix/name-extra/child"], "fix/name"],
415 ])("resolves %s against namespace blockers %j", (preferredBranch, existingNames, expected) => {
416 expect(resolveAutoFeatureBranchName(existingNames, preferredBranch)).toBe(expected);
417 });
418});
419
420describe("formatGeneratedBranchName", () => {
421 it.each([
422 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
423 ["-team//feature@{new}", "team/feature-new/add-search"],
424 [" /?. / ", "add-search"],
425 ])("normalizes invalid static prefix %s", (prefix, expected) => {
426 expect(
427 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
428 ).toBe(expected);
429 });
430 it("uses the model's semantic prefix without the stored static prefix", () => {
431 expect(
432 formatGeneratedBranchName("feat/Add Search", {
433 mode: "semantic",
434 prefix: "t3code",
435 instructions: "",
436 }),
437 ).toBe("feat/add-search");
438 });
439});