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