ds2api / internal /config /api_key_injection_test.go
luckfun233
feat: 收敛 flash 模型系列,新增纯文本技能库与工具调用开关
0ed5879
Raw History Blame Contribute Delete
2.02 kB
package config
import (
"encoding/json"
"testing"
)
func TestConfigAPIKeyInjectionJSONRoundTrip(t *testing.T) {
enabled := true
cfg := Config{APIKeys: []APIKey{
{Key: "sk-plain"},
{
Key: "sk-custom",
Name: "自定义",
ThinkingInjection: &APIKeyInjectionConfig{
Custom: true,
Enabled: &enabled,
Prompt: " key prompt ",
},
},
}}
cfg.NormalizeCredentials()
raw, err := json.Marshal(cfg)
if err != nil {
t.Fatalf("marshal config: %v", err)
}
var decoded Config
if err := json.Unmarshal(raw, &decoded); err != nil {
t.Fatalf("unmarshal config: %v", err)
}
decoded.NormalizeCredentials()
if len(decoded.APIKeys) != 2 {
t.Fatalf("expected two api keys, got %#v", decoded.APIKeys)
}
if decoded.APIKeys[0].ThinkingInjection != nil {
t.Fatalf("expected plain key without injection, got %#v", decoded.APIKeys[0].ThinkingInjection)
}
injection := decoded.APIKeys[1].ThinkingInjection
if injection == nil || !injection.Custom || injection.Enabled == nil || !*injection.Enabled {
t.Fatalf("unexpected injection config: %#v", injection)
}
if injection.Prompt != "key prompt" {
t.Fatalf("expected trimmed prompt, got %q", injection.Prompt)
}
}
func TestAPIKeyInjectionNormalizeDropsEmptyObject(t *testing.T) {
if got := (&APIKeyInjectionConfig{}).Normalize(); got != nil {
t.Fatalf("expected empty injection config to normalize to nil, got %#v", got)
}
enabled := false
if got := (&APIKeyInjectionConfig{Custom: true, Enabled: &enabled}).Normalize(); got == nil || got.Enabled == nil {
t.Fatalf("expected custom config to be kept, got %#v", got)
}
}
func TestConfigCloneIsolatesAPIKeyInjection(t *testing.T) {
enabled := true
cfg := Config{APIKeys: []APIKey{{
Key: "sk-custom",
ThinkingInjection: &APIKeyInjectionConfig{Custom: true, Enabled: &enabled, Prompt: "p"},
}}}
clone := cfg.Clone()
*clone.APIKeys[0].ThinkingInjection.Enabled = false
if !enabled {
t.Fatal("expected clone to own a copy of the injection settings")
}
}