import pytest from datetime import date, timedelta from room_cal.closed import Closed, ClosedData, _build_title, _build_description def test_build_title_all(): assert _build_title(['airbnb', 'booking']) == 'Closed: All' def test_build_title_single_airbnb(): assert _build_title(['airbnb']) == 'Closed: AirBnB' def test_build_title_single_booking(): assert _build_title(['booking']) == 'Closed: Booking.com' def test_build_description(): assert _build_description(['airbnb', 'booking'], {}) == 'AirBnB: Closed\nBooking.com: Closed' @pytest.mark.asyncio async def test_set_and_get_round_trip(): """Find a past open day in CalDAV, block it, read back, then restore.""" closed = Closed() services = ['airbnb', 'booking'] # Scan the past 30–90 days for a date with no existing Closed event. # Past dates are safe: stale test data won't affect live bookings. scan_start = date.today() - timedelta(days=70) scan_end = date.today() - timedelta(days=50) existing_closures = await closed.list(scan_start, scan_end) already_closed = {e.start.date() for e in existing_closures} target = None d = scan_start while d <= scan_end: if d not in already_closed: target = d break d += timedelta(days=1) assert target is not None, "No open date found in the past 50–70 days" try: await closed.set(target, ClosedData(services=services, source={'domain': 'test', 'name': 'Test => ', 'id': 'test'})) stored = await closed.get(target) assert stored is not None, f"No Closed event found after set on {target}" assert sorted(stored.services) == sorted(services), f"Expected {sorted(services)}, got {sorted(stored.services)}" # Verify the CalDAV event title event = await closed._find(target) assert event is not None assert event.title == 'Closed: All' assert event.description == 'AirBnB: Closed\nBooking.com: Closed\nSource: Test => test' finally: await closed.set(target, ClosedData(services=[])) assert await closed.get(target) is None, f"Closed event still present after restore on {target}"