open_new_round_if_needed now withholds opening the next round until ROUND_COOLDOWN_SECONDS (default 30) have passed since the previous round's closed_at, returning None in that window instead of a Round. Without this, the next round opened within one scheduler tick (~5s) of the previous payout confirming — not enough time for a player to notice the round they were in actually resolved. Callers updated: the scheduler treats None as "nothing to do this tick", and place_bet raises a "try again shortly" BetError instead of crashing on a None round. Not in the original flowchart — a deliberate UX addition on top of it, documented as such in CLAUDE.md. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
|
|
from app.config import settings
|
|
from app.db.base import Base
|
|
from app.db.models import Round
|
|
from app.rounds.service import get_active_round, open_new_round_if_needed
|
|
|
|
|
|
@pytest.fixture
|
|
async def session_factory():
|
|
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield async_sessionmaker(engine, expire_on_commit=False)
|
|
await engine.dispose()
|
|
|
|
|
|
async def test_opens_a_round_when_none_exists(session_factory):
|
|
async with session_factory() as session:
|
|
round_ = await open_new_round_if_needed(session)
|
|
await session.commit()
|
|
assert round_.status == "open"
|
|
|
|
|
|
async def test_reuses_existing_open_round(session_factory):
|
|
async with session_factory() as session:
|
|
first = await open_new_round_if_needed(session)
|
|
await session.commit()
|
|
first_id = first.id
|
|
|
|
async with session_factory() as session:
|
|
second = await open_new_round_if_needed(session)
|
|
assert second.id == first_id
|
|
|
|
|
|
@pytest.mark.parametrize("status", ["closing", "drawing", "paying_out"])
|
|
async def test_does_not_open_new_round_while_previous_is_in_progress(session_factory, status):
|
|
async with session_factory() as session:
|
|
session.add(Round(status=status))
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
active = await get_active_round(session)
|
|
assert active is not None
|
|
assert active.status == status
|
|
# open_new_round_if_needed must return the in-progress round, not open a new one
|
|
returned = await open_new_round_if_needed(session)
|
|
assert returned.status == status
|
|
|
|
|
|
async def test_opens_new_round_after_previous_is_closed(session_factory):
|
|
async with session_factory() as session:
|
|
session.add(Round(status="closed"))
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
round_ = await open_new_round_if_needed(session)
|
|
assert round_.status == "open"
|
|
|
|
|
|
async def test_withholds_new_round_during_cooldown(session_factory):
|
|
async with session_factory() as session:
|
|
session.add(Round(status="closed", closed_at=datetime.now(timezone.utc)))
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
round_ = await open_new_round_if_needed(session)
|
|
assert round_ is None
|
|
|
|
|
|
async def test_opens_new_round_once_cooldown_elapses(session_factory):
|
|
stale_close = datetime.now(timezone.utc) - timedelta(seconds=settings.round_cooldown_seconds + 1)
|
|
async with session_factory() as session:
|
|
session.add(Round(status="closed", closed_at=stale_close))
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
round_ = await open_new_round_if_needed(session)
|
|
assert round_.status == "open"
|