Repository navigation
Expand file tree
/
Copy pathsearch_cli_test.go
More file actions
50 lines (47 loc) · 1.63 KB
/
Copy pathsearch_cli_test.go
File metadata and controls
50 lines (47 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
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)
}
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)
}
}