fix(scraper): verify complete article evidence (#40)

This commit is contained in:
2026-11-24 19:24:29 +01:00
parent 70ffe41cfb
commit a55dc2e189
27 changed files with 833 additions and 132 deletions

View File

@ -0,0 +1,44 @@
package articlecontent
import (
"github.com/tradarr/backend/internal/scraper"
"os"
"strings"
"testing"
)
func TestTicket40AssociationAndIncompleteBodies(t *testing.T) {
for _, tc := range []struct{ source, url, root string }{{"yahoo_finance", "https://finance.yahoo.com/news/fixture.html", "caas-body"}, {"cnbc_business", "https://www.cnbc.com/2026/01/02/fixture.html", "ArticleBody-articleBody"}} {
t.Run(tc.source, func(t *testing.T) {
full := `<link rel="canonical" href="` + tc.url + `"><div class="` + tc.root + `"><p>Synthetic editorial introduction.</p><p>Synthetic final editorial paragraph.</p></div>`
for _, marker := range []string{`<div class="Placeholder_content"></div>`, `<div id="px-captcha"></div>`, `<div class="paywall"></div>`, `<div data-content-state="partial"></div>`} {
a := ExtractHTML(tc.source, tc.url, []byte(strings.Replace(full, "</div>", marker+"</div>", 1)))
if a.ContentState == scraper.ContentComplete {
t.Fatal("blocked or partial page marked complete")
}
}
wrong := ExtractHTML(tc.source, tc.url, []byte(strings.Replace(full, tc.url, tc.url+"-other", 1)))
if wrong.ContentState != scraper.ContentUnavailable || wrong.Content != "" || wrong.PublishedAt != nil {
t.Fatal("wrong article accepted")
}
missing := ExtractHTML(tc.source, tc.url, []byte(`<div class="`+tc.root+`"><p>Unassociated body</p></div>`))
if missing.ContentState == scraper.ContentComplete {
t.Fatal("unassociated body marked complete")
}
})
}
}
func TestTicket40SourceSpecificLayoutsPreserveFullEditorialText(t *testing.T) {
for _, tc := range []struct{ source, url, fixture string }{{"yahoo_finance", "https://finance.yahoo.com/news/fixture.html", "yahoo_modern.html"}, {"cnbc_business", "https://www.cnbc.com/2026/01/02/fixture.html", "cnbc_module.html"}} {
t.Run(tc.source, func(t *testing.T) {
raw, err := os.ReadFile("testdata/" + tc.fixture)
if err != nil {
t.Fatal(err)
}
a := ExtractHTML(tc.source, tc.url, raw)
if a.ContentState != scraper.ContentComplete || a.PublishedAt == nil || !strings.Contains(a.Content, "Final editorial paragraph.") || !strings.Contains(a.Content, "Synthetic heading.") || strings.Contains(a.Content, "excluded") {
t.Fatalf("unrecognized or incomplete fixture extraction: state=%s", a.ContentState)
}
})
}
}