Files
traefik-llmgateway/translate_anthropic_cache_test.go
T
lukaszraczylo bc77e16e45 fix(anthropic): keep client cache_control on translated system messages
The translator joined system and developer messages into one string.
This dropped any cache_control marker that the client set on a part.
Now, when a marker exists, system becomes a list of text blocks that
keep the marker. Without markers, the joined string is unchanged.
2026-11-26 01:22:08 +00:00

53 lines
1.4 KiB
Go

package traefikllmgateway
import (
"reflect"
"testing"
)
func TestTranslateSystemKeepsCacheControl(t *testing.T) {
cc := map[string]any{"type": "ephemeral"}
req := map[string]any{
"model": "claude-x",
"messages": []any{
map[string]any{"role": "system", "content": []any{
map[string]any{"type": "text", "text": "A"},
map[string]any{"type": "text", "text": "B", "cache_control": cc},
}},
map[string]any{"role": "user", "content": "hi"},
},
}
out, err := anthropicRequestFromOpenAI(req)
if err != nil {
t.Fatal(err)
}
want := []any{
map[string]any{"type": "text", "text": "A"},
map[string]any{"type": "text", "text": "B", "cache_control": map[string]any{"type": "ephemeral"}},
}
if !reflect.DeepEqual(out["system"], want) {
t.Fatalf("system = %#v, want %#v", out["system"], want)
}
if _, ok := req["messages"].([]any)[0].(map[string]any)["content"].([]any)[1].(map[string]any)["cache_control"]; !ok {
t.Fatal("input mutated")
}
}
func TestTranslateSystemNoMarkersUnchanged(t *testing.T) {
req := map[string]any{
"model": "claude-x",
"messages": []any{
map[string]any{"role": "system", "content": "A"},
map[string]any{"role": "developer", "content": "B"},
map[string]any{"role": "user", "content": "hi"},
},
}
out, err := anthropicRequestFromOpenAI(req)
if err != nil {
t.Fatal(err)
}
if out["system"] != "A\n\nB" {
t.Fatalf("system = %#v", out["system"])
}
}