samhuri.net/test/link_post_test.rb
Sami Samhuri 97105c1501 Fetch og:image from linked URLs when creating link posts
Add Pressa::OpenGraph, a best-effort scraper that pulls og:image (falling
back to twitter:image) from a linked page's HTML. bake new_link and bake
preview_link now use it to prefill the Image front-matter field for link
posts, unless the payload already supplies one. Network failures and
missing tags just resolve to nil so creating a post never blocks on a
slow or broken link; the fetch happens once at draft-creation time, not
on every build.
2026-06-21 23:33:29 -07:00

90 lines
2.7 KiB
Ruby

require "test_helper"
require "pressa/posts/metadata"
class Pressa::LinkPostTest < Minitest::Test
def setup
@now = Time.new(2026, 6, 7, 14, 30, 0, "-07:00")
end
def build(**overrides)
defaults = {
title: "Magical Wristband",
link: "https://example.net/magicband",
now: @now,
author: "Sami Samhuri"
}
Pressa::LinkPost.build(**defaults.merge(overrides))
end
def test_target_path_uses_slug_and_year_month
post = build
assert_equal("posts/2026/06/magical-wristband.md", post.target_path)
assert_equal("magical-wristband.md", post.filename)
end
def test_front_matter_is_parseable_and_complete
post = build(body: "Disney's take on the wearable.", tags: "gear, tech")
meta = Pressa::Posts::PostMetadata.parse(post.content)
assert_equal("Magical Wristband", meta.title)
assert_equal("Sami Samhuri", meta.author)
assert_equal("7th June, 2026", meta.formatted_date)
assert_equal("https://example.net/magicband", meta.link)
assert_equal(%w[gear tech], meta.tags)
assert_includes(post.content, "Disney's take on the wearable.")
end
def test_timestamp_is_iso8601_with_offset
post = build
meta = Pressa::Posts::PostMetadata.parse(post.content)
assert_equal("2026-06-07T14:30:00-07:00", meta.date.strftime("%Y-%m-%dT%H:%M:%S%:z"))
end
def test_title_with_special_characters_round_trips
weird = %(Fish: "And" \\ Chips)
post = build(title: weird)
meta = Pressa::Posts::PostMetadata.parse(post.content)
assert_equal(weird, meta.title)
end
def test_tags_omitted_when_blank
post = build(tags: " ")
refute_includes(post.content, "Tags:")
end
def test_body_optional_for_link_only_posts
post = build(body: nil)
meta = Pressa::Posts::PostMetadata.parse(post.content)
assert_equal("https://example.net/magicband", meta.link)
end
def test_blank_title_raises
error = assert_raises(Pressa::LinkPost::Error) { build(title: " ") }
assert_match(/title/i, error.message)
end
def test_title_with_only_symbols_raises
assert_raises(Pressa::LinkPost::Error) { build(title: "!!!") }
end
def test_blank_link_raises
error = assert_raises(Pressa::LinkPost::Error) { build(link: " ") }
assert_match(/link/i, error.message)
end
def test_image_is_included_in_front_matter_when_given
post = build(image: "https://example.net/preview.png")
meta = Pressa::Posts::PostMetadata.parse(post.content)
assert_equal("https://example.net/preview.png", meta.image)
end
def test_image_is_omitted_when_blank
post = build(image: " ")
refute_includes(post.content, "Image:")
end
def test_image_is_omitted_when_not_given
post = build
refute_includes(post.content, "Image:")
end
end