|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { formatResponse } from "../responses" |
| 3 | + |
| 4 | +describe("formatResponse.toolError", () => { |
| 5 | + it("should format error without tool name when not provided", () => { |
| 6 | + const error = "Something went wrong" |
| 7 | + const result = formatResponse.toolError(error) |
| 8 | + |
| 9 | + expect(result).toBe("Tool Execution Error\n<error>\nSomething went wrong\n</error>") |
| 10 | + }) |
| 11 | + |
| 12 | + it("should format error with tool name when provided", () => { |
| 13 | + const error = "Invalid mode: test_mode" |
| 14 | + const toolName = "switch_mode" |
| 15 | + const result = formatResponse.toolError(error, toolName) |
| 16 | + |
| 17 | + expect(result).toBe("Tool Call Error: switch_mode\n<error>\nInvalid mode: test_mode\n</error>") |
| 18 | + }) |
| 19 | + |
| 20 | + it("should handle undefined error message", () => { |
| 21 | + const result = formatResponse.toolError(undefined, "new_task") |
| 22 | + |
| 23 | + expect(result).toBe("Tool Call Error: new_task\n<error>\nundefined\n</error>") |
| 24 | + }) |
| 25 | + |
| 26 | + it("should work with various tool names", () => { |
| 27 | + const testCases = [ |
| 28 | + { toolName: "write_to_file", expected: "Tool Call Error: write_to_file" }, |
| 29 | + { toolName: "execute_command", expected: "Tool Call Error: execute_command" }, |
| 30 | + { toolName: "apply_diff", expected: "Tool Call Error: apply_diff" }, |
| 31 | + { toolName: "new_task", expected: "Tool Call Error: new_task" }, |
| 32 | + { toolName: "use_mcp_tool", expected: "Tool Call Error: use_mcp_tool" }, |
| 33 | + ] |
| 34 | + |
| 35 | + testCases.forEach(({ toolName, expected }) => { |
| 36 | + const result = formatResponse.toolError("Test error", toolName) |
| 37 | + expect(result).toContain(expected) |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + it("should maintain backward compatibility when tool name is not provided", () => { |
| 42 | + // This ensures existing code that doesn't pass toolName still works |
| 43 | + const error = "Legacy error" |
| 44 | + const result = formatResponse.toolError(error) |
| 45 | + |
| 46 | + // Should not contain "Tool Call Error:" prefix |
| 47 | + expect(result).not.toContain("Tool Call Error:") |
| 48 | + // Should contain generic title |
| 49 | + expect(result).toContain("Tool Execution Error") |
| 50 | + }) |
| 51 | +}) |
0 commit comments