from datetime import date, datetime import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy import text from common.database.db_session_manager import DB_SESSION_MNG from common.enums import PostType from common.utils.kma_time import KST from crud.feed_crud import FeedCRUD from router.router import app from services.bot_comment import _COPY, BOT_NAMES, _moon_key, bot_lines, fill_bot_comments from services.feed_service import LINEUP DAY = date(2001, 1, 2) def test_every_post_type_has_lines_from_known_characters(): for t in {x[1] for x in LINEUP} | {PostType.CARD}: assert bot_lines(t, DAY, 1), t for sets in _COPY.values(): for s in sets: assert 1 <= len(s) <= 2 and len({bot for bot, _ in s}) == len(s) assert all(bot in BOT_NAMES and 0 < len(body) <= 60 for bot, body in s) def test_lines_are_one_or_two_and_same_for_same_post(): counts = {len(bot_lines(PostType.GAK, DAY, post_id)) for post_id in range(40)} assert counts == {1, 2} assert bot_lines(PostType.GAK, DAY, 7) == bot_lines(PostType.GAK, DAY, 7) def test_moon_lines_follow_full_and_new_moon_nights(): assert _moon_key(date(2025, 10, 7)) == "MOON_FULL" # 보름 10.07 12:47 assert _moon_key(date(2024, 4, 8)) == "MOON_NEW" # 삭 04.09 03:21, 그 전날 밤 assert _moon_key(date(2025, 10, 4)) == "MOON" full = {body for s in _COPY["MOON_FULL"] for _, body in s} assert bot_lines(PostType.MOON, date(2025, 10, 7), 3)[0][1] in full @pytest.fixture async def day_posts(): async def wipe(): await DB_SESSION_MNG.execute_lambda_run([ lambda s: s.execute(text(f"DELETE FROM comment WHERE post_id IN (SELECT post_id FROM post WHERE feed_date = '{DAY}')")), lambda s: s.execute(text(f"DELETE FROM post WHERE feed_date = '{DAY}'")), ]) await wipe() rows = [{"feed_date": DAY, "position": i, "scope": "00", "post_type": t.value, "ref_type": 1, "published_at": datetime(2001, 1, 2, 3, tzinfo=KST)} for i, t in enumerate([PostType.WEATHER, PostType.GAK, PostType.MOON, PostType.SUNSET], 1)] await DB_SESSION_MNG.execute_lambda_run([lambda s: FeedCRUD().insert_day(s, rows)]) yield await wipe() async def _counts(): sql = f"""SELECT p.post_id, p.comment_count, count(c.comment_id) FROM post p LEFT JOIN comment c ON c.post_id = p.post_id AND c.bot IS NOT NULL WHERE p.feed_date = '{DAY}' GROUP BY p.post_id, p.comment_count ORDER BY p.post_id""" _, rows = await DB_SESSION_MNG.execute_lambda(lambda s: s.execute(text(sql))) return [tuple(r) for r in rows.all()] @pytest.mark.anyio async def test_fill_adds_one_or_two_comments_once_and_counts_them(day_posts): await fill_bot_comments(DAY, FeedCRUD()) first = await _counts() assert len(first) == 4 and all(1 <= n <= 2 and count == n for _, count, n in first) await fill_bot_comments(DAY, FeedCRUD()) assert await _counts() == first post_id = first[0][0] async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: comments = (await c.get(f"/v1/reaction/post/{post_id}/comment/list")).json()["comments"] assert comments and all(x["official"] and x["nickname"] in BOT_NAMES.values() and not x["mine"] for x in comments) @pytest.mark.anyio async def test_feed_build_puts_character_comments_on_new_posts(offline): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: feed = (await c.get("/v1/feed", params={"region": "1144000000"})).json() assert feed["hero_post"]["comment_count"] >= 1 assert all(p["comment_count"] >= 1 for p in feed["posts"])