"""Tests para pdf_save."""

import sys
import os
import tempfile

sys.path.insert(0, os.path.dirname(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "python", "types", "infra"))

from pdf_create import pdf_create
from pdf_add_page import pdf_add_page
from pdf_add_text import pdf_add_text
from pdf_save import pdf_save


def _make_simple_doc():
    doc = pdf_create(title="Test Save")
    doc = pdf_add_page(doc)
    doc = pdf_add_text(doc, "Contenido de prueba")
    return doc


def test_guardar_pdf_a_archivo():
    doc = _make_simple_doc()
    with tempfile.TemporaryDirectory() as tmpdir:
        path = os.path.join(tmpdir, "test.pdf")
        result = pdf_save(doc, path)
        assert result == path
        assert os.path.exists(path)
        assert os.path.getsize(path) > 0


def test_retornar_bytes_del_pdf():
    doc = _make_simple_doc()
    result = pdf_save(doc)
    assert isinstance(result, bytes)
    assert len(result) > 0


def test_pdf_valido_comienza_con_pdf():
    doc = _make_simple_doc()
    pdf_bytes = pdf_save(doc)
    assert pdf_bytes[:4] == b"%PDF"
