Files
magpie/internal/gateway/search_api_keys_test.go
T
yetone c8eb7b3877 search: a search API's keys are a list, each switched off, moved and removed on its own
Web search took one key per API in one field (辰林鸭~ on Discord: only one
Tavily key). Settings › Models › Web search now lists an API's keys under
it as a provider's accounts are: each masked on its own row with a tick
(off / on), Show, Copy and Remove; several pasted at once (commas,
semicolons, lines), the reply saying how many were new and how many it
had; the order changed by dragging or Alt+↑/↓. A resting key says why and
until when on its row ("asked last until 14:05: rate limited"), from the
gateway's rest (SearchKeyRest, SearchQuota.Ref); rotation on 401/403,
402/432/433 and 429 (#1477) is unchanged.

provider.SearchAPI keeps Key as the keys on, comma-joined, so the gateway
and an older magpie never ask one switched off; keyList is written only
while a key is off. A Key that differs from the list (an older magpie, a
sync) wins, its keys on in its order, the list's off keys after. The last
key on can't be switched off or removed. The GUI names a key by
SearchKeyRef (a hash): POST /api/settings/search-keys (add, ref+on,
ref+remove, arrange) and /api/settings/search-key with ref. Saving an API
with no key keeps its keys, off ones too. Backup and WebDAV sync carry
the off keys, or leave them out with the rest. `magpie search` says
"+N off". docs/subsystems/gateway-routing.md has "The keys as a list".

Run (mote, at this tree, base 3441035a2):
- go test -count=1 ./internal/gui ./internal/provider ./internal/backup
  ./internal/davsync ./internal/cli: ok but gui's
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322, mote, untouched) and
  cli's TestEveryRouteHasACommandOrIsNamed, then fixed (search-keys named
  S2) and run again: ok. ./internal/gateway search tests ok earlier.
- node --test search-key-list, search-keys, search-api, search-signs-in,
  search-order: 84/84, Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and
  420px (search-api/order at their own widths). i18n-keys: only "a
  plugin's words are its own" red, as on main (#5332, #5426, #5482).
- go build ./..., go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. gofmt -l: clean.

Broken by hand, each red then restored:
- SetSearchAPI drops KeyList: provider and gui tests red.
- backup clears Key only / davsync merges Key only: their tests red.
- off keys still put in Key: provider, gui, backup and gateway red.
- the whole key in /api/state: TestSearchKeyListFromSettings red.
- no last-key-on check: TestSearchKeyList red.
- settings.js at HEAD: search-key-list.test.cjs red.

Not tried: a real Tavily account with several keys (the vendor is faked;
no test reaches the network); the real app window (the GUI ran in
Playwright only); Windows and Linux runs of the tests (vet only). The
full Go and GUI suites run after the push, under mote's suite lock.

Reported-by: 辰林鸭~ (Discord)
2026-11-07 21:21:48 +08:00

149 lines
5.6 KiB
Go

