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

git.test.ts

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