Files
magpie/internal/provider/models_perkey_test.go
T
yetone 9852eeb07a provider: a key whose model list didn't answer still takes requests, not left out as unlisted (#1598, wuwei0727)
A provider with several keys keeps the first in key/keyName and the
rest in keys; that split is how it is stored. When the vendor's /models
didn't answer the first key (a 429, a timeout) or a key was added since
the last fetch, the per-key catalog had no list carrying it, and
Provider.Serves read that as "this key sees none of the models". The
gateway's perKeyBarred then put it in unlisted on every request, and
Routing showed four of five keys.

Serves now treats a key that no list marks as unknown, which serves: a
failed list is not an empty one. A key whose own list did answer still
serves only what that list has. Nothing in the saved keys changes.

TestCountTokensKeyFailover's k-unlisted stood for a key outside m1's
list by being in no list at all, which now reads as unknown. Its list
now answers with another model (m2), which is the case the test guards:
it is still skipped.

Run: TestKeyWithNoListStillServes (the reporter's shape: xx1 in key,
xx2..xx5 in keys, xx1's list answers 429, xx5 lists one model) failed
before the fix and passes after; go test -tags nogui ./internal/provider
and ./internal/gateway ok; the full suite found the count_tokens fixture
above, nothing else; gofmt -l clean; GOOS=linux and GOOS=windows go vet
clean. Not tried: the reporter's own providers.json (the attached zip
returned 404) or a real multi-key vendor. A key whose list answers
empty is now tried too, as nothing marks it.
2026-10-11 20:09:14 +08:00

60 lines
2.3 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
)
// #1598 (wuwei0727): five keys, the first in key/keyName and four in
// keys. The vendor's list didn't answer the first key, the other four
// listed every model, so the first was taken to see none of them and was
// left out of every request as unlisted. A key whose list magpie doesn't
// have is unknown, not "sees nothing": it serves until a list says it
// doesn't. A key whose list did answer still serves only that list.
func TestKeyWithNoListStillServes(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
switch r.Header.Get("Authorization") {
case "Bearer sk-xx1":
http.Error(rw, `{"error":{"message":"rate limited"}}`, http.StatusTooManyRequests)
case "Bearer sk-xx5":
_, _ = rw.Write([]byte(`{"data":[{"id":"gpt-5.5"}]}`))
default:
_, _ = rw.Write([]byte(`{"data":[{"id":"gpt-5.5"},{"id":"claude-opus-4-7"}]}`))
}
}))
defer srv.Close()
// the reporter's shape: the first key outside keys, the rest in it
p := Provider{ID: "xx1", Name: "tokenone-xx1", Chat: srv.URL + "/v1", Key: "sk-xx1", KeyName: "xx1",
Keys: []KeyAccount{{Name: "xx2", Key: "sk-xx2"}, {Name: "xx3", Key: "sk-xx3"}, {Name: "xx4", Key: "sk-xx4"}, {Name: "xx5", Key: "sk-xx5"}}}
if _, err := p.fetch(context.Background()); err != nil {
t.Fatal(err)
}
first := p.first()
if first.Key != "sk-xx1" || first.Name != "xx1" {
t.Fatalf("first key %+v", first)
}
for _, m := range []string{"gpt-5.5", "claude-opus-4-7"} {
if !p.Serves(first, m) {
t.Errorf("the first key, whose list didn't answer, is left out of %s", m)
}
}
if p.Serves(p.Keys[3], "claude-opus-4-7") {
t.Error("a key whose list answered without the model serves it")
}
if !p.Serves(p.Keys[3], "gpt-5.5") || !p.Serves(p.Keys[0], "claude-opus-4-7") {
t.Error("a key serves what its own list has")
}
// the keys are left as the user saved them
if p.Key != "sk-xx1" || p.KeyName != "xx1" || len(p.Keys) != 4 {
t.Errorf("keys changed: %q %q %+v", p.Key, p.KeyName, p.Keys)
}
}