fix(scraper): verify complete article evidence (#40)

This commit is contained in:
2026-11-24 19:24:29 +01:00
parent 70ffe41cfb
commit a55dc2e189
27 changed files with 833 additions and 132 deletions

View File

@ -46,8 +46,8 @@ func TestTicket20Yahoo400DiagnosticConnectsSymbolsStatusAndCountWithoutResponseB
var articles int
logs := ticket20CaptureStdout(t, func() {
got, err := yahoo.Scrape(context.Background(), []string{"SSAAPL INDEX"})
if err != nil {
t.Fatalf("Scrape returned error: %v", err)
if err == nil {
t.Fatal("failed RSS must be a source failure, not a reachable empty source")
}
articles = len(got)
})

View File

@ -0,0 +1,29 @@
package yahoofinance
import (
"context"
"github.com/tradarr/backend/internal/scraper"
"io"
"net/http"
"strings"
"testing"
)
func TestTicket40FailedRSSIsNotReportedAsReachableEmptySource(t *testing.T) {
y := &YahooFinance{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 503, Body: io.NopCloser(strings.NewReader("PRIVATE unavailable")), Header: make(http.Header), Request: r}, nil
})}}
a, err := y.Scrape(context.Background(), []string{"FIX"})
if err == nil || len(a) != 0 || strings.Contains(err.Error(), "PRIVATE") {
t.Fatal("failed RSS must produce a sanitized source failure")
}
}
func TestTicket40RSSDescriptionNeverClaimsComplete(t *testing.T) {
y := &YahooFinance{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`<rss><channel><item><title>Fixture</title><link>https://finance.yahoo.com/news/fixture.html</link><description>Synthetic RSS teaser.</description></item></channel></rss>`)), Header: make(http.Header), Request: r}, nil
})}}
a, err := y.Scrape(context.Background(), []string{"FIX"})
if err != nil || len(a) != 1 || scraper.ContentStatus(a[0]) == scraper.ContentComplete {
t.Fatal("RSS description claimed full article")
}
}

View File

@ -3,6 +3,7 @@ package yahoofinance
import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"strings"
@ -37,6 +38,7 @@ type rssFeed struct {
func (y *YahooFinance) Scrape(ctx context.Context, symbols []string) ([]scraper.Article, error) {
var articles []scraper.Article
reachable := false
for i, symbol := range symbols {
if i > 0 {
@ -50,8 +52,12 @@ func (y *YahooFinance) Scrape(ctx context.Context, symbols []string) ([]scraper.
if err != nil {
continue
}
reachable = true
articles = append(articles, items...)
}
if len(symbols) > 0 && !reachable {
return nil, errors.New("yahoofinance: source unavailable")
}
return articles, nil
}