package engines
import (
"context"
"net/http"
"strings"
"testing"
"time"
"github.com/ashie/gosearch/internal/contracts"
)
func TestBingEngine_EmptyQuery(t *testing.T) {
eng := &BingEngine{}
resp, err := eng.Search(context.Background(), contracts.SearchRequest{Query: ""})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Results) != 0 {
t.Errorf("expected 0 results for empty query, got %d", len(resp.Results))
}
}
func TestBingEngine_Name(t *testing.T) {
eng := &BingEngine{}
if eng.Name() != "bing" {
t.Errorf("expected 'bing', got %q", eng.Name())
}
}
func TestBingEngine_Uninitialized(t *testing.T) {
eng := &BingEngine{}
_, err := eng.Search(context.Background(), contracts.SearchRequest{Query: "test"})
if err == nil {
t.Error("expected error for uninitialized client")
}
}
func TestParseBingHTML(t *testing.T) {
html := `
This is a test snippet from Bing.
`
results, err := parseBingHTML(strings.NewReader(html))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
if results[0].Title != "Example Title" {
t.Errorf("expected 'Example Title', got %q", results[0].Title)
}
if *results[0].URL != "https://example.com" {
t.Errorf("expected 'https://example.com', got %q", *results[0].URL)
}
if results[0].Content != "This is a test snippet from Bing." {
t.Errorf("unexpected content: %q", results[0].Content)
}
}
func TestBingEngine_LiveRequest(t *testing.T) {
if testing.Short() {
t.Skip("skipping live request")
}
client := &http.Client{}
eng := &BingEngine{client: client}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
resp, err := eng.Search(ctx, contracts.SearchRequest{
Query: "golang programming language",
})
if err != nil {
t.Fatalf("live search failed: %v", err)
}
t.Logf("bing returned %d results", len(resp.Results))
for _, r := range resp.Results {
if r.Engine != "bing" {
t.Errorf("expected engine 'bing', got %q", r.Engine)
}
}
}