# retoor <retoor@molodetz.nl>

from devplacepy.services.email.config import EmailAccount


def test_defaults_apply_imap_993_and_smtp_587():
    account = EmailAccount.from_row({"username": "me@example.com"})
    assert account.imap_port == 993
    assert account.smtp_port == 587


def test_from_address_falls_back_to_username():
    account = EmailAccount.from_row({"username": "me@example.com"})
    assert account.from_address == "me@example.com"


def test_explicit_from_address_wins():
    account = EmailAccount.from_row(
        {"username": "login@example.com", "from_address": "public@example.com"}
    )
    assert account.from_address == "public@example.com"


def test_boolean_coercion_from_strings():
    account = EmailAccount.from_row(
        {
            "imap_ssl": "true",
            "imap_starttls": "0",
            "smtp_ssl": "yes",
            "smtp_starttls": "on",
        }
    )
    assert account.imap_ssl is True
    assert account.imap_starttls is False
    assert account.smtp_ssl is True
    assert account.smtp_starttls is True


def test_boolean_coercion_from_integers():
    account = EmailAccount.from_row({"imap_ssl": 1, "smtp_ssl": 0})
    assert account.imap_ssl is True
    assert account.smtp_ssl is False


def test_port_coercion_falls_back_on_garbage():
    account = EmailAccount.from_row({"imap_port": "not-a-port", "smtp_port": "2525"})
    assert account.imap_port == 993
    assert account.smtp_port == 2525


def test_missing_fields_become_empty_strings():
    account = EmailAccount.from_row({})
    assert account.label == ""
    assert account.imap_host == ""
    assert account.smtp_host == ""
    assert account.username == ""
    assert account.password == ""
    assert account.from_address == ""
    assert account.from_name == ""
