package articlecontent import ( "context" "github.com/tradarr/backend/internal/scraper" "io" "net/http" "strings" "testing" ) func TestObservedYahooRedirectIdentities(t *testing.T) { initial := "https://finance.yahoo.com/news/fixture-initial.html" final := "https://finance.yahoo.com/news/fixture-final.html" for _, tc := range []struct { name, destination, identity string complete bool }{ {"canonical_initial", final, initial, true}, {"canonical_final", final, final, true}, {"unobserved", final, "https://finance.yahoo.com/news/fixture-other.html", false}, {"third_party_identity", final, "https://example.invalid/fixture.html", false}, {"external_redirect", "https://example.invalid/fixture.html", initial, false}, {"upgrade_before_fetch", "http://finance.yahoo.com/news/fixture-final.html", initial, true}, } { for _, metadata := range []string{"canonical", "NewsArticle"} { t.Run(tc.name+metadata, func(t *testing.T) { calls := 0 client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { calls++ h := make(http.Header) status := 200 body := "" if calls == 1 { status = 302 h.Set("Location", tc.destination) } else { if r.URL.String() != final { t.Fatal("untrusted redirect fetched") } body = `` if metadata == "NewsArticle" { body = `` } body += `

Synthetic full editorial body.

` } return &http.Response{StatusCode: status, Header: h, Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil })} got, err := Enrich(context.Background(), client, "yahoo_finance", []scraper.Article{{URL: initial}}) if err != nil || (got[0].ContentState == scraper.ContentComplete) != tc.complete || got[0].URL != initial { t.Fatal("redirect identity contract failed") } if strings.Contains(tc.name, "redirect") || tc.name == "downgrade" { if calls != 1 { t.Fatal("forbidden redirect fetched") } } }) } } } func TestCNBCEditorialMarkerScopeAndGlobalChallenges(t *testing.T) { url := "https://www.cnbc.com/2026/01/02/fixture.html" root := `

Synthetic complete editorial body.

` for _, tc := range []struct { name, html string state string }{ {"detached_newsletter", root + ``, scraper.ContentComplete}, {"article_sibling_paywall", `
` + root + `
`, scraper.ContentIncomplete}, {"wrapper_partial", `
` + root + `
`, scraper.ContentIncomplete}, {"global_challenge", root + `
`, scraper.ContentIncomplete}, {"replacement_challenge", `
`, scraper.ContentUnavailable}, } { t.Run(tc.name, func(t *testing.T) { got := ExtractHTML("cnbc_business", url, []byte(``+tc.html)) if got.ContentState != tc.state { t.Fatal("editorial/challenge scope contract failed") } }) } }