~bigbes/huntsman

ref: 783841b91eafd678cb3895cfcc8dfd89f290ece7 huntsman/internal/domain/search/providers_test.go -rw-r--r-- 2.4 KiB
783841b9 — Eugene Blikh Initial commit: multi-provider search router 6 days ago
                                                                                
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package search

import (
	"strings"
	"testing"
)

func TestRoute(t *testing.T) {
	providers := providerByID()

	cases := []struct {
		name      string
		input     string
		wantID    string
		wantQuery string
	}{
		{"empty falls back to default", "", "gh", ""},
		{"unknown prefix becomes part of query", "foo bar", "gh", "foo bar"},
		{"space prefix routes to ud", "ud meme", "ud", "meme"},
		{"colon prefix routes to gh", "gh:repo language:go", "gh", "repo language:go"},
		{"steam prefix with multi-word query", "steam half life", "steam", "half life"},
		{"bare prefix returns provider with empty query", "ud", "ud", ""},
		{"prefix is case-insensitive", "UD bar", "ud", "bar"},
		{"leading whitespace is stripped", "   gh    foo", "gh", "foo"},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			got, q, err := Route(tc.input, providers, "gh")
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if got.ID != tc.wantID {
				t.Errorf("provider ID = %q, want %q", got.ID, tc.wantID)
			}
			if q != tc.wantQuery {
				t.Errorf("query = %q, want %q", q, tc.wantQuery)
			}
		})
	}
}

func TestRouteUnknownDefault(t *testing.T) {
	if _, _, err := Route("anything", providerByID(), "nope"); err == nil {
		t.Fatal("expected error for unknown default provider")
	}
}

func TestSearchURLFor(t *testing.T) {
	p, ok := Lookup("ud")
	if !ok {
		t.Fatal("ud provider missing")
	}
	url := p.SearchURLFor("hello world")
	if !strings.Contains(url, "term=hello+world") {
		t.Errorf("expected URL to contain encoded query, got %q", url)
	}

	if got := p.SearchURLFor(""); got != p.HomeURL {
		t.Errorf("empty query should return HomeURL, got %q", got)
	}
}

func TestDescriptionForRouter(t *testing.T) {
	osd := DescriptionForRouter("https://example.com/")
	if len(osd.URLs) != 1 {
		t.Fatalf("expected 1 URL element, got %d", len(osd.URLs))
	}
	want := "https://example.com/search?q={searchTerms}"
	if osd.URLs[0].Template != want {
		t.Errorf("template = %q, want %q", osd.URLs[0].Template, want)
	}
}

func TestDescriptionForProvider(t *testing.T) {
	p, _ := Lookup("gh")
	osd := DescriptionForProvider(p)
	if len(osd.URLs) != 1 {
		t.Fatalf("expected 1 URL element, got %d", len(osd.URLs))
	}
	if !strings.Contains(osd.URLs[0].Template, "{searchTerms}") {
		t.Errorf("template should contain {searchTerms}, got %q", osd.URLs[0].Template)
	}
	if strings.Contains(osd.URLs[0].Template, "{q}") {
		t.Errorf("template should not contain internal {q} placeholder")
	}
}