fix(test): 修复测试文件与函数签名不匹配问题

This commit is contained in:
ianshaw
2026-01-03 06:52:50 -08:00
parent 691e2767a4
commit 75e7c3dd06
2 changed files with 35 additions and 53 deletions

View File

@@ -8,11 +8,11 @@ import (
// TestBuildParts_ThinkingBlockWithoutSignature 测试thinking block无signature时的处理
func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
tests := []struct {
name string
content string
thoughtMode thoughtSignatureMode
expectedParts int
description string
name string
content string
allowDummyThought bool
expectedParts int
description string
}{
{
name: "Claude model - drop thinking without signature",
@@ -21,9 +21,9 @@ func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
{"type": "thinking", "thinking": "Let me think...", "signature": ""},
{"type": "text", "text": "World"}
]`,
thoughtMode: thoughtSignatureModePreserve,
expectedParts: 2, // thinking 内容被丢弃
description: "Claude模型应丢弃无signature的thinking block内容",
allowDummyThought: false,
expectedParts: 2, // thinking 内容被丢弃
description: "Claude模型应丢弃无signature的thinking block内容",
},
{
name: "Claude model - preserve thinking block with signature",
@@ -32,9 +32,9 @@ func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
{"type": "thinking", "thinking": "Let me think...", "signature": "sig_real_123"},
{"type": "text", "text": "World"}
]`,
thoughtMode: thoughtSignatureModePreserve,
expectedParts: 3,
description: "Claude模型应透传带 signature 的 thinking block用于 Vertex 签名链路)",
allowDummyThought: false,
expectedParts: 3,
description: "Claude模型应透传带 signature 的 thinking block用于 Vertex 签名链路)",
},
{
name: "Gemini model - use dummy signature",
@@ -43,27 +43,16 @@ func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
{"type": "thinking", "thinking": "Let me think...", "signature": ""},
{"type": "text", "text": "World"}
]`,
thoughtMode: thoughtSignatureModeDummy,
expectedParts: 3, // 三个block都保留thinking使用dummy signature
description: "Gemini模型应该为无signature的thinking block使用dummy signature",
},
{
name: "Claude model - signature-only thinking block becomes signature-only part",
content: `[
{"type": "text", "text": "Hello"},
{"type": "thinking", "thinking": "", "signature": "sig_only_456"},
{"type": "text", "text": "World"}
]`,
thoughtMode: thoughtSignatureModePreserve,
expectedParts: 3,
description: "Claude模型应将空 thinking + signature 映射为 signature-only part便于 roundtrip",
allowDummyThought: true,
expectedParts: 3, // 三个block都保留thinking使用dummy signature
description: "Gemini模型应该为无signature的thinking block使用dummy signature",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
toolIDToName := make(map[string]string)
parts, err := buildParts(json.RawMessage(tt.content), toolIDToName, tt.thoughtMode)
parts, err := buildParts(json.RawMessage(tt.content), toolIDToName, tt.allowDummyThought)
if err != nil {
t.Fatalf("buildParts() error = %v", err)
@@ -82,14 +71,6 @@ func TestBuildParts_ThinkingBlockWithoutSignature(t *testing.T) {
t.Fatalf("expected thought part with signature sig_real_123, got thought=%v signature=%q",
parts[1].Thought, parts[1].ThoughtSignature)
}
case "Claude model - signature-only thinking block becomes signature-only part":
if len(parts) != 3 {
t.Fatalf("expected 3 parts, got %d", len(parts))
}
if parts[1].Thought || parts[1].Text != "" || parts[1].ThoughtSignature != "sig_only_456" {
t.Fatalf("expected signature-only part, got thought=%v text=%q signature=%q",
parts[1].Thought, parts[1].Text, parts[1].ThoughtSignature)
}
case "Gemini model - use dummy signature":
if len(parts) != 3 {
t.Fatalf("expected 3 parts, got %d", len(parts))
@@ -108,23 +89,9 @@ func TestBuildParts_ToolUseSignatureHandling(t *testing.T) {
{"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "ls"}, "signature": "sig_tool_abc"}
]`
t.Run("Claude preserve tool_use signature", func(t *testing.T) {
toolIDToName := make(map[string]string)
parts, err := buildParts(json.RawMessage(content), toolIDToName, thoughtSignatureModePreserve)
if err != nil {
t.Fatalf("buildParts() error = %v", err)
}
if len(parts) != 1 || parts[0].FunctionCall == nil {
t.Fatalf("expected 1 functionCall part, got %+v", parts)
}
if parts[0].ThoughtSignature != "sig_tool_abc" {
t.Fatalf("expected tool signature sig_tool_abc, got %q", parts[0].ThoughtSignature)
}
})
t.Run("Gemini uses dummy tool_use signature", func(t *testing.T) {
toolIDToName := make(map[string]string)
parts, err := buildParts(json.RawMessage(content), toolIDToName, thoughtSignatureModeDummy)
parts, err := buildParts(json.RawMessage(content), toolIDToName, true)
if err != nil {
t.Fatalf("buildParts() error = %v", err)
}
@@ -135,6 +102,21 @@ func TestBuildParts_ToolUseSignatureHandling(t *testing.T) {
t.Fatalf("expected dummy tool signature %q, got %q", dummyThoughtSignature, parts[0].ThoughtSignature)
}
})
t.Run("Claude model - no signature for tool_use", func(t *testing.T) {
toolIDToName := make(map[string]string)
parts, err := buildParts(json.RawMessage(content), toolIDToName, false)
if err != nil {
t.Fatalf("buildParts() error = %v", err)
}
if len(parts) != 1 || parts[0].FunctionCall == nil {
t.Fatalf("expected 1 functionCall part, got %+v", parts)
}
// Claude 模型不设置 signature
if parts[0].ThoughtSignature != "" {
t.Fatalf("expected no tool signature for Claude, got %q", parts[0].ThoughtSignature)
}
})
}
// TestBuildTools_CustomTypeTools 测试custom类型工具转换

View File

@@ -310,10 +310,10 @@ func TestAPIContracts(t *testing.T) {
"default_concurrency": 5,
"default_balance": 1.25,
"enable_model_fallback": false,
"fallback_model_anthropic": "",
"fallback_model_antigravity": "",
"fallback_model_gemini": "",
"fallback_model_openai": ""
"fallback_model_anthropic": "claude-3-5-sonnet-20241022",
"fallback_model_antigravity": "gemini-2.5-pro",
"fallback_model_gemini": "gemini-2.5-pro",
"fallback_model_openai": "gpt-4o"
}
}`,
},