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 });
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
68describe("isBareGitConfig", () => {
69 it("reads Git's numeric booleans without accepting invalid or overflowing values", () => {
70 for (const value of [
71 "true",
72 "yes",
73 "on",
74 "1",
75 "2",
76 "-1",
77 "+1",
78 "01",
79 "0x10",
80 "1k",
81 "1m",
82 "1g",
83 "2147483647",
84 "-2147483648",
85 ]) {
86 expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(true);
87 }
88 for (const value of [
89 "false",
90 "no",
91 "off",
92 "0",
93 "-0",
94 "00",
95 "0x0",
96 "0k",
97 "08",
98 "1.5",
99 "2no",
100 "2g",
101 "2147483648",
102 "-2147483649",
103 ]) {
104 expect(isBareGitConfig(`[core]\nbare = ${value}\n`), value).toBe(false);
105 }
106 expect(isBareGitConfig("[core]\nbare = 2\nbare = 0\n")).toBe(false);
107 expect(isBareGitConfig("[core]\nbare = false\nbare = -2\n")).toBe(true);
108 });
109
110 it("reads continued bare values after comments and at end of input", () => {
111 const config = ["[core] bare = false # ignored backslash \\", 'bare = "tr\\', 'ue"'].join("\n");
112
113 expect(isBareGitConfig(config)).toBe(true);
114 expect(isBareGitConfig("[core]\nbare = tr\\\nue\\")).toBe(true);
115 });
116});
117
118describe("parseOriginUrlFromGitConfig", () => {
119 it("reads the origin url and ignores other remotes", () => {
120 const config = [
121 "[core]",
122 "\trepositoryformatversion = 0",
123 '[remote "upstream"]',
124 "\turl = https://github.com/other/repo.git",
125 '[remote "origin"]',
126 "\turl = git@github.com:pingdotgg/t3code.git",
127 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
128 '[branch "main"]',
129 "\tremote = origin",
130 ].join("\n");
131 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
132 });
133
134 it("strips inline comments and quotes from the url value", () => {
135 expect(
136 parseOriginUrlFromGitConfig(
137 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
138 ),
139 ).toBe("https://github.com/acme/repo.git");
140 expect(
141 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
142 ).toBe("git@github.com:acme/repo.git");
143 });
144
145 it("accepts legacy dotted headers, header comments, and line continuations", () => {
146 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
147 "git@github.com:a/b.git",
148 );
149 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
150 "git@github.com:a/b.git",
151 );
152 expect(
153 parseOriginUrlFromGitConfig(
154 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
155 ),
156 ).toBe("git@github.com:a/b.git");
157 expect(
158 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
159 ).toBe("git@github.com:a/b.git");
160 expect(
161 parseOriginUrlFromGitConfig(
162 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
163 ),
164 ).toBe("https://github.com/acme/repo.git");
165 });
166
167 it("reads settings on the remote header line", () => {
168 const upstream = '\n[remote "upstream"]\n\turl = https://github.com/other/source.git\n';
169 expect(
170 parseOriginUrlFromGitConfig(`[remote "origin"] url = git@github.com:a/b.git${upstream}`),
171 ).toBe("git@github.com:a/b.git");
172 expect(parseOriginUrlFromGitConfig("[remote.Origin]url=git@github.com:a/b.git\n")).toBe(
173 "git@github.com:a/b.git",
174 );
175 expect(
176 parseOriginUrlFromGitConfig(
177 `[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/*\n\turl = git@github.com:a/b.git${upstream}`,
178 ),
179 ).toBe("git@github.com:a/b.git");
180 });
181
182 it("falls back to the first remote when there is no origin", () => {
183 const config = [
184 '[remote "upstream"]',
185 "\turl = https://github.com/acme/repo.git",
186 '[remote "fork"]',
187 "\turl = https://github.com/me/repo.git",
188 ].join("\n");
189 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
190 });
191});
192
193describe("isTemporaryWorktreeBranch", () => {
194 it("matches generated temporary worktree refs", () => {
195 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
196 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
197 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
198 });
199
200 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
201 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
202 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
203 );
204 });
205
206 it("matches legacy t3code temporary worktree refs", () => {
207 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
208 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
209 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
210 expect(
211 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
212 ).toBe(false);
213 });
214
215 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
216 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
217 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
218 });
219
220 it("matches the flat fallback used when a plain t3 branch exists", () => {
221 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
222 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
223 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
224 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
225 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
226 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
227 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
228 );
229 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
230 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
231 );
232 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
233 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
234 );
235 });
236
237 it("rejects non-temporary refName names", () => {
238 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
239 expect(isTemporaryWorktreeBranch("main")).toBe(false);
240 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
241 });
242});
243
244describe("applyGitStatusStreamEvent", () => {
245 it("treats a remote-only update as a repository when local state is missing", () => {
246 const remote: VcsStatusRemoteResult = {
247 hasUpstream: true,
248 aheadCount: 2,
249 behindCount: 1,
250 pr: null,
251 };
252
253 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
254 isRepo: true,
255 hasPrimaryRemote: false,
256 isDefaultRef: false,
257 refName: null,
258 hasWorkingTreeChanges: false,
259 workingTree: { files: [], insertions: 0, deletions: 0 },
260 hasUpstream: true,
261 aheadCount: 2,
262 behindCount: 1,
263 pr: null,
264 });
265 });
266
267 it("preserves local-only fields when applying a remote update", () => {
268 const current: VcsStatusResult = {
269 isRepo: true,
270 sourceControlProvider: {
271 kind: SourceControlProviderKind.make("github"),
272 name: "GitHub",
273 baseUrl: "https://github.com",
274 },
275 hasPrimaryRemote: true,
276 isDefaultRef: false,
277 refName: "feature/demo",
278 hasWorkingTreeChanges: true,
279 workingTree: {
280 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
281 insertions: 1,
282 deletions: 0,
283 },
284 hasUpstream: false,
285 aheadCount: 0,
286 behindCount: 0,
287 pr: null,
288 };
289
290 const remote: VcsStatusRemoteResult = {
291 hasUpstream: true,
292 aheadCount: 2,
293 behindCount: 1,
294 pr: null,
295 };
296
297 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
298 ...current,
299 hasUpstream: true,
300 aheadCount: 2,
301 behindCount: 1,
302 pr: null,
303 });
304 });
305});
306
307describe("sanitizeFeatureBranchName", () => {
308 it("sanitizes an explicit namespace before preserving it", () => {
309 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
310 "fix/calendar-recruitment-filter",
311 );
312 });
313
314 // regression: #15615 generated branch namespaces are preserved
315 it.each([
316 ["refine toolbar", "feature/refine-toolbar"],
317 [" /?. / ", "feature/update"],
318 ["fix///", "feature/fix"],
319 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
320 expect(sanitizeFeatureBranchName(input)).toBe(expected);
321 });
322
323 it("keeps accented letters as their plain letters", () => {
324 expect(sanitizeFeatureBranchName("fix/Añadir validación del café")).toBe(
325 "fix/anadir-validacion-del-cafe",
326 );
327 });
328});
329
330describe("resolveAutoFeatureBranchName", () => {
331 it("resolves case-insensitive collisions within the generated namespace", () => {
332 expect(
333 resolveAutoFeatureBranchName(
334 ["FIX/refine-toolbar", "fix/refine-toolbar-2"],
335 "fix/refine-toolbar",
336 ),
337 ).toBe("fix/refine-toolbar-3");
338 });
339
340 // regression: #15615 generated branch namespaces are preserved
341 it.each([
342 ["fix/name", ["fix"], "fix-2/name"],
343 ["fix/name", ["fix/name/child"], "fix/name-2"],
344 ["fix/name", ["fixes", "fix/name-extra/child"], "fix/name"],
345 ])("resolves %s against namespace blockers %j", (preferredBranch, existingNames, expected) => {
346 expect(resolveAutoFeatureBranchName(existingNames, preferredBranch)).toBe(expected);
347 });
348});
349
350describe("formatGeneratedBranchName", () => {
351 it.each([
352 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
353 ["-team//feature@{new}", "team/feature-new/add-search"],
354 [" /?. / ", "add-search"],
355 ])("normalizes invalid static prefix %s", (prefix, expected) => {
356 expect(
357 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
358 ).toBe(expected);
359 });
360 it("uses the model's semantic prefix without the stored static prefix", () => {
361 expect(
362 formatGeneratedBranchName("feat/Add Search", {
363 mode: "semantic",
364 prefix: "t3code",
365 instructions: "",
366 }),
367 ).toBe("feat/add-search");
368 });
369});