feat(duckduckgo): detect anti-bot challenge and surface ErrBlocked
DuckDuckGo intermittently serves a CAPTCHA modal ("Unfortunately, bots
use DuckDuckGo too...") instead of search results. The result selector
matches zero elements on that page, so callers used to get
([]Result{}, nil) — silent empty results that look like "no matches."
Detect the challenge via the BEM class .anomaly-modal__title and return
a typed ErrBlocked so callers can distinguish blocked from no-results
and react (retry, fallback to another engine, surface to user).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package duckduckgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitea.stevedudenhoeffer.com/steve/go-extractor"
|
||||
@@ -106,6 +107,26 @@ func TestExtractResults_NoLinks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractResults_Blocked(t *testing.T) {
|
||||
doc := &extractortest.MockDocument{
|
||||
MockNode: extractortest.MockNode{
|
||||
Children: map[string]extractor.Nodes{
|
||||
".anomaly-modal__title": {
|
||||
&extractortest.MockNode{TextValue: "Unfortunately, bots use DuckDuckGo too."},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
results, err := extractResults(doc)
|
||||
if !errors.Is(err, ErrBlocked) {
|
||||
t.Fatalf("expected ErrBlocked, got %v", err)
|
||||
}
|
||||
if results != nil {
|
||||
t.Errorf("expected nil results when blocked, got %v", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearch_UsesMockBrowser(t *testing.T) {
|
||||
doc := &extractortest.MockDocument{
|
||||
URLValue: "https://duckduckgo.com/?q=test",
|
||||
|
||||
Reference in New Issue
Block a user