Files
magpie/internal/gui/fetch_ticks_keys_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

89 lines
3.2 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// young02036 on Discord (v0.1.2965): the editor's Fetch models ticks each
// account's available models, and an account added gets its own ticked.
// The provider's first key stayed on All models through every Fetch, and
// so did any key added without its own list. Through the GUI's own
// routes: add keys, Fetch models, set one to All models, save the editor,
// fetch again.
func TestFetchModelsTicksEveryKeysModels(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("APPDATA", filepath.Join(h, "AppData"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".data"))
var mu sync.Mutex
lists := map[string][]string{"sk-a": {"m1", "m2", "m3", "m4"}, "sk-b": {"m1", "m2"}, "sk-c": {"m3", "m5"}}
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
ids, ok := lists[key]
mu.Unlock()
if !ok {
http.Error(rw, `{"error":{"message":"invalid key"}}`, http.StatusUnauthorized)
return
}
var b []string
for _, id := range ids {
b = append(b, `{"id":"`+id+`"}`)
}
_, _ = rw.Write([]byte(`{"data":[` + strings.Join(b, ",") + `]}`))
}))
t.Cleanup(srv.Close)
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(path, body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", path, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
}
}
models := func(key string) []string {
t.Helper()
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
return p.AccountModels[provider.KeyID(key)]
}
post("/api/provider/save", `{"id":"relay","name":"Relay","chat":"`+srv.URL+`/v1","key":"sk-a","new":true}`)
post("/api/provider/models", `{"id":"relay","typed":true}`)
post("/api/keys/add", `{"id":"relay","key":"sk-b"}`)
post("/api/keys/add", `{"id":"relay","key":"sk-c"}`)
post("/api/provider/models", `{"id":"relay","typed":true}`)
for key, want := range map[string][]string{"sk-a": {"m1", "m2", "m3", "m4"}, "sk-b": {"m1", "m2"}, "sk-c": {"m3", "m5"}} {
if got := models(key); !slices.Equal(got, want) {
t.Errorf("%s after Fetch models: %v, want its own list %v ticked", key, got, want)
}
}
// the user sets sk-c to All models and un-ticks m2 for sk-b, then saves
// the editor; neither is undone by the save or a Fetch
post("/api/provider/accountmodels", `{"id":"relay","account":"`+provider.KeyID("sk-c")+`","allow":[]}`)
post("/api/provider/accountmodels", `{"id":"relay","account":"`+provider.KeyID("sk-b")+`","allow":["m1"]}`)
post("/api/provider/save", `{"id":"relay","from":"relay","name":"Relay","chat":"`+srv.URL+`/v1","key":""}`)
post("/api/provider/models", `{"id":"relay","typed":true}`)
if got := models("sk-c"); got != nil {
t.Errorf("sk-c set to All models, then saved and fetched: %v", got)
}
if got := models("sk-b"); !slices.Equal(got, []string{"m1"}) {
t.Errorf("sk-b's un-tick undone: %v", got)
}
}