Merge pull request 'Mock-based site extractor test infrastructure' (#43) from test/site-extractor-mocks into main
All checks were successful
CI / test (push) Successful in 1m4s
CI / build (push) Successful in 1m7s
CI / vet (push) Successful in 1m7s

This commit was merged in pull request #43.
This commit is contained in:
2026-02-15 16:38:15 +00:00
3 changed files with 407 additions and 0 deletions

103
extractortest/mock.go Normal file
View File

@@ -0,0 +1,103 @@
// Package extractortest provides mock implementations of the extractor
// interfaces for use in unit tests. The mocks support selector-based
// responses so site extractors can exercise their parsing logic without
// a real browser.
package extractortest
import (
"context"
"fmt"
"time"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
)
// MockNode implements extractor.Node with configurable selector responses.
type MockNode struct {
TextValue string
TextErr error
ContentValue string
Attrs map[string]string
// Children maps CSS selectors to the nodes that should be returned.
Children map[string]extractor.Nodes
}
var _ extractor.Node = &MockNode{}
func (m *MockNode) Content() (string, error) { return m.ContentValue, nil }
func (m *MockNode) Text() (string, error) { return m.TextValue, m.TextErr }
func (m *MockNode) Attr(name string) (string, error) {
if m.Attrs == nil {
return "", nil
}
v, ok := m.Attrs[name]
if !ok {
return "", nil
}
return v, nil
}
func (m *MockNode) Screenshot() ([]byte, error) { return nil, nil }
func (m *MockNode) Type(_ string) error { return nil }
func (m *MockNode) Click() error { return nil }
func (m *MockNode) SetHidden(_ bool) error { return nil }
func (m *MockNode) SetAttribute(_, _ string) error { return nil }
func (m *MockNode) Select(selector string) extractor.Nodes {
if m.Children == nil {
return nil
}
return m.Children[selector]
}
func (m *MockNode) SelectFirst(selector string) extractor.Node {
return m.Select(selector).First()
}
func (m *MockNode) ForEach(selector string, fn func(extractor.Node) error) error {
nodes := m.Select(selector)
for _, n := range nodes {
if err := fn(n); err != nil {
return err
}
}
return nil
}
// MockDocument implements extractor.Document with configurable selector responses.
type MockDocument struct {
MockNode
URLValue string
}
var _ extractor.Document = &MockDocument{}
func (m *MockDocument) URL() string { return m.URLValue }
func (m *MockDocument) Refresh() error { return nil }
func (m *MockDocument) Close() error { return nil }
func (m *MockDocument) WaitForNetworkIdle(_ *time.Duration) error { return nil }
// Content on MockDocument returns its own ContentValue (overrides embedded MockNode).
func (m *MockDocument) Content() (string, error) { return m.ContentValue, nil }
// MockBrowser implements extractor.Browser. Configure Documents to map URLs
// to MockDocuments that will be returned from Open.
type MockBrowser struct {
Documents map[string]*MockDocument
}
var _ extractor.Browser = &MockBrowser{}
func (m *MockBrowser) Close() error { return nil }
func (m *MockBrowser) Open(_ context.Context, url string, _ extractor.OpenPageOptions) (extractor.Document, error) {
if m.Documents == nil {
return nil, fmt.Errorf("no documents configured")
}
doc, ok := m.Documents[url]
if !ok {
return nil, fmt.Errorf("no mock document for URL %q", url)
}
return doc, nil
}

View File

@@ -0,0 +1,137 @@
package duckduckgo
import (
"context"
"testing"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
"gitea.stevedudenhoeffer.com/steve/go-extractor/extractortest"
)
func makeResultNode(url, title, desc string) *extractortest.MockNode {
return &extractortest.MockNode{
Children: map[string]extractor.Nodes{
`a[href][target="_self"]`: {
&extractortest.MockNode{Attrs: map[string]string{"href": url}},
},
"h2": {
&extractortest.MockNode{TextValue: title},
},
"span > span": {
&extractortest.MockNode{TextValue: desc},
},
},
}
}
func TestExtractResults(t *testing.T) {
doc := &extractortest.MockDocument{
URLValue: "https://duckduckgo.com/?q=test",
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
`article[id^="r1-"]`: {
makeResultNode("https://example.com", "Example", "An example site"),
makeResultNode("https://golang.org", "Go", "Go programming language"),
},
},
},
}
results, err := extractResults(doc)
if err != nil {
t.Fatalf("extractResults() error: %v", err)
}
if len(results) != 2 {
t.Fatalf("expected 2 results, got %d", len(results))
}
if results[0].URL != "https://example.com" {
t.Errorf("results[0].URL = %q, want %q", results[0].URL, "https://example.com")
}
if results[0].Title != "Example" {
t.Errorf("results[0].Title = %q, want %q", results[0].Title, "Example")
}
if results[0].Description != "An example site" {
t.Errorf("results[0].Description = %q, want %q", results[0].Description, "An example site")
}
if results[1].URL != "https://golang.org" {
t.Errorf("results[1].URL = %q, want %q", results[1].URL, "https://golang.org")
}
if results[1].Title != "Go" {
t.Errorf("results[1].Title = %q, want %q", results[1].Title, "Go")
}
}
func TestExtractResults_NoResults(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
`article[id^="r1-"]`: {},
},
},
}
results, err := extractResults(doc)
if err != nil {
t.Fatalf("extractResults() error: %v", err)
}
if len(results) != 0 {
t.Errorf("expected 0 results, got %d", len(results))
}
}
func TestExtractResults_NoLinks(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
`article[id^="r1-"]`: {
&extractortest.MockNode{
Children: map[string]extractor.Nodes{
`a[href][target="_self"]`: {},
},
},
},
},
},
}
results, err := extractResults(doc)
if err != nil {
t.Fatalf("extractResults() error: %v", err)
}
if len(results) != 0 {
t.Errorf("expected 0 results (no links), got %d", len(results))
}
}
func TestSearch_UsesMockBrowser(t *testing.T) {
doc := &extractortest.MockDocument{
URLValue: "https://duckduckgo.com/?q=test",
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
`article[id^="r1-"]`: {
makeResultNode("https://example.com", "Example", "Example description"),
},
},
},
}
browser := &extractortest.MockBrowser{
Documents: map[string]*extractortest.MockDocument{
"https://duckduckgo.com/?kp=-2&q=test": doc,
},
}
results, err := DefaultConfig.Search(context.Background(), browser, "test")
if err != nil {
t.Fatalf("Search() error: %v", err)
}
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
if results[0].URL != "https://example.com" {
t.Errorf("results[0].URL = %q, want %q", results[0].URL, "https://example.com")
}
}

