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 it("gives an Azure DevOps repository the same key over SSH as over HTTPS", () => {
67 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code")).toBe(
68 "dev.azure.com/t3tools/platform/_git/t3code",
69 );
70 expect(normalizeGitRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code")).toBe(
71 "dev.azure.com/t3tools/platform/_git/t3code",
72 );
73 expect(
74 normalizeGitRemoteUrl("https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code"),
75 ).toBe("dev.azure.com/t3tools/platform/_git/t3code");
76 });
77
78 it("puts the organization back in the host on the name dev.azure.com replaced", () => {
79 expect(
80 normalizeGitRemoteUrl("T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code"),
81 ).toBe("t3tools.visualstudio.com/platform/_git/t3code");
82 expect(normalizeGitRemoteUrl("https://T3Tools.visualstudio.com/Platform/_git/T3Code")).toBe(
83 "t3tools.visualstudio.com/platform/_git/t3code",
84 );
85 });
86});
87
88describe("parseOriginUrlFromGitConfig", () => {
89 it("reads the origin url and ignores other remotes", () => {
90 const config = [
91 "[core]",
92 "\trepositoryformatversion = 0",
93 '[remote "upstream"]',
94 "\turl = https://github.com/other/repo.git",
95 '[remote "origin"]',
96 "\turl = git@github.com:pingdotgg/t3code.git",
97 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
98 '[branch "main"]',
99 "\tremote = origin",
100 ].join("\n");
101 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
102 });
103
104 it("strips inline comments and quotes from the url value", () => {
105 expect(
106 parseOriginUrlFromGitConfig(
107 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
108 ),
109 ).toBe("https://github.com/acme/repo.git");
110 expect(
111 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
112 ).toBe("git@github.com:acme/repo.git");
113 });
114
115 it("accepts legacy dotted headers, header comments, and line continuations", () => {
116 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
117 "git@github.com:a/b.git",
118 );
119 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
120 "git@github.com:a/b.git",
121 );
122 expect(
123 parseOriginUrlFromGitConfig(
124 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
125 ),
126 ).toBe("git@github.com:a/b.git");
127 expect(
128 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
129 ).toBe("git@github.com:a/b.git");
130 expect(
131 parseOriginUrlFromGitConfig(
132 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
133 ),
134 ).toBe("https://github.com/acme/repo.git");
135 });
136
137 it("falls back to the first remote when there is no origin", () => {
138 const config = [
139 '[remote "upstream"]',
140 "\turl = https://github.com/acme/repo.git",
141 '[remote "fork"]',
142 "\turl = https://github.com/me/repo.git",
143 ].join("\n");
144 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
145 });
146});
147
148describe("isTemporaryWorktreeBranch", () => {
149 it("matches generated temporary worktree refs", () => {
150 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
151 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
152 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
153 });
154
155 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
156 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
157 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
158 );
159 });
160
161 it("matches legacy t3code temporary worktree refs", () => {
162 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
163 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
164 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
165 expect(
166 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
167 ).toBe(false);
168 });
169
170 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
171 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
172 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
173 });
174
175 it("matches the flat fallback used when a plain t3 branch exists", () => {
176 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
177 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
178 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
179 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
180 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
181 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
182 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
183 );
184 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
185 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
186 );
187 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
188 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
189 );
190 });
191
192 it("rejects non-temporary refName names", () => {
193 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
194 expect(isTemporaryWorktreeBranch("main")).toBe(false);
195 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
196 });
197});
198
199describe("applyGitStatusStreamEvent", () => {
200 it("treats a remote-only update as a repository when local state is missing", () => {
201 const remote: VcsStatusRemoteResult = {
202 hasUpstream: true,
203 aheadCount: 2,
204 behindCount: 1,
205 pr: null,
206 };
207
208 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
209 isRepo: true,
210 hasPrimaryRemote: false,
211 isDefaultRef: false,
212 refName: null,
213 hasWorkingTreeChanges: false,
214 workingTree: { files: [], insertions: 0, deletions: 0 },
215 hasUpstream: true,
216 aheadCount: 2,
217 behindCount: 1,
218 pr: null,
219 });
220 });
221
222 it("preserves local-only fields when applying a remote update", () => {
223 const current: VcsStatusResult = {
224 isRepo: true,
225 sourceControlProvider: {
226 kind: SourceControlProviderKind.make("github"),
227 name: "GitHub",
228 baseUrl: "https://github.com",
229 },
230 hasPrimaryRemote: true,
231 isDefaultRef: false,
232 refName: "feature/demo",
233 hasWorkingTreeChanges: true,
234 workingTree: {
235 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
236 insertions: 1,
237 deletions: 0,
238 },
239 hasUpstream: false,
240 aheadCount: 0,
241 behindCount: 0,
242 pr: null,
243 };
244
245 const remote: VcsStatusRemoteResult = {
246 hasUpstream: true,
247 aheadCount: 2,
248 behindCount: 1,
249 pr: null,
250 };
251
252 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
253 ...current,
254 hasUpstream: true,
255 aheadCount: 2,
256 behindCount: 1,
257 pr: null,
258 });
259 });
260});
261
262describe("sanitizeFeatureBranchName", () => {
263 it("sanitizes an explicit namespace before preserving it", () => {
264 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
265 "fix/calendar-recruitment-filter",
266 );
267 });
268
269 // regression: #15615 generated branch namespaces are preserved
270 it.each([
271 ["refine toolbar", "feature/refine-toolbar"],
272 [" /?. / ", "feature/update"],
273 ["fix///", "feature/fix"],
274 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
275 expect(sanitizeFeatureBranchName(input)).toBe(expected);
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});