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

83 lines
3.2 KiB
Go

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 = `<link rel="canonical" href="` + tc.identity + `">`
if metadata == "NewsArticle" {
body = `<script type="application/ld+json">{"@type":"NewsArticle","url":"` + tc.identity + `"}</script>`
}
body += `<article><div class="caas-body"><p>Synthetic full editorial body.</p></div></article>`
}
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 := `<div data-module="ArticleBody"><p>Synthetic complete editorial body.</p></div>`
for _, tc := range []struct {
name, html string
state string
}{
{"detached_newsletter", root + `<aside class="newsletter-paywall"></aside>`, scraper.ContentComplete},
{"article_sibling_paywall", `<article>` + root + `<div class="paywall"></div></article>`, scraper.ContentIncomplete},
{"wrapper_partial", `<article data-content-truncated="true">` + root + `</article>`, scraper.ContentIncomplete},
{"global_challenge", root + `<div id="challenge-page"></div>`, scraper.ContentIncomplete},
{"replacement_challenge", `<div id="px-captcha"></div>`, scraper.ContentUnavailable},
} {
t.Run(tc.name, func(t *testing.T) {
got := ExtractHTML("cnbc_business", url, []byte(`<link rel="canonical" href="`+url+`">`+tc.html))
if got.ContentState != tc.state {
t.Fatal("editorial/challenge scope contract failed")
}
})
}
}