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

git.test.ts

343 lines12.5 KB

1import type { VcsStatusRemoteResult, VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 applyGitStatusStreamEvent,
6 formatGeneratedBranchName,
7 buildTemporaryWorktreeBranchName,
8 flattenTemporaryWorktreeBranchName,
9 isTemporaryWorktreeBranch,
10 normalizeGitRemoteUrl,
11 parseGitHubRepositoryNameWithOwnerFromRemoteUrl,
12 parseOriginUrlFromGitConfig,
13 WORKTREE_BRANCH_PREFIX,
14} from "./git.ts";
15​
16describe("normalizeGitRemoteUrl", () => {
17 it("canonicalizes equivalent GitHub remotes across protocol variants", () => {
18 expect(normalizeGitRemoteUrl("git@github.com:T3Tools/T3Code.git")).toBe(
19 "github.com/t3tools/t3code",
20 );
21 expect(normalizeGitRemoteUrl("https://github.com/T3Tools/T3Code.git")).toBe(
22 "github.com/t3tools/t3code",
23 );
24 expect(normalizeGitRemoteUrl("ssh://git@github.com/T3Tools/T3Code")).toBe(
25 "github.com/t3tools/t3code",
26 );
27 });
28​
29 it("preserves nested group paths for providers like GitLab", () => {
30 expect(normalizeGitRemoteUrl("git@gitlab.com:T3Tools/platform/T3Code.git")).toBe(
31 "gitlab.com/t3tools/platform/t3code",
32 );
33 expect(normalizeGitRemoteUrl("https://gitlab.com/T3Tools/platform/T3Code.git")).toBe(
34 "gitlab.com/t3tools/platform/t3code",
35 );
36 });
37​
38 it("drops explicit ports from URL-shaped remotes", () => {
39 expect(normalizeGitRemoteUrl("https://gitlab.company.com:8443/team/project.git")).toBe(
40 "gitlab.company.com/team/project",
41 );
42 expect(normalizeGitRemoteUrl("ssh://git@gitlab.company.com:2222/team/project.git")).toBe(
43 "gitlab.company.com/team/project",
44 );
45 });
46​
47 it("normalizes SCP-like remotes with non-git SSH users", () => {
48 expect(normalizeGitRemoteUrl("gitlab@gitlab.example.com:group/project.git")).toBe(
49 "gitlab.example.com/group/project",
50 );
51 expect(normalizeGitRemoteUrl("deploy@bitbucket.org:workspace/repo.git")).toBe(
52 "bitbucket.org/workspace/repo",
53 );
54 });
55​
56 it("gives an Azure DevOps repository the same key over SSH as over HTTPS", () => {
57 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/Platform/T3Code")).toBe(
58 "dev.azure.com/t3tools/platform/_git/t3code",
59 );
60 expect(normalizeGitRemoteUrl("ssh://git@ssh.dev.azure.com:22/v3/T3Tools/Platform/T3Code")).toBe(
61 "dev.azure.com/t3tools/platform/_git/t3code",
62 );
63 expect(
64 normalizeGitRemoteUrl("https://T3Tools@dev.azure.com/T3Tools/Platform/_git/T3Code"),
65 ).toBe("dev.azure.com/t3tools/platform/_git/t3code");
66 });
67​
68 it("puts the organization back in the host on the name dev.azure.com replaced", () => {
69 expect(
70 normalizeGitRemoteUrl("T3Tools@vs-ssh.visualstudio.com:v3/T3Tools/Platform/T3Code"),
71 ).toBe("t3tools.visualstudio.com/platform/_git/t3code");
72 expect(normalizeGitRemoteUrl("https://T3Tools.visualstudio.com/Platform/_git/T3Code")).toBe(
73 "t3tools.visualstudio.com/platform/_git/t3code",
74 );
75 });
76​
77 it("leaves an Azure SSH host it cannot read as the path it was given", () => {
78 // Not `v3`, and not four segments: rewriting either would invent a repository that the web
79 // spelling has no name for, so the remote stands as it arrived.
80 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v4/T3Tools/Platform/T3Code")).toBe(
81 "ssh.dev.azure.com/v4/t3tools/platform/t3code",
82 );
83 expect(normalizeGitRemoteUrl("git@ssh.dev.azure.com:v3/T3Tools/T3Code")).toBe(
84 "ssh.dev.azure.com/v3/t3tools/t3code",
85 );
86 });
87});
88​
89describe("parseOriginUrlFromGitConfig", () => {
90 it("reads the origin url and ignores other remotes", () => {
91 const config = [
92 "[core]",
93 "\trepositoryformatversion = 0",
94 '[remote "upstream"]',
95 "\turl = https://github.com/other/repo.git",
96 '[remote "origin"]',
97 "\turl = git@github.com:pingdotgg/t3code.git",
98 "\tfetch = +refs/heads/*:refs/remotes/origin/*",
99 '[branch "main"]',
100 "\tremote = origin",
101 ].join("\n");
102 expect(parseOriginUrlFromGitConfig(config)).toBe("git@github.com:pingdotgg/t3code.git");
103 });
104​
105 it("strips inline comments and quotes from the url value", () => {
106 expect(
107 parseOriginUrlFromGitConfig(
108 '[remote "origin"]\n\turl = https://github.com/acme/repo.git # mirror\n',
109 ),
110 ).toBe("https://github.com/acme/repo.git");
111 expect(
112 parseOriginUrlFromGitConfig('[remote "origin"]\n\turl = "git@github.com:acme/repo.git"\n'),
113 ).toBe("git@github.com:acme/repo.git");
114 });
115​
116 it("accepts legacy dotted headers, header comments, and line continuations", () => {
117 expect(parseOriginUrlFromGitConfig("[remote.origin]\n\turl = git@github.com:a/b.git\n")).toBe(
118 "git@github.com:a/b.git",
119 );
120 // Git folds the dotted form to lowercase but keeps quoted names as written.
121 expect(parseOriginUrlFromGitConfig("[remote.Origin]\n\turl = git@github.com:a/b.git\n")).toBe(
122 "git@github.com:a/b.git",
123 );
124 expect(
125 parseOriginUrlFromGitConfig(
126 '[remote "Origin"]\n\turl = git@github.com:x/y.git\n[remote "origin"]\n\turl = git@github.com:a/b.git\n',
127 ),
128 ).toBe("git@github.com:a/b.git");
129 expect(
130 parseOriginUrlFromGitConfig('[remote "origin"] # primary\n\turl = git@github.com:a/b.git\n'),
131 ).toBe("git@github.com:a/b.git");
132 expect(
133 parseOriginUrlFromGitConfig(
134 '[remote "origin"]\n\turl = https://github.com/acme/\\\n\t\trepo.git\n',
135 ),
136 ).toBe("https://github.com/acme/repo.git");
137 });
138​
139 it("falls back to the first remote when there is no origin", () => {
140 const config = [
141 '[remote "upstream"]',
142 "\turl = https://github.com/acme/repo.git",
143 '[remote "fork"]',
144 "\turl = https://github.com/me/repo.git",
145 ].join("\n");
146 expect(parseOriginUrlFromGitConfig(config)).toBe("https://github.com/acme/repo.git");
147 });
148​
149 it("returns null when there is no remote section", () => {
150 expect(parseOriginUrlFromGitConfig("[core]\n\tbare = false\n")).toBeNull();
151 expect(parseOriginUrlFromGitConfig("")).toBeNull();
152 });
153});
154​
155describe("parseGitHubRepositoryNameWithOwnerFromRemoteUrl", () => {
156 it("extracts the owner and repository from common GitHub remote shapes", () => {
157 expect(
158 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("git@github.com:T3Tools/T3Code.git"),
159 ).toBe("T3Tools/T3Code");
160 expect(
161 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("https://github.com/T3Tools/T3Code.git"),
162 ).toBe("T3Tools/T3Code");
163 expect(
164 parseGitHubRepositoryNameWithOwnerFromRemoteUrl("ssh://github.com/T3Tools/T3Code.git"),
165 ).toBe("T3Tools/T3Code");
166 });
167});
168​
169describe("isTemporaryWorktreeBranch", () => {
170 it("matches the generated temporary worktree refName format", () => {
171 expect(
172 isTemporaryWorktreeBranch(
173 buildTemporaryWorktreeBranchName((byteLength) => {
174 expect(byteLength).toBe(4);
175 return "DEADBEEF";
176 }),
177 ),
178 ).toBe(true);
179 });
180​
181 it("matches generated temporary worktree refs", () => {
182 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef`)).toBe(true);
183 expect(isTemporaryWorktreeBranch(` ${WORKTREE_BRANCH_PREFIX}/deadbeef `)).toBe(true);
184 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/DEADBEEF`)).toBe(true);
185 });
186​
187 it("normalizes a UUID-shaped random callback to the canonical 8-hex form", () => {
188 expect(buildTemporaryWorktreeBranchName(() => "f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
189 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e`,
190 );
191 });
192​
193 it("matches legacy t3code temporary worktree refs", () => {
194 expect(isTemporaryWorktreeBranch("t3code/deadbeef")).toBe(true);
195 expect(isTemporaryWorktreeBranch("t3code-deadbeef")).toBe(true);
196 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(true);
197 expect(
198 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
199 ).toBe(false);
200 });
201​
202 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
203 // version nibble is not 4
204 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12")).toBe(false);
205 // variant nibble is not [89ab]
206 expect(isTemporaryWorktreeBranch("t3code/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12")).toBe(false);
207 });
208​
209 it("matches the flat fallback used when a plain t3 branch exists", () => {
210 const flat = flattenTemporaryWorktreeBranchName(`${WORKTREE_BRANCH_PREFIX}/deadbeef`);
211 expect(flat).toBe(`${WORKTREE_BRANCH_PREFIX}-deadbeef`);
212 expect(isTemporaryWorktreeBranch(flat)).toBe(true);
213 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-deadbeef-extra`)).toBe(false);
214 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}-feature`)).toBe(false);
215 expect(flattenTemporaryWorktreeBranchName("t3code/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12")).toBe(
216 `${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`,
217 );
218 expect(flattenTemporaryWorktreeBranchName("t3code-deadbeef")).toBe(
219 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
220 );
221 expect(flattenTemporaryWorktreeBranchName("t3code/deadbeef")).toBe(
222 `${WORKTREE_BRANCH_PREFIX}-deadbeef`,
223 );
224 });
225​
226 it("rejects non-temporary refName names", () => {
227 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
228 expect(isTemporaryWorktreeBranch("main")).toBe(false);
229 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
230 });
231});
232​
233describe("applyGitStatusStreamEvent", () => {
234 it("treats a remote-only update as a repository when local state is missing", () => {
235 const remote: VcsStatusRemoteResult = {
236 hasUpstream: true,
237 aheadCount: 2,
238 behindCount: 1,
239 pr: null,
240 };
241​
242 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
243 isRepo: true,
244 hasPrimaryRemote: false,
245 isDefaultRef: false,
246 refName: null,
247 hasWorkingTreeChanges: false,
248 workingTree: { files: [], insertions: 0, deletions: 0 },
249 hasUpstream: true,
250 aheadCount: 2,
251 behindCount: 1,
252 pr: null,
253 });
254 });
255​
256 it("preserves local-only fields when applying a remote update", () => {
257 const current: VcsStatusResult = {
258 isRepo: true,
259 sourceControlProvider: {
260 kind: "github",
261 name: "GitHub",
262 baseUrl: "https://github.com",
263 },
264 hasPrimaryRemote: true,
265 isDefaultRef: false,
266 refName: "feature/demo",
267 hasWorkingTreeChanges: true,
268 workingTree: {
269 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
270 insertions: 1,
271 deletions: 0,
272 },
273 hasUpstream: false,
274 aheadCount: 0,
275 behindCount: 0,
276 pr: null,
277 };
278​
279 const remote: VcsStatusRemoteResult = {
280 hasUpstream: true,
281 aheadCount: 2,
282 behindCount: 1,
283 pr: null,
284 };
285​
286 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
287 ...current,
288 hasUpstream: true,
289 aheadCount: 2,
290 behindCount: 1,
291 pr: null,
292 });
293 });
294});
295​
296describe("formatGeneratedBranchName", () => {
297 it.each(["t3code", "t3code/"])("joins static prefix %s with one slash", (prefix) => {
298 expect(
299 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
300 ).toBe("t3code/add-search");
301 });
302 it("supports an empty prefix and preserves user prefix casing", () => {
303 expect(
304 formatGeneratedBranchName("Add Search", { mode: "static", prefix: "", instructions: "" }),
305 ).toBe("add-search");
306 expect(
307 formatGeneratedBranchName("Add Search", {
308 mode: "static",
309 prefix: "Team/Julius/",
310 instructions: "",
311 }),
312 ).toBe("Team/Julius/add-search");
313 });
314 it.each([
315 ["release..candidate", "release-candidate/add-search"],
316 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
317 ["-team//feature@{new}", "team/feature-new/add-search"],
318 [" /?. / ", "add-search"],
319 ])("normalizes invalid static prefix %s", (prefix, expected) => {
320 expect(
321 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
322 ).toBe(expected);
323 });
324 it("uses the model's semantic prefix without the stored static prefix", () => {
325 expect(
326 formatGeneratedBranchName("feat/Add Search", {
327 mode: "semantic",
328 prefix: "t3code",
329 instructions: "",
330 }),
331 ).toBe("feat/add-search");
332 });
333 it("preserves the full custom name, including case, dots and length", () => {
334 const branch = `Julius/ABC-123/release.v2-${"x".repeat(70)}`;
335 expect(
336 formatGeneratedBranchName(` ${branch} `, {
337 mode: "custom",
338 prefix: "ignored",
339 instructions: "",
340 }),
341 ).toBe(branch);
342 });
343});