File size: 11,303 Bytes
80ffd2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | package stream
import (
"bytes"
"os"
"strings"
"testing"
)
// TestNativeToolCallConversion tests conversion of native MorphLLM tool calls
// to Claude API format
func TestNativeToolCallConversion(t *testing.T) {
// Read test data - this contains native tool calls from MorphLLM
testData, err := os.ReadFile("/Users/leokun/Desktop/opus-api/一个完整的任务日志/4_upstream_response.txt")
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Verify required events
if !strings.Contains(outputStr, "event: message_start") {
t.Error("Missing message_start event")
}
if !strings.Contains(outputStr, "event: message_stop") {
t.Error("Missing message_stop event")
}
// Check for tool use events
if !strings.Contains(outputStr, "event: content_block_start") {
t.Error("Missing content_block_start event")
}
// Verify tool use content blocks
if !strings.Contains(outputStr, `"type":"tool_use"`) {
t.Error("Missing tool_use content blocks")
}
// warp_grep is a native MorphLLM tool and should be ignored
// We should check for the XML tool calls (Glob, Bash) instead
// Check for Glob tool calls
if !strings.Contains(outputStr, `"name":"Glob"`) {
t.Error("Missing Glob tool call - XML tools not converted")
}
// Check for Bash tool calls
if !strings.Contains(outputStr, `"name":"Bash"`) {
t.Error("Missing Bash tool call - XML tools not converted")
}
// Verify tool input is properly formatted as JSON
if !strings.Contains(outputStr, `"input":{`) {
t.Error("Tool input not properly formatted")
}
// Should NOT contain XML tool call tags in output
if strings.Contains(outputStr, "function_calls") {
t.Error("Output should not contain XML function_calls tags")
}
if strings.Contains(outputStr, "<invoke") {
t.Error("Output should not contain XML invoke tags")
}
// Verify stop reason is tool_use
if !strings.Contains(outputStr, `"stop_reason":"tool_use"`) {
t.Error("Stop reason should be tool_use")
}
t.Logf("Total output length: %d bytes", len(outputStr))
// Print first 2000 chars for inspection
if len(outputStr) > 2000 {
t.Logf("Output preview:\n%s\n...", outputStr[:2000])
} else {
t.Logf("Output:\n%s", outputStr)
}
}
// TestMultipleFunctionCallsBlocks tests handling of multiple separate
// function_calls blocks (which is invalid but may occur)
func TestMultipleFunctionCallsBlocks(t *testing.T) {
testData, err := os.ReadFile("../../test/fixtures/multiple_function_calls.txt")
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Should have proper event structure
if !strings.Contains(outputStr, "event: message_start") {
t.Error("Missing message_start event")
}
if !strings.Contains(outputStr, "event: message_stop") {
t.Error("Missing message_stop event")
}
// Should convert XML tool calls to proper tool_use blocks
toolUseCount := strings.Count(outputStr, `"type":"tool_use"`)
t.Logf("Found %d tool_use blocks", toolUseCount)
if toolUseCount == 0 {
t.Error("Should have converted XML tool calls to tool_use blocks")
}
// Should NOT have XML in output
if strings.Contains(outputStr, "function_calls") {
t.Error("Output should not contain XML function_calls tags")
}
t.Logf("Output preview:\n%s", outputStr[:min(2000, len(outputStr))])
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// TestRealWarpGrepToolCall tests conversion of real warp_grep tool call
// from logs/1 to ensure parameters are properly preserved
func TestRealWarpGrepToolCall(t *testing.T) {
// This test uses the actual SSE response from logs/1/morph_response.txt
// which contains a warp_grep tool call with proper parameters
testData, err := os.ReadFile("../../test/fixtures/real_warp_grep_call.txt")
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Verify basic structure
if !strings.Contains(outputStr, "event: message_start") {
t.Error("Missing message_start event")
}
if !strings.Contains(outputStr, "event: message_stop") {
t.Error("Missing message_stop event")
}
// Check for tool use blocks
if !strings.Contains(outputStr, `"type":"tool_use"`) {
t.Error("Missing tool_use content blocks")
}
// CRITICAL: Verify that tool parameters are NOT empty
// The bug is that input becomes {} instead of containing actual parameters
if strings.Contains(outputStr, `"input":{}`) {
t.Error("CRITICAL BUG: Tool input is empty {}! Parameters were lost during conversion")
}
// Verify Glob tool calls have pattern parameter
if strings.Contains(outputStr, `"name":"Glob"`) {
// Check that Glob calls have pattern parameter
if !strings.Contains(outputStr, `"pattern"`) {
t.Error("Glob tool call missing pattern parameter")
}
// Specific patterns from the real data
if !strings.Contains(outputStr, "package.json") {
t.Error("Missing expected pattern 'package.json' in Glob call")
}
}
// Verify Bash tool calls have command parameter
if strings.Contains(outputStr, `"name":"Bash"`) {
if !strings.Contains(outputStr, `"command"`) {
t.Error("Bash tool call missing command parameter")
}
// Check for specific commands from real data
if !strings.Contains(outputStr, "find") || !strings.Contains(outputStr, "ls") {
t.Error("Missing expected commands in Bash calls")
}
}
// Verify stop reason
if !strings.Contains(outputStr, `"stop_reason":"tool_use"`) {
t.Error("Stop reason should be tool_use")
}
// Should NOT contain XML in output
if strings.Contains(outputStr, "function_calls") {
t.Error("Output should not contain XML function_calls tags")
}
t.Logf("Total output length: %d bytes", len(outputStr))
// Print sample of output for debugging
lines := strings.Split(outputStr, "\n")
t.Logf("Total lines: %d", len(lines))
// Find and print tool_use blocks
for i, line := range lines {
if strings.Contains(line, `"type":"tool_use"`) ||
strings.Contains(line, `"input"`) ||
strings.Contains(line, `"name":"Glob"`) ||
strings.Contains(line, `"name":"Bash"`) {
t.Logf("Line %d: %s", i, line)
}
}
}
// TestLatestMorphResponse tests the latest morph response to verify tool conversion
func TestLatestMorphResponse(t *testing.T) {
testData, err := os.ReadFile("../../test/fixtures/latest_morph_response.txt")
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Check for tool use blocks
toolUseCount := strings.Count(outputStr, `"type":"tool_use"`)
t.Logf("Found %d tool_use blocks", toolUseCount)
if toolUseCount == 0 {
t.Error("Expected tool_use blocks but found none")
}
// Check for specific tools
if !strings.Contains(outputStr, `"name":"Read"`) {
t.Error("Missing Read tool call")
}
if !strings.Contains(outputStr, `"name":"Glob"`) {
t.Error("Missing Glob tool call")
}
// Verify stop reason
if !strings.Contains(outputStr, `"stop_reason":"tool_use"`) {
t.Error("Stop reason should be tool_use, not end_turn")
}
// Print tool blocks for inspection
lines := strings.Split(outputStr, "\n")
t.Logf("Total lines: %d", len(lines))
for i, line := range lines {
if strings.Contains(line, `"type":"tool_use"`) ||
strings.Contains(line, `"stop_reason"`) {
t.Logf("Line %d: %s", i, line)
}
}
}
// TestToolCallsWithTextAfter tests tool calls followed by more text
func TestToolCallsWithTextAfter(t *testing.T) {
testData, err := os.ReadFile("../../test/fixtures/morph_with_text_after_tools.txt")
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Check for tool use blocks
toolUseCount := strings.Count(outputStr, `"type":"tool_use"`)
t.Logf("Found %d tool_use blocks", toolUseCount)
if toolUseCount == 0 {
t.Error("CRITICAL: Expected tool_use blocks but found none - tools after text are being ignored!")
}
// Should NOT contain escaped XML in output
if strings.Contains(outputStr, "\\u003c") {
t.Error("CRITICAL: XML tags are being escaped and output as text instead of being parsed as tools!")
}
// Verify stop reason
if !strings.Contains(outputStr, `"stop_reason":"tool_use"`) {
t.Error("Stop reason should be tool_use when tools are present")
}
// Check for specific tools expected in this response
if !strings.Contains(outputStr, `"name":"Read"`) {
t.Error("Missing Read tool calls")
}
if !strings.Contains(outputStr, `"name":"Glob"`) {
t.Error("Missing Glob tool calls")
}
if !strings.Contains(outputStr, `"name":"Write"`) {
t.Error("Missing Write tool call")
}
// Print summary
t.Logf("Total output length: %d bytes", len(outputStr))
lines := strings.Split(outputStr, "\n")
t.Logf("Total lines: %d", len(lines))
}
// TestTransformFromAbsolutePath tests transformation from an absolute file path
// and writes the output to client_response.txt in the project root
func TestTransformFromAbsolutePath(t *testing.T) {
// Read the absolute path from environment variable or use default
inputPath := os.Getenv("UPSTREAM_STREAM_FILE")
if inputPath == "" {
t.Skip("Set UPSTREAM_STREAM_FILE environment variable to test with custom file")
}
testData, err := os.ReadFile(inputPath)
if err != nil {
t.Fatalf("Failed to read test data from %s: %v", inputPath, err)
}
input := bytes.NewReader(testData)
var output bytes.Buffer
err = TransformMorphToClaudeStream(input, "claude-sonnet-4-5", 0, &output, nil)
if err != nil {
t.Errorf("Transform failed: %v", err)
}
outputStr := output.String()
// Write output to client_response.txt
outputPath := "../../client_response.txt"
if err := os.WriteFile(outputPath, []byte(outputStr), 0644); err != nil {
t.Fatalf("Failed to write output to %s: %v", outputPath, err)
}
t.Logf("Successfully transformed %d bytes from %s", len(testData), inputPath)
t.Logf("Output written to %s (%d bytes)", outputPath, len(outputStr))
}
|