import base64

from app.api.routes import (
    GESNOT_MAX_GENERATED_FILE_BYTES,
    GESNOT_NOTIFICATION_EMAIL,
    _base64_payload_size,
    _gesnot_generated_file,
)


def test_gesnot_notification_address_is_the_operational_mailbox():
    assert GESNOT_NOTIFICATION_EMAIL == "registronotaria@documenta.cl"


def test_generated_file_uses_required_base64_prefix_and_roundtrips():
    original = b"PK\x03\x04documenta-test"
    item = _gesnot_generated_file("documento.docx", "DECLARACION JURADA", original)
    assert item["nombre"] == "documento.docx"
    assert item["descripcion"] == "DECLARACION JURADA"
    assert item["data"].startswith("base64:")
    assert base64.b64decode(item["data"].removeprefix("base64:")) == original
    assert _base64_payload_size(item["data"]) == len(original)


def test_generated_file_rejects_empty_or_oversized_content():
    for content in (b"", b"x" * (GESNOT_MAX_GENERATED_FILE_BYTES + 1)):
        try:
            _gesnot_generated_file("documento.docx", "MATERIA", content)
        except RuntimeError:
            pass
        else:
            raise AssertionError("El archivo inválido debía rechazarse")
