|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { BatchTool } from "../../src/tool/batch" |
| 3 | + |
| 4 | +// BatchTool is a Tool.Info object; call .init() to get schema + helpers. |
| 5 | +async function getToolInfo() { |
| 6 | + return BatchTool.init() |
| 7 | +} |
| 8 | + |
| 9 | +describe("BatchTool: schema validation", () => { |
| 10 | + test("rejects empty tool_calls array", async () => { |
| 11 | + const tool = await getToolInfo() |
| 12 | + const result = tool.parameters.safeParse({ tool_calls: [] }) |
| 13 | + expect(result.success).toBe(false) |
| 14 | + }) |
| 15 | + |
| 16 | + test("accepts single tool call", async () => { |
| 17 | + const tool = await getToolInfo() |
| 18 | + const result = tool.parameters.safeParse({ |
| 19 | + tool_calls: [{ tool: "read", parameters: { file_path: "/tmp/x" } }], |
| 20 | + }) |
| 21 | + expect(result.success).toBe(true) |
| 22 | + }) |
| 23 | + |
| 24 | + test("accepts multiple tool calls", async () => { |
| 25 | + const tool = await getToolInfo() |
| 26 | + const result = tool.parameters.safeParse({ |
| 27 | + tool_calls: [ |
| 28 | + { tool: "read", parameters: { file_path: "/tmp/a" } }, |
| 29 | + { tool: "grep", parameters: { pattern: "foo" } }, |
| 30 | + ], |
| 31 | + }) |
| 32 | + expect(result.success).toBe(true) |
| 33 | + }) |
| 34 | + |
| 35 | + test("rejects tool call without tool name", async () => { |
| 36 | + const tool = await getToolInfo() |
| 37 | + const result = tool.parameters.safeParse({ |
| 38 | + tool_calls: [{ parameters: { file_path: "/tmp/x" } }], |
| 39 | + }) |
| 40 | + expect(result.success).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + test("rejects tool call without parameters object", async () => { |
| 44 | + const tool = await getToolInfo() |
| 45 | + const result = tool.parameters.safeParse({ |
| 46 | + tool_calls: [{ tool: "read" }], |
| 47 | + }) |
| 48 | + expect(result.success).toBe(false) |
| 49 | + }) |
| 50 | + |
| 51 | + test("accepts tool call with empty parameters", async () => { |
| 52 | + const tool = await getToolInfo() |
| 53 | + const result = tool.parameters.safeParse({ |
| 54 | + tool_calls: [{ tool: "read", parameters: {} }], |
| 55 | + }) |
| 56 | + expect(result.success).toBe(true) |
| 57 | + }) |
| 58 | +}) |
| 59 | + |
| 60 | +describe("BatchTool: formatValidationError", () => { |
| 61 | + test("formatValidationError is defined", async () => { |
| 62 | + const tool = await getToolInfo() |
| 63 | + expect(tool.formatValidationError).toBeDefined() |
| 64 | + }) |
| 65 | + |
| 66 | + test("produces readable error message for empty array", async () => { |
| 67 | + const tool = await getToolInfo() |
| 68 | + expect(tool.formatValidationError).toBeDefined() |
| 69 | + const result = tool.parameters.safeParse({ tool_calls: [] }) |
| 70 | + expect(result.success).toBe(false) |
| 71 | + if (!result.success) { |
| 72 | + const msg = tool.formatValidationError!(result.error) |
| 73 | + expect(msg).toContain("Invalid parameters for tool 'batch'") |
| 74 | + expect(msg).toContain("Expected payload format") |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + test("includes field path in type error", async () => { |
| 79 | + const tool = await getToolInfo() |
| 80 | + expect(tool.formatValidationError).toBeDefined() |
| 81 | + const result = tool.parameters.safeParse({ |
| 82 | + tool_calls: [{ tool: 123, parameters: {} }], |
| 83 | + }) |
| 84 | + expect(result.success).toBe(false) |
| 85 | + if (!result.success) { |
| 86 | + const msg = tool.formatValidationError!(result.error) |
| 87 | + expect(msg).toContain("tool_calls") |
| 88 | + } |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe("BatchTool: DISALLOWED set enforcement", () => { |
| 93 | + // The DISALLOWED set prevents recursive batch-in-batch calls. |
| 94 | + // This is a critical safety mechanism — if the LLM can batch the batch tool, |
| 95 | + // it creates infinite recursion. |
| 96 | + // We verify the source code's DISALLOWED set by checking the module exports. |
| 97 | + test("batch tool id is 'batch'", () => { |
| 98 | + expect(BatchTool.id).toBe("batch") |
| 99 | + }) |
| 100 | + |
| 101 | + // The 25-call cap and DISALLOWED enforcement happen inside execute(), |
| 102 | + // which requires a full Session context. We verify the schema allows |
| 103 | + // up to 25+ items at parse time (the cap is enforced at runtime). |
| 104 | + test("schema accepts 25 tool calls (runtime cap is in execute)", async () => { |
| 105 | + const tool = await getToolInfo() |
| 106 | + const calls = Array.from({ length: 25 }, (_, i) => ({ |
| 107 | + tool: `tool_${i}`, |
| 108 | + parameters: {}, |
| 109 | + })) |
| 110 | + const result = tool.parameters.safeParse({ tool_calls: calls }) |
| 111 | + expect(result.success).toBe(true) |
| 112 | + }) |
| 113 | + |
| 114 | + test("schema accepts 26+ tool calls (runtime slices to 25)", async () => { |
| 115 | + const tool = await getToolInfo() |
| 116 | + const calls = Array.from({ length: 30 }, (_, i) => ({ |
| 117 | + tool: `tool_${i}`, |
| 118 | + parameters: {}, |
| 119 | + })) |
| 120 | + const result = tool.parameters.safeParse({ tool_calls: calls }) |
| 121 | + // Schema allows it — the 25-cap is enforced in execute() |
| 122 | + expect(result.success).toBe(true) |
| 123 | + }) |
| 124 | +}) |
0 commit comments