@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 417 lines 14.8 KB
Raw
1 import { SourceControlProviderKind } from "@t3tools/contracts";
2 import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
3 import { describe, expect, it } from "vite-plus/test";
4
5 import {
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
19 describe("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
90 describe("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
142 describe("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 for (const newline of ["\n", "\r\n"]) {
168 for (const comment of ["#", ";"]) {
169 const config = [
170 '[remote "upstream"]',
171 `url = https://github.com/other/repo.git ${comment} ignored backslash \\`,
172 '[remote "origin"]',
173 `url = https://github.com/acme/repo.git ${comment} ignored backslash \\`,
174 `${comment} ignored backslash \\`,
175 '[remote "fork"]',
176 "url = https://github.com/me/repo.git",
177 ].join(newline);
178 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
179 }
180 }
181 });
182
183 it("accepts legacy dotted headers, header comments, and line continuations", () => {
184 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
185 "git@github.com:a/b.git",
186 );
187 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
188 "git@github.com:a/b.git",
189 );
190 expect(
191 parseOriginUrlFromGitConfig(
192 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
193 ),
194 ).toBe("git@github.com:a/b.git");
195 expect(
196 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
197 ).toBe("git@github.com:a/b.git");
198 expect(
199 parseOriginUrlFromGitConfig(
200 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
201 ),
202 ).toBe("https://github.com/acme/\t\trepo.git");
203 });
204
205 it("reads settings on the remote header line", () => {
206 const upstream = '\n[remote "upstream"]\n\turl = https://github.com/other/source.git\n';
207 expect(
208 parseOriginUrlFromGitConfig(`[remote "origin"] url = git@github.com:a/b.git${upstream}`),
209 ).toBe("git@github.com:a/b.git");
210 expect(parseOriginUrlFromGitConfig("[remote.Origin]url=git@github.com:a/b.git\n")).toBe(
211 "git@github.com:a/b.git",
212 );
213 expect(
214 parseOriginUrlFromGitConfig(
215 `[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/*\n\turl = git@github.com:a/b.git${upstream}`,
216 ),
217 ).toBe("git@github.com:a/b.git");
218 });
219
220 it("falls back to the first remote when there is no origin", () => {
221 const config = [
222 '[remote "upstream"]',
223 "\turl = https://github.com/acme/repo.git",
224 '[remote "fork"]',
225 "\turl = https://github.com/me/repo.git",
226 ].join("\n");
227 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
228 });
229 });
230
231 describe("isTemporaryWorktreeBranch", () => {
232 it("matches generated temporary worktree refs", () => {
233 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
234 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
235 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
236 });
237
238 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
239 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
240 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
241 );
242 });
243
244 it("matches legacy t3code temporary worktree refs", () => {
245 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
246 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
247 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
248 expect(
249 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
250 ).toBe(false);
251 });
252
253 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
254 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
255 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
256 });
257
258 it("matches the flat fallback used when a plain t3 branch exists", () => {
259 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
260 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
261 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
262 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
263 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
264 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
265 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
266 );
267 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
268 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
269 );
270 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
271 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
272 );
273 });
274
275 it("rejects non-temporary refName names", () => {
276 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
277 expect(isTemporaryWorktreeBranch("main")).toBe(false);
278 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
279 });
280 });
281
282 describe("applyGitStatusStreamEvent", () => {
283 it("treats a remote-only update as a repository when local state is missing", () => {
284 const remote: VcsStatusRemoteResult = {
285 hasUpstream: true,
286 aheadCount: 2,
287 behindCount: 1,
288 pr: null,
289 };
290
291 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
292 isRepo: true,
293 hasPrimaryRemote: false,
294 isDefaultRef: false,
295 refName: null,
296 hasWorkingTreeChanges: false,
297 workingTree: { files: [], insertions: 0, deletions: 0 },
298 hasUpstream: true,
299 aheadCount: 2,
300 behindCount: 1,
301 pr: null,
302 });
303 });
304
305 it("preserves local-only fields when applying a remote update", () => {
306 const current: VcsStatusResult = {
307 isRepo: true,
308 sourceControlProvider: {
309 kind: SourceControlProviderKind.make("github"),
310 name: "GitHub",
311 baseUrl: "https://github.com",
312 },
313 hasPrimaryRemote: true,
314 isDefaultRef: false,
315 refName: "feature/demo",
316 hasWorkingTreeChanges: true,
317 workingTree: {
318 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
319 insertions: 1,
320 deletions: 0,
321 },
322 hasUpstream: false,
323 aheadCount: 0,
324 behindCount: 0,
325 pr: null,
326 };
327
328 const remote: VcsStatusRemoteResult = {
329 hasUpstream: true,
330 aheadCount: 2,
331 behindCount: 1,
332 pr: null,
333 };
334
335 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
336 ...current,
337 hasUpstream: true,
338 aheadCount: 2,
339 behindCount: 1,
340 pr: null,
341 });
342 });
343 });
344
345 describe("sanitizeFeatureBranchName", () => {
346 it("sanitizes an explicit namespace before preserving it", () => {
347 expect(sanitizeFeatureBranchName(' "FIX//Calendar recruitment filter" ')).toBe(
348 "fix/calendar-recruitment-filter",
349 );
350 });
351
352 // regression: #15615 generated branch namespaces are preserved
353 it.each([
354 ["refine toolbar", "feature/refine-toolbar"],
355 [" /?. / ", "feature/update"],
356 ["fix///", "feature/fix"],
357 ])("keeps the fallback for unprefixed input %s", (input, expected) => {
358 expect(sanitizeFeatureBranchName(input)).toBe(expected);
359 });
360
361 it("keeps accented letters as their plain letters", () => {
362 expect(sanitizeFeatureBranchName("fix/Añadir validación del café")).toBe(
363 "fix/anadir-validacion-del-cafe",
364 );
365 });
366
367 it("keeps words with letters that have no Unicode decomposition whole", () => {
368 expect(sanitizeFeatureBranchName("Größe der Straße korrigieren")).toBe(
369 "feature/grosse-der-strasse-korrigieren",
370 );
371 expect(sanitizeFeatureBranchName("fix/Đặt tên")).toBe("fix/dat-ten");
372 expect(sanitizeFeatureBranchName("Napraw błąd w żółtym przycisku")).toBe(
373 "feature/napraw-blad-w-zoltym-przycisku",
374 );
375 });
376 });
377
378 describe("resolveAutoFeatureBranchName", () => {
379 it("resolves case-insensitive collisions within the generated namespace", () => {
380 expect(
381 resolveAutoFeatureBranchName(
382 ["FIX/refine-toolbar", "fix/refine-toolbar-2"],
383 "fix/refine-toolbar",
384 ),
385 ).toBe("fix/refine-toolbar-3");
386 });
387
388 // regression: #15615 generated branch namespaces are preserved
389 it.each([
390 ["fix/name", ["fix"], "fix-2/name"],
391 ["fix/name", ["fix/name/child"], "fix/name-2"],
392 ["fix/name", ["fixes", "fix/name-extra/child"], "fix/name"],
393 ])("resolves %s against namespace blockers %j", (preferredBranch, existingNames, expected) => {
394 expect(resolveAutoFeatureBranchName(existingNames, preferredBranch)).toBe(expected);
395 });
396 });
397
398 describe("formatGeneratedBranchName", () => {
399 it.each([
400 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
401 ["-team//feature@{new}", "team/feature-new/add-search"],
402 [" /?. / ", "add-search"],
403 ])("normalizes invalid static prefix %s", (prefix, expected) => {
404 expect(
405 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
406 ).toBe(expected);
407 });
408 it("uses the model's semantic prefix without the stored static prefix", () => {
409 expect(
410 formatGeneratedBranchName("feat/Add Search", {
411 mode: "semantic",
412 prefix: "t3code",
413 instructions: "",
414 }),
415 ).toBe("feat/add-search");
416 });
417 });