//! The cadence and the window, as arithmetic. use chrono::{DateTime, Duration, Utc}; use super::*; /// An explicit instant, never the wall clock. const NOW: &str = "2026-09-18T09:00:00Z"; fn at(offset: Duration) -> DateTime { now() + offset } fn now() -> DateTime { DateTime::parse_from_rfc3339(NOW) .expect("a literal instant") .with_timezone(&Utc) } #[test] fn the_two_numbers_are_the_ones_the_design_names() { assert_eq!(SAMPLE_INTERVAL_MINUTES, 15); assert_eq!(RETENTION_DAYS, 30); } #[test] fn a_subject_that_has_never_been_sampled_is_always_due() { assert!(should_sample(None, now())); } #[test] fn a_reading_younger_than_the_interval_is_not_due() { for minutes in [0, 1, 7, 14] { assert!( !should_sample(Some(at(Duration::minutes(-minutes))), now()), "a reading {minutes} minutes old is inside the interval" ); } } #[test] fn a_reading_exactly_one_interval_old_is_due() { // At the boundary and not past it: the helper wakes on a timer, and a rule // that needed strictly more than the interval would skip every other tick. assert!(should_sample( Some(at(Duration::minutes(-SAMPLE_INTERVAL_MINUTES))), now() )); } #[test] fn a_reading_older_than_the_interval_is_due() { for minutes in [16, 60, 60 * 24] { assert!(should_sample( Some(at(Duration::minutes(-minutes))), now() )); } } #[test] fn a_reading_stamped_in_the_future_is_not_due() { // Two Macs' clocks disagree by seconds. Treating a future stamp as overdue // would have them resample each other's rows for as long as the skew lasts. assert!(!should_sample(Some(at(Duration::minutes(1))), now())); assert!(!should_sample(Some(at(Duration::days(1))), now())); } #[test] fn the_cutoff_is_thirty_days_behind_now() { assert_eq!(retention_cutoff(now()), at(Duration::days(-RETENTION_DAYS))); } #[test] fn a_sample_exactly_on_the_boundary_survives_its_last_tick() { // The sweep deletes `sampled_at < cutoff`, so equality is kept. let cutoff = retention_cutoff(now()); assert!(!(cutoff < cutoff)); assert!(at(Duration::days(-RETENTION_DAYS) - Duration::milliseconds(1)) < cutoff); assert!(!(at(Duration::days(-RETENTION_DAYS) + Duration::milliseconds(1)) < cutoff)); } #[test] fn a_full_window_is_a_table_nothing_has_to_page() { assert_eq!(samples_per_subject(), 2_880); // Four Macs and four accounts, which is the household the design sizes for. assert!(samples_per_subject() * 8 < 25_000); } // ── The strip ─────────────────────────────────────────────────────────────── fn reading(offset: Duration, value: f64) -> Reading { Reading { at: at(offset), value, } } #[test] fn a_strip_is_always_the_same_width() { // Nothing reported at all, and a full window of readings, draw the same // number of columns: a strip that narrowed when a Mac was asleep would be a // chart whose axis moved. assert_eq!(thin(&[], now()).len(), STRIP_POINTS); let full: Vec = (0..samples_per_subject()) .map(|index| { reading( Duration::minutes(-index * SAMPLE_INTERVAL_MINUTES), index as f64, ) }) .collect(); assert_eq!(thin(&full, now()).len(), STRIP_POINTS); } #[test] fn a_bucket_nobody_reported_in_is_a_gap_and_never_a_zero() { let strip = thin(&[reading(Duration::minutes(-1), 4.0)], now()); assert_eq!(strip[STRIP_POINTS - 1].value, Some(4.0)); assert_eq!(strip[STRIP_POINTS - 1].samples, 1); // Every other column said nothing, and "nothing was reported" is not a // reading of zero. assert!(strip[..STRIP_POINTS - 1] .iter() .all(|point| point.value.is_none() && point.samples == 0)); } #[test] fn a_bucket_is_the_mean_of_its_readings_and_never_the_peak() { // Three readings inside the newest bucket, one of them a spike. A maximum // would draw seven and a half hours of load out of one spike. let strip = thin( &[ reading(Duration::minutes(-1), 0.0), reading(Duration::minutes(-2), 0.0), reading(Duration::minutes(-3), 9.0), ], now(), ); assert_eq!(strip[STRIP_POINTS - 1].value, Some(3.0)); assert_eq!(strip[STRIP_POINTS - 1].samples, 3); } #[test] fn a_reading_past_the_window_is_dropped_rather_than_clamped() { // The sweep is about to delete it, and a stale reading pushed onto the // first column would be a line that moves when nothing happened. let strip = thin( &[ reading(Duration::days(-RETENTION_DAYS) - Duration::hours(1), 7.0), reading(Duration::hours(1), 8.0), ], now(), ); assert!(strip.iter().all(|point| point.value.is_none())); } #[test] fn the_columns_run_oldest_first_and_start_at_the_cutoff() { let strip = thin(&[], now()); assert_eq!(strip[0].at, retention_cutoff(now())); for pair in strip.windows(2) { assert!(pair[0].at < pair[1].at, "the strip reads left to right"); } // Ninety-six columns over thirty days is one per seven and a half hours. assert_eq!( strip[1].at - strip[0].at, Duration::minutes(RETENTION_DAYS * 24 * 60 / STRIP_POINTS as i64) ); } #[test] fn a_reading_taken_this_instant_lands_in_the_newest_column() { // The last bucket is CLOSED at `now`: there is no hundred-and-first. let strip = thin(&[reading(Duration::zero(), 5.0)], now()); assert_eq!(strip[STRIP_POINTS - 1].value, Some(5.0)); } #[test] fn a_reading_exactly_on_the_cutoff_lands_in_the_oldest_column() { let strip = thin(&[reading(Duration::days(-RETENTION_DAYS), 2.0)], now()); assert_eq!(strip[0].value, Some(2.0)); }