-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathqueries.test.ts
More file actions
641 lines (541 loc) · 20.8 KB
/
Copy pathqueries.test.ts
File metadata and controls
641 lines (541 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import { mkdir, mkdtemp, rm, unlink, writeFile } from "node:fs/promises";
import { devNull, tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createGitClient } from "./client";
import {
anyBranchRefExists,
type ChangedFileInfo,
computeDiffStatsFromFiles,
detectDefaultBranch,
getAllBranches,
getBranchDiffPatchesByPath,
getChangedFilesDetailed,
getGitBusyState,
getLinkedWorktreeMainPath,
listAllFiles,
remoteBranchExists,
splitUnifiedDiffByFile,
} from "./queries";
async function setupRepo(defaultBranch = "main"): Promise<string> {
const dir = await mkdtemp(path.join(tmpdir(), "posthog-code-queries-"));
const git = createGitClient(dir);
await git.init(["--initial-branch", defaultBranch]);
await git.addConfig("user.name", "Test");
await git.addConfig("user.email", "test@example.com");
await git.addConfig("commit.gpgsign", "false");
await writeFile(path.join(dir, "file.txt"), "content\n");
await git.add(["file.txt"]);
await git.commit("initial");
return dir;
}
describe("detectDefaultBranch", () => {
let repoDir: string;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
}
});
it("detects 'main' as default branch", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("main");
});
it("detects 'master' as default branch", async () => {
repoDir = await setupRepo("master");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("master");
});
it("detects non-standard default branch via init.defaultBranch config", async () => {
repoDir = await setupRepo("develop");
const git = createGitClient(repoDir);
// Set init.defaultBranch in the repo's local config
await git.addConfig("init.defaultBranch", "develop");
const result = await detectDefaultBranch(git);
expect(result).toBe("develop");
});
it("falls back to current branch when no standard branch exists", async () => {
repoDir = await setupRepo("trunk");
const git = createGitClient(repoDir);
const result = await detectDefaultBranch(git);
expect(result).toBe("trunk");
});
it("prefers 'main' over other detection methods", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
// Create additional branches
await git.checkoutLocalBranch("develop");
await git.checkout("main");
const result = await detectDefaultBranch(git);
expect(result).toBe("main");
});
it("prefers remote HEAD over local detection", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
// Set up a bare remote with a non-standard default branch
const remoteDir = await mkdtemp(
path.join(tmpdir(), "posthog-code-remote-"),
);
const remoteGit = createGitClient(remoteDir);
await remoteGit.init(["--bare", "--initial-branch", "production"]);
await git.addRemote("origin", remoteDir);
// Push main as production on remote and set HEAD
await git.push(["origin", "main:production"]);
await remoteGit.raw(["symbolic-ref", "HEAD", "refs/heads/production"]);
await git.fetch(["origin"]);
const result = await detectDefaultBranch(git);
expect(result).toBe("production");
await rm(remoteDir, { recursive: true, force: true });
});
});
describe("splitUnifiedDiffByFile", () => {
it("returns an empty map for empty input", () => {
expect(splitUnifiedDiffByFile("")).toEqual(new Map());
});
it("splits a two-file diff keyed by post-image path", () => {
const raw = [
"diff --git a/one.txt b/one.txt",
"index 0000000..1111111 100644",
"--- a/one.txt",
"+++ b/one.txt",
"@@ -1 +1 @@",
"-hello",
"+hello world",
"diff --git a/two.txt b/two.txt",
"new file mode 100644",
"--- /dev/null",
"+++ b/two.txt",
"@@ -0,0 +1 @@",
"+brand new",
"",
].join("\n");
const result = splitUnifiedDiffByFile(raw);
expect([...result.keys()]).toEqual(["one.txt", "two.txt"]);
expect(result.get("one.txt")).toContain("diff --git a/one.txt b/one.txt");
expect(result.get("one.txt")).toContain("+hello world");
expect(result.get("two.txt")).toContain("diff --git a/two.txt b/two.txt");
expect(result.get("two.txt")).toContain("+brand new");
});
it("keys renames by the post-rename (b/) path", () => {
const raw = [
"diff --git a/old.txt b/new.txt",
"similarity index 100%",
"rename from old.txt",
"rename to new.txt",
"",
].join("\n");
const result = splitUnifiedDiffByFile(raw);
expect(result.has("new.txt")).toBe(true);
expect(result.has("old.txt")).toBe(false);
expect(result.get("new.txt")).toContain("rename from old.txt");
});
it("handles binary diffs", () => {
const raw = [
"diff --git a/image.png b/image.png",
"Binary files a/image.png and b/image.png differ",
"",
].join("\n");
const result = splitUnifiedDiffByFile(raw);
expect(result.get("image.png")).toContain("Binary files");
});
});
describe("getBranchDiffPatchesByPath", () => {
let repoDir: string | undefined;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
repoDir = undefined;
}
});
async function setupBranchWithCommits(): Promise<{
repoDir: string;
remoteDir: string;
}> {
const workDir = await mkdtemp(path.join(tmpdir(), "posthog-code-branch-"));
const remoteDir = await mkdtemp(path.join(tmpdir(), "posthog-code-bare-"));
const remoteGit = createGitClient(remoteDir);
await remoteGit.init(["--bare", "--initial-branch", "main"]);
const git = createGitClient(workDir);
await git.init(["--initial-branch", "main"]);
await git.addConfig("user.name", "Test");
await git.addConfig("user.email", "test@example.com");
await git.addConfig("commit.gpgsign", "false");
await git.addRemote("origin", remoteDir);
await writeFile(path.join(workDir, "file.txt"), "line1\nline2\n");
await git.add(["file.txt"]);
await git.commit("initial");
await git.push(["origin", "main"]);
await git.checkoutLocalBranch("feature");
await writeFile(path.join(workDir, "file.txt"), "line1\nchanged\n");
await writeFile(path.join(workDir, "added.txt"), "new file\n");
await git.add(["file.txt", "added.txt"]);
await git.commit("feature work, not pushed");
return { repoDir: workDir, remoteDir };
}
it("returns per-file patches for commits not yet pushed", async () => {
const { repoDir: workDir, remoteDir } = await setupBranchWithCommits();
repoDir = workDir;
try {
const patches = await getBranchDiffPatchesByPath(
workDir,
"main",
"feature",
);
expect(patches.has("file.txt")).toBe(true);
expect(patches.has("added.txt")).toBe(true);
expect(patches.get("file.txt")).toContain("-line2");
expect(patches.get("file.txt")).toContain("+changed");
expect(patches.get("added.txt")).toContain("+new file");
} finally {
await rm(remoteDir, { recursive: true, force: true });
}
});
it("returns deletions keyed by their path", async () => {
const { repoDir: workDir, remoteDir } = await setupBranchWithCommits();
repoDir = workDir;
try {
const git = createGitClient(workDir);
await unlink(path.join(workDir, "file.txt"));
await git.add(["file.txt"]);
await git.commit("delete file.txt");
const patches = await getBranchDiffPatchesByPath(
workDir,
"main",
"feature",
);
expect(patches.get("file.txt")).toContain("deleted file mode");
} finally {
await rm(remoteDir, { recursive: true, force: true });
}
});
});
// Picked to land well past the default 64KB highWaterMark of
// `createReadStream` so the regression test actually exercises the
// across-chunk path of the streaming line counter.
const LINE_COUNT_LARGER_THAN_READ_STREAM_CHUNK = 800_000;
describe("getChangedFilesDetailed > untracked line counts", () => {
let repoDir: string;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
repoDir = "";
}
});
it.each([
{ name: "trailing newline", content: "a\nb\nc\n", expected: 3 },
{ name: "no trailing newline", content: "a\nb\nc", expected: 3 },
{ name: "single byte, no newline", content: "a", expected: 1 },
{ name: "lone newline", content: "\n", expected: 1 },
{ name: "consecutive newlines", content: "\n\n", expected: 2 },
// CRLF: legacy `split("\n")` counted only `\n` separators, so
// `"a\r\nb\r\n"` -> 2 lines. Byte-counter matches.
{ name: "CRLF endings", content: "a\r\nb\r\n", expected: 2 },
])("counts $name as $expected line(s)", async ({ content, expected }) => {
repoDir = await setupRepo();
await writeFile(path.join(repoDir, "f.txt"), content);
const files = await getChangedFilesDetailed(repoDir);
const f = files.find((file) => file.path === "f.txt");
expect(f).toMatchObject({ status: "untracked", linesAdded: expected });
});
it("reports 0 lines for empty untracked files", async () => {
repoDir = await setupRepo();
await writeFile(path.join(repoDir, "empty.txt"), "");
const files = await getChangedFilesDetailed(repoDir);
const empty = files.find((f) => f.path === "empty.txt");
expect(empty).toMatchObject({ status: "untracked", linesAdded: 0 });
});
// Regression guard for the OOM in #2218. Before the fix `countFileLines`
// read each untracked file's full content into memory via
// `fs.readFile(..., "utf-8")`, 16-way concurrent against every untracked
// path returned by `streamGitStatus` (up to 50k). On a monorepo with
// multi-MB build artifacts this exhausted the main-process V8 heap
// (`16 * file_bytes * 2` for V8's UTF-16) and froze the renderer waiting
// on the dead tRPC call. The fix stream-counts via `createReadStream`,
// so peak per-stream memory is ~64KB regardless of file size — the
// multi-MB case below would have OOM'd pre-fix and must still report an
// accurate line count.
it("stream-counts untracked files larger than the streaming chunk size", async () => {
repoDir = await setupRepo();
const content = "a\n".repeat(LINE_COUNT_LARGER_THAN_READ_STREAM_CHUNK);
await writeFile(path.join(repoDir, "huge.txt"), content);
const files = await getChangedFilesDetailed(repoDir);
const huge = files.find((f) => f.path === "huge.txt");
expect(huge).toMatchObject({
status: "untracked",
linesAdded: LINE_COUNT_LARGER_THAN_READ_STREAM_CHUNK,
});
});
// Regression for #2983 follow-up: an untracked binary file (png, mp4, …)
// must not have its newline bytes counted as added lines. Pre-fix this
// surfaced as an inflated diff badge (e.g. +8147) after dropping in a
// screenshot or screen recording.
it.each([["shot.png"], ["clip.mp4"]])(
"reports no line count for untracked binary file %s",
async (name) => {
repoDir = await setupRepo();
// Content packed with newline bytes — what the line counter would have
// tallied if it didn't skip binary files.
await writeFile(path.join(repoDir, name), "\n".repeat(8147));
const files = await getChangedFilesDetailed(repoDir);
const binary = files.find((f) => f.path === name);
expect(binary).toMatchObject({ status: "untracked" });
expect(binary?.linesAdded).toBeUndefined();
expect(binary?.linesRemoved).toBeUndefined();
},
);
});
describe("computeDiffStatsFromFiles", () => {
it("excludes binary files from line totals but still counts them as changed", () => {
const files: ChangedFileInfo[] = [
{
path: "src/app.ts",
status: "modified",
linesAdded: 10,
linesRemoved: 4,
},
// Binary line counts are meaningless newline-byte tallies — exclude them.
{
path: "assets/shot.png",
status: "untracked",
linesAdded: 8147,
linesRemoved: 0,
},
{
path: "assets/clip.mp4",
status: "untracked",
linesAdded: 5000,
linesRemoved: 0,
},
];
expect(computeDiffStatsFromFiles(files)).toEqual({
filesChanged: 3,
linesAdded: 10,
linesRemoved: 4,
});
});
});
describe("getAllBranches", () => {
let repoDir: string | undefined;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
repoDir = undefined;
}
});
async function setupRebaseConflict(dir: string): Promise<void> {
const git = createGitClient(dir);
await git.checkoutLocalBranch("feature");
await writeFile(path.join(dir, "file.txt"), "feature change\n");
await git.add(["file.txt"]);
await git.commit("on feature");
await git.checkout("main");
await writeFile(path.join(dir, "file.txt"), "main change\n");
await git.add(["file.txt"]);
await git.commit("on main");
await git.checkout("feature");
try {
await git.rebase(["main"]);
} catch {
// expected: rebase pauses on conflict, leaving HEAD on a pseudo-branch
}
}
it("returns only real branches, not the rebase pseudo-branch", async () => {
repoDir = await setupRepo("main");
await setupRebaseConflict(repoDir);
const branches = await getAllBranches(repoDir);
expect(branches).toEqual(expect.arrayContaining(["main", "feature"]));
expect(branches).not.toContain("(no");
expect(branches.every((b) => !b.startsWith("("))).toBe(true);
});
});
describe("getGitBusyState", () => {
let repoDir: string | undefined;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
repoDir = undefined;
}
});
it("reports busy=false in a clean repo", async () => {
repoDir = await setupRepo("main");
expect(await getGitBusyState(repoDir)).toEqual({ busy: false });
});
it("detects an in-progress rebase", async () => {
repoDir = await setupRepo("main");
const git = createGitClient(repoDir);
await git.checkoutLocalBranch("feature");
await writeFile(path.join(repoDir, "file.txt"), "feature change\n");
await git.add(["file.txt"]);
await git.commit("on feature");
await git.checkout("main");
await writeFile(path.join(repoDir, "file.txt"), "main change\n");
await git.add(["file.txt"]);
await git.commit("on main");
await git.checkout("feature");
try {
await git.rebase(["main"]);
} catch {
// expected: conflict
}
expect(await getGitBusyState(repoDir)).toEqual({
busy: true,
operation: "rebase",
});
});
});
describe("remoteBranchExists", () => {
let repoDir: string;
let remoteDir: string;
beforeEach(async () => {
remoteDir = await mkdtemp(path.join(tmpdir(), "posthog-code-bare-"));
const remoteGit = createGitClient(remoteDir);
await remoteGit.init(["--bare", "--initial-branch", "main"]);
repoDir = await mkdtemp(path.join(tmpdir(), "posthog-code-queries-"));
const git = createGitClient(repoDir);
await git.init(["--initial-branch", "main"]);
await git.addConfig("user.name", "Test");
await git.addConfig("user.email", "test@example.com");
await git.addConfig("commit.gpgsign", "false");
await git.addRemote("origin", remoteDir);
await writeFile(path.join(repoDir, "file.txt"), "content\n");
await git.add(["file.txt"]);
await git.commit("initial");
await git.push(["origin", "main"]);
await git.checkoutLocalBranch("remote-only");
await writeFile(path.join(repoDir, "extra.txt"), "extra\n");
await git.add(["extra.txt"]);
await git.commit("extra");
await git.push(["origin", "remote-only"]);
await git.checkout("main");
});
afterEach(async () => {
for (const d of [repoDir, remoteDir]) {
await rm(d, { recursive: true, force: true });
}
});
it.each([
{ branch: "main", expected: true },
{ branch: "remote-only", expected: true },
{ branch: "nonexistent", expected: false },
])("returns $expected for branch '$branch'", async ({ branch, expected }) => {
expect(await remoteBranchExists(repoDir, branch)).toBe(expected);
});
it("returns false when the remote is unreachable", async () => {
await createGitClient(repoDir).remote([
"set-url",
"origin",
"/nonexistent/path/to/remote",
]);
expect(await remoteBranchExists(repoDir, "main")).toBe(false);
});
});
describe("anyBranchRefExists", () => {
let repoDir: string;
// Builds refs via plumbing (commit-tree + update-ref) so the fixture also
// works in sandboxes where `git commit` is unavailable.
beforeEach(async () => {
repoDir = await mkdtemp(path.join(tmpdir(), "posthog-code-refs-"));
const git = createGitClient(repoDir);
await git.init(["--initial-branch", "main"]);
await git.addConfig("user.name", "Test");
await git.addConfig("user.email", "test@example.com");
const tree = (
await git.raw(["hash-object", "-w", "-t", "tree", devNull])
).trim();
const sha = (await git.raw(["commit-tree", tree, "-m", "seed"])).trim();
await git.raw(["update-ref", "refs/heads/feat/local", sha]);
await git.raw(["update-ref", "refs/remotes/upstream/feat/remote", sha]);
await git.raw(["update-ref", "refs/tags/feat/tag-only", sha]);
});
afterEach(async () => {
await rm(repoDir, { recursive: true, force: true });
});
it.each([
{ branch: "feat/local", expected: true },
{ branch: "feat/remote", expected: true },
{ branch: "feat/gone", expected: false },
// A tag with the name does not resurrect a deleted branch.
{ branch: "feat/tag-only", expected: false },
])("returns $expected for '$branch'", async ({ branch, expected }) => {
expect(await anyBranchRefExists(repoDir, branch)).toBe(expected);
});
});
describe("getLinkedWorktreeMainPath", () => {
// The `.git` layouts are fabricated with plain fs (no git binary needed):
// a linked worktree is just a `.git` *file* whose `gitdir:` line points at
// `<main>/.git/worktrees/<name>`.
let baseDir: string;
let repoDir: string;
let worktreeDir: string;
beforeEach(async () => {
baseDir = await mkdtemp(path.join(tmpdir(), "posthog-code-wt-"));
repoDir = path.join(baseDir, "main-repo");
worktreeDir = path.join(baseDir, "my-worktree");
await mkdir(path.join(repoDir, ".git", "worktrees", "my-worktree"), {
recursive: true,
});
await mkdir(worktreeDir, { recursive: true });
await writeFile(
path.join(worktreeDir, ".git"),
`gitdir: ${path.join(repoDir, ".git", "worktrees", "my-worktree")}\n`,
);
});
afterEach(async () => {
await rm(baseDir, { recursive: true, force: true });
});
it("returns the main checkout path for a linked worktree", () => {
expect(getLinkedWorktreeMainPath(worktreeDir)).toBe(repoDir);
});
it("resolves a relative gitdir against the worktree", async () => {
await writeFile(
path.join(worktreeDir, ".git"),
"gitdir: ../main-repo/.git/worktrees/my-worktree\n",
);
expect(getLinkedWorktreeMainPath(worktreeDir)).toBe(repoDir);
});
it("returns null for the main checkout (.git is a directory)", () => {
expect(getLinkedWorktreeMainPath(repoDir)).toBeNull();
});
it("returns null for a directory that is not a repository", () => {
expect(getLinkedWorktreeMainPath(baseDir)).toBeNull();
});
it("returns null for a submodule-style .git file", async () => {
await writeFile(
path.join(worktreeDir, ".git"),
"gitdir: ../main-repo/.git/modules/child\n",
);
expect(getLinkedWorktreeMainPath(worktreeDir)).toBeNull();
});
});
describe("listAllFiles", () => {
let repoDir: string;
afterEach(async () => {
if (repoDir) {
await rm(repoDir, { recursive: true, force: true });
}
});
it("combines tracked and untracked files uncapped by default", async () => {
repoDir = await setupRepo();
await writeFile(path.join(repoDir, "untracked.txt"), "content");
const files = await listAllFiles(repoDir);
expect(files.sort()).toEqual(["file.txt", "untracked.txt"]);
});
it("truncates to maxFiles", async () => {
repoDir = await setupRepo();
const git = createGitClient(repoDir);
await writeFile(path.join(repoDir, "b.txt"), "content");
await writeFile(path.join(repoDir, "c.txt"), "content");
await git.add(["b.txt", "c.txt"]);
await git.commit("add more files");
const files = await listAllFiles(repoDir, { maxFiles: 2 });
expect(files.length).toBe(2);
});
it("keeps untracked files over tracked ones when truncating", async () => {
repoDir = await setupRepo();
await writeFile(path.join(repoDir, "untracked.txt"), "content");
const files = await listAllFiles(repoDir, { maxFiles: 1 });
expect(files).toEqual(["untracked.txt"]);
});
});