|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Tool, ToolOverride } from ".."; |
| 3 | +import { applyToolOverrides } from "./applyToolOverrides"; |
| 4 | + |
| 5 | +const mockTool = (name: string, description: string): Tool => ({ |
| 6 | + type: "function", |
| 7 | + displayTitle: name, |
| 8 | + readonly: true, |
| 9 | + group: "test", |
| 10 | + function: { name, description }, |
| 11 | +}); |
| 12 | + |
| 13 | +describe("applyToolOverrides", () => { |
| 14 | + it("should return tools unchanged when no overrides provided", () => { |
| 15 | + const tools = [mockTool("read_file", "Read a file")]; |
| 16 | + const result = applyToolOverrides(tools, undefined); |
| 17 | + expect(result.tools).toEqual(tools); |
| 18 | + expect(result.errors).toHaveLength(0); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return tools unchanged when empty overrides array provided", () => { |
| 22 | + const tools = [mockTool("read_file", "Read a file")]; |
| 23 | + const result = applyToolOverrides(tools, []); |
| 24 | + expect(result.tools).toEqual(tools); |
| 25 | + expect(result.errors).toHaveLength(0); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should override description when specified", () => { |
| 29 | + const tools = [mockTool("read_file", "Original description")]; |
| 30 | + const overrides: ToolOverride[] = [ |
| 31 | + { name: "read_file", description: "New description" }, |
| 32 | + ]; |
| 33 | + const result = applyToolOverrides(tools, overrides); |
| 34 | + expect(result.tools[0].function.description).toBe("New description"); |
| 35 | + expect(result.errors).toHaveLength(0); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should override displayTitle when specified", () => { |
| 39 | + const tools = [mockTool("read_file", "Read a file")]; |
| 40 | + const overrides: ToolOverride[] = [ |
| 41 | + { name: "read_file", displayTitle: "Custom Read File" }, |
| 42 | + ]; |
| 43 | + const result = applyToolOverrides(tools, overrides); |
| 44 | + expect(result.tools[0].displayTitle).toBe("Custom Read File"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should override action phrases when specified", () => { |
| 48 | + const tools = [mockTool("read_file", "Read a file")]; |
| 49 | + tools[0].wouldLikeTo = "read {{{ filepath }}}"; |
| 50 | + tools[0].isCurrently = "reading {{{ filepath }}}"; |
| 51 | + tools[0].hasAlready = "read {{{ filepath }}}"; |
| 52 | + |
| 53 | + const overrides: ToolOverride[] = [ |
| 54 | + { |
| 55 | + name: "read_file", |
| 56 | + wouldLikeTo: "open {{{ filepath }}}", |
| 57 | + isCurrently: "opening {{{ filepath }}}", |
| 58 | + hasAlready: "opened {{{ filepath }}}", |
| 59 | + }, |
| 60 | + ]; |
| 61 | + const result = applyToolOverrides(tools, overrides); |
| 62 | + expect(result.tools[0].wouldLikeTo).toBe("open {{{ filepath }}}"); |
| 63 | + expect(result.tools[0].isCurrently).toBe("opening {{{ filepath }}}"); |
| 64 | + expect(result.tools[0].hasAlready).toBe("opened {{{ filepath }}}"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should disable tools when disabled: true", () => { |
| 68 | + const tools = [ |
| 69 | + mockTool("read_file", "Read"), |
| 70 | + mockTool("write_file", "Write"), |
| 71 | + ]; |
| 72 | + const overrides: ToolOverride[] = [{ name: "read_file", disabled: true }]; |
| 73 | + const result = applyToolOverrides(tools, overrides); |
| 74 | + expect(result.tools).toHaveLength(1); |
| 75 | + expect(result.tools[0].function.name).toBe("write_file"); |
| 76 | + expect(result.errors).toHaveLength(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should warn when override references unknown tool", () => { |
| 80 | + const tools = [mockTool("read_file", "Read")]; |
| 81 | + const overrides: ToolOverride[] = [ |
| 82 | + { name: "unknown_tool", description: "test" }, |
| 83 | + ]; |
| 84 | + const result = applyToolOverrides(tools, overrides); |
| 85 | + expect(result.tools).toHaveLength(1); |
| 86 | + expect(result.errors).toHaveLength(1); |
| 87 | + expect(result.errors[0].message).toContain("unknown_tool"); |
| 88 | + expect(result.errors[0].fatal).toBe(false); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should preserve unmodified fields", () => { |
| 92 | + const tools = [mockTool("read_file", "Original")]; |
| 93 | + tools[0].readonly = true; |
| 94 | + tools[0].group = "Built-In"; |
| 95 | + |
| 96 | + const overrides: ToolOverride[] = [ |
| 97 | + { name: "read_file", description: "New description" }, |
| 98 | + ]; |
| 99 | + const result = applyToolOverrides(tools, overrides); |
| 100 | + expect(result.tools[0].readonly).toBe(true); |
| 101 | + expect(result.tools[0].group).toBe("Built-In"); |
| 102 | + expect(result.tools[0].displayTitle).toBe("read_file"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should override systemMessageDescription", () => { |
| 106 | + const tools = [mockTool("read_file", "Read")]; |
| 107 | + tools[0].systemMessageDescription = { |
| 108 | + prefix: "old prefix", |
| 109 | + exampleArgs: [["filepath", "/old/path"]], |
| 110 | + }; |
| 111 | + |
| 112 | + const overrides: ToolOverride[] = [ |
| 113 | + { |
| 114 | + name: "read_file", |
| 115 | + systemMessageDescription: { |
| 116 | + prefix: "new prefix", |
| 117 | + exampleArgs: [["filepath", "/new/path"]], |
| 118 | + }, |
| 119 | + }, |
| 120 | + ]; |
| 121 | + const result = applyToolOverrides(tools, overrides); |
| 122 | + expect(result.tools[0].systemMessageDescription?.prefix).toBe("new prefix"); |
| 123 | + expect(result.tools[0].systemMessageDescription?.exampleArgs).toEqual([ |
| 124 | + ["filepath", "/new/path"], |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should partially override systemMessageDescription", () => { |
| 129 | + const tools = [mockTool("read_file", "Read")]; |
| 130 | + tools[0].systemMessageDescription = { |
| 131 | + prefix: "old prefix", |
| 132 | + exampleArgs: [["filepath", "/old/path"]], |
| 133 | + }; |
| 134 | + |
| 135 | + const overrides: ToolOverride[] = [ |
| 136 | + { |
| 137 | + name: "read_file", |
| 138 | + systemMessageDescription: { |
| 139 | + prefix: "new prefix", |
| 140 | + // exampleArgs not specified - should preserve original |
| 141 | + }, |
| 142 | + }, |
| 143 | + ]; |
| 144 | + const result = applyToolOverrides(tools, overrides); |
| 145 | + expect(result.tools[0].systemMessageDescription?.prefix).toBe("new prefix"); |
| 146 | + expect(result.tools[0].systemMessageDescription?.exampleArgs).toEqual([ |
| 147 | + ["filepath", "/old/path"], |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should apply multiple overrides", () => { |
| 152 | + const tools = [ |
| 153 | + mockTool("read_file", "Read"), |
| 154 | + mockTool("write_file", "Write"), |
| 155 | + mockTool("delete_file", "Delete"), |
| 156 | + ]; |
| 157 | + |
| 158 | + const overrides: ToolOverride[] = [ |
| 159 | + { name: "read_file", description: "Custom read" }, |
| 160 | + { name: "write_file", disabled: true }, |
| 161 | + { name: "delete_file", displayTitle: "Remove File" }, |
| 162 | + ]; |
| 163 | + |
| 164 | + const result = applyToolOverrides(tools, overrides); |
| 165 | + expect(result.tools).toHaveLength(2); |
| 166 | + expect(result.tools[0].function.description).toBe("Custom read"); |
| 167 | + expect(result.tools[1].displayTitle).toBe("Remove File"); |
| 168 | + expect(result.errors).toHaveLength(0); |
| 169 | + }); |
| 170 | + |
| 171 | + it("should not mutate original tools array", () => { |
| 172 | + const tools = [mockTool("read_file", "Original")]; |
| 173 | + const originalDescription = tools[0].function.description; |
| 174 | + |
| 175 | + const overrides: ToolOverride[] = [ |
| 176 | + { name: "read_file", description: "New description" }, |
| 177 | + ]; |
| 178 | + const result = applyToolOverrides(tools, overrides); |
| 179 | + |
| 180 | + // Original should be unchanged |
| 181 | + expect(tools[0].function.description).toBe(originalDescription); |
| 182 | + // Result should have new description |
| 183 | + expect(result.tools[0].function.description).toBe("New description"); |
| 184 | + }); |
| 185 | +}); |
0 commit comments