from dataclasses import replace from datetime import date, datetime, timezone from types import SimpleNamespace from common.enums import RainType, Sky from services.calc.index_calc import ( OUTING_HOURS, Context, DayForecast, Hour, axis_label, estimate, evaluate, hour_temp, outing, outing_estimate, rain_score, temp_score, zero_rules_text, ) from services.calc.moon import illumination CTX = Context(day=date(2026, 10, 2), is_holiday=False, pm25=10, air_label="좋음", moon=0.5, sunset_score=80, wind_warning=False, lookahead_rain_prob=0) def _defn(**kw): base = dict( weight_temp=30, weight_rain=25, weight_wind=15, weight_humidity=10, weight_air=10, weight_time=10, factor_rules={ "temp": {"type": "range", "lo": 18, "hi": 24}, "rain": {"dir": "dry"}, "wind": {"type": "calm", "ok": 2, "limit": 10}, "humidity": {"type": "range", "lo": 40, "hi": 60}, "time": {"type": "weekday", "fri": 100, "default": 78}, }, zero_rules=[{"field": "rain_prob", "op": ">=", "value": 80}], ) return SimpleNamespace(**{**base, **kw}) def _h(hour, feels=21.0, prob=0, rain=RainType.NONE, wind=1.5, hum=50, sky=Sky.CLEAR): return Hour(hour=hour, feels_c=feels, rain_type=rain, rain_prob=prob, wind_ms=wind, humidity=hum, sky=sky) def test_perfect_evening_scores_100(): r = evaluate(_defn(), [[_h(19, wind=1.0), _h(20, wind=1.0)]], CTX) assert r.score == 100 and r.zero_reason is None assert r.notes["temp"] == "체감 21°, 편한 범위 18~24° 안" def test_good_but_not_perfect_stays_below_100(): assert evaluate(_defn(), [[_h(19, feels=23, hum=58, wind=1.8)]], CTX).score < 95 def test_one_bad_factor_pulls_total_down(): assert evaluate(_defn(), [[_h(19, wind=1.0, prob=60)]], CTX).score < 80 def test_temp_peaks_at_best_and_loses_8_per_degree_outside(): assert temp_score({"type": "range", "lo": 18, "hi": 24}, 21)[0] == 100 assert temp_score({"type": "range", "lo": 18, "hi": 24}, 24)[0] == 85 assert temp_score({"type": "range", "lo": 18, "hi": 24, "best": 22}, 22)[0] == 100 assert temp_score({"type": "range", "lo": 18, "hi": 24}, 15)[0] == 61 assert temp_score({"type": "lower", "zero": 25, "full": 5}, 15)[0] == 50 assert temp_score({"type": "range", "lo": 10, "hi": 20, "offset": -6}, 21)[1].startswith("산 정상 기준 15°") def test_temp_away_rises_outside_comfort(): rule = {"type": "away", "lo": 15, "hi": 24} assert temp_score(rule, 20)[0] == 30 assert temp_score(rule, 34)[0] == 100 assert temp_score(rule, 5)[0] == 100 def test_rain_dry_and_wet(): assert rain_score({"dir": "dry"}, 30, False)[0] == 70 assert rain_score({"dir": "dry"}, 30, True)[0] == 0 assert rain_score({"dir": "wet"}, 30, True)[0] == 100 def test_zero_rule_forces_zero_and_cap_limits(): assert evaluate(_defn(), [[_h(19, prob=80)]], CTX).score == 0 capped = _defn(zero_rules=[{"field": "rain_prob", "op": ">=", "value": 70, "cap": 20}]) r = evaluate(capped, [[_h(19, prob=70)]], CTX) assert r.score <= 20 and r.zero_reason == "비 확률 70% 이상이라 최대 20점" assert evaluate(_defn(), [[_h(19, prob=80)]], CTX).zero_reason == "비 확률 80% 이상이라 0점" def test_lookahead_rain_is_named_in_note(): defn = _defn(windows=[{"from": 9, "to": 18, "lookahead_days": 2}]) r = evaluate(defn, [[_h(10, prob=0)]], replace(CTX, lookahead_rain_prob=60)) assert r.notes["rain"] == "모레까지 비 확률 60%" assert evaluate(defn, [[_h(10, prob=0)]], CTX).notes["rain"] == "비 확률 0%" def test_air_note_has_unit(): assert evaluate(_defn(), [[_h(19)]], CTX).notes["air"] == "초미세먼지 10㎍/㎥, 좋음" def test_axis_label_reads_outward_as_good(): rules = _defn().factor_rules assert [axis_label(a, rules) for a in ("temp", "rain", "wind", "humidity", "air", "time")] == \ ["기온 알맞음", "비 안 옴", "바람 잔잔", "습도 알맞음", "공기 맑음", "요일 알맞음"] wet = {"temp": {"type": "lower"}, "rain": {"dir": "wet"}, "wind": {"type": "strong"}, "humidity": {"type": "higher"}, "time": {"type": "moon"}} assert [axis_label(a, wet) for a in ("temp", "rain", "wind", "humidity", "time")] == ["추움", "비 옴", "바람 셈", "습도 높음", "달 어두움"] def test_best_window_is_chosen(): r = evaluate(_defn(), [[_h(7, feels=5)], [_h(20, feels=21)]], CTX) assert r.window == (20, 20) def test_zero_rules_text(): assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 80}, {"field": "wind_ms", "op": ">=", "value": 14}]) == \ "비 확률 80% 이상이거나 바람 14m/s 이상이면 0점" assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 70}, {"field": "warning", "op": "==", "value": "wind"}]) == \ "비 확률 70% 이상이거나 강풍 특보가 있으면 0점" assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 70, "cap": 20}]) == "비 확률 70% 이상이면 최대 20점" assert zero_rules_text([{"field": "pm25", "op": ">=", "value": 75}]) == "초미세먼지 75㎍/㎥ 이상이면 0점" assert zero_rules_text([]) == "없음 (재미로 보는 지수)" def test_moon_full_and_new(): assert illumination(datetime(2000, 1, 6, 18, 14, tzinfo=timezone.utc)) < 0.01 assert illumination(datetime(2000, 1, 21, 12, tzinfo=timezone.utc)) > 0.95 def _fc(lo=15.0, hi=23.0, am=0, pm=0, sky=Sky.CLEAR): return DayForecast(temp_min_c=lo, temp_max_c=hi, rain_prob_am=am, rain_prob_pm=pm, sky=sky) def test_hour_temp_follows_min_at_dawn_and_max_mid_afternoon(): assert hour_temp(10, 20, 6) == 10 and hour_temp(10, 20, 15) == 20 assert 10 < hour_temp(10, 20, 21) < 20 and hour_temp(10, 20, 30) == 10 def test_estimate_is_flagged_and_unknown_factors_are_70(): r = estimate(_defn(), _fc(), [[19, 20]], CTX) assert r.estimated is True assert r.factors["wind"] == 70 and r.factors["humidity"] == 70 and r.factors["air"] == 70 assert "예상" in r.summary def test_estimate_uses_rain_prob_of_the_window_half_day(): morning, evening = estimate(_defn(), _fc(am=60, pm=0), [[9, 10]], CTX), estimate(_defn(), _fc(am=60, pm=0), [[19, 20]], CTX) assert morning.factors["rain"] == 40 and evening.factors["rain"] == 100 def test_estimate_applies_rain_zero_rule_only(): r = estimate(_defn(zero_rules=[{"field": "rain_prob", "op": ">=", "value": 80}, {"field": "wind_ms", "op": ">=", "value": 1}]), _fc(pm=80), [[19]], CTX) assert r.score == 0 and r.zero_reason.startswith("비 확률 80% 이상") def test_estimate_without_temps_gives_none(): assert estimate(_defn(), _fc(lo=None, hi=None), [[19]], CTX) is None def _day(**kw): return [_h(h, **kw) for h in range(*OUTING_HOURS)] def test_outing_perfect_day_is_100_and_reason_names_rain_temp_sky(): r = outing(_day(wind=1.0)) assert r.score == 100 and r.estimated is False assert r.reason == "비 0% · 체감 21° · 맑음" def test_outing_cloudy_and_hot_days_lose_points(): clear, cloudy, hot = outing(_day()), outing(_day(sky=Sky.CLOUDY)), outing(_day(feels=30.0)) assert cloudy.score < clear.score and hot.score < clear.score assert "흐림" in cloudy.reason def test_outing_rain_caps_score(): r = outing(_day(prob=70)) assert r.score <= 30 and r.capped assert outing([_h(10, prob=30, rain=RainType.RAIN)] + _day()[1:]).score <= 30 def test_outing_needs_all_hours(): assert outing(_day()[:3]) is None def test_outing_estimate_from_mid_forecast(): r = outing_estimate(_fc(lo=12, hi=22, am=10, pm=20)) assert r.estimated is True and r.factors["wind"] == 70 assert r.reason.startswith("비 20% · 낮 ") assert outing_estimate(_fc(pm=60)).score <= 30 assert outing_estimate(_fc(lo=None, hi=None)) is None