feat(scraper): bound article enrichment diagnostics (#40)

This commit is contained in:
2026-11-25 00:07:45 +01:00
parent a3c70670f6
commit 6883b3976b
53 changed files with 4049 additions and 68 deletions

View File

@ -0,0 +1,116 @@
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 = "<pubDate>Fri, 02 Jan 2026 03:04:05 +0000</pubDate>"
}
body = `<rss><channel><item><title>Synthetic fixture headline</title><link>` + articleURL + `</link><description><![CDATA[<p>Synthetic RSS teaser only.</p>]]></description>` + pubDate + `</item></channel></rss>`
} 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 := `<link rel="canonical" href="` + cnbcFixtureURL + `"><meta property="article:published_time" content="2026-01-02T03:04:05Z"><div data-module="ArticleBody"><div class="group"><h2>Synthetic heading.</h2><p>Synthetic initial paragraph.</p><p>Final editorial paragraph.</p></div></div>`
partial := raw + `<div data-content-state="partial"></div>`
challenge := raw + `<div id="px-captcha">Synthetic challenge marker</div>`
wrongArticle := strings.ReplaceAll(raw, cnbcFixtureURL, "https://www.cnbc.com/2026/01/02/other.html")
noDate := strings.Replace(raw, `<meta property="article:published_time" content="2026-01-02T03:04:05Z">`, "", 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 := `<link rel="canonical" href="` + articleURL + `"><div data-module="ArticleBody"><h2>Synthetic heading.</h2><p>Final editorial paragraph.</p></div>`
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")
}
}