from datetime import date, datetime, timedelta, timezone from common.enums import Confidence, SkyGrade, SkyType from common.utils.geo import destination, haversine_km from services.calc.sky_score import GRADE_LABEL, Layers, best_times, confidence, grade, reason, score, situation, sunset_timeline, verdict from services.calc.sun import sun_times CLEAR = Layers(0, 0, 0) def test_high_clouds_overhead_and_clear_horizon_is_best(): canvas = Layers(low=0, mid=20, high=50) assert score(canvas, canvas, CLEAR) == 100 def test_clear_sky_is_only_soft(): assert grade(score(CLEAR, CLEAR, CLEAR)) is SkyGrade.SOFT def test_low_clouds_toward_sun_block_the_light(): canvas = Layers(low=0, mid=20, high=50) assert grade(score(canvas, canvas, Layers(low=90, mid=50, high=0))) is SkyGrade.GRAY def test_overcast_low_clouds_is_gray(): gray = Layers(low=100, mid=100, high=100) assert grade(score(gray, gray, gray)) is SkyGrade.GRAY def test_confidence_from_model_spread(): assert confidence([70, 72, 68, 71]) is Confidence.HIGH assert confidence([40, 80, 60, 90]) is Confidence.LOW assert confidence([70]) is Confidence.LOW def test_destination_point_is_at_given_distance(): lat, lon = destination(37.56, 126.91, 266, 50) assert abs(haversine_km(37.56, 126.91, lat, lon) - 50) < 0.1 assert lon < 126.91 def test_sunset_best_times_order(): sunset = datetime(2026, 10, 2, 9, 15, tzinfo=timezone.utc) dusk = datetime(2026, 10, 2, 9, 41, tzinfo=timezone.utc) labels = [t["label"] for t in best_times(SkyType.SUNSET, sunset, dusk, 50)] assert labels == ["해 져요", "가장 붉을 때", "블루아워"] def test_reason_names_what_decided_the_score(): canvas = Layers(low=0, mid=20, high=50) assert "알맞게" in reason(canvas, canvas, CLEAR) assert "먼 하늘" in reason(canvas, canvas, Layers(low=90, mid=50, high=0)) assert "낮은 구름" in reason(Layers(100, 0, 0), Layers(80, 0, 0), CLEAR) assert "거의 없어" in reason(CLEAR, CLEAR, CLEAR) def test_verdict_bands_and_gray_label_is_not_weather_word(): assert [verdict(v) for v in (60, 59, 40, 39)] == ["go", "maybe", "maybe", "rest"] assert GRADE_LABEL[SkyGrade.GRAY] != "흐림" def test_sunset_timeline_runs_golden_to_blue(): sun = sun_times(date(2026, 10, 6), 37.56, 126.91) rows = sunset_timeline(sun.golden_start_at, sun.sunset_at, sun.sunset_at + timedelta(minutes=15), sun.dusk_end_at) assert [r["kind"] for r in rows] == ["golden", "sun", "peak", "blue"] ats = [datetime.fromisoformat(r["at"]) for r in rows] assert ats == sorted(ats) and 15 <= (ats[1] - ats[0]).total_seconds() / 60 <= 60 def test_situation_follows_reason_order(): from common.enums import SkySituation clear, glow = Layers(0, 0, 0), Layers(0, 40, 50) assert situation(clear, clear, clear) is SkySituation.FEW assert situation(glow, glow, clear) is SkySituation.GLOW assert situation(Layers(0, 90, 95), Layers(0, 90, 95), clear) is SkySituation.THICK assert situation(Layers(80, 0, 0), Layers(70, 0, 0), clear) is SkySituation.LOW_CLOUD assert situation(glow, glow, Layers(70, 0, 0)) is SkySituation.COVERED def test_ranking_summary_only_when_most_spots_share_a_situation(): from types import SimpleNamespace from common.enums import SkySituation from services.sky_service import ranking_summary item = lambda s: SimpleNamespace(score=SimpleNamespace(situation=s.value)) # noqa: E731 assert ranking_summary([item(SkySituation.FEW)] * 8 + [item(SkySituation.GLOW)] * 2) == "오늘은 전국이 맑아 대부분 짧게 끝나요" assert ranking_summary([item(SkySituation.FEW)] * 5 + [item(SkySituation.GLOW)] * 5) is None