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
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A search goes to the search APIs in the order the user arranged them,
|
|
// and never to one switched off, whose key stays (#2286, blackrion).
|
|
func TestSearchAPIOrderAndOff(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
f := newSearchAPIs(t)
|
|
for _, a := range []provider.SearchAPI{f.api("tavily", "tvly-k"), f.api("exa", "exa-k"), f.api("brave", "brave-k")} {
|
|
if err := provider.SetSearchAPI(a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
s := New()
|
|
ask := func() []string {
|
|
t.Helper()
|
|
f.mu.Lock()
|
|
f.asked = nil
|
|
f.mu.Unlock()
|
|
if _, _, err := s.apiSearch(context.Background(), "latest go"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return slices.Clone(f.asked)
|
|
}
|
|
if got := ask(); !slices.Equal(got, []string{"tavily: latest go"}) {
|
|
t.Fatalf("as added: %v", got)
|
|
}
|
|
if err := provider.ArrangeSearchAPIs([]string{"brave", "tavily"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ask(); !slices.Equal(got, []string{"brave: latest go"}) {
|
|
t.Fatalf("brave moved first, asked %v", got)
|
|
}
|
|
if err := provider.SwitchSearchAPI("brave", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ask(); !slices.Equal(got, []string{"tavily: latest go"}) {
|
|
t.Fatalf("brave off, asked %v", got)
|
|
}
|
|
// off and the next one failing: the one after it, never the one off
|
|
f.mu.Lock()
|
|
f.broken = true
|
|
f.mu.Unlock()
|
|
if got := ask(); !slices.Equal(got, []string{"tavily: latest go", "exa: latest go"}) {
|
|
t.Fatalf("tavily down with brave off, asked %v", got)
|
|
}
|
|
if err := provider.SwitchSearchAPI("tavily", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SwitchSearchAPI("exa", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(provider.SearchAPIs()) != 0 {
|
|
t.Fatal("every API off, and one still to be asked")
|
|
}
|
|
var order []string
|
|
for _, a := range provider.StoredSearchAPIs() {
|
|
order = append(order, a.Vendor)
|
|
if a.Key == "" {
|
|
t.Errorf("%s lost its key", a.Vendor)
|
|
}
|
|
}
|
|
if !slices.Equal(order, []string{"brave", "tavily", "exa"}) {
|
|
t.Fatalf("stored %v", order)
|
|
}
|
|
for _, bad := range [][]string{{"brave", "brave"}, {"google"}} {
|
|
if err := provider.ArrangeSearchAPIs(bad); err == nil {
|
|
t.Errorf("%v arranged", bad)
|
|
}
|
|
}
|
|
}
|