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