feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
96
backend/internal/scraper/articlecontent/diagnostics_test.go
Normal file
96
backend/internal/scraper/articlecontent/diagnostics_test.go
Normal file
@ -0,0 +1,96 @@
|
||||
package articlecontent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
)
|
||||
|
||||
func TestGranularInstrumentationDoesNotChangeArticleDecisions(t *testing.T) {
|
||||
const target = "https://www.cnbc.com/2026/02/01/fixture.html"
|
||||
for _, part := range []string{
|
||||
`<div data-module="ArticleBody"><p>Synthetic editorial text.</p></div><aside class="paywall"></aside>`,
|
||||
`<article><div data-module="ArticleBody"><p>Synthetic editorial text.</p></div><div class="paywall"></div></article>`,
|
||||
`<div data-module="ArticleBody"><p>Synthetic editorial text.</p><div data-content-state="partial"></div></div>`,
|
||||
`<aside id="challengeGate"></aside>`,
|
||||
`<script type="application/ld+json">{"@type":"NewsArticle","url":"` + target + `","isAccessibleForFree":false,"articleBody":"conflicting synthetic metadata"}</script><div data-module="ArticleBody"><p>Synthetic editorial text.</p></div>`,
|
||||
} {
|
||||
client := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Header: make(http.Header), Request: r, Body: io.NopCloser(strings.NewReader(`<link rel="canonical" href="` + target + `">` + part))}, nil
|
||||
})}
|
||||
plain, _ := Enrich(context.Background(), client, "cnbc_business", []scraper.Article{{URL: target}})
|
||||
ctx, meter := scraper.WithFetchMetrics(context.Background())
|
||||
measured, _ := Enrich(ctx, client, "cnbc_business", []scraper.Article{{URL: target}})
|
||||
if !reflect.DeepEqual(plain, measured) || !meter.Finish().GranularSupported {
|
||||
t.Fatal("diagnostics altered article result")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestGranularEvidenceIsDeduplicatedPerFetchAndUnextractedBodiesAreNotConflicts(t *testing.T) {
|
||||
const target = "https://finance.yahoo.com/news/fixture.html"
|
||||
duplicate := `<link rel="canonical" href="` + target + `"><link rel="canonical" href="` + target + `"><div class="caas-body"><p>Synthetic body.</p><div class="paywall"></div><div class="paywall"></div></div>`
|
||||
m := fetchMetricsForFixturePages(t, "yahoo_finance", map[string]string{target: duplicate})
|
||||
if m.IdentityEvidence.Canonical.Associated != 1 || m.BlockingEvidence.Paywall.ArticleRoot != 1 {
|
||||
t.Fatal("duplicate evidence inflated counters")
|
||||
}
|
||||
// Rejected association returns before extraction: do not invent a conflict
|
||||
// by comparing metadata with an unextracted empty content string.
|
||||
mismatch := `<link rel="canonical" href="https://finance.yahoo.com/news/other.html"><script type="application/ld+json">{"@type":"NewsArticle","url":"` + target + `","articleBody":"Synthetic body."}</script><div class="caas-body"><p>Synthetic body.</p></div>`
|
||||
m = fetchMetricsForFixturePages(t, "yahoo_finance", map[string]string{target: mismatch})
|
||||
if m.BlockingEvidence.BodyConflict.OutsideRoot != 0 || m.IdentityEvidence.JSONLD.Associated != 1 {
|
||||
t.Fatal("unextracted body conflict was invented")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataDiagnosticValidityAndAbsenceAreIndependentOfParserTrust(t *testing.T) {
|
||||
const target = "https://finance.yahoo.com/news/fixture.html"
|
||||
for _, kind := range []string{"canonical", "og"} {
|
||||
for _, tc := range []struct {
|
||||
name, value string
|
||||
want scraper.MetadataIdentityCounts
|
||||
}{
|
||||
{"associated", target, scraper.MetadataIdentityCounts{Associated: 1}},
|
||||
{"relative_associated", "/news/fixture.html", scraper.MetadataIdentityCounts{Associated: 1}},
|
||||
{"same_source_other", "/news/other.html", scraper.MetadataIdentityCounts{SameSourceDifferentIdentity: 1}},
|
||||
{"valid_same_host_port", "https://finance.yahoo.com:444/news/fixture.html", scraper.MetadataIdentityCounts{SameSourceDifferentIdentity: 1}},
|
||||
{"valid_other_host", "https://example.invalid/fixture", scraper.MetadataIdentityCounts{ValidOtherHost: 1}},
|
||||
{"invalid_syntax", "http://[invalid", scraper.MetadataIdentityCounts{Invalid: 1}},
|
||||
{"invalid_scheme", "javascript:fixture", scraper.MetadataIdentityCounts{Invalid: 1}},
|
||||
{"invalid_port", "https://example.invalid:99999/fixture", scraper.MetadataIdentityCounts{Invalid: 1}},
|
||||
{"invalid_credentials", "https://private:canary@example.invalid/fixture", scraper.MetadataIdentityCounts{Invalid: 1}},
|
||||
{"empty", "", scraper.MetadataIdentityCounts{Absent: 1}},
|
||||
} {
|
||||
t.Run(kind+"/"+tc.name, func(t *testing.T) {
|
||||
tag := `<link rel="canonical" href="` + tc.value + `">`
|
||||
if kind == "og" {
|
||||
tag = `<meta property="og:url" content="` + tc.value + `">`
|
||||
}
|
||||
m := fetchMetricsForFixturePages(t, "yahoo_finance", map[string]string{target: tag + `<div class="caas-body"><p>Synthetic editorial text.</p></div>`})
|
||||
got, other := m.IdentityEvidence.Canonical, m.IdentityEvidence.OG
|
||||
if kind == "og" {
|
||||
got, other = other, got
|
||||
}
|
||||
if got != tc.want || other.Absent != 1 {
|
||||
t.Fatal("metadata validity/absence category contract failed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChallengeIsOneGlobalPageSignalEvenAcrossBothScopes(t *testing.T) {
|
||||
const target = "https://www.cnbc.com/2026/02/01/fixture.html"
|
||||
raw := `<link rel="canonical" href="` + target + `"><aside id="challengeGate"></aside><div data-module="ArticleBody"><p>Synthetic editorial text.</p><div id="challengeGate"></div></div>`
|
||||
m := fetchMetricsForFixturePages(t, "cnbc_business", map[string]string{target: raw})
|
||||
if m.BlockingEvidence.Challenge.GlobalPage != 1 || m.Content.Incomplete != 1 {
|
||||
t.Fatal("global challenge semantics changed")
|
||||
}
|
||||
data := requireAnonymousAggregate(t, m)
|
||||
requireNoAggregateCounter(t, data, []string{"blocking_evidence", "challenge", "article_root"})
|
||||
requireNoAggregateCounter(t, data, []string{"blocking_evidence", "challenge", "outside_root"})
|
||||
}
|
||||
Reference in New Issue
Block a user