| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { SystemMessageToolCodeblocksFramework } from "."; |
| 3 | import { detectToolCallStart } from "../detectToolCallStart"; |
| 4 | |
| 5 | describe("detectToolCallStart", () => { |
| 6 | let framework = new SystemMessageToolCodeblocksFramework(); |
| 7 | it("detects standard tool call start", () => { |
| 8 | const buffer = "```tool\nTOOL_NAME: example_tool"; |
| 9 | const result = detectToolCallStart(buffer, framework); |
| 10 | |
| 11 | expect(result.isInToolCall).toBe(true); |
| 12 | expect(result.isInPartialStart).toBe(false); |
| 13 | expect(result.modifiedBuffer).toBe(buffer); |
| 14 | }); |
| 15 | |
| 16 | it("detects tool_name start without codeblock", () => { |
| 17 | const buffer = "tool_name: example_tool"; |
| 18 | const result = detectToolCallStart(buffer, framework); |
| 19 | |
| 20 | expect(result.isInToolCall).toBe(true); |
| 21 | expect(result.isInPartialStart).toBe(false); |
| 22 | expect(result.modifiedBuffer).toBe("```tool\nTOOL_NAME: example_tool"); |
| 23 | }); |
| 24 | |
| 25 | it("detects case-insensitive tool call start", () => { |
| 26 | const buffer = "```ToOl\nTOOL_NAME: example_tool"; |
| 27 | const result = detectToolCallStart(buffer, framework); |
| 28 | |
| 29 | expect(result.isInToolCall).toBe(true); |
| 30 | expect(result.isInPartialStart).toBe(false); |
| 31 | expect(result.modifiedBuffer).toBe(buffer); |
| 32 | }); |
| 33 | |
| 34 | it("detects case-insensitive tool_name start", () => { |
| 35 | const buffer = "ToOl_NaMe: example_tool"; |
| 36 | const result = detectToolCallStart(buffer, framework); |
| 37 | |
| 38 | expect(result.isInToolCall).toBe(true); |
| 39 | expect(result.isInPartialStart).toBe(false); |
| 40 | expect(result.modifiedBuffer).toBe("```tool\nTOOL_NAME: example_tool"); |
| 41 | }); |
| 42 | |
| 43 | it("identifies partial tool call start", () => { |
| 44 | const buffer = "```to"; |
| 45 | const result = detectToolCallStart(buffer, framework); |
| 46 | |
| 47 | expect(result.isInToolCall).toBe(false); |
| 48 | expect(result.isInPartialStart).toBe(true); |
| 49 | expect(result.modifiedBuffer).toBe(buffer); |
| 50 | }); |
| 51 | |
| 52 | it("identifies partial tool_name start", () => { |
| 53 | const buffer = "tool_na"; |
| 54 | const result = detectToolCallStart(buffer, framework); |
| 55 | |
| 56 | expect(result.isInToolCall).toBe(false); |
| 57 | expect(result.isInPartialStart).toBe(true); |
| 58 | expect(result.modifiedBuffer).toBe(buffer); |
| 59 | }); |
| 60 | |
| 61 | it("does not detect tool call in unrelated text", () => { |
| 62 | const buffer = "This is some regular text."; |
| 63 | const result = detectToolCallStart(buffer, framework); |
| 64 | |
| 65 | expect(result.isInToolCall).toBe(false); |
| 66 | expect(result.isInPartialStart).toBe(false); |
| 67 | expect(result.modifiedBuffer).toBe(buffer); |
| 68 | }); |
| 69 | |
| 70 | it("does not detect tool call in similar but different markdown block", () => { |
| 71 | const buffer = "```javascript\nconst x = 10;"; |
| 72 | const result = detectToolCallStart(buffer, framework); |
| 73 | |
| 74 | expect(result.isInToolCall).toBe(false); |
| 75 | expect(result.isInPartialStart).toBe(false); |
| 76 | expect(result.modifiedBuffer).toBe(buffer); |
| 77 | }); |
| 78 | }); |