package extract
import (
"net/url"
"testing"
"time"
)
func TestReadPageMetaTags(t *testing.T) {
base, _ := url.Parse("https://news.example/2025/story")
m := readPageMeta(parse(t, `
Fallback Title | News
`), base)
want := pageMeta{
Title: "The Story", Author: "Ada Lovelace", SiteName: "Example News", Description: "What happened.",
Image: "https://news.example/img/lead.jpg",
Published: time.Date(2025, 3, 14, 9, 30, 0, 0, time.FixedZone("", 3600)),
}
if m.Title != want.Title || m.Author != want.Author || m.SiteName != want.SiteName ||
m.Description != want.Description || m.Image != want.Image || !m.Published.Equal(want.Published) {
t.Errorf("got %+v\nwant %+v", m, want)
}
}
func TestReadPageMetaJSONLD(t *testing.T) {
base, _ := url.Parse("https://blog.example/post")
m := readPageMeta(parse(t, `
Title Tag
`), base)
if m.Title != "JSON-LD Headline" {
t.Errorf("Title = %q", m.Title)
}
if m.Author != "Grace Hopper, Alan Turing" {
t.Errorf("Author = %q (profile URLs must not count as names)", m.Author)
}
if m.SiteName != "The Blog" {
t.Errorf("SiteName = %q", m.SiteName)
}
if m.Image != "https://cdn.example/hero.png" {
t.Errorf("Image = %q", m.Image)
}
if !m.Published.Equal(time.Date(2024, 11, 2, 0, 0, 0, 0, time.UTC)) {
t.Errorf("Published = %v", m.Published)
}
}
func TestReadPageMetaImages(t *testing.T) {
base, _ := url.Parse("https://site.example/a")
tests := map[string]string{
``: "https://site.example/t.jpg",
``: "https://cdn.example/x.jpg",
``: "",
``: "",
``: "",
}
for head, want := range tests {
if got := readPageMeta(parse(t, ""+head+""), base).Image; got != want {
t.Errorf("%s: Image = %q, want %q", head, got, want)
}
}
}
func TestExtractCoverImageAndFallbacks(t *testing.T) {
a, err := extractFile(t, "photo-essay.html", "https://letterpress.example/essays/print-shop")
if err != nil {
t.Fatal(err)
}
if a.ImageURL != "https://letterpress.example/img/press.jpg" {
t.Errorf("ImageURL = %q", a.ImageURL)
}
if a.SiteName != "Letterpress Weekly" || a.Author != "Grace Hopper" {
t.Errorf("SiteName = %q, Author = %q", a.SiteName, a.Author)
}
}