|
| 1 | +import type { MockedFunction } from "vitest" |
| 2 | + |
| 3 | +import { listFiles } from "../../../services/glob/list-files" |
| 4 | +import { getWorkspaceReadablePath, isPathOutsideWorkspace, resolvePathInWorkspace } from "../../../utils/pathUtils" |
| 5 | +import type { ToolUse } from "../../../shared/tools" |
| 6 | +import { listFilesTool } from "../ListFilesTool" |
| 7 | + |
| 8 | +vi.mock("../../../services/glob/list-files", () => ({ |
| 9 | + listFiles: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock("../../../utils/pathUtils", async () => { |
| 13 | + const actual = await vi.importActual<typeof import("../../../utils/pathUtils")>("../../../utils/pathUtils") |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + resolvePathInWorkspace: vi.fn(), |
| 17 | + getWorkspaceReadablePath: vi.fn(), |
| 18 | + isPathOutsideWorkspace: vi.fn(), |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +vi.mock("../../prompts/responses", () => ({ |
| 23 | + formatResponse: { |
| 24 | + formatFilesList: vi.fn(), |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +describe("listFilesTool", () => { |
| 29 | + const cwd = "/workspace/primary" |
| 30 | + const relPath = "secondary/src" |
| 31 | + const absolutePath = "/workspace/secondary/src" |
| 32 | + |
| 33 | + const mockedListFiles = listFiles as MockedFunction<typeof listFiles> |
| 34 | + const mockedResolvePathInWorkspace = resolvePathInWorkspace as MockedFunction<typeof resolvePathInWorkspace> |
| 35 | + const mockedGetWorkspaceReadablePath = getWorkspaceReadablePath as MockedFunction<typeof getWorkspaceReadablePath> |
| 36 | + const mockedIsPathOutsideWorkspace = isPathOutsideWorkspace as MockedFunction<typeof isPathOutsideWorkspace> |
| 37 | + |
| 38 | + let task: any |
| 39 | + let askApproval: ReturnType<typeof vi.fn> |
| 40 | + let pushToolResult: ReturnType<typeof vi.fn> |
| 41 | + let handleError: ReturnType<typeof vi.fn> |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + |
| 46 | + mockedResolvePathInWorkspace.mockResolvedValue(absolutePath) |
| 47 | + mockedGetWorkspaceReadablePath.mockReturnValue("secondary/src") |
| 48 | + mockedIsPathOutsideWorkspace.mockReturnValue(false) |
| 49 | + mockedListFiles.mockResolvedValue([["a.ts", "b.ts"], false]) |
| 50 | + |
| 51 | + task = { |
| 52 | + cwd, |
| 53 | + consecutiveMistakeCount: 0, |
| 54 | + didToolFailInCurrentTurn: false, |
| 55 | + recordToolError: vi.fn(), |
| 56 | + rooIgnoreController: {}, |
| 57 | + rooProtectedController: {}, |
| 58 | + providerRef: { |
| 59 | + deref: vi.fn().mockReturnValue({ |
| 60 | + getState: vi.fn().mockResolvedValue({ showRooIgnoredFiles: true }), |
| 61 | + }), |
| 62 | + }, |
| 63 | + sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing path"), |
| 64 | + ask: vi.fn().mockResolvedValue(undefined), |
| 65 | + } |
| 66 | + |
| 67 | + askApproval = vi.fn().mockResolvedValue(true) |
| 68 | + pushToolResult = vi.fn() |
| 69 | + handleError = vi.fn() |
| 70 | + }) |
| 71 | + |
| 72 | + it("lists files from the resolved workspace path and surfaces the workspace-readable path", async () => { |
| 73 | + const { formatResponse } = await import("../../prompts/responses") |
| 74 | + vi.mocked(formatResponse.formatFilesList).mockReturnValue("formatted file list") |
| 75 | + |
| 76 | + await listFilesTool.execute({ path: relPath, recursive: true }, task, { |
| 77 | + askApproval, |
| 78 | + pushToolResult, |
| 79 | + handleError, |
| 80 | + }) |
| 81 | + |
| 82 | + expect(mockedResolvePathInWorkspace).toHaveBeenCalledWith(cwd, relPath) |
| 83 | + expect(mockedListFiles).toHaveBeenCalledWith(absolutePath, true, 200) |
| 84 | + expect(formatResponse.formatFilesList).toHaveBeenCalledWith( |
| 85 | + absolutePath, |
| 86 | + ["a.ts", "b.ts"], |
| 87 | + false, |
| 88 | + task.rooIgnoreController, |
| 89 | + true, |
| 90 | + task.rooProtectedController, |
| 91 | + ) |
| 92 | + expect(askApproval).toHaveBeenCalledWith("tool", expect.stringContaining('"path":"secondary/src"')) |
| 93 | + expect(pushToolResult).toHaveBeenCalledWith("formatted file list") |
| 94 | + }) |
| 95 | + |
| 96 | + it("uses the resolved workspace path in partial payloads", async () => { |
| 97 | + const block: ToolUse<"list_files"> = { |
| 98 | + type: "tool_use", |
| 99 | + name: "list_files", |
| 100 | + params: { path: relPath, recursive: "true" }, |
| 101 | + partial: true, |
| 102 | + } |
| 103 | + |
| 104 | + await listFilesTool.handlePartial(task, block) |
| 105 | + |
| 106 | + expect(task.ask).toHaveBeenCalledWith("tool", expect.stringContaining('"path":"secondary/src"'), true) |
| 107 | + expect(task.ask).toHaveBeenCalledWith("tool", expect.stringContaining('"tool":"listFilesRecursive"'), true) |
| 108 | + }) |
| 109 | +}) |
0 commit comments