Files
magpie/internal/provider/modelsame_test.go
T
yetone f65d02f080 providers: a model a vendor names its own way is merged with the same model from other providers — by its id as Volcengine Ark spells it, or by the model the user says it is the same as in Names & levels (kyzhouxu, #583: 火山引擎获取到的默认 ds 4.1 flash 模型的名称跟其他供应商的名称不一样,没有自动合并到同一个模型分组) — the groups magpie finds merged a model by its id lowercased, its vendor prefix and an eight-digit date taken off and a version's dot made a dash, so Ark's deepseek-v4-1-flash-260910 (a six-digit YYMMDD snapshot) was a group of its own beside ali-tp's deepseek-v4.1-flash. sameModel now also takes off Ark's YYMMDD (a year from 23 to 39, a month and a day that are one), Vertex's "@YYYYMMDD", reads Fireworks' "p" between digits (deepseek-v4p1) as the dot and "_" as "-"; -flash, -thinking, ":batch", a four-digit release (qwen's -2507, kimi-k2-0905) and a number that is no date (-128000, -261301) stay apart. For an id no rule matches (an Ark endpoint id, a relay's own name), each model in a provider's Names & levels now has a "Same as" (等同于) box, its placeholder the name it is merged by now: the model it is the same as, as any vendor spells it, staged in draft.modelPrefs and made with the provider's Save like the other prefs (Cancel drops it, Restore default empties it, nothing sent or redrawn or scrolled before the Save). It is kept in settings' ModelSameAs by "<provider>/<model>" (renamed with the provider like every Model* map), set by provider.SetModelSame (its own id however spelt keeps nothing; a model the provider doesn't serve, or a name of no model, is refused) and taken by autoGroups, AutoStandIn and the agents' reseat (AutoGroupOf); the agents are told once. TestSameModelVendorSpellings (Ark, Fireworks, Vertex, underscores, and pairs that must stay apart) and TestModelSameAsMergesFoundGroup (c/ep-sol-x joins auto-sol once said, GroupFor("sol") finds it, back out when cleared, refusals) are new; TestProviderSaveModelPrefs covers a Save's same; provider-same-as.test.cjs (WebKit, en and zh: placeholder, staging, unsaved, Restore default, Cancel, one Save carrying exactly the changes, no scroll, Chinese for each string) fails without the change and passes with it; provider-levels-save and provider-detect pass. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-02 23:39:40 +08:00

132 lines
5.2 KiB
Go

package provider
import (
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// The groups magpie finds merge a model however a vendor spells its id:
// Volcengine Ark's dated deepseek-v4-1-flash-260910 is ali's
// deepseek-v4.1-flash (kyzhouxu, #583). Models that differ stay apart.
func TestSameModelVendorSpellings(t *testing.T) {
for in, want := range map[string]string{
// Ark's YYMMDD snapshot
"deepseek-v4-1-flash-260910": "deepseek-v4-1-flash",
"deepseek-v4.1-flash": "deepseek-v4-1-flash",
"DeepSeek-V4.1-Flash": "deepseek-v4-1-flash",
"deepseek-ai/DeepSeek-V4.1-Flash": "deepseek-v4-1-flash",
"doubao-seed-1-6-250615": "doubao-seed-1-6",
"doubao-seed-1.6-thinking-250715": "doubao-seed-1-6-thinking",
// Fireworks: its path, and "p" for the dot
"accounts/fireworks/models/deepseek-v4p1-flash": "deepseek-v4-1-flash",
"accounts/fireworks/models/qwen2p5-72b": "qwen2-5-72b",
// "_" for "-", and Vertex's "@" date
"deepseek_v4_1_flash": "deepseek-v4-1-flash",
"claude-opus-5-5@20260801": "claude-opus-5-5",
// not a date: kept
"deepseek-v4-1-flash-128000": "deepseek-v4-1-flash-128000",
"m-221301": "m-221301",
"m-261301": "m-261301",
"m-260100": "m-260100",
"qwen3-235b-a22b-2507": "qwen3-235b-a22b-2507",
"kimi-k2-0905": "kimi-k2-0905",
"gpt-4o": "gpt-4o",
"o3-pro": "o3-pro",
"llama-3p": "llama-3p",
} {
if got := sameModel(in); got != want {
t.Errorf("sameModel(%q) = %q, want %q", in, got, want)
}
}
// genuinely different models: never one group
for _, pair := range [][2]string{
{"deepseek-v4-1-flash-260910", "deepseek-v4.1"},
{"deepseek-v4-1-260910", "deepseek-v4.1-flash"},
{"deepseek-v4.1-thinking", "deepseek-v4.1"},
{"qwen3-235b-a22b-2507", "qwen3-235b-a22b"},
{"claude-opus-5.5:batch", "claude-opus-5.5"},
{"deepseek-v4.1-flash", "deepseek-v4.11-flash"},
} {
if sameModel(pair[0]) == sameModel(pair[1]) {
t.Errorf("%s and %s merged as %s", pair[0], pair[1], sameModel(pair[0]))
}
}
e := func(p, m string) Entry { return Entry{ID: p + "/" + m, Model: m, Name: m, Provider: Provider{ID: p}} }
gs := autoGroups([]Entry{e("volcengine", "deepseek-v4-1-flash-260910"), e("ali-tp", "deepseek-v4.1-flash")}, nil)
if len(gs) != 1 || gs[0].ID != "auto-deepseek-v4-1-flash" || !slices.Equal(gs[0].Members, []string{"volcengine/deepseek-v4-1-flash-260910", "ali-tp/deepseek-v4.1-flash"}) {
t.Fatalf("groups: %+v", gs)
}
}
// A model whose id no rule matches up with another vendor's is merged
// with it once the user says, in the provider's Names & levels, which
// model it is the same as (#583): saved with the provider's Save, kept in
// settings' ModelSameAs, and taken by the groups magpie finds.
func TestModelSameAsMergesFoundGroup(t *testing.T) {
prefsHome(t)
touched := 0
catalog.Changed = func() { touched++ }
t.Cleanup(func() { catalog.Changed = nil })
if err := Save(Provider{ID: "c", Name: "C", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"ep-sol-x"}}); err != nil {
t.Fatal(err)
}
members := func(gid string) []string {
_, ms, ok := FindGroup(GroupPrefix + gid)
if !ok {
return nil
}
var out []string
for _, m := range ms {
out = append(out, m.ID)
}
return out
}
if got := members("auto-sol"); !slices.Equal(got, []string{"a/sol", "b/sol"}) {
t.Fatalf("before: %v", got)
}
touched = 0
same := "Vendor/SOL"
if err := SetModelPrefs("c", map[string]ModelPref{"ep-sol-x": {Same: &same}}); err != nil {
t.Fatal(err)
}
if touched != 1 || settings.Load().ModelSameAs["c/ep-sol-x"] != "Vendor/SOL" {
t.Fatalf("told %d, saved %v", touched, settings.Load().ModelSameAs)
}
if got := members("auto-sol"); !slices.Equal(got, []string{"a/sol", "b/sol", "c/ep-sol-x"}) {
t.Fatalf("after: %v", got)
}
if AutoGroupOf("c", "ep-sol-x") != "auto-sol" || AutoGroupID("ep-sol-x") != "auto-ep-sol-x" {
t.Fatalf("AutoGroupOf %s", AutoGroupOf("c", "ep-sol-x"))
}
if g, ok := GroupFor("sol"); !ok || g != GroupPrefix+"auto-sol" {
t.Fatalf("GroupFor: %s %v", g, ok)
}
// the same again: nothing to tell
if err := SetModelPrefs("c", map[string]ModelPref{"ep-sol-x": {Same: &same}}); err != nil || touched != 1 {
t.Fatalf("%v, told %d", err, touched)
}
// its own id however spelt is no mapping: nothing kept
own := "c/EP_Sol-X"
if err := SetModelPrefs("c", map[string]ModelPref{"ep-sol-x": {Same: &own}}); err != nil {
t.Fatal(err)
}
if _, ok := settings.Load().ModelSameAs["c/ep-sol-x"]; ok || touched != 2 {
t.Fatalf("kept %v, told %d", settings.Load().ModelSameAs, touched)
}
if got := members("auto-sol"); !slices.Equal(got, []string{"a/sol", "b/sol"}) {
t.Fatalf("restored: %v", got)
}
// a model the provider doesn't serve, or a name of no model, is refused
if err := SetModelPrefs("c", map[string]ModelPref{"nope": {Same: &same}}); err == nil || !strings.Contains(err.Error(), "nope") {
t.Fatalf("got %v", err)
}
junk := "--/"
if err := SetModelPrefs("c", map[string]ModelPref{"ep-sol-x": {Same: &junk}}); err == nil {
t.Fatal("a name of no model was taken")
}
}