"""Tests para split_vevents_to_vcalendars. Puros, deterministas, sin I/O."""

import sys

import infra.split_vevents_to_vcalendars  # noqa: F401

mod = sys.modules["infra.split_vevents_to_vcalendars"]
split = mod.split_vevents_to_vcalendars

_TWO = (
    "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Google//EN\r\n"
    "BEGIN:VEVENT\r\nUID:a@x\r\nSUMMARY:Reunion\r\nEND:VEVENT\r\n"
    "BEGIN:VEVENT\r\nUID:b@x\r\nSUMMARY:Comida\r\nEND:VEVENT\r\n"
    "END:VCALENDAR\r\n"
)

_WITH_TZ = (
    "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Google//EN\r\n"
    "BEGIN:VTIMEZONE\r\nTZID:Europe/Madrid\r\nEND:VTIMEZONE\r\n"
    "BEGIN:VEVENT\r\nUID:a@x\r\nDTSTART;TZID=Europe/Madrid:20260101T100000\r\nEND:VEVENT\r\n"
    "BEGIN:VEVENT\r\nUID:b@x\r\nDTSTART;TZID=Europe/Madrid:20260102T100000\r\nEND:VEVENT\r\n"
    "END:VCALENDAR\r\n"
)


def test_dos_vevents_devuelve_dos_vcalendars():
    cals = split(_TWO)
    assert len(cals) == 2


def test_cada_salida_tiene_un_solo_vevent():
    for cal in split(_TWO):
        assert cal.count("BEGIN:VEVENT") == 1
        assert cal.count("END:VEVENT") == 1
        assert cal.startswith("BEGIN:VCALENDAR")
        assert cal.rstrip("\r\n").endswith("END:VCALENDAR")


def test_header_vcalendar_correcto():
    cal = split(_TWO)[0]
    assert "VERSION:2.0" in cal
    assert "PRODID:" in cal
    assert "CALSCALE:GREGORIAN" in cal


def test_vtimezone_se_replica_en_cada_salida():
    cals = split(_WITH_TZ)
    assert len(cals) == 2
    for cal in cals:
        assert "BEGIN:VTIMEZONE" in cal
        assert "TZID:Europe/Madrid" in cal


def test_salida_termina_en_crlf():
    cal = split(_TWO)[0]
    assert cal.endswith("END:VCALENDAR\r\n")


def test_input_vacio_devuelve_lista_vacia():
    assert split("") == []
    assert split("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n") == []
