Download internal/config/api_key_injection_test.go from a3216/ds2api: direct link, hf CLI and curl.
- Browser
- Download file 2.02 kB
-
https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/api_key_injection_test.go
- Command line
-
hf download hf://spaces/a3216/ds2api/internal/config/api_key_injection_test.go
-
curl -L -o api_key_injection_test.go https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/api_key_injection_test.go
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") | |
| } | |
| } | |