"""§22.4c — the unreviewed/reviewed_at/reviewed_by entry frontmatter fields."""
from __future__ import annotations

from app import entry as entry_mod


def test_parse_defaults_unreviewed_false_when_absent():
    text = "---\nslug: ohm\ntitle: OHM\nstate: active\n---\n\nBody.\n"
    e = entry_mod.parse(text)
    assert e.unreviewed is False
    assert e.reviewed_at is None
    assert e.reviewed_by is None


def test_parse_reads_review_fields():
    text = (
        "---\nslug: ohm\ntitle: OHM\nstate: active\n"
        "unreviewed: true\nreviewed_at: '2026-06-03'\nreviewed_by: ben\n---\n\nBody.\n"
    )
    e = entry_mod.parse(text)
    assert e.unreviewed is True
    assert e.reviewed_at == "2026-06-03"
    assert e.reviewed_by == "ben"


def test_serialize_emits_review_fields_only_when_meaningful():
    e = entry_mod.Entry(slug="a", title="A", state="super-draft")
    assert "unreviewed" not in entry_mod.serialize(e)
    assert "reviewed_at" not in entry_mod.serialize(e)
    e2 = entry_mod.Entry(slug="b", title="B", state="active", unreviewed=True)
    assert "unreviewed: true" in entry_mod.serialize(e2)


def test_round_trip_preserves_review_fields():
    e = entry_mod.Entry(
        slug="b", title="B", state="active",
        unreviewed=False, reviewed_at="2026-06-03", reviewed_by="ben",
    )
    back = entry_mod.parse(entry_mod.serialize(e))
    assert back.reviewed_at == "2026-06-03"
    assert back.reviewed_by == "ben"
    assert back.unreviewed is False
