|
| 1 | +import { NativeToolCallParser } from "../NativeToolCallParser" |
| 2 | + |
| 3 | +describe("NativeToolCallParser - Additional Coverage", () => { |
| 4 | + beforeEach(() => { |
| 5 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 6 | + NativeToolCallParser.clearRawChunkState() |
| 7 | + }) |
| 8 | + |
| 9 | + describe("hasActiveStreamingToolCalls", () => { |
| 10 | + it("should return false when no streaming tool calls are active", () => { |
| 11 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 12 | + expect(NativeToolCallParser.hasActiveStreamingToolCalls()).toBe(false) |
| 13 | + }) |
| 14 | + |
| 15 | + it("should return true when there is an active streaming tool call", () => { |
| 16 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 17 | + NativeToolCallParser.startStreamingToolCall("toolu_123", "read_file") |
| 18 | + expect(NativeToolCallParser.hasActiveStreamingToolCalls()).toBe(true) |
| 19 | + |
| 20 | + // Finalize to clear state |
| 21 | + NativeToolCallParser.finalizeStreamingToolCall("toolu_123") |
| 22 | + expect(NativeToolCallParser.hasActiveStreamingToolCalls()).toBe(false) |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + describe("getStreamingToolName", () => { |
| 27 | + it("should return undefined for a non-existent tool call id", () => { |
| 28 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 29 | + expect(NativeToolCallParser.getStreamingToolName("toolu_nonexistent")).toBeUndefined() |
| 30 | + }) |
| 31 | + |
| 32 | + it("should return the name of an active streaming tool call", () => { |
| 33 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 34 | + const testId = "toolu_stream_name_123" |
| 35 | + NativeToolCallParser.startStreamingToolCall(testId, "write_to_file") |
| 36 | + |
| 37 | + expect(NativeToolCallParser.getStreamingToolName(testId)).toBe("write_to_file") |
| 38 | + |
| 39 | + // Cleanup |
| 40 | + NativeToolCallParser.finalizeStreamingToolCall(testId) |
| 41 | + }) |
| 42 | + |
| 43 | + it("should return undefined after the tool call is finalized", () => { |
| 44 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 45 | + const testId = "toolu_stream_name_456" |
| 46 | + NativeToolCallParser.startStreamingToolCall(testId, "codebase_search") |
| 47 | + NativeToolCallParser.finalizeStreamingToolCall(testId) |
| 48 | + |
| 49 | + expect(NativeToolCallParser.getStreamingToolName(testId)).toBeUndefined() |
| 50 | + }) |
| 51 | + |
| 52 | + it("should return the name for MCP tools", () => { |
| 53 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 54 | + const testId = "toolu_mcp_name_789" |
| 55 | + const mcpToolName = "mcp--my-server--my-tool" |
| 56 | + NativeToolCallParser.startStreamingToolCall(testId, mcpToolName) |
| 57 | + |
| 58 | + expect(NativeToolCallParser.getStreamingToolName(testId)).toBe(mcpToolName) |
| 59 | + |
| 60 | + // Cleanup |
| 61 | + NativeToolCallParser.finalizeStreamingToolCall(testId) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe("processStreamingChunk with MCP tools", () => { |
| 66 | + it("should return null for MCP tools during streaming (wait for final)", () => { |
| 67 | + const id = "toolu_mcp_stream_123" |
| 68 | + NativeToolCallParser.startStreamingToolCall(id, "mcp--my-server--get_config") |
| 69 | + |
| 70 | + // For MCP tools, processStreamingChunk should return null |
| 71 | + const result = NativeToolCallParser.processStreamingChunk(id, '{"key":"value"}') |
| 72 | + expect(result).toBeNull() |
| 73 | + }) |
| 74 | + |
| 75 | + it("should return final McpToolUse for MCP tools on finalize", () => { |
| 76 | + const id = "toolu_mcp_finalize_123" |
| 77 | + NativeToolCallParser.startStreamingToolCall(id, "mcp--my-server--get_config") |
| 78 | + |
| 79 | + // Add arguments via processStreamingChunk (doesn't return partial for MCP) |
| 80 | + NativeToolCallParser.processStreamingChunk(id, '{"key":"value"}') |
| 81 | + |
| 82 | + const result = NativeToolCallParser.finalizeStreamingToolCall(id) |
| 83 | + expect(result).not.toBeNull() |
| 84 | + // MCP tools return "mcp_tool_use" type, not "tool_use" |
| 85 | + expect(result?.type).toMatch(/^(tool_use|mcp_tool_use)$/) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + describe("finalizeRawChunks edge cases", () => { |
| 90 | + it("should return empty array when no raw chunks are tracked", () => { |
| 91 | + NativeToolCallParser.clearRawChunkState() |
| 92 | + const events = NativeToolCallParser.finalizeRawChunks() |
| 93 | + expect(events).toHaveLength(0) |
| 94 | + }) |
| 95 | + |
| 96 | + it("should not emit events for tools that haven't started", () => { |
| 97 | + NativeToolCallParser.clearRawChunkState() |
| 98 | + |
| 99 | + // process a raw chunk without hasStarted (just tracking metadata) |
| 100 | + // This simulates the case where only arguments are received without start |
| 101 | + const result = NativeToolCallParser.processRawChunk({ |
| 102 | + index: 1, |
| 103 | + id: "toolu_partial", |
| 104 | + name: "read_file", |
| 105 | + arguments: undefined, // No arguments means no delta event, but still tracks |
| 106 | + }) |
| 107 | + |
| 108 | + // finalize should not emit for tools that haven't hasStarted |
| 109 | + const events = NativeToolCallParser.finalizeRawChunks() |
| 110 | + // The behavior depends on implementation - just verify it doesn't crash |
| 111 | + expect(Array.isArray(events)).toBe(true) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + describe("clearRawChunkState", () => { |
| 116 | + it("should clear all raw chunk tracking state", () => { |
| 117 | + // Add some raw chunks first |
| 118 | + NativeToolCallParser.processRawChunk({ |
| 119 | + index: 1, |
| 120 | + id: "toolu_clear_test", |
| 121 | + name: "read_file", |
| 122 | + arguments: '{"path":"test.ts"}', |
| 123 | + }) |
| 124 | + |
| 125 | + // Clear state |
| 126 | + NativeToolCallParser.clearRawChunkState() |
| 127 | + |
| 128 | + // Finalize should return empty array |
| 129 | + const events = NativeToolCallParser.finalizeRawChunks() |
| 130 | + expect(events).toHaveLength(0) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe("clearAllStreamingToolCalls", () => { |
| 135 | + it("should clear all streaming tool call state", () => { |
| 136 | + NativeToolCallParser.startStreamingToolCall("toolu_clear_stream_1", "read_file") |
| 137 | + NativeToolCallParser.startStreamingToolCall("toolu_clear_stream_2", "write_to_file") |
| 138 | + |
| 139 | + expect(NativeToolCallParser.hasActiveStreamingToolCalls()).toBe(true) |
| 140 | + |
| 141 | + // Clear all |
| 142 | + NativeToolCallParser.clearAllStreamingToolCalls() |
| 143 | + |
| 144 | + expect(NativeToolCallParser.hasActiveStreamingToolCalls()).toBe(false) |
| 145 | + expect(NativeToolCallParser.getStreamingToolName("toolu_clear_stream_1")).toBeUndefined() |
| 146 | + expect(NativeToolCallParser.getStreamingToolName("toolu_clear_stream_2")).toBeUndefined() |
| 147 | + }) |
| 148 | + }) |
| 149 | +}) |
0 commit comments