Add sanitized real html pages (google,yandex,baidu) for tests. Raw search HTML parser tests.

This commit is contained in:
Rustem Kamalov
2026-04-14 00:41:35 +03:00
parent 518ccdbdbd
commit 9e22f68c6f
23 changed files with 490 additions and 235 deletions

View File

@@ -4,44 +4,10 @@ import (
"net/url"
"strings"
"testing"
"time"
"github.com/karust/openserp/core"
)
var browser *core.Browser
func init() {
opts := core.BrowserOpts{IsHeadless: false, IsLeakless: false, UseStealth: true, Timeout: time.Second * 5, LeavePageOpen: true}
browser, _ = core.NewBrowser(opts)
}
func TestSearchBing(t *testing.T) {
bing := New(*browser, core.SearchEngineOptions{})
query := core.Query{Text: "golang programming", Limit: 10}
results, err := bing.Search(query)
if err != nil {
t.Fatalf("Cannot [SearchBing]: %s", err)
}
if len(results) == 0 {
t.Fatalf("[SearchBing] returned empty result")
}
// Check that we have some basic fields populated
firstResult := results[0]
if firstResult.Title == "" {
t.Errorf("First result missing title: %+v", firstResult)
}
if firstResult.URL == "" {
t.Errorf("First result missing URL: %+v", firstResult)
}
if firstResult.Rank == 0 {
t.Errorf("First result missing rank: %+v", firstResult)
}
}
func TestBuildImageURL(t *testing.T) {
tests := []struct {
name string
@@ -65,7 +31,7 @@ func TestBuildImageURL(t *testing.T) {
name: "image query with filetype",
query: core.Query{Text: "dogs", Filetype: "png"},
wantErr: false,
wantCont: "q=dogs+filetype%3Apng",
wantCont: "q=dogs",
},
{
name: "empty query",
@@ -103,59 +69,3 @@ func TestBuildImageURL(t *testing.T) {
})
}
}
func TestBingImageSearch(t *testing.T) {
bing := New(*browser, core.SearchEngineOptions{RateTime: 5})
query := core.Query{
Text: "golden puppy",
Limit: 25,
Filetype: "jpg",
}
results, err := bing.SearchImage(query)
if err != nil {
t.Fatalf("Cannot search Bing images: %s", err)
}
if len(results) == 0 {
t.Fatalf("Bing image search returned empty result")
}
// Check that we have image results with proper fields
firstResult := results[0]
if firstResult.URL == "" {
t.Errorf("First result missing image URL: %+v", firstResult)
}
if firstResult.Title == "" {
t.Errorf("First result missing title: %+v", firstResult)
}
// Check that we have either image URL or source URL
hasImageURL := firstResult.URL != ""
hasSourceURL := firstResult.URL != ""
if !hasImageURL && !hasSourceURL {
t.Errorf("First result should have either image URL or source URL: %+v", firstResult)
}
// For image results, URL should typically point to an image file
if hasImageURL {
// Check if it looks like an image URL (common extensions)
imageExtensions := []string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
hasImageExtension := false
for _, ext := range imageExtensions {
if strings.Contains(strings.ToLower(firstResult.URL), ext) {
hasImageExtension = true
break
}
}
if !hasImageExtension {
t.Logf("Image URL doesn't have common extension (might be valid): %s", firstResult.URL)
}
}
t.Logf("Found %d image results", len(results))
t.Logf("First result - Title: %s", firstResult.Title)
t.Logf("First result - Image URL: %s", firstResult.URL)
t.Logf("First result - Source URL: %s", firstResult.URL)
}