Files
magpie/internal/backup/search_api_test.go
T
yetone ec13d1162a search: the search APIs are dragged into the order they are asked in, and each switches off without losing its key (#2286, blackrion)
The search APIs were asked in the order they were added, and the only way
to keep one out of the chain (to try another alone, or rest one whose
credit is nearly spent) was to remove it and paste its key again later.

- provider: SearchAPI.Off; SearchAPIs() leaves an API switched off out, so
  neither a model's search nor the web_search MCP asks it. SwitchSearchAPI
  keeps key, URL and place; SetSearchAPI keeps Off. ArrangeSearchAPIs puts
  them in the vendors' order, refuses an unknown or twice-named vendor, and
  keeps one not named after them. RestoreSearchAPIs now puts a backup's
  APIs in the backup's order (one only here after them), so a restored
  order and Off come back as they were; sync already mirrors the list.
- GUI: each API's row and its keys' row are one unit, dragged off its
  buttons with dragRows (as routing's groups, #779) or moved with Alt+↑/↓,
  renumbered at once, focus kept, redrawn as saved if the order is
  refused. A lib-switch per row posts {vendor, on}; one off is dimmed and
  says Off; Search first with counts only APIs that are on. The row's
  buttons wrap instead of running past a 420px window.
- CLI: magpie search off|on <api>, move <api> <n>; the list says off.
- POST /api/settings/search-api takes {arrange} and {vendor, on}.
- Docs: gateway-routing "The search APIs' order, and one switched off",
  gui-shell, README (en, zh-CN).

Run: go test -tags nogui ./internal/provider ./internal/cli ./internal/gui
./internal/gateway (TestSearch*) ./internal/backup ./internal/davsync;
GOOS=linux/windows go vet of the touched packages; gofmt -l clean.
GUI: search-order.test.cjs 16/16 (chromium + webkit, en/zh/ja/de,
1100/420), and search-api, search-first, search-instead, search-keys,
search-mcp, searcher-pick, i18n-keys, gui-ja, gui-de, gui-zh-tw 100/100.
Broken by hand: SearchAPIs without the Off filter turns
TestSearchAPIOrderAndOff and TestSearchCLIOffAndMove red; the GUI
counting off APIs for Search first turns search-order red.
Full suite: SUITE_LINE
2026-10-15 23:08:00 +08:00

114 lines
3.4 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)
}
}