package gateway
import (
"context"
"io"
"net/http"
"net/http/httptest"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A search API given several keys (#1477) goes on to the next when one is
// out of credit (Tavily's 432), and the next search doesn't start from the
// keys refused, which rest (search_pool_test.go). A failure that isn't about the key (a 500) doesn't spend
// the other keys.
func TestSearchAPIKeysTakeTurns(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var mu sync.Mutex
var asked []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
asked = append(asked, key)
mu.Unlock()
switch key {
case "tvly-spent":
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(432)
io.WriteString(w, `{"detail":{"error":"This request exceeds your plan's set usage limit. Please upgrade your plan or contact support@tavily.com"}}`)
case "tvly-down":
http.Error(w, `{"detail":{"error":"down"}}`, 500)
case "tvly-good":
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"query":"x","results":[{"title":"Go downloads","url":"https://go.dev/dl/","content":"go1.27.1","score":0.9}]}`)
default:
http.Error(w, `{"detail":{"error":"Unauthorized: missing or invalid API key."}}`, 401)
}
}))
defer srv.Close()
s := New()
search := func(keys string) (string, error) {
t.Helper()
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily", Key: keys, URL: srv.URL}); err != nil {
t.Fatal(err)
}
mu.Lock()
asked = nil
mu.Unlock()
said, _, err := s.apiSearch(context.Background(), "latest go")
return said, err
}
took := func() []string { mu.Lock(); defer mu.Unlock(); return slices.Clone(asked) }
forgetSearchKeys()
if said, err := search("tvly-spent, tvly-bad\ntvly-good"); err != nil || !strings.Contains(said, "go1.27.1") {
t.Fatalf("spent, bad, good: %q, %v", said, err)
}
if got := took(); !slices.Equal(got, []string{"tvly-spent", "tvly-bad", "tvly-good"}) {
t.Errorf("asked with %v, want each key in turn", got)
}
if _, err := search("tvly-spent,tvly-bad,tvly-good"); err != nil {
t.Fatal(err)
}
if got := took(); !slices.Equal(got, []string{"tvly-good"}) {
t.Errorf("the next search asked with %v, want the key that answered, the others resting", got)
}
forgetSearchKeys()
if _, err := search("tvly-down,tvly-good"); err == nil {
t.Error("a 500 on the first key went on to the next")
}
if got := took(); !slices.Equal(got, []string{"tvly-down"}) {
t.Errorf("asked with %v after a 500, want the first key alone", got)
}
forgetSearchKeys()
_, err := search("tvly-spent,tvly-bad")
if err == nil || !strings.Contains(err.Error(), "key 1 (") || !strings.Contains(err.Error(), "usage limit") || !strings.Contains(err.Error(), "key 2 (") {
t.Errorf("every key refused: %v", err)
}
if as := provider.StoredSearchAPIs(); len(as) != 1 || as[0].Key != "tvly-spent,tvly-bad" || as[0].MaskedKey() != "tvly…pent, ••••••••" {
t.Errorf("stored %+v, shown %q", as, as[0].MaskedKey())
}
}
// A key switched off in its list (辰林鸭~ on Discord) is never asked, and
// each key refused says why it rests, as the list shows it: Tavily's 433
// out of credit, a 429 rate limited for as long as its Retry-After, a 401
// turned away. The key that answered rests not at all.
func TestSearchKeyOffAndWhyItRests(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var mu sync.Mutex
var asked []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
asked = append(asked, key)
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
switch key {
case "tvly-limited":
w.Header().Set("Retry-After", "120")
w.WriteHeader(429)
io.WriteString(w, `{"detail":{"error":"Your request has been blocked due to excessive requests."}}`)
case "tvly-paygo":
w.WriteHeader(433)
io.WriteString(w, `{"detail":{"error":"This request exceeds the pay-as-you-go limit."}}`)
case "tvly-good":
io.WriteString(w, `{"query":"x","results":[{"title":"Go","url":"https://go.dev/dl/","content":"go1.27.1","score":0.9}]}`)
default:
w.WriteHeader(401)
io.WriteString(w, `{"detail":{"error":"Unauthorized: missing or invalid API key."}}`)
}
}))
defer srv.Close()
forgetSearchKeys()
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily", Key: "tvly-off,tvly-limited,tvly-paygo,tvly-bad,tvly-good", URL: srv.URL, Pick: provider.SearchPickOrder}); err != nil {
t.Fatal(err)
}
if err := provider.SwitchSearchKey("tavily", provider.SearchKeyRef("tvly-off"), false); err != nil {
t.Fatal(err)
}
said, _, err := New().apiSearch(context.Background(), "latest go")
if err != nil || !strings.Contains(said, "go1.27.1") {
t.Fatalf("%q, %v", said, err)
}
mu.Lock()
got := slices.Clone(asked)
mu.Unlock()
if !slices.Equal(got, []string{"tvly-limited", "tvly-paygo", "tvly-bad", "tvly-good"}) {
t.Fatalf("asked with %v", got)
}
for key, want := range map[string]string{"tvly-limited": "rate limited", "tvly-paygo": "out of credit", "tvly-bad": "turned away", "tvly-good": "", "tvly-off": ""} {
why, until := SearchKeyRest("tavily", key)
if why != want || (want == "") != until.IsZero() {
t.Errorf("%s rests %q until %v, want %q", key, why, until, want)
}
}
if _, until := SearchKeyRest("tavily", "tvly-limited"); time.Until(until) < 90*time.Second || time.Until(until) > 125*time.Second {
t.Errorf("a 429 with Retry-After 120 rests until %v", until)
}
}