Elffuss BraidHub

kikocis/continue public

main · 3.4 KiBRawPermalink
1import { expect, test, vi } from "vitest";
2import { ToolExtras } from "../..";
3import * as walkDirModule from "../../indexing/walkDir";
4import { lsToolImpl, resolveLsToolDirPath } from "./lsTool";
5
6vi.mock("../../indexing/walkDir");
7
8const mockExtras = {
9 ide: {
10 fileExists: async (path: string) => true,
11 getWorkspaceDirs: async () => ["dir1"],
12 },
13} as unknown as ToolExtras;
14
15test("resolveLsToolDirPath handles undefined path", () => {
16 expect(resolveLsToolDirPath(undefined)).toBe("/");
17});
18
19test("resolveLsToolDirPath handles empty string", () => {
20 expect(resolveLsToolDirPath("")).toBe("/");
21});
22
23test("resolveLsToolDirPath handles dot", () => {
24 expect(resolveLsToolDirPath(".")).toBe("/");
25});
26
27test("resolveLsToolDirPath handles dot relative", () => {
28 expect(resolveLsToolDirPath("./hi")).toBe("/hi");
29});
30
31test("resolveLsToolDirPath normalizes backslashes to forward slashes", () => {
32 expect(resolveLsToolDirPath("path\\to\\dir")).toBe("path/to/dir");
33});
34
35test("resolveLsToolDirPath preserves forward slashes", () => {
36 expect(resolveLsToolDirPath("path/to/dir")).toBe("path/to/dir");
37});
38
39test("lsToolImpl truncates output when entries exceed MAX_LS_TOOL_LINES", async () => {
40 // Generate more than MAX_LS_TOOL_LINES entries (which is 200)
41 const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`);
42
43 // Mock walkDir to return our large array
44 vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries);
45
46 const result = await lsToolImpl(
47 { dirPath: "testDir", recursive: true },
48 mockExtras,
49 );
50
51 // Check that the result contains the truncation message
52 expect(result.length).toBe(2);
53 expect(result[0].content).toContain("file1.txt");
54 expect(result[0].content).toContain("file199.txt");
55 expect(result[1].content).toContain("Try using a non-recursive search");
56 expect(result[1].content).toContain("50 ls entries were truncated");
57
58 // Check that only MAX_LS_TOOL_LINES entries are included
59 const contentLines = result[0].content.split("\n");
60 // Account for the truncation message line
61 expect(contentLines.length).toBe(200);
62
63 // Check the suggestion to use non-recursive search is included
64 expect(result[1].content).toContain("Try using a non-recursive search");
65});
66
67test("lsToolImpl shows truncation message without suggestion for non-recursive search", async () => {
68 // Generate more than MAX_LS_TOOL_LINES entries
69 const mockEntries = Array.from({ length: 250 }, (_, i) => `file${i}.txt`);
70
71 // Mock walkDir to return our large array
72 vi.spyOn(walkDirModule, "walkDir").mockResolvedValue(mockEntries);
73
74 const result = await lsToolImpl(
75 { dirPath: "testDir", recursive: false },
76 mockExtras,
77 );
78
79 // Check that the result contains the truncation message
80 expect(result.length).toBe(2);
81 expect(result[1].content).toContain("50 ls entries were truncated");
82
83 // Check that the suggestion to use non-recursive search is NOT included
84 expect(result[1].content).not.toContain("Try using a non-recursive search");
85});
86
87test("lsToolImpl shows message when no files are found", async () => {
88 // Mock walkDir to return empty array
89 vi.spyOn(walkDirModule, "walkDir").mockResolvedValue([]);
90
91 const result = await lsToolImpl({ dirPath: "emptyDir" }, mockExtras);
92
93 // Check that the result contains the "no files found" message
94 expect(result.length).toBe(1);
95 expect(result[0].content).toBe("No files/folders found in emptyDir");
96});