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

git.test.ts

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