from datetime import datetime, timedelta, timezone from types import SimpleNamespace from common.enums import PostType from common.utils.kma_time import KST from services.calc.moon import altitude, nearest, night, phase_name, view_line from services.feed_service import FeedService, _tonight def test_true_phases_match_published_times_within_five_minutes(): known = [(True, datetime(2024, 4, 23, 23, 49, tzinfo=timezone.utc)), (False, datetime(2024, 4, 8, 18, 21, tzinfo=timezone.utc)), (True, datetime(2025, 10, 7, 3, 47, tzinfo=timezone.utc)), (False, datetime(2017, 8, 21, 18, 30, tzinfo=timezone.utc))] for full, at in known: assert abs((nearest(at, full) - at).total_seconds()) < 300 def test_phase_names(): assert phase_name(datetime(2025, 10, 7, 3, 47, tzinfo=timezone.utc)) == "보름달" assert phase_name(datetime(2024, 4, 8, 18, 21, tzinfo=timezone.utc)) == "삭" def test_tonight_follows_feed_day_starting_at_3am(): assert _tonight(datetime(2024, 4, 9, 1, 0, tzinfo=KST)) == datetime(2024, 4, 8, 22, 0, tzinfo=KST) assert _tonight(datetime(2024, 4, 8, 20, 0, tzinfo=KST)) == datetime(2024, 4, 8, 22, 0, tzinfo=KST) assert _tonight(datetime(2024, 4, 9, 9, 0, tzinfo=KST)) == datetime(2024, 4, 9, 22, 0, tzinfo=KST) def _moon_post(now: datetime): base = dict(post_id=1, post_type=PostType.MOON, like_count=0, comment_count=0, published_at=now) c = SimpleNamespace(now=now, region=SimpleNamespace(short_name="분당구", lat=37.38, lon=127.12), hourly=[], board=None) return FeedService._moon(None, base, c) def test_moon_post_every_night_with_full_and_new_moon_stories(): full = _moon_post(datetime(2025, 10, 7, 20, 0, tzinfo=KST)) # 보름 10.07 12:47 assert full.title == "오늘 밤 보름달" and "12:4" in full.slides[0].line new = _moon_post(datetime(2024, 4, 9, 1, 0, tzinfo=KST)) # 삭 04.09 03:21, 자정 넘긴 새벽에도 그 밤 assert new.title == "오늘 밤 달 없는 밤" plain = _moon_post(datetime(2025, 10, 4, 20, 0, tzinfo=KST)) # 보름 사흘 전 assert plain.title == "오늘 밤 차오르는 달" and plain.slides[0].rows[-1].label == "다음 보름" and plain.slides[0].rows[-1].value.startswith("10.07") SEOUL = (37.57, 126.98) def _night_of(at): """그 시각과 23시가 가장 가까운 밤.""" return (at.astimezone(KST) - timedelta(hours=11)).date() def test_full_moon_is_up_all_night_and_highest_near_solar_midnight(): full = nearest(datetime(2026, 10, 25, tzinfo=KST), True) day = _night_of(full) m = night(day, *SEOUL) assert m.name == "보름달" and set(range(20, 29)) <= set(m.up_hours) start = datetime.combine(day, datetime.min.time(), tzinfo=KST) peak = max((start + timedelta(hours=22, minutes=10 * i) for i in range(24)), key=lambda t: altitude(t, *SEOUL)) assert 23 <= (peak.hour if peak.hour >= 12 else peak.hour + 24) <= 25 assert view_line(m, 0.0) == "밤새 잘 보여요" assert view_line(m, 0.8) == "구름에 가려요" def test_first_quarter_sets_around_midnight_and_new_moon_is_not_seen(): new = nearest(datetime(2026, 10, 12, tzinfo=KST), False) quarter = night(_night_of(new + timedelta(days=7.4)), *SEOUL) assert quarter.set_at is not None and quarter.set_at.astimezone(KST).hour in (21, 22, 23, 0, 1, 2) assert view_line(quarter, 0.0).endswith("까지 보여요") assert view_line(night(_night_of(new), *SEOUL), 0.0) == "달이 안 보여요" def test_waning_moon_rises_late(): full = nearest(datetime(2026, 10, 25, tzinfo=KST), True) m = night(_night_of(full + timedelta(days=7.4)), *SEOUL) assert m.rise_at is not None and m.rise_at.astimezone(KST).hour in (21, 22, 23, 0, 1, 2) assert view_line(m, 0.0).endswith("에 떠요")