From 415840088e80ffed0a3592553e9e645a8fed7c74 Mon Sep 17 00:00:00 2001 From: IanShaw027 <131567472+IanShaw027@users.noreply.github.com> Date: Fri, 16 Jan 2026 00:14:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(lint):=20=E4=BF=AE=E5=A4=8D=E5=89=A9?= =?UTF-8?q?=E4=BD=99=E7=9A=84errcheck=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复了测试文件中剩余的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 错误都已修复。 --- .../service/openai_tool_corrector_test.go | 98 ++++++++++++++++--- 1 file changed, 84 insertions(+), 14 deletions(-) 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"])