Files
TradarrV2/backend/internal/scraper/articlecontent/enrichment_test.go

76 lines
3.9 KiB
Go

package articlecontent
import (
"context"
"github.com/tradarr/backend/internal/scraper"
"io"
"net/http"
"os"
"strings"
"testing"
)
type roundTrip func(*http.Request) (*http.Response, error)
func (f roundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestTicket39SourceFixtures(t *testing.T) {
for _, source := range []string{"yahoo_finance", "cnbc_business"} {
t.Run(source, func(t *testing.T) {
raw, err := os.ReadFile("testdata/" + source + ".html")
if err != nil {
t.Fatal(err)
}
a := ExtractHTML(source, "https://fixture.invalid/article", raw)
if a.ContentState != scraper.ContentComplete || !strings.Contains(a.Content, "Final editorial paragraph.") || strings.Contains(a.Content, "excluded") || a.PublishedAt == nil {
t.Fatalf("bad full content: state=%s date=%v", a.ContentState, a.PublishedAt)
}
a = ExtractHTML(source, "https://fixture.invalid/article", append(raw, []byte(`<div class="paywall">subscribe</div>`)...))
if a.ContentState != scraper.ContentIncomplete {
t.Fatal("paywall claimed complete")
}
})
}
}
func TestTicket39FetchRestrictionsAndFullBody(t *testing.T) {
calls := 0
body := "<div class=caas-body><p>" + strings.Repeat("Full editorial paragraph. ", 2000) + "</p></div>"
client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) {
calls++
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
})}
in := []scraper.Article{{Title: "original", URL: "https://finance.yahoo.com/news/fixture.html"}, {Title: "blocked", URL: "http://127.0.0.1/secret"}}
got, err := Enrich(context.Background(), client, "yahoo_finance", in)
if err != nil || calls != 1 || got[0].ContentState != scraper.ContentComplete || len(got[0].Content) < 30000 || got[0].Title != "original" || got[1].ContentState != scraper.ContentUnavailable {
t.Fatal("full extraction/restriction failed")
}
}
func TestTicket39UnrecognizedAndForeignMetadataDoNotInventContentOrDate(t *testing.T) {
for _, source := range []string{"yahoo_finance", "cnbc_business"} {
a := ExtractHTML(source, "https://fixture.invalid/current", []byte(`<html><body><main><p>Generic consent or navigation text.</p></main><script type="application/ld+json">{"@type":"NewsArticle","url":"https://fixture.invalid/other","articleBody":"not this article","datePublished":"2026-01-02T03:04:05Z"}</script></body></html>`))
if a.Content != "" || a.ContentState != scraper.ContentUnavailable || a.PublishedAt != nil {
t.Fatal("unverified content/date accepted")
}
}
}
func TestTicket39RedirectCannotLeaveSource(t *testing.T) {
calls := 0
client := &http.Client{Transport: roundTrip(func(*http.Request) (*http.Response, error) {
calls++
return &http.Response{StatusCode: 302, Body: io.NopCloser(strings.NewReader("PRIVATE_BODY")), Header: http.Header{"Location": []string{"http://127.0.0.1/private"}}}, nil
})}
got, err := Enrich(context.Background(), client, "yahoo_finance", []scraper.Article{{Title: "Fixture", URL: "https://finance.yahoo.com/news/fixture.html"}})
if err != nil || calls != 1 || got[0].ContentState != scraper.ContentUnavailable || got[0].Content != "" {
t.Fatal("redirect followed or private data returned")
}
}
func TestTicket39EditorialHeadingsListsAndQuotesAreNotDropped(t *testing.T) {
for _, tc := range []struct{ source, root string }{{"yahoo_finance", "caas-body"}, {"cnbc_business", "ArticleBody-articleBody"}} {
a := ExtractHTML(tc.source, "https://fixture.invalid/current", []byte(`<div class="`+tc.root+`"><h2>Editorial heading</h2><p>First paragraph.</p><ul><li>Editorial list item</li></ul><blockquote>Editorial quotation</blockquote></div>`))
for _, text := range []string{"Editorial heading", "First paragraph.", "Editorial list item", "Editorial quotation"} {
if !strings.Contains(a.Content, text) {
t.Fatal("editorial text omitted")
}
}
}
}