Files
magpie/internal/provider/key_models_refresh_test.go
T
yetone 7661a0aa72 provider: Fetch models ticks each key's own models, the first key's too (young02036)
young02036 on Discord (#feedback, v0.1.2965): "点击供应商的获取模型,给这个
供应商的所有账号默认勾选上可用模型,和新增账号之后默认勾选上可用模型这个功能
好像失效了".

Cause: no later change broke it; the two commits that built it each left
keys out. 0431793db (v0.1.2674) gives a key its own list only when it is
added beside others and its list is short, and says "the keys it had keep
their lists". 1a67ca614 (v0.1.2942) carries on each Fetch only what a key's
list gained and lost, and only for a key that already had a list of its
own ("a key serving every model still does"). So every key on All models
stayed there through every Fetch: the provider's first key, keys added
before per-key lists, and a key whose list wasn't read in the 8s when it
was added. In the reporter's setup that is the key the provider was made
with, so the feature reads as gone.

Fix (refreshKeyModels): a key on All models whose fetched list leaves out
a model the provider's other keys list is given that list. A key whose
list has every model stays on All models, so later additions still reach
it; a lone key stays too. Keys with lists of their own work as before
(un-ticks and ticks kept).

The user's choice is never overridden: a key the user set to All models
(`accountmodels` with none, the picker's All models) is now remembered in
Provider.AccountModelsAll and left alone by Fetch. The editor's save keeps
it, a key replaced in place moves it (moveKeyed), accountSettings and
clone carry it, normalize drops a mark for a key that has a list.
Not covered: a key set back to All models before this release has no mark
and is narrowed once to its own list on the next Fetch; the user can
press All models again and it stays.

Tests:
- TestFetchTicksEachKeysListOnAllModels: keys saved with no lists are
  ticked on Fetch; a key set to All models before and after stays so
  through two fetches; an un-tick survives; a key whose list failed stays
  All models and gets its list once it reads.
- TestFetchLeavesALoneKeyOnAllModels.
- internal/gui TestFetchModelsTicksEveryKeysModels through the real
  routes: provider/save, provider/models, keys/add, accountmodels, the
  editor's save, against an httptest relay with a list per key.
- TestFetchRefreshesEachKeysModels and TestNewKeyTakesItsOwnModels pinned
  the old rule (sk-b / sk-old left on All models); they now expect the
  key's own list.

Broken by hand (mote): the old `was == nil -> continue` put back: the
new provider test, the GUI test (sk-a, the first key: [] want
[m1 m2 m3 m4]) and the two updated tests go red. The AccountModelsAll
check removed: sk-c/sk-d set to All models get narrowed, red. The editor
save not keeping AccountModelsAll: the GUI test's sk-c red.

Run (mote, at this tree): gofmt -l clean; go vet of the touched packages,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok;
go test -tags nogui -count=1 -timeout 45m ./internal/provider ./internal/gui
./internal/cli ./internal/gateway ./internal/tui: all ok (provider 223s,
gui 138s, cli 106s, gateway 797s, tui 26s).
GUI: account-models, account-models-available, account-refresh,
gateway-key-models, gateway-key-models-redraw (each in Chromium and
WebKit, as the files run): 94 of 94 pass.

Rebased on origin/main 8e97e26d6 (google.go, migrate_zcode.go): ./internal/provider
run again at this head, ok (157s).

Not tried: a real relay with per-group keys; Windows and Linux test runs
(nothing OS-specific changed). No app.js change.
2026-11-05 00:11:02 +08:00

294 lines
10 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
)
// lists is a relay's models for each key, changed between fetches.
type relayLists struct {
mu sync.Mutex
by map[string][]string
}
func (l *relayLists) set(key string, ids ...string) {
l.mu.Lock()
defer l.mu.Unlock()
l.by[key] = ids
}
func (l *relayLists) serve(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
l.mu.Lock()
ids, ok := l.by[key]
l.mu.Unlock()
if !ok {
http.Error(rw, `{"error":{"message":"invalid key"}}`, http.StatusUnauthorized)
return
}
var b strings.Builder
b.WriteString(`{"data":[`)
for i, id := range ids {
if i > 0 {
b.WriteString(",")
}
b.WriteString(`{"id":"` + id + `"}`)
}
b.WriteString(`]}`)
_, _ = rw.Write([]byte(b.String()))
}))
t.Cleanup(srv.Close)
return srv
}
// young02036 on Discord: a provider's keys each serve the models of their
// own group, and each key's list was read only when it was added. Fetch
// models now carries what each key's list gained and lost into that key's
// models, and leaves what the user un-ticked, or ticked, as they set it.
func TestFetchRefreshesEachKeysModels(t *testing.T) {
prefillHome(t)
l := &relayLists{by: map[string][]string{
"sk-a": {"gpt-5.5", "deepseek-v4", "glm-5"},
"sk-b": {"kimi-k3"},
}}
srv := l.serve(t)
if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-a",
Keys: []KeyAccount{{Key: "sk-b"}}}); err != nil {
t.Fatal(err)
}
fetch := func() {
t.Helper()
p, _ := Find("relay")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
}
models := func(key string) []string {
p, _ := Find("relay")
return p.AccountModels[keyID(key)]
}
fetch()
// a key added takes its own list, as the GUI's add does it
l.set("sk-c", "deepseek-v4", "qwen3-max")
had := KeysHeld("relay")
if err := AddKey("relay", "C group", "sk-c", ""); err != nil {
t.Fatal(err)
}
PrefillNewKeys(context.Background(), had, "relay")
fetch()
if got := models("sk-c"); !slices.Equal(got, []string{"deepseek-v4", "qwen3-max"}) {
t.Fatalf("the added key's models %v", got)
}
// the user narrows sk-a by hand: glm-5 un-ticked
if err := SetAccountModels("relay", keyID("sk-a"), []string{"gpt-5.5", "deepseek-v4"}); err != nil {
t.Fatal(err)
}
// the groups change: sk-a's gives up deepseek-v4 and takes kimi-k3,
// sk-c's takes glm-5
l.set("sk-a", "gpt-5.5", "glm-5", "kimi-k3")
l.set("sk-c", "deepseek-v4", "qwen3-max", "glm-5")
fetch()
if got := models("sk-a"); !slices.Equal(got, []string{"gpt-5.5", "kimi-k3"}) {
t.Errorf("sk-a after a fetch: %v, want [gpt-5.5 kimi-k3] (deepseek-v4 gone, glm-5 still un-ticked)", got)
}
if got := models("sk-c"); !slices.Equal(got, []string{"deepseek-v4", "qwen3-max", "glm-5"}) {
t.Errorf("sk-c after a fetch: %v, want glm-5 ticked", got)
}
// sk-b was on All models and its list is short of the provider's: the
// first fetch ticked its own list, which hasn't changed since
if got := models("sk-b"); !slices.Equal(got, []string{"kimi-k3"}) {
t.Errorf("sk-b's models %v, want its own list [kimi-k3]", got)
}
// the gateway sends sk-c the model its group took on
if p, _ := Find("relay"); !p.AccountServes(keyID("sk-c"), "glm-5") || p.AccountServes(keyID("sk-a"), "deepseek-v4") {
t.Error("the keys' models don't hold the gateway's requests")
}
// the user un-ticks glm-5 for sk-c and ticks deepseek-v4 for sk-a,
// which its list hasn't; fetched again, both stay as they set them
if err := SetAccountModels("relay", keyID("sk-c"), []string{"deepseek-v4", "qwen3-max"}); err != nil {
t.Fatal(err)
}
if err := SetAccountModels("relay", keyID("sk-a"), []string{"gpt-5.5", "kimi-k3", "deepseek-v4"}); err != nil {
t.Fatal(err)
}
fetch()
fetch()
if got := models("sk-c"); !slices.Equal(got, []string{"deepseek-v4", "qwen3-max"}) {
t.Errorf("sk-c's un-tick undone by a fetch: %v", got)
}
if got := models("sk-a"); !slices.Equal(got, []string{"gpt-5.5", "kimi-k3", "deepseek-v4"}) {
t.Errorf("sk-a's tick undone by a fetch: %v", got)
}
// a key whose list can't be read now keeps its models
l.mu.Lock()
delete(l.by, "sk-c")
l.mu.Unlock()
l.set("sk-a", "gpt-5.5")
fetch()
if got := models("sk-c"); !slices.Equal(got, []string{"deepseek-v4", "qwen3-max"}) {
t.Errorf("sk-c's models changed while its list failed: %v", got)
}
// the provider's own picks, what agents get, stay the user's
if p, _ := Find("relay"); len(p.Models) != 0 {
t.Errorf("the provider's picks were set: %v", p.Models)
}
}
// young02036 on Discord: the editor's Select all available ticks every
// model a key's own list has, un-ticked ones too. KeyListed is that list,
// by the ids the provider lists them by; a key no list marks is unknown.
func TestKeyListedIsTheKeysOwnList(t *testing.T) {
prefillHome(t)
l := &relayLists{by: map[string][]string{
"sk-a": {"gpt-5.5", "glm-5"},
"sk-b": {"kimi-k3", "glm-5"},
}}
srv := l.serve(t)
if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-a",
Keys: []KeyAccount{{Key: "sk-b"}}}); err != nil {
t.Fatal(err)
}
if got := KeyListed("relay", keyID("sk-b")); got != nil {
t.Fatalf("before any fetch: %v, want nil", got)
}
p, _ := Find("relay")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
// the user un-ticks glm-5 for sk-b: its list still has it
if err := SetAccountModels("relay", keyID("sk-b"), []string{"kimi-k3"}); err != nil {
t.Fatal(err)
}
if got := KeyListed("relay", strings.ToUpper(keyID("sk-b"))); !slices.Equal(got, []string{"glm-5", "kimi-k3"}) {
t.Errorf("sk-b's list: %v, want [glm-5 kimi-k3]", got)
}
if got := KeyListed("relay", keyID("sk-a")); !slices.Equal(got, []string{"glm-5", "gpt-5.5"}) {
t.Errorf("sk-a's list: %v, want [glm-5 gpt-5.5]", got)
}
if got := KeyListed("relay", keyID("sk-none")); got != nil {
t.Errorf("a key no list marks: %v, want nil", got)
}
if got := KeyListed("nope", keyID("sk-a")); got != nil {
t.Errorf("no such provider: %v", got)
}
}
// young02036 on Discord (v0.1.2965): Fetch models ticked each key's
// available models only for keys that already had a list of their own, so
// a provider's first key, keys added before per-key lists and keys whose
// list was read late all stayed on All models. Fetch now ticks each key on
// All models with its own list when that list is short of the provider's;
// a key the user set to All models, or narrowed, stays as they set it.
func TestFetchTicksEachKeysListOnAllModels(t *testing.T) {
prefillHome(t)
l := &relayLists{by: map[string][]string{
"sk-a": {"m1", "m2", "m3", "m4"},
"sk-b": {"m1", "m2"},
"sk-c": {"m3", "m5"},
"sk-d": {"m4"},
}}
srv := l.serve(t)
// every key saved without a list, as keys added before per-key models
// and a key whose read failed when it was added are
if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-a",
Keys: []KeyAccount{{Key: "sk-b"}, {Key: "sk-c"}, {Key: "sk-d"}}}); err != nil {
t.Fatal(err)
}
fetch := func() {
t.Helper()
p, _ := Find("relay")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
}
models := func(key string) []string {
p, _ := Find("relay")
return p.AccountModels[keyID(key)]
}
// the user sets sk-d to All models before any fetch
if err := SetAccountModels("relay", keyID("sk-d"), nil); err != nil {
t.Fatal(err)
}
fetch()
if got := models("sk-b"); !slices.Equal(got, []string{"m1", "m2"}) {
t.Errorf("sk-b after a fetch: %v, want its list [m1 m2] ticked", got)
}
if got := models("sk-c"); !slices.Equal(got, []string{"m3", "m5"}) {
t.Errorf("sk-c after a fetch: %v, want its list [m3 m5] ticked", got)
}
if got := models("sk-a"); !slices.Equal(got, []string{"m1", "m2", "m3", "m4"}) {
t.Errorf("sk-a after a fetch: %v, want its list [m1 m2 m3 m4] ticked", got)
}
if got := models("sk-d"); got != nil {
t.Errorf("sk-d was set to All models by the user and was given %v", got)
}
if p, _ := Find("relay"); p.AccountServes(keyID("sk-b"), "m5") || !p.AccountServes(keyID("sk-c"), "m5") || !p.AccountServes(keyID("sk-d"), "m5") {
t.Error("the gateway would send m5 to a key whose list hasn't it")
}
// the user un-ticks m2 for sk-b, and sets sk-c to All models
if err := SetAccountModels("relay", keyID("sk-b"), []string{"m1"}); err != nil {
t.Fatal(err)
}
if err := SetAccountModels("relay", keyID("sk-c"), []string{}); err != nil {
t.Fatal(err)
}
fetch()
fetch()
if got := models("sk-b"); !slices.Equal(got, []string{"m1"}) {
t.Errorf("sk-b's un-tick undone by a fetch: %v", got)
}
if got := models("sk-c"); got != nil {
t.Errorf("sk-c set to All models was narrowed by a fetch: %v", got)
}
if got := models("sk-d"); got != nil {
t.Errorf("sk-d set to All models was narrowed by a fetch: %v", got)
}
// a key whose list can't be read stays on All models until it can
if err := AddKey("relay", "E", "sk-e", ""); err != nil {
t.Fatal(err)
}
fetch()
if got := models("sk-e"); got != nil {
t.Errorf("sk-e's list failed and it was given %v", got)
}
l.set("sk-e", "m2", "m5")
fetch()
if got := models("sk-e"); !slices.Equal(got, []string{"m2", "m5"}) {
t.Errorf("sk-e once its list reads: %v, want [m2 m5]", got)
}
// the provider's own picks, what agents get, stay the user's
if p, _ := Find("relay"); len(p.Models) != 0 {
t.Errorf("the provider's picks were set: %v", p.Models)
}
}
// One key alone has nothing to be short of: it stays on All models.
func TestFetchLeavesALoneKeyOnAllModels(t *testing.T) {
prefillHome(t)
l := &relayLists{by: map[string][]string{"sk-a": {"m1", "m2"}}}
srv := l.serve(t)
if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-a"}); err != nil {
t.Fatal(err)
}
p, _ := Find("relay")
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
if p, _ := Find("relay"); len(p.AccountModels) != 0 {
t.Errorf("a lone key was given %v", p.AccountModels)
}
}