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

git.test.ts

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