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

git.test.ts

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