Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

git.test.ts

393 lines13.8 KB

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