package storage

import (
	"testing"
	"time"
)

func TestReplayCacheCheckAndMarkLivesOnlyInMemory(t *testing.T) {
	cache := NewReplayCache(40 * time.Millisecond)

	replayed, err := cache.CheckAndMark("token-hash")
	if err != nil {
		t.Fatalf("CheckAndMark returned error: %v", err)
	}
	if replayed {
		t.Fatal("first token use should not be marked as replayed")
	}

	replayed, err = cache.CheckAndMark("token-hash")
	if err != nil {
		t.Fatalf("CheckAndMark returned error on second use: %v", err)
	}
	if !replayed {
		t.Fatal("second token use should be marked as replayed")
	}

	time.Sleep(60 * time.Millisecond)

	replayed, err = cache.CheckAndMark("token-hash")
	if err != nil {
		t.Fatalf("CheckAndMark returned error after ttl expiry: %v", err)
	}
	if replayed {
		t.Fatal("expired token entry should be forgotten from memory")
	}
}
