package ingestion import ( "net/http" "net/http/httptest" "strings" "testing" ) 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) } }