Files
magpie/internal/gui/keys_import_test.go
T
yetone 0b11514cf1 gui: many keys are added to a provider at once, and a key the gateway rests says why and until when on its row (361 on Discord: 密钥比较多的话,需要批量导入的话就不方便了) — a provider with several keys took them one at a time, each pasted into Add another key's field. Add another key now has Paste several, a box that takes any number of keys, one a line or separated by commas, semicolons or spaces (full-width ones too), quotes trimmed, repeats counted once, and says how many it holds; Add (or Cmd/Ctrl+Enter) posts keys/import, which adds the ones the provider hasn't got in order (the first becomes its key when it has none) and saves once, and the note says N keys added, M already there. Pasting several keys into the one key's field opens the box with them. Each key's row in the editor, when the gateway is passing it over (rate limited, out of credit, refused…), says Resting · 429 rate limited · back at 14:05, from the same rests the Routing page counts; rotation order and failover were there already (routing modes, rests). zh, ja, de strings. TestSplitKeys, TestAddKeys, TestKeysImportAndRest (no raw key in the reply) fail on the old code (don't build); key-import.test.cjs (WebKit, en and zh: the box, its count, the post, the note, a paste opening it, the rest line, no scroll on a click) fails 4 of 4 on the old assets. account-models, account-arrange, account-return and click-scroll pass on WebKit. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestDevinSignedInOnlyInMagpie and TestClientGoneRecordedAsCanceled timed out once each under load and pass on their own).
2026-10-05 07:54:16 +08:00

80 lines
2.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// keys/import adds the keys pasted at once (361 on Discord), says how many
// were new, and a key the gateway rests says so in its row.
func TestKeysImportAndRest(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
p := provider.Provider{ID: "import-test", Name: "Import", Chat: "https://example.invalid/v1", Key: "first"}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
until := time.Now().Add(10 * time.Minute)
was := keyRestOf
keyRestOf = func(key string) (gateway.Rest, bool) {
if key == p.ID+"#"+provider.KeyID("two") {
return gateway.Rest{Why: "rate", Status: 429, Until: until}, true
}
return gateway.Rest{}, false
}
defer func() { keyRestOf = was }()
mux := http.NewServeMux()
providerRoutes(mux, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/keys/import", strings.NewReader(`{"id":"import-test","key":"first\ntwo, three\n\n'four'"}`)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var st providersJSON
if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil {
t.Fatal(err)
}
if st.Added != 3 || st.Had != 1 {
t.Fatalf("added %d had %d", st.Added, st.Had)
}
var info *providerJSON
for i := range st.Providers {
if st.Providers[i].ID == p.ID {
info = &st.Providers[i]
}
}
if info == nil || len(info.KeyList) != 4 {
t.Fatalf("keys: %+v", info)
}
for i, k := range info.KeyList {
if (k.Rest != nil) != (i == 1) {
t.Fatalf("key %d rest %+v", i, k.Rest)
}
}
if r := info.KeyList[1].Rest; r.Status != 429 || r.Why != "rate" || !r.Until.Equal(until) {
t.Fatalf("rest %+v", r)
}
for _, k := range []string{`"first"`, `"two"`, `"three"`, `"four"`} {
if strings.Contains(w.Body.String(), `"key":`+k) {
t.Fatal("leaked a key")
}
}
// pasting only keys it has is refused, saying so
w = httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/keys/import", strings.NewReader(`{"id":"import-test","key":"two three"}`)))
if w.Code == 200 || !strings.Contains(w.Body.String(), "already has") {
t.Fatalf("%d %s", w.Code, w.Body)
}
}