View File

@@ -0,0 +1,167 @@
package powerball
import (
"context"
"testing"
"time"
"gitea.stevedudenhoeffer.com/steve/go-extractor"
"gitea.stevedudenhoeffer.com/steve/go-extractor/extractortest"
)
func makePowerballDoc() *extractortest.MockDocument {
return &extractortest.MockDocument{
URLValue: "https://www.powerball.com/",
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"#numbers .title-date": {
&extractortest.MockNode{TextValue: "Sat, Feb 15, 2026"},
},
"div.game-ball-group div.white-balls": {
&extractortest.MockNode{TextValue: "7"},
&extractortest.MockNode{TextValue: "14"},
&extractortest.MockNode{TextValue: "21"},
&extractortest.MockNode{TextValue: "35"},
&extractortest.MockNode{TextValue: "62"},
},
"div.game-ball-group div.powerball": {
&extractortest.MockNode{TextValue: "10"},
},
"span.power-play span.multiplier": {
&extractortest.MockNode{TextValue: "3X"},
},
"div.next-powerball h5.title-date": {
&extractortest.MockNode{TextValue: "Mon, Feb 17, 2026"},
},
"div.next-powerball div.game-detail-group span.game-jackpot-number": {
&extractortest.MockNode{TextValue: "$1.5 Billion"},
},
},
},
}
}
func TestGetDrawing(t *testing.T) {
doc := makePowerballDoc()
drawing, err := getDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getDrawing() error: %v", err)
}
expectedDate := time.Date(2026, 2, 15, 0, 0, 0, 0, time.UTC)
if !drawing.Date.Equal(expectedDate) {
t.Errorf("Date = %v, want %v", drawing.Date, expectedDate)
}
expectedNums := [5]int{7, 14, 21, 35, 62}
if drawing.Numbers != expectedNums {
t.Errorf("Numbers = %v, want %v", drawing.Numbers, expectedNums)
}
if drawing.PowerBall != 10 {
t.Errorf("PowerBall = %d, want 10", drawing.PowerBall)
}
if drawing.PowerPlay != 3 {
t.Errorf("PowerPlay = %d, want 3", drawing.PowerPlay)
}
}
func TestGetNextDrawing(t *testing.T) {
doc := makePowerballDoc()
nd, err := getNextDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getNextDrawing() error: %v", err)
}
if nd.Date != "Mon, Feb 17, 2026" {
t.Errorf("Date = %q, want %q", nd.Date, "Mon, Feb 17, 2026")
}
if nd.JackpotDollars != 1500000000 {
t.Errorf("JackpotDollars = %d, want 1500000000", nd.JackpotDollars)
}
}
func TestGetNextDrawing_Million(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"div.next-powerball h5.title-date": {
&extractortest.MockNode{TextValue: "Wed, Feb 19, 2026"},
},
"div.next-powerball div.game-detail-group span.game-jackpot-number": {
&extractortest.MockNode{TextValue: "$250 Million"},
},
},
},
}
nd, err := getNextDrawing(context.Background(), doc)
if err != nil {
t.Fatalf("getNextDrawing() error: %v", err)
}
if nd.JackpotDollars != 250000000 {
t.Errorf("JackpotDollars = %d, want 250000000", nd.JackpotDollars)
}
}
func TestGetCurrent_Integration(t *testing.T) {
doc := makePowerballDoc()
browser := &extractortest.MockBrowser{
Documents: map[string]*extractortest.MockDocument{
"https://www.powerball.com/": doc,
},
}
drawing, nd, err := GetCurrent(context.Background(), browser)
if err != nil {
t.Fatalf("GetCurrent() error: %v", err)
}
if drawing.PowerBall != 10 {
t.Errorf("PowerBall = %d, want 10", drawing.PowerBall)
}
if nd.JackpotDollars != 1500000000 {
t.Errorf("JackpotDollars = %d, want 1500000000", nd.JackpotDollars)
}
}
func TestGetDrawing_MissingDate(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{},
},
}
_, err := getDrawing(context.Background(), doc)
if err == nil {
t.Error("expected error for missing date element")
}
}
func TestGetDrawing_WrongBallCount(t *testing.T) {
doc := &extractortest.MockDocument{
MockNode: extractortest.MockNode{
Children: map[string]extractor.Nodes{
"#numbers .title-date": {
&extractortest.MockNode{TextValue: "Sat, Feb 15, 2026"},
},
"div.game-ball-group div.white-balls": {
&extractortest.MockNode{TextValue: "7"},
&extractortest.MockNode{TextValue: "14"},
},
},
},
}
_, err := getDrawing(context.Background(), doc)
if err == nil {
t.Error("expected error for wrong number of white balls")
}
}