feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
104
backend/internal/ingestionbench/runtime_test.go
Normal file
104
backend/internal/ingestionbench/runtime_test.go
Normal file
@ -0,0 +1,104 @@
|
||||
package ingestionbench
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLiveNeedsExplicitOptIn(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
mode string
|
||||
allow, want bool
|
||||
}{{"offline", false, true}, {"live", false, false}, {"live", true, true}, {"offline", true, false}, {"", true, false}} {
|
||||
if (ValidateMode(c.mode, c.allow) == nil) != c.want {
|
||||
t.Fatal("unexpected mode gate")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestLiveEgressAndCredentialDestinationAreRestricted(t *testing.T) {
|
||||
for _, s := range []string{"127.0.0.1", "10.0.0.1", "169.254.169.254", "::1", "fc00::1", "::ffff:127.0.0.1", "100.100.100.200", "198.18.0.1", "203.0.113.1"} {
|
||||
if publicIP(netip.MustParseAddr(s)) {
|
||||
t.Fatal("private IP accepted")
|
||||
}
|
||||
}
|
||||
for _, s := range []string{"http://evil.invalid:3001", "http://user:secret@ingestion-scraper:3001", "http://ingestion-scraper:3001/other"} {
|
||||
if _, err := TrustedScraperURL(s); err == nil {
|
||||
t.Fatal("unsafe credential destination")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestOfflineProductionCollectorsAndEnrichersMeasureNullableDates(t *testing.T) {
|
||||
// The RPC test transport verifies the Go production adapter; the actual Node
|
||||
// route is covered by the offline fixture-service test and executable smoke.
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/bloomberg/scrape" {
|
||||
io.WriteString(w, `{"articles":[]}`)
|
||||
return
|
||||
}
|
||||
t.Error("unexpected RPC")
|
||||
}))
|
||||
defer s.Close()
|
||||
rpc, err := OfflineRPCClient(s.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := Run(context.Background(), ProductionSources(&http.Client{Transport: OfflineTransport{}}, rpc, s.URL, "offline-fixture", "offline-fixture", "FIXTURE"), 3)
|
||||
if err != nil || len(rows) != 3 {
|
||||
t.Fatal("offline bench failed")
|
||||
}
|
||||
for _, row := range rows[:2] {
|
||||
if row.Listed != 3 || row.Enriched != 3 || row.Content.Complete != 1 || row.Content.Incomplete != 1 || row.Content.Unavailable != 1 || row.Dates.Missing != 1 || row.Dates.Observed != 2 {
|
||||
t.Fatalf("unexpected aggregates %+v", row)
|
||||
}
|
||||
}
|
||||
req, _ := http.NewRequest("GET", "https://external.invalid/private", strings.NewReader(""))
|
||||
if _, err := (OfflineTransport{}).RoundTrip(req); err == nil {
|
||||
t.Fatal("offline transport escaped fixture set")
|
||||
}
|
||||
}
|
||||
func TestBenchArticleMetricsAreProducedByActualOfflineFetches(t *testing.T) {
|
||||
sources := ProductionSources(&http.Client{Transport: OfflineTransport{}}, RPCClient(), LiveScraperEndpoint, "offline-fixture", "offline-fixture", "FIXTURE")
|
||||
rows, err := Run(context.Background(), sources[:2], 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
m := r.Fetch
|
||||
if !m.Supported || m.Attempted != 3 || r.NotFetched == nil || *r.NotFetched != 0 || m.HTTP.TwoXX != 3 || m.Root.Yes != 2 || m.Root.No != 1 || m.Identity.Yes != 2 || m.Identity.No != 1 || m.Blocking.Yes != 2 || m.Blocking.No != 1 || m.Length.Zero != 1 || m.Length.Small != 2 || m.Content.Complete != 1 || m.Content.Incomplete != 1 || m.Content.Unavailable != 1 {
|
||||
t.Fatalf("fetch evidence matrix invalid: %+v", m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchRetainsGranularAggregatesFromProductionEnrichers(t *testing.T) {
|
||||
sources := ProductionSources(&http.Client{Transport: OfflineTransport{}}, RPCClient(), LiveScraperEndpoint, "offline-fixture", "offline-fixture", "FIXTURE")
|
||||
rows, err := Run(context.Background(), sources[:2], 3)
|
||||
if err != nil {
|
||||
t.Fatal("offline aggregate fixture failed")
|
||||
}
|
||||
for _, row := range rows {
|
||||
m := row.Fetch
|
||||
if !m.GranularSupported || m.IdentityEvidence.Canonical.Associated != 2 || m.BlockingEvidence.Paywall.ArticleRoot != 1 || m.BlockingEvidence.Challenge.GlobalPage != 1 {
|
||||
t.Fatal("bench lost granular evidence")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchCarriesCandidateBodyLengthAggregates(t *testing.T) {
|
||||
sources := ProductionSources(&http.Client{Transport: OfflineTransport{}}, RPCClient(), LiveScraperEndpoint, "offline-fixture", "offline-fixture", "FIXTURE")
|
||||
rows, err := Run(context.Background(), sources[:2], 3)
|
||||
if err != nil {
|
||||
t.Fatal("offline bench failed")
|
||||
}
|
||||
for _, row := range rows {
|
||||
m := row.Fetch
|
||||
if !m.CandidateBodySupported || m.CandidateLength.Zero != 1 || m.CandidateLength.Small != 2 || m.CandidateLength.Medium != 0 || m.CandidateLength.Large != 0 {
|
||||
t.Fatal("bench lost candidate body buckets")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user