361 imported hundreds of keys into one provider, and its Accounts list drew a row for each (sk-o…d3a2, Make first, Remove), filling the screen. A list of more than 8 keys now shows the first five and "All {n} keys" ("{n} in use · {m} failing"). Opened, it shows a filter over the name, the masked key and the state ("off", "resting", the reason: rate limited, sign-in required…), at most 50 rows with "{n} more — type to narrow them down", and Show fewer; Escape clears the filter, then closes. Above the rows, keys can be removed at once: those turned off, those failing for good (the gateway rests them for a refused sign-in or no credit), or, with something typed, those the filter matches. The first click asks ("Remove {n} keys? Click again"); the second posts keys/remove-many (provider.RemoveKeys: one save; the first key's place goes to the next key that is on and stays; removing every key in use is refused). Dragging or Alt+↑/↓ on a shown row sends every key's place, the folded ones where they were: provider/arrange needs the full order. Several keys pasted into a provider's API key field (one a line, or comma/space separated, de-duplicated, empties dropped) are kept comma separated and counted ("3 keys"). On save the first is the key and the others are added to its accounts (AddKeys; keys it has already are passed over), and a probe asks with the first. Paste several in Add another key (0b11514c) already did this on an existing provider. Round-robin was already there: with more than one key on, the editor's Routing has Rotate (轮流) beside Smart, Order, Least used, Pace and Weight. en/zh/ja/de.
TestRemoveKeys (internal/provider) and TestProviderSaveSplitsKeysAndRemovesMany (internal/gui: a new provider saved with "sk-a, sk-b, sk-c, sk-b", saved again with one new key, one key alone, keys/remove-many) are new and fail without the change. provider-many-keys.test.cjs, also new, uses 300 keys in Chromium and WebKit, en and zh. It checks that five rows are drawn folded and 50 opened, the filter by masked end and by name keeps the focus, the armed second click posts the three refused keys, Alt+↑ sends all 300 in order, and the API key field counts a pasted list. It also checks that nothing scrolls, there is no <select> and no left border. It fails on the unchanged assets. key-import, account-arrange, key-weight, key-weight-fit, account-models, account-cap, account-mask, nokey-editor and factory-key pass in WebKit; click-scroll passes alone (it failed once under parallel load). go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass (TestClientGoneRecordedAsCanceled and TestUserPath failed under load and pass alone).
58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Many keys at once (361 on Discord: a provider added with hundreds of
|
|
// keys): pasted into the key field of a new provider, the first is its key
|
|
// and the others its accounts, each once; and keys/remove-many removes the
|
|
// ones named in one go, saying how many.
|
|
func TestProviderSaveSplitsKeysAndRemovesMany(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())
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(path, body string) 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)
|
|
}
|
|
return w.Body.String()
|
|
}
|
|
post("/api/provider/save", `{"id":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","key":"sk-a, sk-b, sk-c, sk-b","new":true}`)
|
|
p, err := provider.Find("relay")
|
|
if err != nil || p.Key != "sk-a" || len(p.Keys) != 2 || p.Keys[0].Key != "sk-b" || p.Keys[1].Key != "sk-c" || p.Keys[0].Off {
|
|
t.Fatalf("%v: key %q, others %+v", err, p.Key, p.Keys)
|
|
}
|
|
// saved again with some it has and one new: the first replaces the
|
|
// key, the new one is added, those it has stay once
|
|
post("/api/provider/save", `{"id":"relay","from":"relay","name":"Relay","chat":"http://127.0.0.1:1/v1","key":"sk-a\nsk-c\nsk-d"}`)
|
|
p, _ = provider.Find("relay")
|
|
if got := len(p.KeyList()); p.Key != "sk-a" || got != 4 {
|
|
t.Fatalf("key %q, %d keys: %+v", p.Key, got, p.Keys)
|
|
}
|
|
// one key alone is saved as it is
|
|
post("/api/provider/save", `{"id":"solo","name":"Solo","chat":"http://127.0.0.1:1/v1","key":"sk-only","new":true}`)
|
|
if p, _ := provider.Find("solo"); p.Key != "sk-only" || len(p.Keys) != 0 {
|
|
t.Fatalf("solo %q %+v", p.Key, p.Keys)
|
|
}
|
|
body := post("/api/keys/remove-many", `{"id":"relay","refs":["`+provider.KeyID("sk-a")+`","`+provider.KeyID("sk-c")+`"]}`)
|
|
if !strings.Contains(body, `"removed":2`) {
|
|
t.Fatalf("answer %s", body)
|
|
}
|
|
if p, _ := provider.Find("relay"); p.Key != "sk-b" || len(p.Keys) != 1 || p.Keys[0].Key != "sk-d" {
|
|
t.Fatalf("left %q %+v", p.Key, p.Keys)
|
|
}
|
|
}
|