from time import time


def test_update_guard_detects_duplicate_key():
    from services import telegram_update_guard as guard

    # 使用可變動 key，避免被歷史資料干擾
    unique = f"unit-test-{int(time() * 1000)}"

    # 清掉本機快取，避免測試順序影響
    guard._seen_update_ids.clear()
    guard._seen_update_id_set.clear()

    assert guard.is_duplicate_update(unique, namespace="pytest") is False
    assert guard.is_duplicate_update(unique, namespace="pytest") is True


def test_update_guard_separates_namespace():
    from services import telegram_update_guard as guard

    guard._seen_update_ids.clear()
    guard._seen_update_id_set.clear()

    event_id = f"namespace-check-{int(time() * 1000)}"
    assert guard.is_duplicate_update(event_id, namespace="a") is False
    assert guard.is_duplicate_update(event_id, namespace="b") is False
