package cnbcbusiness import ( "context" "fmt" "io" "net/http" "strings" "testing" "time" "github.com/tradarr/backend/internal/scraper" ) const cnbcFixtureURL = "https://www.cnbc.com/2026/01/02/fixture.html" type cnbcIngestionTransport struct { t *testing.T html string articleURL string date bool rssOnly bool } func (f cnbcIngestionTransport) RoundTrip(r *http.Request) (*http.Response, error) { articleURL := f.articleURL if articleURL == "" { articleURL = cnbcFixtureURL } var body string if r.URL.String() == feedURL { pubDate := "" if f.date { pubDate = "Fri, 02 Jan 2026 03:04:05 +0000" } body = `Synthetic fixture headline` + articleURL + `Synthetic RSS teaser only.

]]>
` + pubDate + `
` } else if r.URL.String() == articleURL { if f.rssOnly { return &http.Response{StatusCode: 503, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("fixture unavailable")), Request: r}, nil } body = f.html } else { f.t.Errorf("collector/enricher requested an unexpected fixture route") return nil, fmt.Errorf("unexpected fixture route") } return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil } func TestIngestionPipelineCNBCRSSListThenArticleEnrichment(t *testing.T) { raw := `

Synthetic heading.

Synthetic initial paragraph.

Final editorial paragraph.

` partial := raw + `
` challenge := raw + `
Synthetic challenge marker
` wrongArticle := strings.ReplaceAll(raw, cnbcFixtureURL, "https://www.cnbc.com/2026/01/02/other.html") noDate := strings.Replace(raw, ``, "", 1) for _, tc := range []struct { name, html string date, rssOnly, wantComplete, wantDate bool }{ {name: "complete body and reliable date", html: raw, date: true, wantComplete: true, wantDate: true}, {name: "RSS description alone", html: "", rssOnly: true}, {name: "partial body", html: partial, date: true, wantDate: true}, {name: "challenge", html: challenge, date: true, wantDate: true}, {name: "complete body with nullable date", html: noDate, wantComplete: true}, {name: "wrong article association", html: wrongArticle, date: true, wantDate: true}, } { t.Run(tc.name, func(t *testing.T) { c := &CNBCBusiness{client: &http.Client{Transport: cnbcIngestionTransport{t: t, html: tc.html, date: tc.date, rssOnly: tc.rssOnly}}} listed, err := c.Scrape(context.Background(), nil) if err != nil || len(listed) != 1 { t.Fatal("CNBC fixture collector did not return the single listed article") } if listed[0].URL != cnbcFixtureURL || listed[0].Content != "Synthetic RSS teaser only." || scraper.ContentStatus(listed[0]) == scraper.ContentComplete { t.Fatal("RSS teaser was treated as the listed article body") } got, err := c.Enrich(context.Background(), listed) if err != nil || len(got) != 1 { t.Fatal("CNBC fixture article enrichment failed") } if got[0].URL != listed[0].URL || got[0].Title != listed[0].Title { t.Fatal("CNBC enrichment changed or detached the listed article") } if (scraper.ContentStatus(got[0]) == scraper.ContentComplete) != tc.wantComplete { t.Fatal("CNBC full-body classification mismatch") } if tc.wantComplete && !strings.Contains(got[0].Content, "Final editorial paragraph.") { t.Fatal("CNBC final editorial text was not collected") } if (got[0].PublishedAt != nil) != tc.wantDate { t.Fatal("CNBC publication-date availability mismatch") } if tc.wantDate && !got[0].PublishedAt.Equal(time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)) { t.Fatal("CNBC did not associate the expected publication date") } if tc.name == "wrong article association" && (got[0].Content != "" || scraper.ContentStatus(got[0]) != scraper.ContentUnavailable) { t.Fatal("CNBC accepted body content for another article") } }) } } func TestCNBCAcceptedRSSLinkPathIsNotDroppedByEnricher(t *testing.T) { // The RSS collector accepts this same-source .html link, but the enrichment // port currently permits only date-shaped paths. articleURL := "https://www.cnbc.com/business/synthetic-story.html" html := `

Synthetic heading.

Final editorial paragraph.

` c := &CNBCBusiness{client: &http.Client{Transport: cnbcIngestionTransport{t: t, html: html, articleURL: articleURL}}} listed, err := c.Scrape(context.Background(), nil) if err != nil || len(listed) != 1 { t.Fatal("CNBC collector dropped an accepted RSS article") } got, err := c.Enrich(context.Background(), listed) if err != nil || len(got) != 1 || scraper.ContentStatus(got[0]) != scraper.ContentComplete || !strings.Contains(got[0].Content, "Final editorial paragraph.") { t.Fatal("CNBC enricher discarded a same-source RSS article URL with available full content") } }