"""이익 청산이 유효한 ban 을 해제하지 않는다 (loss_tracker 회귀).""" from datetime import datetime, timedelta, timezone import pytest from app.core.config import settings from app.services.loss_tracker import ConsecutiveLossTracker def _banned(): t = ConsecutiveLossTracker( ban_count=settings.CONSECUTIVE_LOSS_BAN_COUNT, ban_calendar_days=settings.CONSECUTIVE_LOSS_BAN_DAYS, ) for _ in range(settings.CONSECUTIVE_LOSS_BAN_COUNT): t.record_trade_result("105560", True) assert t.is_banned("105560") return t def test_profit_does_not_lift_active_ban(): t = _banned() before = t._tracker["105560"]["ban_until"] t.record_trade_result("105560", False) assert t.is_banned("105560") assert t._tracker["105560"]["ban_until"] == before @pytest.mark.asyncio async def test_safe_path_profit_keeps_ban(): t = _banned() await t.record_trade_result_safe("105560", False) assert await t.is_banned_safe("105560") def test_profit_after_expiry_resets(): t = _banned() t._tracker["105560"]["ban_until"] = datetime.now(timezone.utc) - timedelta(seconds=1) t.record_trade_result("105560", False) assert t._tracker["105560"]["ban_until"] is None assert t._tracker["105560"]["consecutive_losses"] == 0 def test_profit_without_ban_resets_counter(): t = ConsecutiveLossTracker(ban_count=3, ban_calendar_days=42) t.record_trade_result("X", True) t.record_trade_result("X", False) assert t._tracker["X"]["consecutive_losses"] == 0