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{ `

Synthetic editorial text.

`, `

Synthetic editorial text.

`, `

Synthetic editorial text.

`, ``, `

Synthetic editorial text.

`, } { 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(`` + 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 := `

Synthetic body.

` 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 := `

Synthetic body.

` 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 := `` if kind == "og" { tag = `` } m := fetchMetricsForFixturePages(t, "yahoo_finance", map[string]string{target: tag + `

Synthetic editorial text.

`}) 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 := `

Synthetic editorial text.

` 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"}) }