from datetime import datetime
import pytest
from httpx import ASGITransport, AsyncClient
from common.utils.kma_time import KST
from router.router import app
from router.v1.feed.protocol import Bands, Hero, HeroStat, Res_Feed, TopIndex
from services.seo_service import SeoService, fill, today_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 test_today_line_has_temp_status_slot_firsts_and_sunset():
line = today_line(_feed(), datetime(2026, 10, 14, 17, 52, tzinfo=KST))
assert line == "지금 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 "오늘 서울 중구 날씨, 뭐 할까?
지금 20°" in page.text
assert "해 지는 시각 오후" in page.text and "
- " in page.text
assert missing.status_code == 404