package articlecontent import ( "context" "github.com/tradarr/backend/internal/scraper" "io" "net/http" "strings" "testing" ) func TestAcceptedListingURLsUseHTTPSAndKeepListingIdentity(t *testing.T) { for _, tc := range []struct{ source, listing, fetch string }{{"yahoo_finance", "http://finance.yahoo.com/m/fixture/story?story=1#fragment", "https://finance.yahoo.com/m/fixture/story?story=1"}, {"cnbc_business", "http://www.cnbc.com/business/story?story=1#fragment", "https://www.cnbc.com/business/story?story=1"}} { t.Run(tc.source, func(t *testing.T) { calls := 0 root := "caas-body" if tc.source == "cnbc_business" { root = "ArticleBody-articleBody" } client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { calls++ if r.URL.String() != tc.fetch { t.Fatal("unexpected fetch identity") } body := `

Entire fixture editorial body.

` return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil })} got, err := Enrich(context.Background(), client, tc.source, []scraper.Article{{URL: tc.listing, Title: "listing title"}}) if err != nil || calls != 1 || len(got) != 1 || got[0].URL != tc.listing || got[0].Title != "listing title" || got[0].PublishedAt != nil || scraper.ContentStatus(got[0]) != scraper.ContentComplete { t.Fatal("accepted listing identity or nullable-date body lost") } }) } } func TestUnsafeListingURLsNeverFetchOrDisappear(t *testing.T) { for _, url := range []string{"https://127.0.0.1/private", "https://finance.yahoo.com.evil.invalid/story", "https://user:secret@finance.yahoo.com/story", "https://finance.yahoo.com:443/story", "file:///private", "https://www.cnbc.com/story"} { calls := 0 c := &http.Client{Transport: roundTrip(func(*http.Request) (*http.Response, error) { calls++; t.Fatal("unsafe fetch"); return nil, nil })} got, err := Enrich(context.Background(), c, "yahoo_finance", []scraper.Article{{URL: url, Title: "preserved"}}) if err != nil || calls != 0 || len(got) != 1 || got[0].URL != url || scraper.ContentStatus(got[0]) != scraper.ContentUnavailable { t.Fatal("unsafe listing lost or fetched") } } } func TestSameSourceRedirectRequiresObservedChainAndKeepsListing(t *testing.T) { original := "https://finance.yahoo.com/m/story" final := "https://finance.yahoo.com/news/story" calls := 0 c := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { calls++ if calls == 1 { return &http.Response{StatusCode: 302, Body: io.NopCloser(strings.NewReader("")), Header: http.Header{"Location": []string{final}}, Request: r}, nil } body := `

Entire fixture editorial body.

` return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil })} got, err := Enrich(context.Background(), c, "yahoo_finance", []scraper.Article{{URL: original, Title: "preserved"}}) if err != nil || calls != 2 || len(got) != 1 || got[0].URL != original || scraper.ContentStatus(got[0]) != scraper.ContentComplete { t.Fatal("verified same-source redirect lost listing association") } } func TestQueryIdentitiesAndUnobservedRedirectsCannotCrossAssociateBodies(t *testing.T) { first := "https://finance.yahoo.com/news/story?id=1" second := "https://finance.yahoo.com/news/story?id=2" calls := 0 c := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { calls++ body := `

Entire fixture body.

` return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil })} got, err := Enrich(context.Background(), c, "yahoo_finance", []scraper.Article{{URL: first}, {URL: second}}) if err != nil || calls != 2 || scraper.ContentStatus(got[0]) != scraper.ContentComplete || scraper.ContentStatus(got[1]) != scraper.ContentUnavailable { t.Fatal("semantic query identities were merged") } c.Transport = roundTrip(func(r *http.Request) (*http.Response, error) { fake, _ := http.NewRequest("GET", first, nil) body := `

Entire fixture body.

` return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: fake}, nil }) got, err = Enrich(context.Background(), c, "yahoo_finance", []scraper.Article{{URL: second}}) if err != nil || scraper.ContentStatus(got[0]) != scraper.ContentUnavailable { t.Fatal("unobserved redirect accepted") } } func TestRedirectUpgradeNeverSendsHTTPAndNullableDateDoesNotInvalidateBody(t *testing.T) { first := "https://www.cnbc.com/business/original" final := "https://www.cnbc.com/business/final" calls := 0 c := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) { calls++ if r.URL.Scheme != "https" { t.Fatal("plaintext request") } if calls == 1 { return &http.Response{StatusCode: 302, Body: io.NopCloser(strings.NewReader("")), Header: http.Header{"Location": []string{strings.Replace(final, "https://", "http://", 1)}}, Request: r}, nil } body := `

Entire fixture body.

` return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header), Request: r}, nil })} got, err := Enrich(context.Background(), c, "cnbc_business", []scraper.Article{{URL: first}}) if err != nil || calls != 2 || got[0].URL != first || got[0].PublishedAt != nil || scraper.ContentStatus(got[0]) != scraper.ContentComplete { t.Fatal("redirect or independent date contract broken") } }