"""Tests for mailer.py's message construction (no network — _build() only)."""
from notifications import mailer


def test_build_sets_message_id_and_date():
    """RFC 5322 requires both; Gmail rejects a message outright ("Messages
    missing a valid Message-ID header are not accepted") rather than merely
    spam-scoring it when either is absent."""
    msg = mailer._build("you@example.com", "Subject", "Body text.", None)
    assert msg["Message-ID"]
    assert msg["Message-ID"].startswith("<") and msg["Message-ID"].endswith(">")
    assert msg["Date"]


def test_build_message_id_uses_the_sending_domain():
    """Not the sending host's own hostname (e.g. a VPS's provider-assigned
    name) -- keeps the Message-ID consistent with the From/SPF/DKIM domain."""
    msg = mailer._build("you@example.com", "Subject", "Body text.", None)
    assert "@thermograph.org>" in msg["Message-ID"]


def test_build_sets_from_to_subject():
    msg = mailer._build("you@example.com", "Hello", "Body text.", None)
    assert msg["To"] == "you@example.com"
    assert msg["Subject"] == "Hello"
    assert "thermograph.org" in msg["From"]


def test_build_adds_html_alternative_when_given():
    msg = mailer._build("you@example.com", "Subject", "Body text.", "<p>Body</p>")
    assert msg.get_body(preferencelist=("html",)) is not None
    assert msg.get_body(preferencelist=("plain",)) is not None


def test_build_is_plain_text_only_without_html():
    msg = mailer._build("you@example.com", "Subject", "Body text.", None)
    assert msg.get_body(preferencelist=("html",)) is None
