"""Tests for the destination meta-tag parser.""" from __future__ import annotations from app.services.meta_tags.parse_html import parse_meta_tags BASE = "https://dest.example.com/article/42" def test_full_og_page(): html = """ HTML Title ignored""" p = parse_meta_tags(html, BASE) assert p.title == "OG Title" # og beats twitter beats assert p.description == "OG Desc" assert p.image == "https://dest.example.com/og.png" assert p.color == "#FF5733" assert p.site_name == "Dest" assert p.og["title"] == "OG Title" assert p.twitter["title"] == "TW Title" def test_fallback_chain_title(): html = "<head><title>Only Title" assert parse_meta_tags(html, BASE).title == "Only Title" html = 'T' assert parse_meta_tags(html, BASE).title == "TW" def test_property_name_attribute_mixups_accepted(): # Sites swap property=/name= constantly — accept both for both families. html = """ """ p = parse_meta_tags(html, BASE) assert p.title == "via name" assert p.description == "via property" def test_first_tag_wins(): html = """ """ assert parse_meta_tags(html, BASE).title == "first" def test_relative_image_resolved_against_final_url(): html = '' assert parse_meta_tags(html, BASE).image == "https://dest.example.com/img/og.png" def test_http_image_dropped(): html = '' assert parse_meta_tags(html, BASE).image is None def test_bad_theme_color_dropped(): html = '' assert parse_meta_tags(html, BASE).color is None def test_body_meta_ignored(): # Stop at : preview crawlers only read head, and so do we. html = """T """ p = parse_meta_tags(html, BASE) assert p.title == "T" def test_broken_html_tolerated(): html = '
' assert parse_meta_tags(html, BASE).title == "ok" def test_entities_decoded(): html = '' assert parse_meta_tags(html, BASE).title == "A & B" def test_no_tags_page(): p = parse_meta_tags("", BASE) assert p.title is None and p.description is None and p.image is None def test_html_title_and_description_exposed_raw(): # The audit surface needs the raw /<meta description> even when # og tags exist and win the normalized picks. html = """<head> <title>HTML Title """ p = parse_meta_tags(html, BASE) assert p.title == "OG Title" assert p.html_title == "HTML Title" assert p.html_description == "HTML Desc" def test_favicon_prefers_apple_touch_icon(): html = """ """ p = parse_meta_tags(html, BASE) assert p.favicon == "https://dest.example.com/apple-icon.png" def test_favicon_picks_largest_declared_size(): html = """ """ assert parse_meta_tags(html, BASE).favicon == "https://dest.example.com/big.png" def test_favicon_defaults_to_convention_path(): p = parse_meta_tags("T", BASE) assert p.favicon == "https://dest.example.com/favicon.ico"