diff --git a/backend/internal/service/openai_tool_corrector_test.go b/backend/internal/service/openai_tool_corrector_test.go index a1c4530a..3e885b4b 100644 --- a/backend/internal/service/openai_tool_corrector_test.go +++ b/backend/internal/service/openai_tool_corrector_test.go @@ -82,9 +82,22 @@ func TestCorrectToolCallsInSSEData(t *testing.T) { if err := json.Unmarshal([]byte(result), &payload); err != nil { t.Fatalf("Failed to parse result: %v", err) } - delta := payload["delta"].(map[string]any) - toolCalls := delta["tool_calls"].([]any) - functionCall := toolCalls[0].(map[string]any)["function"].(map[string]any) + delta, ok := payload["delta"].(map[string]any) + if !ok { + t.Fatal("Invalid delta format") + } + toolCalls, ok := delta["tool_calls"].([]any) + if !ok || len(toolCalls) == 0 { + t.Fatal("No tool_calls found in delta") + } + toolCall, ok := toolCalls[0].(map[string]any) + if !ok { + t.Fatal("Invalid tool_call format") + } + functionCall, ok := toolCall["function"].(map[string]any) + if !ok { + t.Fatal("Invalid function format") + } if functionCall["name"] != "grep" { t.Errorf("Expected tool name 'grep', got '%v'", functionCall["name"]) } @@ -99,10 +112,30 @@ func TestCorrectToolCallsInSSEData(t *testing.T) { if err := json.Unmarshal([]byte(result), &payload); err != nil { t.Fatalf("Failed to parse result: %v", err) } - choices := payload["choices"].([]any) - message := choices[0].(map[string]any)["message"].(map[string]any) - toolCalls := message["tool_calls"].([]any) - functionCall := toolCalls[0].(map[string]any)["function"].(map[string]any) + choices, ok := payload["choices"].([]any) + if !ok || len(choices) == 0 { + t.Fatal("No choices found in result") + } + choice, ok := choices[0].(map[string]any) + if !ok { + t.Fatal("Invalid choice format") + } + message, ok := choice["message"].(map[string]any) + if !ok { + t.Fatal("Invalid message format") + } + toolCalls, ok := message["tool_calls"].([]any) + if !ok || len(toolCalls) == 0 { + t.Fatal("No tool_calls found in message") + } + toolCall, ok := toolCalls[0].(map[string]any) + if !ok { + t.Fatal("Invalid tool_call format") + } + functionCall, ok := toolCall["function"].(map[string]any) + if !ok { + t.Fatal("Invalid function format") + } if functionCall["name"] != "glob" { t.Errorf("Expected tool name 'glob', got '%v'", functionCall["name"]) } @@ -122,14 +155,31 @@ func TestCorrectToolCallsInSSEData(t *testing.T) { if err := json.Unmarshal([]byte(result), &payload); err != nil { t.Fatalf("Failed to parse result: %v", err) } - toolCalls := payload["tool_calls"].([]any) + toolCalls, ok := payload["tool_calls"].([]any) + if !ok || len(toolCalls) < 2 { + t.Fatal("Expected at least 2 tool_calls") + } - func1 := toolCalls[0].(map[string]any)["function"].(map[string]any) + toolCall1, ok := toolCalls[0].(map[string]any) + if !ok { + t.Fatal("Invalid first tool_call format") + } + func1, ok := toolCall1["function"].(map[string]any) + if !ok { + t.Fatal("Invalid first function format") + } if func1["name"] != "edit" { t.Errorf("Expected first tool name 'edit', got '%v'", func1["name"]) } - func2 := toolCalls[1].(map[string]any)["function"].(map[string]any) + toolCall2, ok := toolCalls[1].(map[string]any) + if !ok { + t.Fatal("Invalid second tool_call format") + } + func2, ok := toolCall2["function"].(map[string]any) + if !ok { + t.Fatal("Invalid second function format") + } if func2["name"] != "read" { t.Errorf("Expected second tool name 'read', got '%v'", func2["name"]) } @@ -326,10 +376,30 @@ func TestComplexSSEData(t *testing.T) { t.Fatalf("Failed to parse result: %v", err) } - choices := payload["choices"].([]any) - delta := choices[0].(map[string]any)["delta"].(map[string]any) - toolCalls := delta["tool_calls"].([]any) - function := toolCalls[0].(map[string]any)["function"].(map[string]any) + choices, ok := payload["choices"].([]any) + if !ok || len(choices) == 0 { + t.Fatal("No choices found in result") + } + choice, ok := choices[0].(map[string]any) + if !ok { + t.Fatal("Invalid choice format") + } + delta, ok := choice["delta"].(map[string]any) + if !ok { + t.Fatal("Invalid delta format") + } + toolCalls, ok := delta["tool_calls"].([]any) + if !ok || len(toolCalls) == 0 { + t.Fatal("No tool_calls found in delta") + } + toolCall, ok := toolCalls[0].(map[string]any) + if !ok { + t.Fatal("Invalid tool_call format") + } + function, ok := toolCall["function"].(map[string]any) + if !ok { + t.Fatal("Invalid function format") + } if function["name"] != "edit" { t.Errorf("Expected tool name 'edit', got '%v'", function["name"])