tiny-counter 0.1.0

Track event counts across time windows with fixed memory and fast queries
Documentation
//! Tests for .cooldown() rate limiting constraint

use chrono::Duration;
use std::sync::Arc;
use tiny_counter::{EventStore, TestClock};

#[test]
fn cooldown_enforces_wait_period() {
    let store = EventStore::new();

    // First call should succeed
    let result = store
        .limit()
        .cooldown("action", Duration::hours(1))
        .check_and_record("action");
    assert!(result.is_ok());

    // Immediate second call should fail (still in cooldown)
    let result = store
        .limit()
        .cooldown("action", Duration::hours(1))
        .check_and_record("action");
    assert!(result.is_err());
}

#[test]
fn cooldown_allows_after_duration() {
    let fixed_time = chrono::Utc::now();
    let clock = TestClock::build_for_testing_at(fixed_time);

    let store = EventStore::builder()
        .with_clock(Arc::new(clock.clone()))
        .build()
        .unwrap();

    store.record("action");

    // Advance clock past cooldown
    clock.advance(Duration::hours(2));

    // Should now be allowed
    let result = store
        .limit()
        .cooldown("action", Duration::hours(1))
        .check_and_record("action");
    assert!(result.is_ok());
}