"""Tests for JTI replay cache."""

import time

import pytest

from ect import new_jti_cache


def test_jti_cache_seen_and_add():
    cache = new_jti_cache(10, 60)
    assert cache.seen("jti-1") is False
    cache.add("jti-1")
    assert cache.seen("jti-1") is True
    assert cache.seen("jti-2") is False
    cache.add("jti-2")
    assert cache.seen("jti-2") is True


def test_jti_cache_expiry():
    cache = new_jti_cache(10, 1)  # 1 second TTL
    cache.add("jti-1")
    assert cache.seen("jti-1") is True
    time.sleep(1.1)
    assert cache.seen("jti-1") is False


def test_jti_cache_max_size_eviction():
    cache = new_jti_cache(2, 60)
    cache.add("jti-1")
    cache.add("jti-2")
    cache.add("jti-3")
    assert cache.seen("jti-3") is True


def test_jti_cache_add_when_already_present():
    cache = new_jti_cache(2, 60)
    cache.add("jti-1")
    cache.add("jti-1")
    assert cache.seen("jti-1") is True
