"""Tests for the Calendar source adapter."""
from datetime import date, timedelta
import pytest
import respx
from httpx import Response
from daily_briefing.sources.calendar import CalendarSource
class TestCalendarSource:
"""Tests for CalendarSource."""
@respx.mock
@pytest.mark.asyncio
async def test_get_events(self, sample_ical_data, today):
"""Test fetching events from iCal feed."""
source = CalendarSource(
name="Test Calendar",
ical_url="https://example.com/calendar.ics",
)
respx.get("https://example.com/calendar.ics").mock(
return_value=Response(200, text=sample_ical_data)
)
events = await source.get_events(today, today)
assert len(events) == 2
assert events[0].title == "Team Standup"
assert events[1].title == "Product Review"
@respx.mock
@pytest.mark.asyncio
async def test_get_events_empty(self, today):
"""Test fetching events when calendar is empty."""
source = CalendarSource(
name="Test Calendar",
ical_url="https://example.com/calendar.ics",
)
respx.get("https://example.com/calendar.ics").mock(
return_value=Response(200, text="BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR")
)
events = await source.get_events(today, today)
assert len(events) == 0
@respx.mock
@pytest.mark.asyncio
async def test_get_travel_info(self, sample_travel_ical_data, today):
"""Test fetching travel info from TripIt-like feed."""
source = CalendarSource(
name="TripIt",
ical_url="https://example.com/tripit.ics",
is_travel_source=True,
)
respx.get("https://example.com/tripit.ics").mock(
return_value=Response(200, text=sample_travel_ical_data)
)
travel = await source.get_travel_info(today, today + timedelta(days=7))
assert len(travel) >= 1
assert any("Flight" in t.title or "San Francisco" in str(t.destination) for t in travel)
@respx.mock
@pytest.mark.asyncio
async def test_health_check_success(self):
"""Test health check when feed is accessible."""
source = CalendarSource(
name="Test Calendar",
ical_url="https://example.com/calendar.ics",
)
respx.head("https://example.com/calendar.ics").mock(
return_value=Response(200)
)
health = await source.health_check()
assert health["status"] == "healthy"
@respx.mock
@pytest.mark.asyncio
async def test_health_check_failure(self):
"""Test health check when feed is inaccessible."""
source = CalendarSource(
name="Test Calendar",
ical_url="https://example.com/calendar.ics",
)
respx.head("https://example.com/calendar.ics").mock(
return_value=Response(404)
)
health = await source.health_check()
assert health["status"] == "error"
def test_disabled_source(self):
"""Test that disabled source returns empty results."""
source = CalendarSource(
name="Test Calendar",
ical_url="https://example.com/calendar.ics",
enabled=False,
)
assert not source.enabled
@pytest.mark.asyncio
async def test_missing_url(self, today):
"""Test that missing URL returns empty results."""
source = CalendarSource(
name="Test Calendar",
ical_url="",
)
events = await source.get_events(today, today)
assert len(events) == 0