package account

import (
	"testing"
	"time"
)

// TestSendLimiter checks the per-recipient cooldown and the rolling-hour cap, and
// that recipients are throttled independently.
func TestSendLimiter(t *testing.T) {
	base := time.Now()
	now := base
	l := NewSendLimiter(time.Minute, 3)
	l.now = func() time.Time { return now }

	if !l.Allow("a") {
		t.Fatal("send 1 should be allowed")
	}
	if l.Allow("a") {
		t.Fatal("immediate resend must be blocked by the cooldown")
	}
	if !l.Allow("b") {
		t.Fatal("a different recipient is independent")
	}

	now = base.Add(time.Minute)
	if !l.Allow("a") {
		t.Fatal("send 2 after the cooldown should be allowed")
	}
	now = base.Add(2 * time.Minute)
	if !l.Allow("a") {
		t.Fatal("send 3 should be allowed")
	}
	now = base.Add(3 * time.Minute)
	if l.Allow("a") {
		t.Fatal("send 4 within the hour must be blocked by the cap")
	}

	now = base.Add(time.Hour + time.Minute)
	if !l.Allow("a") {
		t.Fatal("after the rolling hour the cap resets")
	}
}
