"""Tests for SignalDedup."""
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo

from ai_trade.rate_limit import SignalDedup

KST = ZoneInfo("Asia/Seoul")


def test_is_recent_returns_false_for_new_ticker_action(tmp_dedup_db):
    dedup = SignalDedup(tmp_dedup_db)
    assert dedup.is_recent("005930", "buy") is False


def test_is_recent_returns_true_within_24h(tmp_dedup_db):
    dedup = SignalDedup(tmp_dedup_db)
    dedup.record("005930", "buy", confidence=0.82)
    assert dedup.is_recent("005930", "buy") is True


def test_is_recent_returns_false_after_24h(tmp_dedup_db, monkeypatch):
    dedup = SignalDedup(tmp_dedup_db)
    # Record with a timestamp 25 hours ago
    now = datetime.now(KST)
    fake_now = now - timedelta(hours=25)
    monkeypatch.setattr(
        "ai_trade.rate_limit._now_iso", lambda: fake_now.isoformat()
    )
    dedup.record("005930", "buy", confidence=0.82)
    # Reset to real now for is_recent check
    monkeypatch.setattr(
        "ai_trade.rate_limit._now_iso", lambda: now.isoformat()
    )
    assert dedup.is_recent("005930", "buy", within_hours=24) is False
