package search
import (
"encoding/xml"
"strings"
"testing"
)
func TestMarshalEmitsXMLProlog(t *testing.T) {
osd := DescriptionForRouter("https://example.com")
body, err := Marshal(osd)
if err != nil {
t.Fatalf("Marshal: %v", err)
}
if !strings.HasPrefix(string(body), "<?xml") {
t.Errorf("missing XML prolog: %q", body[:min(40, len(body))])
}
// Round-trip should succeed.
var got OpenSearchDescription
if err := xml.Unmarshal(body, &got); err != nil {
t.Fatalf("unmarshal round-trip: %v", err)
}
if got.ShortName != osd.ShortName {
t.Errorf("ShortName lost in round-trip: %q vs %q", got.ShortName, osd.ShortName)
}
}
func TestDescriptionForRouterStripsTrailingSlash(t *testing.T) {
osd := DescriptionForRouter("https://example.com///")
if osd.URLs[0].Template != "https://example.com/search?q={searchTerms}" {
t.Errorf("template = %q", osd.URLs[0].Template)
}
if osd.SearchForm != "https://example.com/" {
t.Errorf("SearchForm = %q", osd.SearchForm)
}
}
func TestDescriptionForProviderConvertsPlaceholder(t *testing.T) {
p := Provider{
ID: "x",
Name: "X",
SearchURL: "https://x.example/?q={q}",
HomeURL: "https://x.example/",
}
osd := DescriptionForProvider(p)
if osd.URLs[0].Template != "https://x.example/?q={searchTerms}" {
t.Errorf("template = %q", osd.URLs[0].Template)
}
if osd.Image != nil {
t.Errorf("Image should be nil when IconURL empty, got %+v", osd.Image)
}
}
func TestDescriptionForProviderIncludesIcon(t *testing.T) {
p := Provider{
ID: "x",
Name: "X",
SearchURL: "https://x.example/?q={q}",
HomeURL: "https://x.example/",
IconURL: "https://x.example/favicon.ico",
}
osd := DescriptionForProvider(p)
if osd.Image == nil {
t.Fatal("Image should not be nil")
}
if osd.Image.Value != p.IconURL {
t.Errorf("Image.Value = %q", osd.Image.Value)
}
if osd.Image.Width != 16 || osd.Image.Height != 16 {
t.Errorf("Image dimensions = %dx%d", osd.Image.Width, osd.Image.Height)
}
}