from datetime import datetime, timezone from types import SimpleNamespace from common.enums import PostType from common.utils.kma_time import KST from services.calc.moon import nearest, phase_name 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="분당구"), 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")