test: update signature cache tests

Revert gemini translator changes for scheme A

Co-Authored-By: Warp <agent@warp.dev>
This commit is contained in:
sowar1987
2026-01-21 14:23:00 +08:00
parent a2f8f59192
commit 22ce65ac72
7 changed files with 81 additions and 78 deletions

View File

@@ -4,18 +4,20 @@ import (
"testing" "testing"
"time" "time"
) )
const testModelName = "claude-sonnet-4-5"
func TestCacheSignature_BasicStorageAndRetrieval(t *testing.T) { func TestCacheSignature_BasicStorageAndRetrieval(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sessionID := "test-session-1"
text := "This is some thinking text content" text := "This is some thinking text content"
signature := "abc123validSignature1234567890123456789012345678901234567890" signature := "abc123validSignature1234567890123456789012345678901234567890"
// Store signature // Store signature
CacheSignature("test-model", text, signature) CacheSignature(testModelName, sessionID, text, signature)
// Retrieve signature // Retrieve signature
retrieved := GetCachedSignature("test-model", text) retrieved := GetCachedSignature(testModelName, sessionID, text)
if retrieved != signature { if retrieved != signature {
t.Errorf("Expected signature '%s', got '%s'", signature, retrieved) t.Errorf("Expected signature '%s', got '%s'", signature, retrieved)
} }
@@ -27,29 +29,28 @@ func TestCacheSignature_DifferentModelGroups(t *testing.T) {
text := "Same text across models" text := "Same text across models"
sig1 := "signature1_1234567890123456789012345678901234567890123456" sig1 := "signature1_1234567890123456789012345678901234567890123456"
sig2 := "signature2_1234567890123456789012345678901234567890123456" sig2 := "signature2_1234567890123456789012345678901234567890123456"
CacheSignature(testModelName, "session-a", text, sig1)
CacheSignature(testModelName, "session-b", text, sig2)
CacheSignature("claude-sonnet-4-5-thinking", text, sig1) if GetCachedSignature(testModelName, "session-a", text) != sig1 {
CacheSignature("gpt-4o", text, sig2) t.Error("Session-a signature mismatch")
if GetCachedSignature("claude-sonnet-4-5-thinking", text) != sig1 {
t.Error("Claude signature mismatch")
} }
if GetCachedSignature("gpt-4o", text) != sig2 { if GetCachedSignature(testModelName, "session-b", text) != sig2 {
t.Error("GPT signature mismatch") t.Error("Session-b signature mismatch")
} }
} }
func TestCacheSignature_NotFound(t *testing.T) { func TestCacheSignature_NotFound(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
// Non-existent cache entry // Non-existent session
if got := GetCachedSignature("test-model", "some text"); got != "" { if got := GetCachedSignature(testModelName, "nonexistent", "some text"); got != "" {
t.Errorf("Expected empty string for missing entry, got '%s'", got) t.Errorf("Expected empty string for nonexistent session, got '%s'", got)
} }
// Existing cache but different text // Existing session but different text
CacheSignature("test-model", "text-a", "sigA12345678901234567890123456789012345678901234567890") CacheSignature(testModelName, "session-x", "text-a", "sigA12345678901234567890123456789012345678901234567890")
if got := GetCachedSignature("test-model", "text-b"); got != "" { if got := GetCachedSignature(testModelName, "session-x", "text-b"); got != "" {
t.Errorf("Expected empty string for different text, got '%s'", got) t.Errorf("Expected empty string for different text, got '%s'", got)
} }
} }
@@ -58,11 +59,12 @@ func TestCacheSignature_EmptyInputs(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
// All empty/invalid inputs should be no-ops // All empty/invalid inputs should be no-ops
CacheSignature("test-model", "", "sig12345678901234567890123456789012345678901234567890") CacheSignature(testModelName, "", "text", "sig12345678901234567890123456789012345678901234567890")
CacheSignature("test-model", "text", "") CacheSignature(testModelName, "session", "", "sig12345678901234567890123456789012345678901234567890")
CacheSignature("test-model", "text", "short") // Too short CacheSignature(testModelName, "session", "text", "")
CacheSignature(testModelName, "session", "text", "short") // Too short
if got := GetCachedSignature("test-model", "text"); got != "" { if got := GetCachedSignature(testModelName, "session", "text"); got != "" {
t.Errorf("Expected empty after invalid cache attempts, got '%s'", got) t.Errorf("Expected empty after invalid cache attempts, got '%s'", got)
} }
} }
@@ -70,12 +72,12 @@ func TestCacheSignature_EmptyInputs(t *testing.T) {
func TestCacheSignature_ShortSignatureRejected(t *testing.T) { func TestCacheSignature_ShortSignatureRejected(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sessionID := "test-short-sig"
text := "Some text" text := "Some text"
shortSig := "abc123" // Less than 50 chars shortSig := "abc123" // Less than 50 chars
CacheSignature(testModelName, sessionID, text, shortSig)
CacheSignature("test-model", text, shortSig) if got := GetCachedSignature(testModelName, sessionID, text); got != "" {
if got := GetCachedSignature("test-model", text); got != "" {
t.Errorf("Short signature should be rejected, got '%s'", got) t.Errorf("Short signature should be rejected, got '%s'", got)
} }
} }
@@ -83,18 +85,17 @@ func TestCacheSignature_ShortSignatureRejected(t *testing.T) {
func TestClearSignatureCache_ModelGroup(t *testing.T) { func TestClearSignatureCache_ModelGroup(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sigClaude := "validSig1234567890123456789012345678901234567890123456" sig := "validSig1234567890123456789012345678901234567890123456"
sigGpt := "validSig9876543210987654321098765432109876543210987654" CacheSignature(testModelName, "session-1", "text", sig)
CacheSignature("claude-sonnet-4-5-thinking", "text", sigClaude) CacheSignature(testModelName, "session-2", "text", sig)
CacheSignature("gpt-4o", "text", sigGpt)
ClearSignatureCache("claude-sonnet-4-5-thinking") ClearSignatureCache(GetModelGroup(testModelName) + "#session-1")
if got := GetCachedSignature("claude-sonnet-4-5-thinking", "text"); got != "" { if got := GetCachedSignature(testModelName, "session-1", "text"); got != "" {
t.Error("Claude cache should be cleared") t.Error("session-1 should be cleared")
} }
if got := GetCachedSignature("gpt-4o", "text"); got != sigGpt { if got := GetCachedSignature(testModelName, "session-2", "text"); got != sig {
t.Error("GPT cache should still exist") t.Error("session-2 should still exist")
} }
} }
@@ -102,16 +103,16 @@ func TestClearSignatureCache_AllSessions(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sig := "validSig1234567890123456789012345678901234567890123456" sig := "validSig1234567890123456789012345678901234567890123456"
CacheSignature("test-model", "text", sig) CacheSignature(testModelName, "session-1", "text", sig)
CacheSignature("test-model", "text", sig) CacheSignature(testModelName, "session-2", "text", sig)
ClearSignatureCache("") ClearSignatureCache("")
if got := GetCachedSignature("test-model", "text"); got != "" { if got := GetCachedSignature(testModelName, "session-1", "text"); got != "" {
t.Error("cache should be cleared") t.Error("session-1 should be cleared")
} }
if got := GetCachedSignature("test-model", "text"); got != "" { if got := GetCachedSignature(testModelName, "session-2", "text"); got != "" {
t.Error("cache should be cleared") t.Error("session-2 should be cleared")
} }
} }
@@ -130,7 +131,7 @@ func TestHasValidSignature(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
result := HasValidSignature("claude-sonnet-4-5-thinking", tt.signature) result := HasValidSignature(tt.signature)
if result != tt.expected { if result != tt.expected {
t.Errorf("HasValidSignature(%q) = %v, expected %v", tt.signature, result, tt.expected) t.Errorf("HasValidSignature(%q) = %v, expected %v", tt.signature, result, tt.expected)
} }
@@ -141,19 +142,20 @@ func TestHasValidSignature(t *testing.T) {
func TestCacheSignature_TextHashCollisionResistance(t *testing.T) { func TestCacheSignature_TextHashCollisionResistance(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sessionID := "hash-test-session"
// Different texts should produce different hashes // Different texts should produce different hashes
text1 := "First thinking text" text1 := "First thinking text"
text2 := "Second thinking text" text2 := "Second thinking text"
sig1 := "signature1_1234567890123456789012345678901234567890123456" sig1 := "signature1_1234567890123456789012345678901234567890123456"
sig2 := "signature2_1234567890123456789012345678901234567890123456" sig2 := "signature2_1234567890123456789012345678901234567890123456"
CacheSignature(testModelName, sessionID, text1, sig1)
CacheSignature(testModelName, sessionID, text2, sig2)
CacheSignature("test-model", text1, sig1) if GetCachedSignature(testModelName, sessionID, text1) != sig1 {
CacheSignature("test-model", text2, sig2)
if GetCachedSignature("test-model", text1) != sig1 {
t.Error("text1 signature mismatch") t.Error("text1 signature mismatch")
} }
if GetCachedSignature("test-model", text2) != sig2 { if GetCachedSignature(testModelName, sessionID, text2) != sig2 {
t.Error("text2 signature mismatch") t.Error("text2 signature mismatch")
} }
} }
@@ -161,12 +163,12 @@ func TestCacheSignature_TextHashCollisionResistance(t *testing.T) {
func TestCacheSignature_UnicodeText(t *testing.T) { func TestCacheSignature_UnicodeText(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sessionID := "unicode-session"
text := "한글 텍스트와 이모지 🎉 그리고 特殊文字" text := "한글 텍스트와 이모지 🎉 그리고 特殊文字"
sig := "unicodeSig123456789012345678901234567890123456789012345" sig := "unicodeSig123456789012345678901234567890123456789012345"
CacheSignature(testModelName, sessionID, text, sig)
CacheSignature("test-model", text, sig) if got := GetCachedSignature(testModelName, sessionID, text); got != sig {
if got := GetCachedSignature("test-model", text); got != sig {
t.Errorf("Unicode text signature retrieval failed, got '%s'", got) t.Errorf("Unicode text signature retrieval failed, got '%s'", got)
} }
} }
@@ -174,14 +176,14 @@ func TestCacheSignature_UnicodeText(t *testing.T) {
func TestCacheSignature_Overwrite(t *testing.T) { func TestCacheSignature_Overwrite(t *testing.T) {
ClearSignatureCache("") ClearSignatureCache("")
sessionID := "overwrite-session"
text := "Same text" text := "Same text"
sig1 := "firstSignature12345678901234567890123456789012345678901" sig1 := "firstSignature12345678901234567890123456789012345678901"
sig2 := "secondSignature1234567890123456789012345678901234567890" sig2 := "secondSignature1234567890123456789012345678901234567890"
CacheSignature(testModelName, sessionID, text, sig1)
CacheSignature(testModelName, sessionID, text, sig2) // Overwrite
CacheSignature("test-model", text, sig1) if got := GetCachedSignature(testModelName, sessionID, text); got != sig2 {
CacheSignature("test-model", text, sig2) // Overwrite
if got := GetCachedSignature("test-model", text); got != sig2 {
t.Errorf("Expected overwritten signature '%s', got '%s'", sig2, got) t.Errorf("Expected overwritten signature '%s', got '%s'", sig2, got)
} }
} }
@@ -193,13 +195,13 @@ func TestCacheSignature_ExpirationLogic(t *testing.T) {
// This test verifies the expiration check exists // This test verifies the expiration check exists
// In a real scenario, we'd mock time.Now() // In a real scenario, we'd mock time.Now()
sessionID := "expiration-test"
text := "text" text := "text"
sig := "validSig1234567890123456789012345678901234567890123456" sig := "validSig1234567890123456789012345678901234567890123456"
CacheSignature(testModelName, sessionID, text, sig)
CacheSignature("test-model", text, sig)
// Fresh entry should be retrievable // Fresh entry should be retrievable
if got := GetCachedSignature("test-model", text); got != sig { if got := GetCachedSignature(testModelName, sessionID, text); got != sig {
t.Errorf("Fresh entry should be retrievable, got '%s'", got) t.Errorf("Fresh entry should be retrievable, got '%s'", got)
} }

View File

@@ -249,7 +249,6 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
fid := tc.Get("id").String() fid := tc.Get("id").String()
fname := tc.Get("function.name").String() fname := tc.Get("function.name").String()
fargs := tc.Get("function.arguments").String() fargs := tc.Get("function.arguments").String()
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.id", fid)
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.name", fname) node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.name", fname)
node, _ = sjson.SetRawBytes(node, "parts."+itoa(p)+".functionCall.args", []byte(fargs)) node, _ = sjson.SetRawBytes(node, "parts."+itoa(p)+".functionCall.args", []byte(fargs))
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature) node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiCLIFunctionThoughtSignature)
@@ -265,13 +264,12 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
pp := 0 pp := 0
for _, fid := range fIDs { for _, fid := range fIDs {
if name, ok := tcID2Name[fid]; ok { if name, ok := tcID2Name[fid]; ok {
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.id", fid)
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.name", name) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.name", name)
resp := toolResponses[fid] resp := toolResponses[fid]
if resp == "" { if resp == "" {
resp = "{}" resp = "{}"
} }
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.output", []byte(resp)) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.result", []byte(resp))
pp++ pp++
} }
} }

View File

@@ -149,11 +149,7 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}` functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
fcName := functionCallResult.Get("name").String() fcName := functionCallResult.Get("name").String()
fcID := functionCallResult.Get("id").String() functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
if fcID == "" {
fcID = fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1))
}
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fcID)
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex) functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName) functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() { if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {

View File

@@ -46,6 +46,19 @@ func ConvertGeminiCLIRequestToGemini(_ string, inputRawJSON []byte, _ bool) []by
} }
} }
gjson.GetBytes(rawJSON, "contents").ForEach(func(key, content gjson.Result) bool {
if content.Get("role").String() == "model" {
content.Get("parts").ForEach(func(partKey, part gjson.Result) bool {
if part.Get("functionCall").Exists() {
rawJSON, _ = sjson.SetBytes(rawJSON, fmt.Sprintf("contents.%d.parts.%d.thoughtSignature", key.Int(), partKey.Int()), "skip_thought_signature_validator")
} else if part.Get("thoughtSignature").Exists() {
rawJSON, _ = sjson.SetBytes(rawJSON, fmt.Sprintf("contents.%d.parts.%d.thoughtSignature", key.Int(), partKey.Int()), "skip_thought_signature_validator")
}
return true
})
}
return true
})
return common.AttachDefaultSafetySettings(rawJSON, "safetySettings") return common.AttachDefaultSafetySettings(rawJSON, "safetySettings")
} }

View File

@@ -255,7 +255,6 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
fid := tc.Get("id").String() fid := tc.Get("id").String()
fname := tc.Get("function.name").String() fname := tc.Get("function.name").String()
fargs := tc.Get("function.arguments").String() fargs := tc.Get("function.arguments").String()
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.id", fid)
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.name", fname) node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".functionCall.name", fname)
node, _ = sjson.SetRawBytes(node, "parts."+itoa(p)+".functionCall.args", []byte(fargs)) node, _ = sjson.SetRawBytes(node, "parts."+itoa(p)+".functionCall.args", []byte(fargs))
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiFunctionThoughtSignature) node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".thoughtSignature", geminiFunctionThoughtSignature)
@@ -271,13 +270,12 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
pp := 0 pp := 0
for _, fid := range fIDs { for _, fid := range fIDs {
if name, ok := tcID2Name[fid]; ok { if name, ok := tcID2Name[fid]; ok {
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.id", fid)
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.name", name) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.name", name)
resp := toolResponses[fid] resp := toolResponses[fid]
if resp == "" { if resp == "" {
resp = "{}" resp = "{}"
} }
toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.output", []byte(resp)) toolNode, _ = sjson.SetBytes(toolNode, "parts."+itoa(pp)+".functionResponse.response.result", []byte(resp))
pp++ pp++
} }
} }

View File

@@ -187,11 +187,7 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}` functionCallTemplate := `{"id": "","index": 0,"type": "function","function": {"name": "","arguments": ""}}`
fcName := functionCallResult.Get("name").String() fcName := functionCallResult.Get("name").String()
fcID := functionCallResult.Get("id").String() functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1)))
if fcID == "" {
fcID = fmt.Sprintf("%s-%d-%d", fcName, time.Now().UnixNano(), atomic.AddUint64(&functionCallIDCounter, 1))
}
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "id", fcID)
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex) functionCallTemplate, _ = sjson.Set(functionCallTemplate, "index", functionCallIndex)
functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName) functionCallTemplate, _ = sjson.Set(functionCallTemplate, "function.name", fcName)
if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() { if fcArgsResult := functionCallResult.Get("args"); fcArgsResult.Exists() {

View File

@@ -291,9 +291,9 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte
if outputRaw != "" && outputRaw != "null" { if outputRaw != "" && outputRaw != "null" {
output := gjson.Parse(outputRaw) output := gjson.Parse(outputRaw)
if output.Type == gjson.JSON { if output.Type == gjson.JSON {
functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.output", output.Raw) functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw)
} else { } else {
functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.output", outputRaw) functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw)
} }
} }
functionContent, _ = sjson.SetRaw(functionContent, "parts.-1", functionResponse) functionContent, _ = sjson.SetRaw(functionContent, "parts.-1", functionResponse)