from datetime import date, datetime from types import SimpleNamespace from common.enums import EventKind from common.utils.kma_time import KST from services.week_service import DayPlan, EventItem from services.weekend_plan import day_plan, forecast, pick_events SAT, SUN = date(2026, 10, 10), date(2026, 10, 11) def _day(d: date, lo=12.0, hi=26.0, rainy=False, rain_hours=None, prob=10) -> DayPlan: weekday = "토" if d.weekday() == 5 else "일" return DayPlan(day=d, weekday=weekday, holiday=None, icon="rain" if rainy else "sun", temp_min_c=lo, temp_max_c=hi, headline="", summary="", pick="", rain_hours=rain_hours, is_rainy=rainy, rain_prob=prob) def _event(name, start, end, dist, image="https://img/x.jpg", kind=EventKind.FESTIVAL) -> EventItem: e = SimpleNamespace(event_id=hash(name), name=name, kind=kind, icon="fireworks", start_date=start, end_date=end, place_name="경기도 고양시 일산동구 호수로 595", image_url=image, source_id="1") return EventItem(event=e, status="live", status_label="진행 중", fit=None, distance_km=dist) def test_forecast_both_clear_with_big_gap_warns_about_jacket(): f = forecast([_day(SAT), _day(SUN, lo=13)]) assert f.line.startswith("토·일 둘 다 맑음!") assert "겉옷" in f.tip and "12°" in f.tip def test_forecast_one_rainy_day_picks_the_other(): f = forecast([_day(SAT, rainy=True, prob=70), _day(SUN)]) assert f.line.startswith("일요일이 정답!") assert f.tip == "토요일은 비 70%, 우산 꼭 챙겨요!" def test_saturday_plan_runs_morning_to_sunset_with_nearest_event(): events = [_event("먼 축제", SAT, SUN, 40.0), _event("가까운 축제", SAT, SUN, 12.0)] card = day_plan(_day(SAT), datetime(2026, 10, 10, 18, 3, tzinfo=KST), pick_events(SAT, events), 13.0) assert [s.time for s in card.steps] == ["오전", "오전", "12~16시", "오후", "18:03"] assert card.steps[1].title == "가까운 축제" assert card.steps[-1].icon == "sunset" assert card.eyebrow == "토요일 10.10 · 이렇게 놀아요" def test_sunday_plan_leads_with_events_ending_today_and_ends_early(): events = [_event("이어지는 축제", SAT, date(2026, 10, 20), 5.0), _event("끝나는 축제", date(2026, 10, 1), SUN, 30.0)] card = day_plan(_day(SUN), None, pick_events(SUN, events), 13.0) assert card.line == "끝나는 축제\n오늘이 마지막 날!" assert card.steps[-1].title == "일찍 귀가 추천" and "13°" in card.steps[-1].sub assert any(s.title == "끝나는 축제" and s.sub.startswith("오늘이 마지막 날") for s in card.steps) def test_afternoon_rain_moves_outdoor_time_before_it(): card = day_plan(_day(SAT, rainy=True, rain_hours=(15, 19), prob=60), None, [], None) assert [s.time for s in card.steps[:2]] == ["~15시", "15~19시"] assert card.line.startswith("비 오는 토요일") def test_saturday_ignores_ending_hook_and_sunday_skips_saturday_events(): near = _event("가까운 축제", SAT, SUN, 3.0) ends_sat = _event("토요일 끝", SAT, SAT, 50.0) far = _event("먼 축제", SAT, SUN, 40.0) sat = pick_events(SAT, [ends_sat, near, far]) assert [e.event.name for e in sat] == ["가까운 축제", "먼 축제"] assert day_plan(_day(SAT), None, sat, None).line.startswith("해 좋은 낮") assert pick_events(SUN, [near, far], frozenset(e.event.event_id for e in sat)) == [] def test_festival_and_concert_come_before_nearer_galleries(): events = [_event("옆 전시1", SAT, SUN, 0.5, kind=EventKind.EXHIBITION), _event("옆 전시2", SAT, SUN, 0.6, kind=EventKind.EXHIBITION), _event("먼 공연", SAT, SAT, 20.0, kind=EventKind.CONCERT), _event("먼 축제", SAT, SUN, 25.0)] assert [e.event.name for e in pick_events(SAT, events)] == ["먼 축제", "먼 공연"] only_galleries = events[:2] assert [e.event.name for e in pick_events(SAT, only_galleries)] == ["옆 전시1", "옆 전시2"] def test_cloudy_weekend_is_not_called_clear(): cloudy = [_day(SAT), _day(SUN)] for d in cloudy: d.icon = "cloud" f = forecast(cloudy) assert "맑음" not in f.line card = day_plan(cloudy[0], None, [], None) assert all(s.icon != "sun" for s in card.steps) and not card.line.startswith("해 좋은") def test_event_step_uses_event_icon(): e = _event("가까운 축제", SAT, SUN, 3.0) e.event.icon = "photo" card = day_plan(_day(SAT), None, [e], None) assert next(s for s in card.steps if s.title == "가까운 축제").icon == "photo"