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

gitParsers.test.ts

417 lines14.9 KB

1import { describe, expect, it } from "vitest";
2​
3import {
4 deriveLocalBranchNameFromRemoteRef,
5 normalizeRemoteUrl,
6 parseBranchAb,
7 parseBranchLine,
8 parseDefaultBranchFromRemoteHeadRef,
9 parseNumstatEntries,
10 parsePorcelainPath,
11 parseRemoteFetchUrls,
12 parseRemoteNames,
13 parseRemoteRefWithRemoteNames,
14 parseTrackingBranchByUpstreamRef,
15 sanitizeRemoteName,
16} from "./gitParsers.ts";
17​
18// ── parseBranchAb ──
19​
20describe("parseBranchAb", () => {
21 it("parses valid ahead/behind counts", () => {
22 expect(parseBranchAb("+3 -5")).toEqual({ ahead: 3, behind: 5 });
23 expect(parseBranchAb("+0 -0")).toEqual({ ahead: 0, behind: 0 });
24 expect(parseBranchAb("+100 -42")).toEqual({ ahead: 100, behind: 42 });
25 });
26​
27 it("returns zeros for invalid format", () => {
28 expect(parseBranchAb("")).toEqual({ ahead: 0, behind: 0 });
29 expect(parseBranchAb("garbage")).toEqual({ ahead: 0, behind: 0 });
30 expect(parseBranchAb("3 -5")).toEqual({ ahead: 0, behind: 0 });
31 expect(parseBranchAb("+3 5")).toEqual({ ahead: 0, behind: 0 });
32 });
33​
34 it("returns zeros for partial matches", () => {
35 expect(parseBranchAb("+3")).toEqual({ ahead: 0, behind: 0 });
36 expect(parseBranchAb("-5")).toEqual({ ahead: 0, behind: 0 });
37 });
38});
39​
40// ── parseNumstatEntries ──
41​
42describe("parseNumstatEntries", () => {
43 it("parses standard numstat output", () => {
44 const stdout = "10\t5\tsrc/app.ts\n3\t1\tsrc/index.ts\n";
45 expect(parseNumstatEntries(stdout)).toEqual([
46 { path: "src/app.ts", insertions: 10, deletions: 5 },
47 { path: "src/index.ts", insertions: 3, deletions: 1 },
48 ]);
49 });
50​
51 it("handles binary files (- for insertions/deletions)", () => {
52 const stdout = "-\t-\timage.png\n";
53 expect(parseNumstatEntries(stdout)).toEqual([
54 { path: "image.png", insertions: 0, deletions: 0 },
55 ]);
56 });
57​
58 it("skips empty lines", () => {
59 const stdout = "10\t5\tsrc/app.ts\n\n\n3\t1\tsrc/index.ts\n";
60 expect(parseNumstatEntries(stdout)).toHaveLength(2);
61 });
62​
63 it("handles Windows line endings (CRLF)", () => {
64 const stdout = "10\t5\tsrc/app.ts\r\n3\t1\tsrc/index.ts\r\n";
65 expect(parseNumstatEntries(stdout)).toEqual([
66 { path: "src/app.ts", insertions: 10, deletions: 5 },
67 { path: "src/index.ts", insertions: 3, deletions: 1 },
68 ]);
69 });
70​
71 it("returns empty array for empty input", () => {
72 expect(parseNumstatEntries("")).toEqual([]);
73 expect(parseNumstatEntries("\n")).toEqual([]);
74 expect(parseNumstatEntries(" \n \n")).toEqual([]);
75 });
76​
77 it("handles simple renames (old => new)", () => {
78 // Git format: "10\t5\told.ts => new.ts"
79 const stdout = "10\t5\told.ts => new.ts\n";
80 const result = parseNumstatEntries(stdout);
81 expect(result).toEqual([{ path: "new.ts", insertions: 10, deletions: 5 }]);
82 });
83​
84 it("handles full path renames", () => {
85 const stdout = "10\t5\tsrc/old/file.ts => src/new/file.ts\n";
86 const result = parseNumstatEntries(stdout);
87 expect(result).toEqual([{ path: "src/new/file.ts", insertions: 10, deletions: 5 }]);
88 });
89​
90 // ── C009 Bug: braced rename syntax ──
91 // Git uses braced notation for partial renames: src/{old => new}/file.ts
92 // The current implementation slices after " => " producing "new}/file.ts"
93 // instead of expanding the braces to "src/new/file.ts".
94 it.fails("handles braced rename syntax (C009 bug)", () => {
95 // Git numstat format for partial path renames:
96 // "10\t5\tsrc/{old => new}/file.ts"
97 const stdout = "10\t5\tsrc/{old => new}/file.ts\n";
98 const result = parseNumstatEntries(stdout);
99 // Expected: braces expanded to full destination path
100 expect(result).toEqual([{ path: "src/new/file.ts", insertions: 10, deletions: 5 }]);
101 });
102​
103 it.fails("handles braced rename at root level (C009 bug)", () => {
104 // "{old-name.ts => new-name.ts}" at root
105 const stdout = "10\t5\t{old-name.ts => new-name.ts}\n";
106 const result = parseNumstatEntries(stdout);
107 expect(result).toEqual([{ path: "new-name.ts", insertions: 10, deletions: 5 }]);
108 });
109​
110 it.fails("handles braced rename with nested directories (C009 bug)", () => {
111 // "a/b/{c => d}/e/f.ts"
112 const stdout = "3\t1\ta/b/{c => d}/e/f.ts\n";
113 const result = parseNumstatEntries(stdout);
114 expect(result).toEqual([{ path: "a/b/d/e/f.ts", insertions: 3, deletions: 1 }]);
115 });
116​
117 it("handles single line with no trailing newline", () => {
118 const stdout = "10\t5\tsrc/app.ts";
119 expect(parseNumstatEntries(stdout)).toEqual([
120 { path: "src/app.ts", insertions: 10, deletions: 5 },
121 ]);
122 });
123​
124 it("handles tab-separated paths (rename with tab in original format)", () => {
125 // When git uses two-path format for renames: "10\t5\told\tnew"
126 const stdout = "10\t5\told.ts\tnew.ts\n";
127 const result = parseNumstatEntries(stdout);
128 // With pathParts.length > 1, takes the last part
129 expect(result[0]?.path).toBe("new.ts");
130 });
131});
132​
133// ── parsePorcelainPath ──
134​
135describe("parsePorcelainPath", () => {
136 it("parses untracked files (? prefix)", () => {
137 expect(parsePorcelainPath("? src/new-file.ts")).toBe("src/new-file.ts");
138 expect(parsePorcelainPath("? README.md")).toBe("README.md");
139 });
140​
141 it("parses ignored files (! prefix)", () => {
142 expect(parsePorcelainPath("! node_modules/package.json")).toBe("node_modules/package.json");
143 });
144​
145 it("returns null for empty path after prefix", () => {
146 expect(parsePorcelainPath("? ")).toBeNull();
147 expect(parsePorcelainPath("? ")).toBeNull();
148 });
149​
150 it("parses changed files (1 prefix) with tab-separated path", () => {
151 // Porcelain v2 format: "1 .M N... 100644 100644 100644 <hash> <hash> <TAB>file"
152 const line = "1 .M N... 100644 100644 100644 abc123 def456\tsrc/app.ts";
153 expect(parsePorcelainPath(line)).toBe("src/app.ts");
154 });
155​
156 it("parses renamed files (2 prefix) with tab-separated paths", () => {
157 // Porcelain v2 rename: "2 R. N... <modes> <hashes> R100\told\tnew"
158 const line = "2 R. N... 100644 100644 100644 abc123 def456 R100\told.ts\tnew.ts";
159 expect(parsePorcelainPath(line)).toBe("old.ts");
160 });
161​
162 it("parses unmerged files (u prefix)", () => {
163 const line = "u UU N... 100644 100644 100644 100644 abc123 def456 ghi789\tconflict.ts";
164 expect(parsePorcelainPath(line)).toBe("conflict.ts");
165 });
166​
167 it("returns null for unrecognized line formats", () => {
168 expect(parsePorcelainPath("# branch.oid abc123")).toBeNull();
169 expect(parsePorcelainPath("")).toBeNull();
170 expect(parsePorcelainPath("some random text")).toBeNull();
171 });
172​
173 it("falls back to last whitespace-separated part when no tab present", () => {
174 const line = "1 .M N... 100644 100644 100644 abc123 def456 src/app.ts";
175 expect(parsePorcelainPath(line)).toBe("src/app.ts");
176 });
177});
178​
179// ── parseBranchLine ──
180​
181describe("parseBranchLine", () => {
182 it("parses current branch (asterisk prefix)", () => {
183 expect(parseBranchLine("* main")).toEqual({ name: "main", current: true });
184 expect(parseBranchLine("* feature/new")).toEqual({ name: "feature/new", current: true });
185 });
186​
187 it("parses non-current branch", () => {
188 expect(parseBranchLine(" develop")).toEqual({ name: "develop", current: false });
189 expect(parseBranchLine(" feature/old")).toEqual({ name: "feature/old", current: false });
190 });
191​
192 it("parses worktree branch (+ prefix)", () => {
193 expect(parseBranchLine("+ wt-branch")).toEqual({ name: "wt-branch", current: false });
194 });
195​
196 it("returns null for empty lines", () => {
197 expect(parseBranchLine("")).toBeNull();
198 expect(parseBranchLine(" ")).toBeNull();
199 });
200​
201 it("returns null for symbolic refs", () => {
202 expect(parseBranchLine(" origin/HEAD -> origin/main")).toBeNull();
203 expect(parseBranchLine(" remotes/origin/HEAD -> origin/main")).toBeNull();
204 });
205​
206 it("returns null for detached HEAD", () => {
207 expect(parseBranchLine("* (HEAD detached at origin/main)")).toBeNull();
208 expect(parseBranchLine("* (HEAD detached at abc1234)")).toBeNull();
209 });
210​
211 it("trims whitespace from branch names", () => {
212 expect(parseBranchLine(" main ")).toEqual({ name: "main", current: false });
213 });
214});
215​
216// ── parseRemoteNames ──
217​
218describe("parseRemoteNames", () => {
219 it("parses remote names from output", () => {
220 const result = parseRemoteNames("origin\nfork\n");
221 expect(result).toEqual(["origin", "fork"]);
222 });
223​
224 it("returns empty array for empty input", () => {
225 expect(parseRemoteNames("")).toEqual([]);
226 expect(parseRemoteNames("\n")).toEqual([]);
227 });
228​
229 it("filters out blank lines", () => {
230 const result = parseRemoteNames("origin\n\n\nfork\n");
231 expect(result).toHaveLength(2);
232 });
233​
234 it("sorts by descending length", () => {
235 const result = parseRemoteNames("ab\nabcde\nabc\n");
236 expect(result).toEqual(["abcde", "abc", "ab"]);
237 });
238​
239 it("trims whitespace from names", () => {
240 const result = parseRemoteNames(" origin \n fork \n");
241 expect(result).toEqual(["origin", "fork"]);
242 });
243});
244​
245// ── sanitizeRemoteName ──
246​
247describe("sanitizeRemoteName", () => {
248 it("preserves valid remote names", () => {
249 expect(sanitizeRemoteName("origin")).toBe("origin");
250 expect(sanitizeRemoteName("my-fork")).toBe("my-fork");
251 expect(sanitizeRemoteName("upstream.v2")).toBe("upstream.v2");
252 expect(sanitizeRemoteName("user_repo")).toBe("user_repo");
253 });
254​
255 it("replaces invalid characters with hyphens", () => {
256 expect(sanitizeRemoteName("my fork")).toBe("my-fork");
257 expect(sanitizeRemoteName("user@repo")).toBe("user-repo");
258 expect(sanitizeRemoteName("a/b/c")).toBe("a-b-c");
259 });
260​
261 it("strips leading and trailing hyphens", () => {
262 expect(sanitizeRemoteName("-origin-")).toBe("origin");
263 expect(sanitizeRemoteName("---test---")).toBe("test");
264 });
265​
266 it("returns 'fork' for empty or all-invalid input", () => {
267 expect(sanitizeRemoteName("")).toBe("fork");
268 expect(sanitizeRemoteName(" ")).toBe("fork");
269 expect(sanitizeRemoteName("@@@")).toBe("fork");
270 });
271​
272 it("trims whitespace before sanitizing", () => {
273 expect(sanitizeRemoteName(" origin ")).toBe("origin");
274 });
275});
276​
277// ── normalizeRemoteUrl ──
278​
279describe("normalizeRemoteUrl", () => {
280 it("strips trailing slashes and .git suffix", () => {
281 expect(normalizeRemoteUrl("https://github.com/org/repo.git")).toBe(
282 "https://github.com/org/repo",
283 );
284 expect(normalizeRemoteUrl("https://github.com/org/repo/")).toBe(
285 "https://github.com/org/repo",
286 );
287 expect(normalizeRemoteUrl("https://github.com/org/repo.GIT///")).toBe(
288 "https://github.com/org/repo",
289 );
290 });
291​
292 it("lowercases the URL", () => {
293 expect(normalizeRemoteUrl("HTTPS://GitHub.com/Org/Repo")).toBe("https://github.com/org/repo");
294 });
295​
296 it("trims whitespace", () => {
297 expect(normalizeRemoteUrl(" https://example.com/repo ")).toBe("https://example.com/repo");
298 });
299});
300​
301// ── parseRemoteFetchUrls ──
302​
303describe("parseRemoteFetchUrls", () => {
304 it("parses fetch URLs from git remote -v output", () => {
305 const stdout = [
306 "origin\thttps://github.com/org/repo.git (fetch)",
307 "origin\thttps://github.com/org/repo.git (push)",
308 "upstream\thttps://github.com/upstream/repo.git (fetch)",
309 "upstream\thttps://github.com/upstream/repo.git (push)",
310 ].join("\n");
311 const result = parseRemoteFetchUrls(stdout);
312 expect(result.size).toBe(2);
313 expect(result.get("origin")).toBe("https://github.com/org/repo.git");
314 expect(result.get("upstream")).toBe("https://github.com/upstream/repo.git");
315 });
316​
317 it("ignores push-only entries", () => {
318 const stdout = "origin\thttps://example.com/repo.git (push)\n";
319 expect(parseRemoteFetchUrls(stdout).size).toBe(0);
320 });
321​
322 it("returns empty map for empty input", () => {
323 expect(parseRemoteFetchUrls("").size).toBe(0);
324 expect(parseRemoteFetchUrls("\n").size).toBe(0);
325 });
326});
327​
328// ── parseRemoteRefWithRemoteNames ──
329​
330describe("parseRemoteRefWithRemoteNames", () => {
331 const remotes = ["origin", "upstream"];
332​
333 it("parses remote ref into components", () => {
334 expect(parseRemoteRefWithRemoteNames("origin/main", remotes)).toEqual({
335 remoteRef: "origin/main",
336 remoteName: "origin",
337 localBranch: "main",
338 });
339 });
340​
341 it("handles nested branch names", () => {
342 expect(parseRemoteRefWithRemoteNames("upstream/feature/foo", remotes)).toEqual({
343 remoteRef: "upstream/feature/foo",
344 remoteName: "upstream",
345 localBranch: "feature/foo",
346 });
347 });
348​
349 it("returns null for unrecognized remotes", () => {
350 expect(parseRemoteRefWithRemoteNames("fork/main", remotes)).toBeNull();
351 });
352​
353 it("returns null for empty input", () => {
354 expect(parseRemoteRefWithRemoteNames("", remotes)).toBeNull();
355 expect(parseRemoteRefWithRemoteNames(" ", remotes)).toBeNull();
356 });
357​
358 it("returns null when branch name after remote is empty", () => {
359 expect(parseRemoteRefWithRemoteNames("origin/", remotes)).toBeNull();
360 });
361});
362​
363// ── parseTrackingBranchByUpstreamRef ──
364​
365describe("parseTrackingBranchByUpstreamRef", () => {
366 it("finds the branch tracking a given upstream ref", () => {
367 const stdout = "main\torigin/main\ndev\torigin/dev\n";
368 expect(parseTrackingBranchByUpstreamRef(stdout, "origin/main")).toBe("main");
369 expect(parseTrackingBranchByUpstreamRef(stdout, "origin/dev")).toBe("dev");
370 });
371​
372 it("returns null when no match", () => {
373 const stdout = "main\torigin/main\n";
374 expect(parseTrackingBranchByUpstreamRef(stdout, "origin/dev")).toBeNull();
375 });
376​
377 it("returns null for empty input", () => {
378 expect(parseTrackingBranchByUpstreamRef("", "origin/main")).toBeNull();
379 expect(parseTrackingBranchByUpstreamRef("\n", "origin/main")).toBeNull();
380 });
381});
382​
383// ── deriveLocalBranchNameFromRemoteRef ──
384​
385describe("deriveLocalBranchNameFromRemoteRef", () => {
386 it("strips the remote prefix", () => {
387 expect(deriveLocalBranchNameFromRemoteRef("origin/main")).toBe("main");
388 expect(deriveLocalBranchNameFromRemoteRef("upstream/feature/foo")).toBe("feature/foo");
389 });
390​
391 it("returns null for invalid refs", () => {
392 expect(deriveLocalBranchNameFromRemoteRef("main")).toBeNull();
393 expect(deriveLocalBranchNameFromRemoteRef("")).toBeNull();
394 expect(deriveLocalBranchNameFromRemoteRef("/main")).toBeNull();
395 expect(deriveLocalBranchNameFromRemoteRef("origin/")).toBeNull();
396 });
397});
398​
399// ── parseDefaultBranchFromRemoteHeadRef ──
400​
401describe("parseDefaultBranchFromRemoteHeadRef", () => {
402 it("extracts branch name from remote HEAD ref", () => {
403 expect(parseDefaultBranchFromRemoteHeadRef("refs/remotes/origin/main", "origin")).toBe("main");
404 expect(
405 parseDefaultBranchFromRemoteHeadRef("refs/remotes/upstream/develop", "upstream"),
406 ).toBe("develop");
407 });
408​
409 it("returns null for non-matching prefix", () => {
410 expect(parseDefaultBranchFromRemoteHeadRef("refs/heads/main", "origin")).toBeNull();
411 expect(parseDefaultBranchFromRemoteHeadRef("", "origin")).toBeNull();
412 });
413​
414 it("returns null for empty branch after prefix", () => {
415 expect(parseDefaultBranchFromRemoteHeadRef("refs/remotes/origin/", "origin")).toBeNull();
416 });
417});