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