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

git.test.ts

340 lines12.2 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 UUID-shaped temporary worktree refs from older mobile builds", () => {
194 expect(
195 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`),
196 ).toBe(true);
197 });
198​
199 it("rejects UUID-shaped refs that are not RFC 4122 v4", () => {
200 // version nibble is not 4
201 expect(
202 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-1d48-b4f2-9cf0aa54ab12`),
203 ).toBe(false);
204 // variant nibble is not [89ab]
205 expect(
206 isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-c4f2-9cf0aa54ab12`),
207 ).toBe(false);
208 });
209​
210 it("matches the flat fallback used when a plain t3code 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(
217 flattenTemporaryWorktreeBranchName(
218 `${WORKTREE_BRANCH_PREFIX}/f4ae4e0e-f971-4d48-b4f2-9cf0aa54ab12`,
219 ),
220 ).toBe(`${WORKTREE_BRANCH_PREFIX}-f4ae4e0e`);
221 });
222​
223 it("rejects non-temporary refName names", () => {
224 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/feature/demo`)).toBe(false);
225 expect(isTemporaryWorktreeBranch("main")).toBe(false);
226 expect(isTemporaryWorktreeBranch(`${WORKTREE_BRANCH_PREFIX}/deadbeef-extra`)).toBe(false);
227 });
228});
229​
230describe("applyGitStatusStreamEvent", () => {
231 it("treats a remote-only update as a repository when local state is missing", () => {
232 const remote: VcsStatusRemoteResult = {
233 hasUpstream: true,
234 aheadCount: 2,
235 behindCount: 1,
236 pr: null,
237 };
238​
239 expect(applyGitStatusStreamEvent(null, { _tag: "remoteUpdated", remote })).toEqual({
240 isRepo: true,
241 hasPrimaryRemote: false,
242 isDefaultRef: false,
243 refName: null,
244 hasWorkingTreeChanges: false,
245 workingTree: { files: [], insertions: 0, deletions: 0 },
246 hasUpstream: true,
247 aheadCount: 2,
248 behindCount: 1,
249 pr: null,
250 });
251 });
252​
253 it("preserves local-only fields when applying a remote update", () => {
254 const current: VcsStatusResult = {
255 isRepo: true,
256 sourceControlProvider: {
257 kind: "github",
258 name: "GitHub",
259 baseUrl: "https://github.com",
260 },
261 hasPrimaryRemote: true,
262 isDefaultRef: false,
263 refName: "feature/demo",
264 hasWorkingTreeChanges: true,
265 workingTree: {
266 files: [{ path: "src/demo.ts", insertions: 1, deletions: 0 }],
267 insertions: 1,
268 deletions: 0,
269 },
270 hasUpstream: false,
271 aheadCount: 0,
272 behindCount: 0,
273 pr: null,
274 };
275​
276 const remote: VcsStatusRemoteResult = {
277 hasUpstream: true,
278 aheadCount: 2,
279 behindCount: 1,
280 pr: null,
281 };
282​
283 expect(applyGitStatusStreamEvent(current, { _tag: "remoteUpdated", remote })).toEqual({
284 ...current,
285 hasUpstream: true,
286 aheadCount: 2,
287 behindCount: 1,
288 pr: null,
289 });
290 });
291});
292​
293describe("formatGeneratedBranchName", () => {
294 it.each(["t3code", "t3code/"])("joins static prefix %s with one slash", (prefix) => {
295 expect(
296 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
297 ).toBe("t3code/add-search");
298 });
299 it("supports an empty prefix and preserves user prefix casing", () => {
300 expect(
301 formatGeneratedBranchName("Add Search", { mode: "static", prefix: "", instructions: "" }),
302 ).toBe("add-search");
303 expect(
304 formatGeneratedBranchName("Add Search", {
305 mode: "static",
306 prefix: "Team/Julius/",
307 instructions: "",
308 }),
309 ).toBe("Team/Julius/add-search");
310 });
311 it.each([
312 ["release..candidate", "release-candidate/add-search"],
313 [" Team / Jules.lock/", "Team/Jules-lock/add-search"],
314 ["-team//feature@{new}", "team/feature-new/add-search"],
315 [" /?. / ", "add-search"],
316 ])("normalizes invalid static prefix %s", (prefix, expected) => {
317 expect(
318 formatGeneratedBranchName("Add Search", { mode: "static", prefix, instructions: "" }),
319 ).toBe(expected);
320 });
321 it("uses the model's semantic prefix without the stored static prefix", () => {
322 expect(
323 formatGeneratedBranchName("feat/Add Search", {
324 mode: "semantic",
325 prefix: "t3code",
326 instructions: "",
327 }),
328 ).toBe("feat/add-search");
329 });
330 it("preserves the full custom name, including case, dots and length", () => {
331 const branch = `Julius/ABC-123/release.v2-${"x".repeat(70)}`;
332 expect(
333 formatGeneratedBranchName(` ${branch} `, {
334 mode: "custom",
335 prefix: "ignored",
336 instructions: "",
337 }),
338 ).toBe(branch);
339 });
340});