package ingestion import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/PuerkitoBio/goquery" "pete/internal/safehttp" ) func init() { // httptest servers bind to 127.0.0.1; allow the safe-dial check to // accept them in tests only. safehttp.AllowPrivate = true } func TestFetchOGImage(t *testing.T) { cases := []struct { name string body string want string // suffix match }{ { name: "og:image property", body: ``, want: "https://example.com/lead.jpg", }, { name: "content before property", body: ``, want: "https://example.com/swap.jpg", }, { name: "twitter:image fallback", body: ``, want: "https://example.com/twit.jpg", }, { name: "relative URL resolved", body: ``, want: "/img/lead.jpg", }, { name: "no og tags", body: `nothing`, want: "", }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.Write([]byte(c.body)) })) defer srv.Close() got := FetchOGImage(srv.URL + "/article") if c.want == "" { if got != "" { t.Errorf("expected empty, got %q", got) } return } if !strings.HasSuffix(got, c.want) { t.Errorf("got %q, want suffix %q", got, c.want) } }) } } func TestFirstImgSrc(t *testing.T) { in := `

Body x more

` if got := firstImgSrc(in); got != "https://cdn.example.com/lead.png" { t.Errorf("got %q", got) } if got := firstImgSrc(""); got != "" { t.Errorf("empty input should yield empty, got %q", got) } } // TestExtractBodyCharsANNLayout regresses a false-positive paywall flag on // Anime News Network: their layout has no
/
tags and most of // the prose lives outside

tags, inside

containers. // Before the fix, body-length came out below PaywallBodyThreshold and the // poller flagged the story as gated. func TestExtractBodyCharsANNLayout(t *testing.T) { body := `

Yen Press Licenses Wistoria

Yen Press announced on Friday during its panel that it has licensed seven manga and two novel series for release in December 2026 and winter 2027.

Snowmelt and Agapanthus is a manga series being released in the coming season; the publisher described it as a romance story with supernatural undertones, drawn in a delicate watercolor style that should appeal to fans of slice-of-life fantasy. Witch and Cat follows a young magic apprentice and her talking familiar through a series of low-stakes adventures across a sleepy countryside town, leaning into cozy atmosphere over plot. Sinful Is the Angel Who Loves is positioned for an older audience and runs in the publisher's mature imprint. Each release will be available in print and digital across English-language markets, with the first volumes scheduled for the December 2026 wave.
` doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)) if err != nil { t.Fatalf("parse: %v", err) } if n := extractBodyChars(doc); n < PaywallBodyThreshold { t.Fatalf("extractBodyChars = %d, want >= %d (ANN-style body was incorrectly treated as paywalled)", n, PaywallBodyThreshold) } }