package sendguard

import (
	"testing"
)

func TestRateLimiter_blocksBurst(t *testing.T) {
	lim := NewRateLimiter(60, 2)
	if err := lim.Allow("user-1"); err != nil {
		t.Fatalf("first: %v", err)
	}
	if err := lim.Allow("user-1"); err != nil {
		t.Fatalf("second: %v", err)
	}
	if err := lim.Allow("user-1"); err == nil {
		t.Fatal("expected rate limit on third immediate request")
	}
}

func TestRateLimiter_perUser(t *testing.T) {
	lim := NewRateLimiter(60, 1)
	if err := lim.Allow("a"); err != nil {
		t.Fatalf("user a: %v", err)
	}
	if err := lim.Allow("b"); err != nil {
		t.Fatalf("user b should have separate bucket: %v", err)
	}
}
