use tiny_counter::{EventStore, Schedule};
#[test]
fn during_business_hours() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let business_time = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 17) .unwrap()
.and_hms_opt(10, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(business_time);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let schedule = Schedule::hours(9, 17).unwrap();
let result = store.limit().during(schedule).check("action");
assert!(result.is_ok());
}
#[test]
fn outside_business_hours_rejects() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let after_hours = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 17)
.unwrap()
.and_hms_opt(20, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(after_hours);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let schedule = Schedule::hours(9, 17).unwrap();
let result = store.limit().during(schedule).check("action");
assert!(result.is_err());
}
#[test]
fn outside_of_schedule_allows_after_hours() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let after_hours = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 17)
.unwrap()
.and_hms_opt(20, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(after_hours);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let schedule = Schedule::hours(9, 17).unwrap();
let result = store.limit().outside_of(schedule).check("maintenance");
assert!(result.is_ok());
}
#[test]
fn during_weekdays() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let monday = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 17)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(monday);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let result = store
.limit()
.during(Schedule::weekdays())
.check("work_task");
assert!(result.is_ok());
}
#[test]
fn during_weekends() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let saturday = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 15)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(saturday);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let result = store
.limit()
.during(Schedule::weekends())
.check("leisure_activity");
assert!(result.is_ok());
}
#[test]
fn weekdays_rejects_weekend() {
use chrono::{NaiveDate, TimeZone, Utc};
use std::sync::Arc;
use tiny_counter::TestClock;
let sunday = Utc.from_utc_datetime(
&NaiveDate::from_ymd_opt(2024, 6, 16)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap(),
);
let clock = TestClock::build_for_testing_at(sunday);
let store = EventStore::builder()
.with_clock(Arc::new(clock))
.build()
.unwrap();
let result = store
.limit()
.during(Schedule::weekdays())
.check("work_task");
assert!(result.is_err());
}
#[test]
fn invalid_schedule_hours_rejected() {
let result = Schedule::hours(9, 25);
assert!(result.is_err());
let result = Schedule::hours(17, 9);
assert!(result.is_err());
let result = Schedule::hours(9, 9);
assert!(result.is_err());
}