from datetime import datetime, timedelta from types import SimpleNamespace import pytest from httpx import ASGITransport, AsyncClient from common.utils.kma_time import KST from router.router import app from router.v1.watch.watch import timeline REGION = "1141000000" SUN = datetime(2026, 10, 6, 18, 0, tzinfo=KST) def _sunset(score: int) -> SimpleNamespace: return SimpleNamespace(score=score, best_times=[ {"at": SUN.isoformat(), "kind": "sun", "label": "해 져요"}, {"at": (SUN + timedelta(minutes=15)).isoformat(), "kind": "peak", "label": "가장 붉을 때"}, {"at": (SUN + timedelta(minutes=30)).isoformat(), "kind": "twilight", "label": "보랏빛 하늘"}, ]) def test_timeline_switches_to_sunset_two_hours_before_and_back_after_twilight(): now = SUN.replace(hour=12) assert [(t.start_at.hour, t.start_at.minute, t.kind) for t in timeline(now, _sunset(80), True)] == [ (12, 0, "gak"), (16, 0, "sunset"), (18, 30, "gak"), ] def test_timeline_starts_with_sunset_inside_window(): now = SUN.replace(hour=17) assert [t.kind for t in timeline(now, _sunset(80), True)] == ["sunset", "gak"] def test_timeline_skips_weak_tomorrow_or_finished_sunset(): noon, late = SUN.replace(hour=12), SUN.replace(hour=19) assert [t.kind for t in timeline(noon, _sunset(59), True)] == ["gak"] assert [t.kind for t in timeline(noon, _sunset(80), False)] == ["gak"] assert [t.kind for t in timeline(late, _sunset(80), True)] == ["gak"] assert [t.kind for t in timeline(noon, None, True)] == ["gak"] @pytest.mark.anyio async def test_watch_summary_bundles_now_top_picks_sunset(offline): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/watch/summary", params={"region": REGION, "pick": "laundry, nope,running,carwash,bike"})).json() assert body["result"]["success"] is True assert body["region_code"] == REGION and body["mode"] in ("morning", "day", "evening", "night") assert body["refresh_at"] > body["issued_at"] assert body["now"]["rain_2h"] is False and body["now"]["temp_c"] == 20.0 assert len(body["top"]) == 3 assert [p["code"] for p in body["picks"]] == ["laundry", "running"] assert body["sunset"]["best_at"] > body["sunset"]["sunset_at"] assert all(s["at"] > body["issued_at"] for s in body["slots"]) assert body["timeline"][0]["start_at"] == body["issued_at"] @pytest.mark.anyio async def test_watch_summary_unknown_region_fails(offline): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/watch/summary", params={"region": "0000000000"})).json() assert body["result"]["success"] is False