Files
magpie/internal/cli/search_cli_test.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

83 lines
3.0 KiB
Go

package cli
import (
"os"
"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)
}
}