| 1 | import { vi } from "vitest"; |
| 2 | |
| 3 | import { formatToolArgument } from "./formatters.js"; |
| 4 | |
| 5 | import { formatToolCall } from "./index.js"; |
| 6 | |
| 7 | describe("formatToolCall", () => { |
| 8 | beforeEach(() => { |
| 9 | // Mock process.cwd to return a consistent value |
| 10 | vi.spyOn(process, "cwd").mockReturnValue("/Users/test/project"); |
| 11 | }); |
| 12 | |
| 13 | afterEach(() => { |
| 14 | vi.restoreAllMocks(); |
| 15 | }); |
| 16 | |
| 17 | it("should format tool name without arguments", () => { |
| 18 | expect(formatToolCall("Write")).toBe("Write"); |
| 19 | expect(formatToolCall("Read", {})).toBe("Read"); |
| 20 | expect(formatToolCall("non_existent_tool", null)).toBe("non_existent_tool"); |
| 21 | }); |
| 22 | |
| 23 | it("should format tool name with relative path argument", () => { |
| 24 | expect(formatToolCall("Write", { filepath: "README.md" })).toBe( |
| 25 | "Write(README.md)", |
| 26 | ); |
| 27 | expect(formatToolCall("Read", { filepath: "src/index.ts" })).toBe( |
| 28 | "Read(src/index.ts)", |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | it("should convert absolute paths to relative paths", () => { |
| 33 | expect( |
| 34 | formatToolCall("Write", { |
| 35 | filepath: "/Users/test/project/README.md", |
| 36 | }), |
| 37 | ).toBe("Write(README.md)"); |
| 38 | |
| 39 | expect( |
| 40 | formatToolCall("Read", { |
| 41 | filepath: "/Users/test/project/src/components/App.tsx", |
| 42 | }), |
| 43 | ).toBe("Read(src/components/App.tsx)"); |
| 44 | }); |
| 45 | |
| 46 | it("should handle absolute paths outside the project", () => { |
| 47 | expect(formatToolCall("Write", { filepath: "/Users/other/file.txt" })).toBe( |
| 48 | "Write(../../other/file.txt)", |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | it("should handle non-path arguments", () => { |
| 53 | expect(formatToolCall("Search", { pattern: "TODO" })).toBe("Search(TODO)"); |
| 54 | expect(formatToolCall("Search", { pattern: 123 })).toBe("Search(123)"); |
| 55 | }); |
| 56 | |
| 57 | it("should use first argument when multiple are provided", () => { |
| 58 | expect( |
| 59 | formatToolCall("Write", { |
| 60 | filepath: "test.txt", |
| 61 | content: "Hello world", |
| 62 | }), |
| 63 | ).toBe("Write(test.txt)"); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe("formatToolArgument", () => { |
| 68 | beforeEach(() => { |
| 69 | vi.spyOn(process, "cwd").mockReturnValue("/Users/test/project"); |
| 70 | }); |
| 71 | |
| 72 | afterEach(() => { |
| 73 | vi.restoreAllMocks(); |
| 74 | }); |
| 75 | |
| 76 | it("should return empty string for null/undefined", () => { |
| 77 | expect(formatToolArgument(null)).toBe(""); |
| 78 | expect(formatToolArgument(undefined)).toBe(""); |
| 79 | }); |
| 80 | |
| 81 | it("should convert absolute paths to relative", () => { |
| 82 | expect(formatToolArgument("/Users/test/project/README.md")).toBe( |
| 83 | "README.md", |
| 84 | ); |
| 85 | expect(formatToolArgument("/Users/test/project/src/index.ts")).toBe( |
| 86 | "src/index.ts", |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | it("should preserve relative paths", () => { |
| 91 | expect(formatToolArgument("README.md")).toBe("README.md"); |
| 92 | expect(formatToolArgument("./src/index.ts")).toBe("./src/index.ts"); |
| 93 | }); |
| 94 | |
| 95 | it("should handle non-string values", () => { |
| 96 | expect(formatToolArgument(123)).toBe("123"); |
| 97 | expect(formatToolArgument(true)).toBe("true"); |
| 98 | expect(formatToolArgument({ key: "value" })).toBe("[object Object]"); |
| 99 | }); |
| 100 | }); |