Files
magpie/internal/backup/search_api_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

160 lines
4.9 KiB
Go

package backup
import (
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The search APIs (#419) go in a backup with the providers: their keys
// too, or none without keys, when the keys already here stay.
func TestBackupSearchAPIs(t *testing.T) {
home(t)
for _, a := range []provider.SearchAPI{{Vendor: "tavily", Key: "tvly-secret"}, {Vendor: "searxng", URL: "https://sx.example.com"}} {
if err := provider.SetSearchAPI(a); err != nil {
t.Fatal(err)
}
}
with, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
without, err := Collect(false, "test")
if err != nil {
t.Fatal(err)
}
if without.Searches == nil || len(*without.Searches) != 2 || (*without.Searches)[0].Key != "" || (*without.Searches)[1].URL != "https://sx.example.com" {
t.Fatalf("without keys: %+v", without.Searches)
}
data, err := Seal(with, "pass")
if err != nil {
t.Fatal(err)
}
home(t) // another machine
got, err := Open(data, "pass")
if err != nil {
t.Fatal(err)
}
if _, err := Restore(got, All); err != nil {
t.Fatal(err)
}
if as := provider.SearchAPIs(); len(as) != 2 || as[0].Vendor != "tavily" || as[0].Key != "tvly-secret" || as[1].Vendor != "searxng" {
t.Fatalf("restored %+v", as)
}
// one without keys keeps the key here, and one with no key here asks for it
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily", Key: "tvly-here"}); err != nil {
t.Fatal(err)
}
*without.Searches = append(*without.Searches, provider.SearchAPI{Vendor: "brave"})
r, err := Restore(without, Parts{Providers: true})
if err != nil {
t.Fatal(err)
}
if as := provider.StoredSearchAPIs(); len(as) != 3 || as[0].Key != "tvly-here" {
t.Fatalf("restored without keys %+v", as)
}
if !slices.Contains(r.NeedKey, "Brave Search") || slices.Contains(r.NeedKey, "Tavily") {
t.Fatalf("need key %v", r.NeedKey)
}
// a backup from a magpie before them leaves them alone
old := without
old.Searches = nil
if _, err := Restore(old, Parts{Providers: true}); err != nil {
t.Fatal(err)
}
if len(provider.StoredSearchAPIs()) != 3 {
t.Fatal("an older backup took the search APIs away")
}
}
// A backup carries the search APIs' order and which are switched off
// (#2286): put back, they are asked as they were, and one only here after.
func TestBackupSearchAPIsOrderAndOff(t *testing.T) {
home(t)
for _, a := range []provider.SearchAPI{{Vendor: "tavily", Key: "t"}, {Vendor: "exa", Key: "e"}, {Vendor: "brave", Key: "b"}} {
if err := provider.SetSearchAPI(a); err != nil {
t.Fatal(err)
}
}
if err := provider.ArrangeSearchAPIs([]string{"brave", "exa", "tavily"}); err != nil {
t.Fatal(err)
}
if err := provider.SwitchSearchAPI("exa", false); err != nil {
t.Fatal(err)
}
b, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
home(t) // another machine, with its own in another order
for _, a := range []provider.SearchAPI{{Vendor: "you"}, {Vendor: "tavily", Key: "t"}, {Vendor: "exa", Key: "e"}} {
if err := provider.SetSearchAPI(a); err != nil {
t.Fatal(err)
}
}
if _, err := Restore(b, Parts{Providers: true}); err != nil {
t.Fatal(err)
}
var stored, asked []string
for _, a := range provider.StoredSearchAPIs() {
stored = append(stored, a.Vendor)
}
for _, a := range provider.SearchAPIs() {
asked = append(asked, a.Vendor)
}
if !slices.Equal(stored, []string{"brave", "exa", "tavily", "you"}) || !slices.Equal(asked, []string{"brave", "tavily", "you"}) {
t.Fatalf("stored %v, asked %v", stored, asked)
}
}
// A key switched off in a search API's list (辰林鸭~ on Discord) goes in a
// backup with its keys and comes back off; one without keys carries none
// of them, on or off, and the list here stays whole.
func TestBackupSearchKeysOff(t *testing.T) {
home(t)
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily", Key: "tvly-a,tvly-b"}); err != nil {
t.Fatal(err)
}
if err := provider.SwitchSearchKey("tavily", provider.SearchKeyRef("tvly-a"), false); err != nil {
t.Fatal(err)
}
want := []provider.SearchKey{{Key: "tvly-a", Off: true}, {Key: "tvly-b"}}
with, err := Collect(true, "test")
if err != nil {
t.Fatal(err)
}
without, err := Collect(false, "test")
if err != nil {
t.Fatal(err)
}
if s := *without.Searches; len(s) != 1 || s[0].HasKeys() {
t.Fatalf("a backup without keys holds %+v", s)
}
data, err := Seal(with, "pass")
if err != nil {
t.Fatal(err)
}
home(t) // another machine
got, err := Open(data, "pass")
if err != nil {
t.Fatal(err)
}
if _, err := Restore(got, All); err != nil {
t.Fatal(err)
}
if as := provider.StoredSearchAPIs(); len(as) != 1 || !slices.Equal(as[0].SearchKeys(), want) || as[0].Key != "tvly-b" {
t.Fatalf("restored %+v", as)
}
if _, err := Restore(without, Parts{Providers: true}); err != nil {
t.Fatal(err)
}
if as := provider.StoredSearchAPIs(); len(as) != 1 || !slices.Equal(as[0].SearchKeys(), want) {
t.Fatalf("a backup without keys took keys here away: %+v", as)
}
}