Files
magpie/internal/cli/search_cli_test.go
T
yetoneandmdwsk88 4e0e8e7933 gateway, settings, gui, cli: the results a search is answered with can be set, 1 to 20 (#5545)
Claude Code's WebSearch and Codex's web_search never name a count, so
magpie answered every such search with searchHits, 6 pages (#5545,
mdwsk88: more pages for research-heavy work, fewer to save context).

settings.SearchCount holds the count. 0 means the default, 6
(SearchCountDefault), and the most is 20 (SearchCountMost), the most
Brave and Tavily return. Save refuses a value out of range, so nothing
is written. Settings.SearchPages reads it. The gateway's searchCount
falls back to it when a search names no count. The web_search MCP tool
still answers a named count with that count, and its schema's default
says the count set. It reaches every search API (Brave, Tavily, Exa,
You, the plugins' searchers), as a named count already did.

- Settings › Models › Web search: "Results per search", segments 3, 6,
  10, 15 and 20, plus a count set by hand in settings.json.
  POST /api/settings/search-count sets it alone. settingsapi.Apply
  keeps it as it keeps search-all, so a save of the Settings form
  doesn't put it back.
- CLI: magpie search count says the count; magpie search count <n>
  sets it; magpie search count default puts 6 back. A bad value is
  refused and nothing is written.
- docs/subsystems/gateway-routing.md says it.
- New strings are in zh, zh-TW, ja and de.

Run at this tree (mote-run, macOS):
- go test -count=1: internal/settings, settingsapi, cli, gateway
  (TestSearchMCPCount, TestSearchCountReachesEveryAPI,
  TestSearchCountSetting), and gui (TestSettingsSearchCountSetOnItsOwn, and
  TestSettingsSaveKeepsWhatItDoesNotSend with SearchCount set): ok.
  internal/gateway whole: ok with -timeout 45m (847s). internal/gui
  whole: ok but TestMainWindowCorners/macOS_26_SDK (#3949, mote is on
  macOS 27).
  The first cli run was red on TestEveryRouteHasACommandOrIsNamed:
  the new route had no CLI entry. cliForRoute maps it to search count
  now.
- Broken by hand: searchCount returning searchHits again makes
  TestSearchCountSetting fail ("set 11: a search naming no count asks
  for 6, want 11"). Restored, it passes.
- GUI: node --test --test-concurrency=4 over routing-*, usage-*,
  ledger-*, panel-*, call-*, search-*, searcher-*, settings-*, i18n-*
  and gui-* (Chromium and WebKit; the files cover en, zh, zh-TW, ja
  and de), with #2398 and #5545 in one tree: 4961 of 5016 ok. The 55
  reds are in 12 files. i18n-keys' "a plugin's words are its own"
  names other files' strings (#5327, #5426, #5482). The other 11 files
  were run alone at dc49feca9 (this tree without either change) and at
  this tree: 52 and 51 reds, the same tests. The one apart is
  usage-focus-steady, which flakes (#3501). Each is filed: #5392,
  #3414, #5048, #3799, #3415, #3954, #4627, #4628, #5223, #1529.
  routing-canceled-row.test.cjs: 10 of 10. With the .rt-req.gone rule
  taken out, its dot is the answered green rgb(24, 154, 88) and it
  fails. Restored, it passes.
- Not tried: a real Claude Code WebSearch through this tree with a live
  search API key. The tests use fake search APIs.

Co-authored-by: mdwsk88 <11055210+mdwsk88@users.noreply.github.com>
Reported-by: mdwsk88 (dawei on Discord)
2026-11-07 04:03:01 +08:00

185 lines
6.4 KiB
Go

package cli
import (
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// 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)
}
}
}
// magpie search count (#5545, mdwsk88): a number from 1 to 20 is written,
// "default" puts 6 back, and a flag, a word or a number out of range is
// refused with nothing written.
func TestSearchCLICount(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{"count"}); err != nil {
t.Fatal(err)
}
if n := settings.Load().SearchPages(); n != 6 {
t.Fatalf("by default %d", n)
}
for _, c := range []struct {
arg string
want int
}{{"12", 12}, {"20", 20}, {"1", 1}, {"6", 6}, {"default", 0}} {
if err := searchCmd([]string{"count", c.arg}); err != nil {
t.Fatalf("%s: %v", c.arg, err)
}
if got := settings.Load().SearchCount; got != c.want {
t.Errorf("count %s wrote %d, want %d", c.arg, got, c.want)
}
}
if err := searchCmd([]string{"count", "9"}); err != nil {
t.Fatal(err)
}
for _, bad := range [][]string{{"count", "0"}, {"count", "21"}, {"count", "-3"}, {"count", "--help"}, {"count", "many"}, {"count", "7.5"}, {"count", "8", "9"}} {
if err := searchCmd(bad); err == nil {
t.Errorf("%v taken", bad)
}
if got := settings.Load().SearchCount; got != 9 {
t.Fatalf("%v wrote %d", bad, got)
}
}
s := settings.Load()
s.SearchCount = 21
if err := settings.Save(s); err == nil {
t.Error("settings took a count of 21")
}
}