fix: 让消息 cache_control 改写默认关闭
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
@@ -188,6 +190,40 @@ func TestAddMessageCacheBreakpoints_StringContentPromoted(t *testing.T) {
|
||||
require.Equal(t, "5m", gjson.GetBytes(out, "messages.0.content.0.cache_control.ttl").String())
|
||||
}
|
||||
|
||||
func TestRewriteMessageCacheControlIfEnabled_DefaultKeepsClientAnchors(t *testing.T) {
|
||||
body := []byte(`{"messages":[
|
||||
{"role":"user","content":[{"type":"text","text":"stable","cache_control":{"type":"ephemeral","ttl":"1h"}}]},
|
||||
{"role":"assistant","content":[{"type":"text","text":"ok"}]},
|
||||
{"role":"user","content":[{"type":"text","text":"latest","cache_control":{"type":"ephemeral","ttl":"5m"}}]}
|
||||
]}`)
|
||||
|
||||
out := (&GatewayService{}).rewriteMessageCacheControlIfEnabled(context.Background(), body)
|
||||
|
||||
require.JSONEq(t, string(body), string(out))
|
||||
require.Equal(t, "1h", gjson.GetBytes(out, "messages.0.content.0.cache_control.ttl").String())
|
||||
require.Equal(t, "5m", gjson.GetBytes(out, "messages.2.content.0.cache_control.ttl").String())
|
||||
}
|
||||
|
||||
func TestRewriteMessageCacheControlIfEnabled_OptInPreservesLegacyRewrite(t *testing.T) {
|
||||
body := []byte(`{"messages":[
|
||||
{"role":"user","content":[{"type":"text","text":"stable","cache_control":{"type":"ephemeral","ttl":"1h"}}]},
|
||||
{"role":"assistant","content":[{"type":"text","text":"ok"}]},
|
||||
{"role":"user","content":[{"type":"text","text":"latest","cache_control":{"type":"ephemeral","ttl":"1h"}}]},
|
||||
{"role":"assistant","content":[{"type":"text","text":"done"}]}
|
||||
]}`)
|
||||
repo := &gatewayTTLSettingRepo{data: map[string]string{
|
||||
SettingKeyRewriteMessageCacheControl: "true",
|
||||
}}
|
||||
gatewayForwardingCache.Store(&cachedGatewayForwardingSettings{})
|
||||
svc := &GatewayService{settingService: NewSettingService(repo, &config.Config{})}
|
||||
|
||||
out := svc.rewriteMessageCacheControlIfEnabled(context.Background(), body)
|
||||
|
||||
require.Equal(t, "5m", gjson.GetBytes(out, "messages.0.content.0.cache_control.ttl").String())
|
||||
require.False(t, gjson.GetBytes(out, "messages.2.content.0.cache_control").Exists())
|
||||
require.Equal(t, "5m", gjson.GetBytes(out, "messages.3.content.0.cache_control.ttl").String())
|
||||
}
|
||||
|
||||
func TestBuildToolNameRewriteFromBody_ReverseOrderedByLengthDesc(t *testing.T) {
|
||||
// 超过阈值触发动态映射,验证 ReverseOrdered 按假名长度倒序排列
|
||||
body := []byte(`{"tools":[
|
||||
|
||||
Reference in New Issue
Block a user