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