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, fixtureArticleURL(source), 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, fixtureArticleURL(source), append(raw, []byte(`
subscribe
`)...)) if a.ContentState != scraper.ContentIncomplete { t.Fatal("paywall claimed complete") } }) } } func TestTicket39FetchRestrictionsAndFullBody(t *testing.T) { calls := 0 body := `` + "

" + strings.Repeat("Full editorial paragraph. ", 2000) + "

" 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(`

Generic consent or navigation text.

`)) 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(`

Editorial heading

First paragraph.

Editorial quotation
`)) for _, text := range []string{"Editorial heading", "First paragraph.", "Editorial list item", "Editorial quotation"} { if !strings.Contains(a.Content, text) { t.Fatal("editorial text omitted") } } } } func fixtureArticleURL(source string) string { if source == "yahoo_finance" { return "https://finance.yahoo.com/news/fixture.html" } return "https://www.cnbc.com/2026/01/02/fixture.html" }