Files
magpie/internal/cli/search_cli_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

142 lines
5.0 KiB
Go

package cli
import (
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie search add, with a key and an address in either order, and rm.
func TestSearchCLI(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := searchCmd([]string{"add", "tavily"}); err == nil {
t.Error("tavily added without a key")
}
if err := searchCmd([]string{"add", "google", "k"}); err == nil {
t.Error("an unknown search API added")
}
if err := searchCmd([]string{"add", "searxng"}); err == nil {
t.Error("SearXNG added without its address")
}
for _, args := range [][]string{{"add", "Tavily", "tvly-1"}, {"add", "searxng", "url=https://sx.example.com"}, {"add", "brave", "url=https://brave.example.com", "b-1"}} {
if err := searchCmd(args); err != nil {
t.Fatalf("%v: %v", args, err)
}
}
// a new address alone keeps the key
if err := searchCmd([]string{"add", "brave", "url=https://brave2.example.com"}); err != nil {
t.Fatal(err)
}
as := provider.SearchAPIs()
if len(as) != 3 || as[0] != (provider.SearchAPI{Vendor: "tavily", Key: "tvly-1"}) || as[1].URL != "https://sx.example.com" ||
as[2] != (provider.SearchAPI{Vendor: "brave", Key: "b-1", URL: "https://brave2.example.com"}) {
t.Fatalf("%+v", as)
}
// a new key alone keeps the base URL the user set (#1580); url= with
// nothing puts the vendor's own back
if err := searchCmd([]string{"add", "brave", "b-2"}); err != nil {
t.Fatal(err)
}
if as := provider.SearchAPIs(); as[2] != (provider.SearchAPI{Vendor: "brave", Key: "b-2", URL: "https://brave2.example.com"}) {
t.Fatalf("a new key took the base URL away: %+v", as[2])
}
if err := searchCmd([]string{"add", "brave", "url="}); err != nil {
t.Fatal(err)
}
if as := provider.SearchAPIs(); as[2] != (provider.SearchAPI{Vendor: "brave", Key: "b-2"}) || as[2].Base() != "https://api.search.brave.com" {
t.Fatalf("url= kept the base URL: %+v", as[2])
}
for _, bad := range []string{"url=ftp://x.example.com", "url=https://x.example.com/?k=1", "url=https://x.example.com/#a", "url=x.example.com"} {
if err := searchCmd([]string{"add", "tavily", "tvly-2", bad}); err == nil {
t.Errorf("%s taken", bad)
}
}
if err := searchCmd(nil); err != nil {
t.Fatal(err)
}
if err := searchCmd([]string{"rm", "tavily"}); err != nil {
t.Fatal(err)
}
if err := searchCmd([]string{"rm", "tavily"}); err == nil {
t.Error("removed twice")
}
if as := provider.SearchAPIs(); len(as) != 2 || as[0].Vendor != "searxng" {
t.Fatalf("after rm %+v", as)
}
}
// magpie search add you, with no key, adds You.com's free search (#1626).
func TestSearchCLIYouWithoutKey(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := searchCmd([]string{"add", "you"}); err != nil {
t.Fatal(err)
}
if as := provider.SearchAPIs(); len(as) != 1 || as[0] != (provider.SearchAPI{Vendor: "you"}) {
t.Fatalf("%+v", as)
}
}
// magpie search off keeps an API, key and all, out of every search, on
// puts it back, and move puts it at another place in the order (#2286).
func TestSearchCLIOffAndMove(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, args := range [][]string{{"add", "tavily", "tvly-1"}, {"add", "exa", "e-1"}, {"add", "brave", "b-1"}} {
if err := searchCmd(args); err != nil {
t.Fatalf("%v: %v", args, err)
}
}
vendors := func(as []provider.SearchAPI) (vs []string) {
for _, a := range as {
vs = append(vs, a.Vendor)
}
return vs
}
if err := searchCmd([]string{"off", "exa"}); err != nil {
t.Fatal(err)
}
if got := vendors(provider.SearchAPIs()); !slices.Equal(got, []string{"tavily", "brave"}) {
t.Fatalf("exa off, asked %v", got)
}
if s := provider.StoredSearchAPIs(); len(s) != 3 || s[1].Key != "e-1" {
t.Fatalf("off lost the key: %+v", s)
}
// a new key leaves it off
if err := searchCmd([]string{"add", "exa", "e-2"}); err != nil {
t.Fatal(err)
}
if got := vendors(provider.SearchAPIs()); !slices.Equal(got, []string{"tavily", "brave"}) {
t.Fatalf("a new key switched exa on: %v", got)
}
if err := searchCmd([]string{"off", "google"}); err == nil {
t.Error("switched off one not set up")
}
if err := searchCmd([]string{"on", "exa"}); err != nil {
t.Fatal(err)
}
if err := searchCmd([]string{"move", "brave", "1"}); err != nil {
t.Fatal(err)
}
if got := vendors(provider.SearchAPIs()); !slices.Equal(got, []string{"brave", "tavily", "exa"}) {
t.Fatalf("after move %v", got)
}
if err := searchCmd([]string{"move", "brave", "3"}); err != nil {
t.Fatal(err)
}
if got := vendors(provider.SearchAPIs()); !slices.Equal(got, []string{"tavily", "exa", "brave"}) {
t.Fatalf("after move to last %v", got)
}
for _, bad := range [][]string{{"move", "brave", "4"}, {"move", "brave", "0"}, {"move", "brave"}, {"move", "google", "1"}} {
if err := searchCmd(bad); err == nil {
t.Errorf("%v taken", bad)
}
}
}