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("falls back to the first remote when there is no origin", () => {
117 const config = [
118 '[remote "upstream"]',
119 "\turl = https://github.com/acme/repo.git",
120 '[remote "fork"]',
121 "\turl = https://github.com/me/repo.git",
122 ].join("\n");
123 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
124 });
125});
126
127describe("isTemporaryWorktreeBranch", () => {
128 it("matches generated temporary worktree refs", () => {
129 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
130 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
131 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
132 });
133
134 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
135 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
136 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
137 );
138 });
139
140 it("matches legacy t3code temporary worktree refs", () => {
141 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
142 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
143 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
144 expect(
145 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
146 ).toBe(false);
147 });
148
149 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
150 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
151 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
152 });
153
154 it("matches the flat fallback used when a plain t3 branch exists", () => {
155 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
156 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
157 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
158 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
159 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
160 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
161 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
162 );
163 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
164 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
165 );
166 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
167 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
168 );
169 });
170
171 it("rejects non-temporary refName names", () => {
172 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
173 expect(isTemporaryWorktreeBranch("main")).toBe(false);
174 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
175 });
176});
177
178describe("applyGitStatusStreamEvent", () => {
179 it("treats a remote-only update as a repository when local state is missing", () => {
180 const remote: VcsStatusRemoteResult = {
181 hasUpstream: true,
182 aheadCount: 2,
183 behindCount: 1,
184 pr: null,
185 };
186
187 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
188 isRepo: true,
189 hasPrimaryRemote: false,
190 isDefaultRef: false,
191 refName: null,
192 hasWorkingTreeChanges: false,
193 workingTree: { files: [], insertions: 0, deletions: 0 },
194 hasUpstream: true,
195 aheadCount: 2,
196 behindCount: 1,
197 pr: null,
198 });
199 });
200
201 it("preserves local-only fields when applying a remote update", () => {
202 const current: VcsStatusResult = {
203 isRepo: true,
204 sourceControlProvider: {
205 kind: SourceControlProviderKind.make("github"),
206 name: "GitHub",
207 baseUrl: "https://github.com",
208 },
209 hasPrimaryRemote: true,
210 isDefaultRef: false,
211 refName: "feature/demo",
212 hasWorkingTreeChanges: true,
213 workingTree: {
214 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
215 insertions: 1,
216 deletions: 0,
217 },
218 hasUpstream: false,
219 aheadCount: 0,
220 behindCount: 0,
221 pr: null,
222 };
223
224 const remote: VcsStatusRemoteResult = {
225 hasUpstream: true,
226 aheadCount: 2,
227 behindCount: 1,
228 pr: null,
229 };
230
231 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
232 ...current,
233 hasUpstream: true,
234 aheadCount: 2,
235 behindCount: 1,
236 pr: null,
237 });
238 });
239});
240
241describe("sanitizeFeatureBranchName", () => {
242 it("sanitizes an explicit namespace before preserving it", () => {
243 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
244 "fix/calendar-recruitment-filter",
245 );
246 });
247
248 // regression: #15615 generated branch namespaces are preserved
249 it.each([
250 ["refine toolbar", "feature/refine-toolbar"],
251 [" /?. / ", "feature/update"],
252 ["fix///", "feature/fix"],
253 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
254 expect(sanitizeFeatureBranchName(input)).toBe(expected);
255 });
256});
257
258describe("resolveAutoFeatureBranchName", () => {
259 it("resolves case-insensitive collisions within the generated namespace", () => {
260 expect(
261 resolveAutoFeatureBranchName(
262 ["FIX/refine-toolbar", "fix/refine-toolbar-2"],
263 "fix/refine-toolbar",
264 ),
265 ).toBe("fix/refine-toolbar-3");
266 });
267
268 // regression: #15615 generated branch namespaces are preserved
269 it.each([
270 ["fix/name", ["fix"], "fix-2/name"],
271 ["fix/name", ["fix/name/child"], "fix/name-2"],
272 ["fix/name", ["fixes", "fix/name-extra/child"], "fix/name"],
273 ])("resolves %s against namespace blockers %j", (preferredBranch, existingNames, expected) => {
274 expect(resolveAutoFeatureBranchName(existingNames, preferredBranch)).toBe(expected);
275 });
276});
277
278describe("formatGeneratedBranchName", () => {
279 it.each([
280 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
281 ["-team//feature@{new}", "team/feature-new/add-search"],
282 [" /?. / ", "add-search"],
283 ])("normalizes invalid static prefix %s", (prefix, expected) => {
284 expect(
285 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
286 ).toBe(expected);
287 });
288 it("uses the model's semantic prefix without the stored static prefix", () => {
289 expect(
290 formatGeneratedBranchName("feat/Add Search", {
291 mode: "semantic",
292 prefix: "t3code",
293 instructions: "",
294 }),
295 ).toBe("feat/add-search");
296 });
297});