samsa/internal/engines/bing_test.go
Franz Kafka 8e9aae062b
Some checks failed
Build and Push Docker Image / build-and-push (push) Failing after 11s
Mirror to GitHub / mirror (push) Failing after 5s
Tests / test (push) Successful in 42s
rename: kafka → samsa
Full project rename from kafka to samsa (after Gregor Samsa, who
woke one morning from uneasy dreams to find himself transformed).

- Module: github.com/metamorphosis-dev/kafka → samsa
- Binary: cmd/kafka/ → cmd/samsa/
- CSS: kafka.css → samsa.css
- UI: all 'kafka' product names, titles, localStorage keys → samsa
- localStorage keys: kafka-theme → samsa-theme, kafka-engines → samsa-engines
- OpenSearch: ShortName, LongName, description, URLs updated
- AGPL headers: 'kafka' → 'samsa'
- Docs, configs, examples updated
- Cache key prefix: kafka: → samsa:
2026-03-22 23:44:55 +00:00

102 lines
2.7 KiB
Go

package engines
import (
"context"
"net/http"
"strings"
"testing"
"time"
"github.com/metamorphosis-dev/samsa/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 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)
}
// Bing may block non-browser requests gracefully (return 0 results).
// The important thing is it doesn't crash.
t.Logf("bing returned %d results (total: %d)", len(resp.Results), resp.NumberOfResults)
t.Logf("unresponsive: %v", resp.UnresponsiveEngines)
if len(resp.UnresponsiveEngines) > 0 {
t.Skipf("bing blocked: %v", resp.UnresponsiveEngines[0])
}
if len(resp.Results) > 0 {
for _, r := range resp.Results {
if r.Engine != "bing" {
t.Errorf("expected engine 'bing', got %q", r.Engine)
}
if r.URL == nil || *r.URL == "" {
t.Error("expected non-empty URL")
}
}
}
}
func TestBingEngine_BlockedGracefully(t *testing.T) {
// Verify that when Bing returns HTML (bot detection), we get a valid
// response with unresponsive_engines instead of an error.
html := `<html><body>Bing requires JavaScript</body></html>`
// This test verifies the structure of the blocked response.
resp := contracts.SearchResponse{
Query: "test",
NumberOfResults: 0,
Results: []contracts.MainResult{},
Answers: []map[string]any{},
Corrections: []string{},
Infoboxes: []map[string]any{},
Suggestions: []string{},
UnresponsiveEngines: [][2]string{{"bing", "blocked by bot detection"}},
}
if len(resp.Results) != 0 {
t.Error("expected 0 results when blocked")
}
if len(resp.UnresponsiveEngines) != 1 {
t.Error("expected 1 unresponsive engine")
}
_ = html // just to use the variable
_ = strings.TrimSpace // use strings
}