Files
magpie/internal/provider/provider_order_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

103 lines
3.3 KiB
Go

package provider
import (
"slices"
"testing"
)
// #499: a provider added later can be put first. The order is the list's
// and the one they are tried in: a bare model id goes to the first that
// serves it, and the group magpie finds of a model several serve tries
// the first one first. One added after stays last; a rename keeps its place.
func TestProviderOrder(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b", "c"} {
if err := Save(Provider{ID: id, Name: id, Key: "sk-" + id, Chat: "https://" + id + ".example.com/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
}
ids := func() []string {
var out []string
for _, p := range All() {
out = append(out, p.ID)
}
return out
}
if got := ids(); !slices.Equal(got, []string{"a", "b", "c"}) {
t.Fatalf("as added: %v", got)
}
if err := SetOrder([]string{"c", "a"}); err != nil {
t.Fatal(err)
}
if got := ids(); !slices.Equal(got, []string{"c", "a", "b"}) {
t.Fatalf("after SetOrder(c, a): %v", got)
}
if p, _, ok := Resolve("m"); !ok || p.ID != "c" {
t.Fatalf("m resolves to %q, not c", p.ID)
}
g := slices.IndexFunc(autoGroups(providerEntries(), nil), func(g Group) bool { return g.ID == AutoGroupID("m") })
if g < 0 {
t.Fatal("no group found of m")
}
if ms := autoGroups(providerEntries(), nil)[g].Members; !slices.Equal(ms, []string{"c/m", "a/m", "b/m"}) {
t.Fatalf("group of m tries %v", ms)
}
if err := Save(Provider{ID: "d", Name: "d", Key: "sk-d", Chat: "https://d.example.com/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
if got := ids(); !slices.Equal(got, []string{"c", "a", "b", "d"}) {
t.Fatalf("d added: %v", got)
}
if err := Rename("c", "cc"); err != nil {
t.Fatal(err)
}
if got := ids(); !slices.Equal(got, []string{"cc", "a", "b", "d"}) {
t.Fatalf("c renamed: %v", got)
}
for _, bad := range [][]string{{"a", "a"}, {"nope"}} {
if err := SetOrder(bad); err == nil {
t.Fatalf("SetOrder(%v) taken", bad)
}
}
}
// Sync's order (MirrorOrder): the other computer's ids first, as it has
// them, then those named here alone (an account only this computer has)
// in the order they have here; none from the other leaves this one's.
func TestMirrorOrder(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b", "c", "d"} {
if err := Save(Provider{ID: id, Name: id, Key: "sk-" + id, Chat: "https://" + id + ".example.com/v1"}); err != nil {
t.Fatal(err)
}
}
if err := SetOrder([]string{"d", "a", "c", "b"}); err != nil {
t.Fatal(err)
}
if err := MirrorOrder(nil); err != nil {
t.Fatal(err)
}
if got, _ := StoredOrder(); !slices.Equal(got, []string{"d", "a", "c", "b"}) {
t.Fatalf("none from the other: %v", got)
}
if err := MirrorOrder([]string{"c", "a", "elsewhere"}); err != nil {
t.Fatal(err)
}
if got, _ := StoredOrder(); !slices.Equal(got, []string{"c", "a", "elsewhere", "d", "b"}) {
t.Fatalf("mirrored: %v", got)
}
var listed []string
for _, p := range All() {
listed = append(listed, p.ID)
}
if !slices.Equal(listed, []string{"c", "a", "d", "b"}) {
t.Fatalf("listed: %v", listed)
}
}