tiny-counter 0.1.0

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

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

#[test]
fn within_requires_recent_prerequisite() {
    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();

    // Record prerequisite
    store.record("login");

    // Action within 1 hour should succeed
    let result = store
        .limit()
        .within("login", Duration::hours(1))
        .check("action");
    assert!(result.is_ok());

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

    // Action should now fail (prerequisite too old)
    let result = store
        .limit()
        .within("login", Duration::hours(1))
        .check("action");
    assert!(result.is_err());
}