"""mailu — recipe-specific functional test #1 (Phase 2 P3 §4.3 create-an-object + read-back).

Create a mailbox (the characteristic mailu object) via the admin CLI, then read it back from the
authoritative config export and assert it is present. Real create→persist→read-back, not health-only.
"""

from __future__ import annotations

import os
import sys
import uuid

sys.path.insert(0, os.path.dirname(__file__))
import _mailu  # noqa: E402


def test_create_mailbox_and_read_back(live_app):
    mail_domain = live_app  # recipe_meta sets MAIL_DOMAIN = the per-run domain
    local = "ccci-" + uuid.uuid4().hex[:8]
    password = "CcCi-" + uuid.uuid4().hex[:16] + "-Aa1!"

    _mailu.ensure_domain(live_app, mail_domain)
    email = _mailu.create_user(live_app, local, mail_domain, password)

    cfg = _mailu.config_export(live_app)
    emails = _mailu.user_emails(cfg)
    assert email in emails, (
        f"created mailbox {email} not present in mailu config-export users {emails}"
    )
