from dataclasses import replace from datetime import date from services.calc.index_calc import Context, evaluate from scripts.gen_weather_score_table import straighten_top from services.index_service import EARLY, WEATHER_SCORE_DEF, WEATHER_SCORE_TABLE, calibrated_weather, weather_slot_scores from tests.test_index_calc import _defn, _h PERFECT = Context(day=date(2026, 10, 2), is_holiday=False, pm25=8, air_label="좋음", moon=0, sunset_score=None, wind_warning=False, lookahead_rain_prob=0) SLOTS = ("morning", "day", "evening", "night") def test_weather_score_has_no_worst_factor_pull(): """해 난 비율만 0점(밤)이어도 나머지가 만점이면 비중대로 85점. 가장 낮은 요소로 25% 더 깎지 않는다.""" hours = [_h(h, feels=21.0, prob=0, wind=0.8, hum=50, sunshine_ratio=0.0) for h in range(19, 23)] r = evaluate(WEATHER_SCORE_DEF, [hours], PERFECT) assert r.factors["sun"] == 0 assert r.score == 85 def test_gak_scores_keep_worst_factor_pull(): """각 점수는 그대로: 한 요소가 0점이면 섞은 점수에서 25% 깎인다.""" defn = _defn(zero_rules=[]) r = evaluate(defn, [[_h(19, wind=12.0)]], PERFECT) assert r.factors["wind"] == 0 plain = round(sum(r.factors[a] * getattr(defn, f"weight_{a}", 0) for a in r.factors) / 100) assert r.score == round(plain * 0.75) def test_table_covers_every_slot_and_never_goes_down(): for slot in SLOTS: table = WEATHER_SCORE_TABLE[slot] assert len(table) == 101 assert all(0 <= v <= 100 for v in table) assert all(a <= b for a, b in zip(table, table[1:])) assert table[100] == 100 def test_table_makes_90_rare_and_centers_60(): """구간마다 원래 점수 중앙값 근처가 60점 근처로, 90점 이상은 원래 점수 상위 몇 칸뿐.""" for slot in SLOTS: table = WEATHER_SCORE_TABLE[slot] assert any(55 <= v <= 65 for v in table) assert sum(v >= 90 for v in table) < sum(v < 90 for v in table) def test_calibrated_uses_slot_table_and_last_night_for_early(): assert calibrated_weather("day", 70) == WEATHER_SCORE_TABLE["day"][70] assert calibrated_weather(EARLY, 70) == WEATHER_SCORE_TABLE["night"][70] def test_weather_slot_scores_keeps_results_with_calibrated_scores(): """점수 근거(factors)는 그대로 두고 점수만 표시 점수로 바꾼 결과를 돌려준다.""" results = {k: replace(evaluate(WEATHER_SCORE_DEF, [[_h(12)]], PERFECT), score=s) for k, s in {"morning": 40, "night": 60, EARLY: 60}.items()} shown = weather_slot_scores(results) assert {k: r.score for k, r in shown.items()} == {"morning": WEATHER_SCORE_TABLE["morning"][40], "night": WEATHER_SCORE_TABLE["night"][60], EARLY: WEATHER_SCORE_TABLE["night"][60]} assert all(shown[k].factors == results[k].factors for k in results) assert results["morning"].score == 40 # 원래 결과는 바뀌지 않는다 def test_straighten_top_makes_100_only_at_the_top_and_keeps_the_rest(): clamped = list(range(0, 80)) + [85, 90, 95] + [100] * 18 # 맨 위가 100으로 잘린 표 (원래 80~82 → 85·90·95, 83 이상은 모두 100) fixed = straighten_top(clamped, top=100) assert fixed[:80] == clamped[:80] # 아래쪽은 그대로 assert fixed[100] == 100 and max(fixed[:100]) < 100 above = fixed[80:] assert all(a < b for a, b in zip(above, above[1:])) or above[-1] == 100 # 위쪽은 구별되는 점수로 오른다 assert straighten_top(clamped, top=90)[90] == 100 # 완벽한 점수가 100보다 낮은 구간(밤)은 거기서 100 def test_weather_table_gives_100_only_at_the_best_reachable_original_score(): assert WEATHER_SCORE_TABLE["day"][99] < 100 == WEATHER_SCORE_TABLE["day"][100] night_top = 100 - WEATHER_SCORE_DEF.weight_sun # 밤은 해 난 비율이 늘 0점이라 원래 점수가 85까지만 나온다 assert WEATHER_SCORE_TABLE["night"][night_top - 1] < 100 == WEATHER_SCORE_TABLE["night"][night_top]