feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
106
backend/internal/scraper/fetch_metrics_test.go
Normal file
106
backend/internal/scraper/fetch_metrics_test.go
Normal file
@ -0,0 +1,106 @@
|
||||
package scraper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFetchMetricsContainOnlyAggregatesAndKnownBuckets(t *testing.T) {
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
EnableFetchMetrics(ctx)
|
||||
RecordFetch(ctx, FetchObservation{HTTPStatus: 200, Root: ObservedYes, Identity: IdentityYes, Blocking: ObservedNo, Length: 501, State: ContentComplete})
|
||||
RecordFetch(ctx, FetchObservation{Network: true, State: ContentUnavailable})
|
||||
got := m.Finish()
|
||||
if !got.Supported || got.Attempted != 2 || got.HTTP.TwoXX != 1 || got.HTTP.Network != 1 || got.Root.NotEvaluated != 1 || got.Length.Medium != 1 || got.Length.Zero != 1 {
|
||||
t.Fatal("invalid aggregation")
|
||||
}
|
||||
b, _ := json.Marshal(got)
|
||||
for _, s := range []string{"url", "title", `"body"`, "cookie", "secret", "error"} {
|
||||
if strings.Contains(string(b), s) {
|
||||
t.Fatal("unsafe diagnostic field")
|
||||
}
|
||||
}
|
||||
RecordFetch(ctx, FetchObservation{HTTPStatus: 403})
|
||||
if m.Finish().Attempted != 2 {
|
||||
t.Fatal("late observation changed sealed snapshot")
|
||||
}
|
||||
}
|
||||
func TestRPCMetricsAreBoundedAndInconsistentAggregatesRemainUnsupported(t *testing.T) {
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
EnableFetchMetrics(ctx)
|
||||
RecordFetch(ctx, FetchObservation{HTTPStatus: 403, State: ContentUnavailable})
|
||||
valid := m.Finish()
|
||||
for _, mutate := range []func(*FetchMetrics){func(c *FetchMetrics) { c.Attempted = 5 }, func(c *FetchMetrics) { c.Root.Yes++ }, func(c *FetchMetrics) { c.HTTP.FourXX = -1 }, func(c *FetchMetrics) { c.SchemaVersion = 99 }} {
|
||||
c := valid
|
||||
mutate(&c)
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
if MergeFetchMetrics(ctx, c, 3) || m.Finish().Supported {
|
||||
t.Fatal("invalid RPC metrics accepted")
|
||||
}
|
||||
}
|
||||
ctx, m = WithFetchMetrics(context.Background())
|
||||
if !MergeFetchMetrics(ctx, valid, 3) || m.Finish().HTTP.FourXX != 1 {
|
||||
t.Fatal("valid aggregate rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGranularRPCBoundsAndLoginAllowlist(t *testing.T) {
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
RecordFetch(ctx, FetchObservation{HTTPStatus: 200, State: ContentComplete, GranularSupported: true, IdentityEvidence: IdentityEvidenceCounts{JSONLD: JSONLDIdentityCounts{Associated: 1}}, BlockingEvidence: BlockingEvidenceCounts{Paywall: ScopedEvidenceCounts{OutsideRoot: 1}}})
|
||||
valid := m.Finish()
|
||||
valid.Login = "unconfirmed"
|
||||
for _, mutate := range []func(*FetchMetrics){
|
||||
func(c *FetchMetrics) { c.IdentityEvidence.JSONLD.Associated = -1 },
|
||||
func(c *FetchMetrics) { c.IdentityEvidence.Canonical.Invalid = -1 },
|
||||
func(c *FetchMetrics) { c.IdentityEvidence.OG.Absent = 2 },
|
||||
func(c *FetchMetrics) { c.BlockingEvidence.Challenge.GlobalPage = 2 },
|
||||
func(c *FetchMetrics) { c.BlockingEvidence.Paywall.OutsideRoot = 2 },
|
||||
func(c *FetchMetrics) { c.GranularSupported = false },
|
||||
func(c *FetchMetrics) { c.Login = "PRIVATE_RAW_REASON" },
|
||||
} {
|
||||
bad := valid
|
||||
mutate(&bad)
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
if MergeFetchMetrics(ctx, bad, 3) || m.Finish().Supported {
|
||||
t.Fatal("untrusted granular aggregate accepted")
|
||||
}
|
||||
}
|
||||
ctx, m = WithFetchMetrics(context.Background())
|
||||
if !MergeFetchMetrics(ctx, valid, 3) {
|
||||
t.Fatal("valid granular metrics rejected")
|
||||
}
|
||||
got := m.Finish()
|
||||
if got.IdentityEvidence.JSONLD.Associated != 1 || got.BlockingEvidence.Paywall.OutsideRoot != 1 || got.Login != "unconfirmed" {
|
||||
t.Fatal("aggregate lost evidence")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateLengthRPCValidationAndOldProducerCompatibility(t *testing.T) {
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
RecordFetch(ctx, FetchObservation{HTTPStatus: 200, CandidateBodySupported: true, CandidateBodyLength: 2001, State: ContentUnavailable})
|
||||
valid := m.Finish()
|
||||
for _, mutate := range []func(*FetchMetrics){
|
||||
func(c *FetchMetrics) { c.CandidateLength.Large = -1 },
|
||||
func(c *FetchMetrics) { c.CandidateLength.Small = 1 },
|
||||
func(c *FetchMetrics) { c.CandidateBodySupported = false },
|
||||
} {
|
||||
bad := valid
|
||||
mutate(&bad)
|
||||
ctx, m := WithFetchMetrics(context.Background())
|
||||
if MergeFetchMetrics(ctx, bad, 3) || m.Finish().Supported {
|
||||
t.Fatal("invalid candidate aggregate accepted")
|
||||
}
|
||||
}
|
||||
ctx, m = WithFetchMetrics(context.Background())
|
||||
if !MergeFetchMetrics(ctx, valid, 3) || m.Finish().CandidateLength.Large != 1 {
|
||||
t.Fatal("candidate aggregate lost at RPC boundary")
|
||||
}
|
||||
valid.CandidateBodySupported = false
|
||||
valid.CandidateLength = LengthCounts{}
|
||||
ctx, m = WithFetchMetrics(context.Background())
|
||||
if !MergeFetchMetrics(ctx, valid, 3) || m.Finish().CandidateBodySupported {
|
||||
t.Fatal("old producer was confused with measured empty bodies")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user