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,49 @@
package articlecontent
import (
"fmt"
"strings"
"testing"
"github.com/tradarr/backend/internal/scraper"
)
func TestCandidateLengthUnicodeBucketsAndExcludedText(t *testing.T) {
for _, source := range []string{"yahoo_finance", "cnbc_business"} {
pages := map[string]string{}
root, base := "caas-body", "https://finance.yahoo.com/news/fixture-"
if source == "cnbc_business" {
root = "ArticleBody-articleBody"
base = "https://www.cnbc.com/2026/02/01/fixture-"
}
for i, n := range []int{0, 1, 500, 501, 2000, 2001} {
target := fmt.Sprintf("%s%d.html", base, i)
pages[target] = `<link rel="canonical" href="` + base + `other.html"><div class="` + root + `"><p>` + strings.Repeat("界", n) + `</p><nav>Excluded navigation</nav><div hidden>Excluded hidden</div><script>Excluded script</script><div class="skeleton">Excluded skeleton</div><div id="article-placeholder">Excluded placeholder</div></div><p>Excluded outside text</p>`
}
m := fetchMetricsForFixturePages(t, source, pages)
if !m.CandidateBodySupported || m.CandidateLength != (scraper.LengthCounts{Zero: 1, Small: 2, Medium: 2, Large: 1}) || m.Length.Zero != 6 || m.Content.Unavailable != 6 {
t.Fatal("candidate Unicode/exclusion buckets changed article results")
}
}
}
func TestCandidateLengthNormalizesEditorialWhitespaceWithoutRetainingContent(t *testing.T) {
const target = "https://finance.yahoo.com/news/fixture.html"
pages := map[string]string{target: `<link rel="canonical" href="` + target + `"><div class="caas-body"><h2> Synthetic heading </h2><p>A <em> Unicode 界 </em> body<br> text.</p><aside>Excluded sidebar</aside></div>`}
m := fetchMetricsForFixturePages(t, "yahoo_finance", pages)
if m.CandidateLength.Small != 1 || m.Length.Small != 1 || m.Content.Complete != 1 {
t.Fatal("candidate length changed valid extraction")
}
}
func TestCandidateLengthDoesNotInventAnEditorialRoot(t *testing.T) {
const target = "https://finance.yahoo.com/news/fixture.html"
for _, raw := range []string{
`<main><p>Synthetic generic page text.</p></main>`,
`<div class="caas-body"><p>First ambiguous root.</p></div><div class="caas-body"><p>Second ambiguous root.</p></div>`,
`<aside><div class="caas-body"><p>Excluded root.</p></div></aside>`,
} {
m := fetchMetricsForFixturePages(t, "yahoo_finance", map[string]string{target: raw})
if m.CandidateLength.Zero != 1 || m.Content.Unavailable != 1 {
t.Fatal("unknown/ambiguous/excluded root was measured as editorial body")
}
}
}