Download internal/config/model_profile_test.go from a3216/ds2api: direct link, hf CLI and curl.
- Browser
- Download file 9.37 kB
-
https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/model_profile_test.go
- Command line
-
hf download hf://spaces/a3216/ds2api/internal/config/model_profile_test.go
-
curl -L -o model_profile_test.go https://huggingface.co/spaces/a3216/ds2api/resolve/main/internal/config/model_profile_test.go
9.37 kB
| package config | |
| import ( | |
| "encoding/json" | |
| "strings" | |
| "testing" | |
| ) | |
| func TestPromptStyleDefaultsToV2(t *testing.T) { | |
| var cfg Config | |
| if got := cfg.PromptStyleMode(); got != PromptStyleV2 { | |
| t.Fatalf("prompt style=%q want %q", got, PromptStyleV2) | |
| } | |
| store := &Store{cfg: Config{}} | |
| if got := store.PromptStyleMode(); got != PromptStyleV2 { | |
| t.Fatalf("store prompt style=%q want %q", got, PromptStyleV2) | |
| } | |
| cfg.PromptStyle = "legacy" | |
| if got := cfg.PromptStyleMode(); got != PromptStyleLegacy { | |
| t.Fatalf("prompt style=%q want legacy", got) | |
| } | |
| // 未知风格不报错,回落 v2(与 model_type 未知回落 default 一致)。 | |
| cfg.PromptStyle = "v3" | |
| if got := cfg.PromptStyleMode(); got != PromptStyleV2 { | |
| t.Fatalf("unknown style must fall back to v2, got %q", got) | |
| } | |
| } | |
| func TestNormalizeCredentialsKeepsKnownPromptStyle(t *testing.T) { | |
| cfg := Config{PromptStyle: " LEGACY "} | |
| cfg.NormalizeCredentials() | |
| if cfg.PromptStyle != PromptStyleLegacy { | |
| t.Fatalf("expected legacy style normalized to lower case, got %q", cfg.PromptStyle) | |
| } | |
| } | |
| func TestModelProfilesRoundTrip(t *testing.T) { | |
| raw := `{ | |
| "prompt_style": "legacy", | |
| "model_profiles": [ | |
| {"id": "deepseek-writer", "base": "deepseek-v4-flash", "name": "写作", "remark": "长文", "prompt": " writing spec "}, | |
| {"id": "deepseek-quiet", "base": "deepseek-v4-flash-nothinking"} | |
| ] | |
| }` | |
| var cfg Config | |
| if err := json.Unmarshal([]byte(raw), &cfg); err != nil { | |
| t.Fatalf("unmarshal: %v", err) | |
| } | |
| cfg.NormalizeCredentials() | |
| if len(cfg.ModelProfiles) != 2 { | |
| t.Fatalf("expected 2 profiles, got %#v", cfg.ModelProfiles) | |
| } | |
| first := cfg.ModelProfiles[0] | |
| if first.Prompt != "writing spec" { | |
| t.Fatalf("expected trimmed prompt, got %q", first.Prompt) | |
| } | |
| if first.Base != "deepseek-v4-flash" { | |
| t.Fatalf("unexpected base %q", first.Base) | |
| } | |
| encoded, err := json.Marshal(cfg) | |
| if err != nil { | |
| t.Fatalf("marshal: %v", err) | |
| } | |
| var decoded Config | |
| if err := json.Unmarshal(encoded, &decoded); err != nil { | |
| t.Fatalf("re-unmarshal: %v", err) | |
| } | |
| decoded.NormalizeCredentials() | |
| if len(decoded.ModelProfiles) != 2 || decoded.PromptStyle != PromptStyleLegacy { | |
| t.Fatalf("profiles lost in round trip: %#v style=%q", decoded.ModelProfiles, decoded.PromptStyle) | |
| } | |
| } | |
| func TestNormalizeModelProfilesDedupesAndDropsEmpty(t *testing.T) { | |
| items := NormalizeModelProfiles([]ModelProfileConfig{ | |
| {ID: " a ", Base: " deepseek-v4-flash "}, | |
| {ID: "A", Base: "deepseek-v4-flash-search"}, | |
| {ID: "", Base: "deepseek-v4-flash"}, | |
| {ID: "b", Base: "deepseek-v4-flash"}, | |
| }) | |
| if len(items) != 2 { | |
| t.Fatalf("expected 2 profiles after dedupe, got %#v", items) | |
| } | |
| if items[0].ID != "a" || items[0].Base != "deepseek-v4-flash" { | |
| t.Fatalf("unexpected first profile: %#v", items[0]) | |
| } | |
| if items[1].ID != "b" { | |
| t.Fatalf("unexpected second profile: %#v", items[1]) | |
| } | |
| if got := NormalizeModelProfiles(nil); got != nil { | |
| t.Fatalf("expected nil for empty input, got %#v", got) | |
| } | |
| } | |
| func TestValidateModelProfiles(t *testing.T) { | |
| ok := []ModelProfileConfig{{ID: "deepseek-writer", Base: "deepseek-v4-flash"}} | |
| if err := ValidateModelProfiles(ok); err != nil { | |
| t.Fatalf("expected valid profiles, got %v", err) | |
| } | |
| cases := []struct { | |
| name string | |
| items []ModelProfileConfig | |
| want string | |
| }{ | |
| {name: "empty id", items: []ModelProfileConfig{{Base: "deepseek-v4-flash"}}, want: "id cannot be empty"}, | |
| {name: "bad charset", items: []ModelProfileConfig{{ID: "bad id", Base: "deepseek-v4-flash"}}, want: "must be 1-64"}, | |
| {name: "duplicate", items: []ModelProfileConfig{ | |
| {ID: "a", Base: "deepseek-v4-flash"}, | |
| {ID: "A", Base: "deepseek-v4-flash"}, | |
| }, want: "duplicate"}, | |
| {name: "shadows builtin", items: []ModelProfileConfig{{ID: "deepseek-v4-flash", Base: "deepseek-v4-flash"}}, want: "shadows a built-in"}, | |
| {name: "missing base", items: []ModelProfileConfig{{ID: "a"}}, want: "base cannot be empty"}, | |
| {name: "bad base", items: []ModelProfileConfig{{ID: "a", Base: "deepseek-v4-pro"}}, want: "base must be one of"}, | |
| {name: "too many", items: makeManyProfiles(MaxModelProfiles + 1), want: "cannot exceed"}, | |
| {name: "prompt too long", items: []ModelProfileConfig{ | |
| {ID: "a", Base: "deepseek-v4-flash", Prompt: strings.Repeat("字", MaxPromptProfileChars+1)}, | |
| }, want: "prompt cannot exceed"}, | |
| } | |
| for _, tc := range cases { | |
| t.Run(tc.name, func(t *testing.T) { | |
| err := ValidateModelProfiles(tc.items) | |
| if err == nil || !strings.Contains(err.Error(), tc.want) { | |
| t.Fatalf("error=%v want contains %q", err, tc.want) | |
| } | |
| }) | |
| } | |
| } | |
| func makeManyProfiles(n int) []ModelProfileConfig { | |
| items := make([]ModelProfileConfig, 0, n) | |
| for i := 0; i < n; i++ { | |
| items = append(items, ModelProfileConfig{ | |
| ID: "m" + string(rune('a'+i%26)) + string(rune('a'+i/26)), | |
| Base: "deepseek-v4-flash", | |
| }) | |
| } | |
| return items | |
| } | |
| func TestResolveModelUsesModelProfiles(t *testing.T) { | |
| store := &Store{cfg: Config{ | |
| ModelProfiles: []ModelProfileConfig{ | |
| {ID: "deepseek-writer", Base: "deepseek-v4-flash"}, | |
| {ID: "DeepSeek-Quiet", Base: "deepseek-v4-flash-nothinking"}, | |
| }, | |
| }} | |
| if got, ok := ResolveModel(store, "deepseek-writer"); !ok || got != "deepseek-v4-flash" { | |
| t.Fatalf("resolved=%q ok=%v", got, ok) | |
| } | |
| // 大小写不敏感。 | |
| if got, ok := ResolveModel(store, "deepseek-quiet"); !ok || got != "deepseek-v4-flash-nothinking" { | |
| t.Fatalf("resolved=%q ok=%v", got, ok) | |
| } | |
| // 内置模型与别名解析不受影响。 | |
| if got, ok := ResolveModel(store, "deepseek-v4-flash"); !ok || got != "deepseek-v4-flash" { | |
| t.Fatalf("builtin resolved=%q ok=%v", got, ok) | |
| } | |
| if _, ok := ResolveModel(store, "not-a-model"); ok { | |
| t.Fatal("expected unknown model to fail") | |
| } | |
| } | |
| func TestStoreResolveModelProfile(t *testing.T) { | |
| store := &Store{cfg: Config{ModelProfiles: []ModelProfileConfig{ | |
| {ID: "deepseek-writer", Base: "deepseek-v4-flash", Prompt: " writing spec "}, | |
| {ID: "deepseek-plain", Base: "deepseek-v4-flash"}, | |
| {ID: "deepseek-legacy", Base: "deepseek-v4-flash", Style: "legacy"}, | |
| }}} | |
| // 老配置只写了 prompt:按 custom 处理,正文生效。 | |
| got := store.ResolveModelProfile("deepseek-writer") | |
| if !got.Found || got.Style != ModelProfileStyleCustom || got.Prompt != "writing spec" { | |
| t.Fatalf("unexpected settings: %#v", got) | |
| } | |
| // 什么都没配:inherit。 | |
| got = store.ResolveModelProfile("deepseek-plain") | |
| if !got.Found || got.Style != ModelProfileStyleInherit || got.Prompt != "" { | |
| t.Fatalf("unexpected settings: %#v", got) | |
| } | |
| got = store.ResolveModelProfile("deepseek-legacy") | |
| if !got.Found || got.Style != ModelProfileStyleLegacy { | |
| t.Fatalf("unexpected settings: %#v", got) | |
| } | |
| // 未命中内置模型时完全跟随全局。 | |
| if got := store.ResolveModelProfile("deepseek-v4-flash"); got.Found { | |
| t.Fatalf("expected no profile for builtin model, got %#v", got) | |
| } | |
| } | |
| func TestModelProfileStylesNormalize(t *testing.T) { | |
| cases := map[string]string{ | |
| "": ModelProfileStyleInherit, | |
| "inherit": ModelProfileStyleInherit, | |
| "V2": ModelProfileStyleV2, | |
| "writing": ModelProfileStyleWriting, | |
| " legacy": ModelProfileStyleLegacy, | |
| "custom": ModelProfileStyleCustom, | |
| "nope": ModelProfileStyleInherit, | |
| } | |
| for input, want := range cases { | |
| if got := NormalizeModelProfileStyle(input); got != want { | |
| t.Fatalf("style %q -> %q, want %q", input, got, want) | |
| } | |
| } | |
| // 老配置兼容:无 style + 有 prompt ⇒ custom;无 style + 无 prompt ⇒ inherit。 | |
| if got := (ModelProfileConfig{Prompt: "x"}).EffectiveStyle(); got != ModelProfileStyleCustom { | |
| t.Fatalf("expected custom for legacy prompt-only entry, got %q", got) | |
| } | |
| if got := (ModelProfileConfig{}).EffectiveStyle(); got != ModelProfileStyleInherit { | |
| t.Fatalf("expected inherit for empty entry, got %q", got) | |
| } | |
| } | |
| func TestValidateModelProfilesRejectsUnknownStyle(t *testing.T) { | |
| err := ValidateModelProfiles([]ModelProfileConfig{{ID: "a", Base: "deepseek-v4-flash", Style: "nope"}}) | |
| if err == nil || !strings.Contains(err.Error(), "style must be one of") { | |
| t.Fatalf("error=%v", err) | |
| } | |
| } | |
| func TestOpenAIModelsIncludeProfiles(t *testing.T) { | |
| store := &Store{cfg: Config{ModelProfiles: []ModelProfileConfig{ | |
| {ID: "deepseek-writer", Base: "deepseek-v4-flash", Name: "写作"}, | |
| }}} | |
| models := OpenAIModelsFor(store) | |
| found := false | |
| for _, model := range models { | |
| if model.ID == "deepseek-writer" { | |
| found = true | |
| if model.OwnedBy != "ds2api" { | |
| t.Fatalf("unexpected owned_by %q", model.OwnedBy) | |
| } | |
| } | |
| if model.ID == "deepseek-v4-flash" { | |
| if model.OwnedBy != "deepseek" { | |
| t.Fatalf("builtin must keep its owned_by, got %q", model.OwnedBy) | |
| } | |
| } | |
| } | |
| if !found { | |
| t.Fatalf("expected custom model in list, got %#v", models) | |
| } | |
| info, ok := OpenAIModelByID(store, "deepseek-writer") | |
| if !ok || info.ID != "deepseek-writer" { | |
| t.Fatalf("expected profile by id, got %#v ok=%v", info, ok) | |
| } | |
| if _, ok := OpenAIModelByID(store, "not-a-model"); ok { | |
| t.Fatal("expected unknown model to be missing") | |
| } | |
| } | |
| func TestModelProfilesCloneIsIndependent(t *testing.T) { | |
| cfg := Config{ModelProfiles: []ModelProfileConfig{{ID: "a", Base: "deepseek-v4-flash"}}} | |
| clone := cfg.Clone() | |
| if len(clone.ModelProfiles) != 1 { | |
| t.Fatalf("clone lost profiles: %#v", clone.ModelProfiles) | |
| } | |
| clone.ModelProfiles[0].ID = "b" | |
| if cfg.ModelProfiles[0].ID != "a" { | |
| t.Fatal("mutating the clone must not affect the original") | |
| } | |
| } | |