fix(lint): 修复剩余的errcheck错误
修复了测试文件中剩余的6处类型断言未检查错误:
- 第115-118行:choices.message.tool_calls 的类型断言链
- 第140和145行:multiple tool calls 测试的类型断言
- 第343和345行:ComplexSSEData 测试的类型断言
**修复模式:**
所有类型断言都改为使用 ok 检查:
```go
// 修复前
choices := payload["choices"].([]any)
// 修复后
choices, ok := payload["choices"].([]any)
if !ok || len(choices) == 0 {
t.Fatal("No choices found in result")
}
```
**测试验证:**
- ✅ TestCorrectToolCallsInSSEData - 所有子测试通过
- ✅ TestComplexSSEData - 通过
- ✅ TestCorrectToolParameters - 通过
- ✅ 所有类型断言都有 ok 检查
- ✅ 添加了数组长度验证
现在所有 errcheck 错误都已修复。
This commit is contained in:
@@ -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"])
|
||||
|
||||
Reference in New Issue
Block a user