File size: 2,024 Bytes
0ed5879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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")
	}
}