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")
}
}