easyai-ai-gateway/apps/api/internal/runner/cache_affinity_test.go

43 lines
1.4 KiB
Go

package runner
import "testing"
func TestBuildCacheAffinityKeyUsesExplicitKeyFirst(t *testing.T) {
body := map[string]any{
"cacheAffinityKey": "session-a",
"sessionId": "session-b",
"messages": []any{map[string]any{"role": "user", "content": "hello"}},
}
got := buildCacheAffinityKey("chat.completions", "text_generate", body)
if got == "" || got[:9] != "explicit:" {
t.Fatalf("expected explicit cache affinity key, got %q", got)
}
}
func TestBuildCacheAffinityKeyUsesSessionContinuity(t *testing.T) {
got := buildCacheAffinityKey("chat.completions", "text_generate", map[string]any{
"session_id": "conversation-1",
})
if got == "" || got[:11] != "session_id:" {
t.Fatalf("expected session cache affinity key, got %q", got)
}
}
func TestBuildCacheAffinityKeyFallsBackToPromptHash(t *testing.T) {
body := map[string]any{
"messages": []any{
map[string]any{"role": "system", "content": "stable"},
map[string]any{"role": "user", "content": "hello"},
},
"tools": []any{map[string]any{"type": "function", "function": map[string]any{"name": "lookup"}}},
}
first := buildCacheAffinityKey("chat.completions", "text_generate", body)
second := buildCacheAffinityKey("chat.completions", "text_generate", body)
if first == "" || first != second || first[:7] != "prompt:" {
t.Fatalf("expected stable prompt hash cache affinity key, first=%q second=%q", first, second)
}
}