from datetime import datetime
import pytest
from httpx import ASGITransport, AsyncClient
from common.utils.kma_time import KST
from common.database.model.models import WeatherHourly
from router.router import app
from router.v1.feed.protocol import Bands, Hero, HeroStat, Res_Feed, TopIndex
from services.seo_service import SeoService, day_summary, fill, today_line, tomorrow_line
REGION = "1114000000"
TEMPLATE = (
'
'
f'\n 서울 중구 오늘 날씨, 뭐 할까?
소개 글
'
)
def _top(name: str, score: int, ranks: dict[str, int]) -> TopIndex:
return TopIndex(index_id=1, code="x", name=name, icon="x", score=score, rank=1, slot_ranks=ranks)
def _feed() -> Res_Feed:
stories = [_top("러닝각", 80, {"morning": 1, "day": 2}), _top("빨래각", 77, {"day": 1}), _top("별보기각", 70, {"night": 1})]
hero = Hero(now_at=datetime(2026, 10, 14, 9), sky_mode="morning", temp_c=13.4,
status=[HeroStat(icon="m", label="습도", value="보통", tone="normal"), HeroStat(icon="m", label="미세먼지", value="좋음", tone="normal")])
return Res_Feed(hero=hero, stories=stories, bands=Bands(top5=stories[:2]))
def _hour(h: int, temp: float, sky: int = 1, rain: int = 1, prob: int = 0) -> WeatherHourly:
return WeatherHourly(forecast_at=datetime(2026, 10, 14, h, tzinfo=KST), temp_c=temp, sky=sky, rain_type=rain, rain_prob=prob)
def test_day_summary_min_max_prob_and_daytime_sky():
rows = [_hour(3, 8.6, sky=4), _hour(9, 12, sky=3), _hour(12, 19.5, sky=1, prob=10), _hour(15, 20.2, sky=1), _hour(21, 14)]
assert day_summary(rows) == "최저 9°·최고 20°, 강수확률 10%, 맑음."
assert day_summary([_hour(12, 15, rain=2, prob=70)]) == "최저 15°·최고 15°, 강수확률 70%, 비."
def test_today_line_has_day_now_slot_firsts_and_sunset():
line = today_line(_feed(), [_hour(12, 19.5, prob=10)], datetime(2026, 10, 14, 17, 52, tzinfo=KST))
assert line == "최저 20°·최고 20°, 강수확률 10%, 맑음. 지금 13°, 습도 보통·미세먼지 좋음. 아침은 러닝각, 낮은 빨래각, 밤은 별보기각. 해 지는 시각 오후 5시 52분."
def test_fill_puts_today_into_description_and_body():
html = fill(TEMPLATE, _feed(), [], "서울 중구", None)
assert html.count('content="지금 13°, 습도 보통·미세먼지 좋음.') == 2
assert "서울 중구 오늘 날씨, 뭐 할까?
지금 13°" in html
assert "
러닝각 80점빨래각 77점" in html
def test_fill_keeps_template_when_no_weather():
assert fill(TEMPLATE, Res_Feed(), [], "서울 중구", None) == TEMPLATE
@pytest.mark.anyio
async def test_today_page_fills_real_feed(offline, monkeypatch):
async def template(self, path):
return TEMPLATE if path == "today/seoul/jung" else None
monkeypatch.setattr(SeoService, "template", template)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
page = await client.get("/v1/seo/today/seoul/jung")
missing = await client.get("/v1/seo/today/seoul/nowhere")
assert page.status_code == 200 and "서울 중구 오늘 날씨, 뭐 할까?
최저 " in page.text and "지금 20°" in page.text
assert "해 지는 시각 오후" in page.text and "
- " in page.text
assert missing.status_code == 404
@pytest.mark.anyio
async def test_gu_page_of_city_has_its_own_address(offline, monkeypatch):
async def template(self, path):
return TEMPLATE if path == "today/gyeonggi/seongnam/bundang" else None
monkeypatch.setattr(SeoService, "template", template)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
page = await client.get("/v1/seo/today/gyeonggi/seongnam/bundang")
assert page.status_code == 200 and "지금 20°" in page.text
def test_tomorrow_line_has_day_and_sunset():
line = tomorrow_line([_hour(12, 19.5, prob=10)], datetime(2026, 10, 15, 17, 51, tzinfo=KST))
assert line == "최저 20°·최고 20°, 강수확률 10%, 맑음. 해 지는 시각 오후 5시 51분."
assert tomorrow_line([], datetime(2026, 10, 15, 17, 51, tzinfo=KST)) == ""
@pytest.mark.anyio
async def test_tomorrow_page_fills_tomorrow_forecast(offline, monkeypatch):
tmpl = TEMPLATE.replace("오늘 날씨", "내일 날씨")
async def template(self, path):
return tmpl if path == "tomorrow/gyeonggi/seongnam/bundang" else None
monkeypatch.setattr(SeoService, "template", template)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
page = await client.get("/v1/seo/tomorrow/gyeonggi/seongnam/bundang")
missing = await client.get("/v1/seo/tomorrow/seoul/nowhere")
assert page.status_code == 200 and "
서울 중구 내일 날씨, 뭐 할까?
최저 " in page.text
assert "지금 " not in page.text and "
" not in page.text
assert missing.status_code == 404