Files
magpie/account_models_cli_test.go
T
yetone 01d595817c providers: each account of a subscription, and each key, can be kept for some of the provider's models only (#474: 增加账号级别的模型选项, a low-quota but verified account kept for the cheap models) — every account or key a provider had served every model it did, so one couldn't be spared for some. An account's or key's row in its provider's editor now has a models badge: a click opens the provider's models as chips, Save keeps the account for those picked, All models gives it every one again; one kept for some says how many, always shown. magpie provider account-models <id> [account|key [models…|all]] does the same and lists them. It is kept in providers.json as accountModels, an account by its name in lower case, a key by its id; one not in it serves every model, as before. The gateway never sends a model to an account or key whose list leaves it out, in every routing mode, in drag order, in a group and as a fallback, and never falls back to it all the same; the trace says it was left out as set to serve other models. When every account or key there is leaves the model out, the request is refused with a 403 saying so and how to change it, not "none of X's models is ready". TestKeyModelsNarrowCandidates, TestAccountModelsNeverChosen, TestAccountModelsCmd and account-models.test.cjs fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; account-models, click-scroll, old-webkit, account-arrange, account-proxy, account-return, routing-account-order, panel-routing, unlisted-models pass.
2026-10-02 05:50:23 +08:00

63 lines
2.1 KiB
Go

package main
import (
"os"
"reflect"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider account-models gives one key, by its name or its id, the
// models it alone serves; all gives it every one again, and a key the
// provider doesn't have is an error (#474).
func TestAccountModelsCmd(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k1", Keys: []provider.KeyAccount{{Name: "Team", Key: "k2"}}, Chat: "http://127.0.0.1:1/v1"}); err != nil {
t.Fatal(err)
}
stored := func() map[string][]string {
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
return p.AccountModels
}
if err := providerCmd([]string{"provider", "account-models", "relay", "team", "cheap", "mini", "cheap"}); err != nil {
t.Fatal(err)
}
if got, want := stored(), map[string][]string{provider.KeyID("k2"): {"cheap", "mini"}}; !reflect.DeepEqual(got, want) {
t.Fatalf("by name: %v", got)
}
if err := providerCmd([]string{"provider", "account-models", "relay", provider.KeyID("k1"), "big"}); err != nil {
t.Fatal(err)
}
if got := stored(); len(got) != 2 || !reflect.DeepEqual(got[provider.KeyID("k1")], []string{"big"}) {
t.Fatalf("by id: %v", got)
}
// a save of the provider's other settings keeps them
p, _ := provider.Find("relay")
p.Models = []string{"cheap", "mini", "big"}
if err := provider.Save(*p); err != nil {
t.Fatal(err)
}
for _, ref := range []string{"team", provider.KeyID("k1")} {
if err := providerCmd([]string{"provider", "account-models", "relay", ref, "all"}); err != nil {
t.Fatal(err)
}
}
if got := stored(); got != nil {
t.Fatalf("all: %v", got)
}
if err := providerCmd([]string{"provider", "account-models", "relay", "nobody", "big"}); err == nil {
t.Fatal("an unknown key was taken")
}
if err := providerCmd([]string{"provider", "account-models", "relay"}); err != nil {
t.Fatal(err)
}
}