Open History to see the last commit
1import { assert, beforeEach, describe, it, vi } from "vitest";
2import type { ProcessRunOptions, ProcessRunResult } from "./processRunner";
3
4const { runProcessMock } = vi.hoisted(() => ({
5 runProcessMock:
6 vi.fn<
7 (
8 command: string,
9 args: readonly string[],
10 options?: ProcessRunOptions,
11 ) => Promise<ProcessRunResult>
12 >(),
13}));
14
15vi.mock("./processRunner", () => ({
16 runProcess: runProcessMock,
17}));
18
19function processResult(
20 overrides: Partial<ProcessRunResult> & Pick<ProcessRunResult, "stdout" | "code">,
21): ProcessRunResult {
22 return {
23 stdout: overrides.stdout,
24 code: overrides.code,
25 stderr: overrides.stderr ?? "",
26 signal: overrides.signal ?? null,
27 timedOut: overrides.timedOut ?? false,
28 stdoutTruncated: overrides.stdoutTruncated ?? false,
29 stderrTruncated: overrides.stderrTruncated ?? false,
30 };
31}
32
33describe("searchWorkspaceEntries git-ignore chunking", () => {
34 beforeEach(() => {
35 runProcessMock.mockReset();
36 vi.resetModules();
37 });
38
39 it("chunks git check-ignore stdin to avoid building giant strings", async () => {
40 const ignoredPaths = Array.from(
41 { length: 5000 },
42 (_, index) => `ignored/${index.toString().padStart(5, "0")}/${"x".repeat(80)}.ts`,
43 );
44 const keptPaths = ["src/keep.ts", "docs/readme.md"];
45 const listedPaths = [...ignoredPaths, ...keptPaths];
46 let checkIgnoreCalls = 0;
47
48 runProcessMock.mockImplementation(async (_command, args, options) => {
49 if (args[0] === "rev-parse") {
50 return processResult({ code: 0, stdout: "true\n" });
51 }
52
53 if (args[0] === "ls-files") {
54 return processResult({ code: 0, stdout: `${listedPaths.join("\0")}\0` });
55 }
56
57 if (args[0] === "check-ignore") {
58 checkIgnoreCalls += 1;
59 const chunkPaths = (options?.stdin ?? "").split("\0").filter((value) => value.length > 0);
60 const chunkIgnored = chunkPaths.filter((value) => value.startsWith("ignored/"));
61 return processResult({
62 code: chunkIgnored.length > 0 ? 0 : 1,
63 stdout: chunkIgnored.length > 0 ? `${chunkIgnored.join("\0")}\0` : "",
64 });
65 }
66
67 throw new Error(`Unexpected command: git ${args.join(" ")}`);
68 });
69
70 const { searchWorkspaceEntries } = await import("./workspaceEntries");
71 const result = await searchWorkspaceEntries({
72 cwd: "/virtual/workspace",
73 query: "",
74 limit: 100,
75 });
76
77 assert.isAbove(checkIgnoreCalls, 1);
78 assert.isFalse(result.entries.some((entry) => entry.path.startsWith("ignored/")));
79 assert.isTrue(result.entries.some((entry) => entry.path === "src/keep.ts"));
80 });
81});