use chrono::{DateTime, Duration, Utc};
use serde::Serialize;
use super::ids::ProjectKey;
use super::period::Span;
use super::units::{Pct, PctPerHour, Usd, UsdPerPct};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
pub enum WindowKind {
FiveHour,
SevenDay,
SevenDayOpus,
SevenDaySonnet,
}
impl WindowKind {
pub const ALL: [WindowKind; 4] = [
WindowKind::FiveHour,
WindowKind::SevenDay,
WindowKind::SevenDayOpus,
WindowKind::SevenDaySonnet,
];
pub fn from_key(k: &str) -> Option<Self> {
Some(match k {
"five_hour" => WindowKind::FiveHour,
"seven_day" => WindowKind::SevenDay,
"seven_day_opus" => WindowKind::SevenDayOpus,
"seven_day_sonnet" => WindowKind::SevenDaySonnet,
_ => return None,
})
}
pub fn key(self) -> &'static str {
match self {
WindowKind::FiveHour => "five_hour",
WindowKind::SevenDay => "seven_day",
WindowKind::SevenDayOpus => "seven_day_opus",
WindowKind::SevenDaySonnet => "seven_day_sonnet",
}
}
pub fn label(self) -> &'static str {
match self {
WindowKind::FiveHour => "5-hour",
WindowKind::SevenDay => "weekly",
WindowKind::SevenDayOpus => "weekly opus",
WindowKind::SevenDaySonnet => "weekly sonnet",
}
}
pub fn length(self) -> Duration {
match self {
WindowKind::FiveHour => Duration::hours(5),
WindowKind::SevenDay | WindowKind::SevenDayOpus | WindowKind::SevenDaySonnet => {
Duration::days(7)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct Reading {
pub kind: WindowKind,
pub utilization: f64,
pub resets_at: DateTime<Utc>,
pub fetched_at: DateTime<Utc>,
}
impl Reading {
pub fn parse(
kind: WindowKind,
utilization: f64,
resets_at: DateTime<Utc>,
fetched_at: DateTime<Utc>,
) -> Option<Reading> {
let sane = utilization.is_finite()
&& utilization >= 0.0
&& resets_at <= fetched_at + kind.length() + Duration::hours(1);
sane.then_some(Reading {
kind,
utilization,
resets_at,
fetched_at,
})
}
pub fn window(&self) -> Span {
let end = self.window_end();
Span {
start: end - self.kind.length(),
end,
}
}
pub fn window_end(&self) -> DateTime<Utc> {
let ms = self.resets_at.timestamp_millis();
let minute = 60_000;
let rounded = (ms + minute / 2).div_euclid(minute) * minute;
DateTime::<Utc>::from_timestamp_millis(rounded).unwrap_or(self.resets_at)
}
pub fn same_window(&self, other: &Reading) -> bool {
self.kind == other.kind && self.window_end() == other.window_end()
}
pub fn is_reset(&self, now: DateTime<Utc>) -> bool {
now >= self.window_end()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub enum Level {
Ok,
Warn,
Bad,
Reached,
}
impl Level {
pub const WARN_AT: f64 = 60.0;
pub const BAD_AT: f64 = 85.0;
pub const REACHED_AT: f64 = 100.0;
pub fn of(pct: f64) -> Level {
if pct >= Self::REACHED_AT {
Level::Reached
} else if pct >= Self::BAD_AT {
Level::Bad
} else if pct >= Self::WARN_AT {
Level::Warn
} else {
Level::Ok
}
}
}
pub fn current_window(r: &Reading, now: DateTime<Utc>) -> Span {
let mut w = r.window();
while w.end <= now {
w = Span {
start: w.end,
end: w.end + r.kind.length(),
};
}
w
}
pub fn was_reset(before: Span, after: Span) -> bool {
after.start > before.start
}
pub const RESET_FALL: f64 = 2.0;
pub const SOURCES_DISAGREE_FOR: Duration = Duration::minutes(15);
pub fn effective_start(
history: &[Reading],
cur: &Reading,
) -> (DateTime<Utc>, Option<DateTime<Utc>>) {
let nominal = cur.window().start;
let mut hist: Vec<Reading> = history
.iter()
.filter(|h| h.same_window(cur) && h.fetched_at <= cur.fetched_at)
.copied()
.collect();
hist.push(*cur);
hist.sort_by(|a, b| {
a.fetched_at
.cmp(&b.fetched_at)
.then(b.utilization.total_cmp(&a.utilization))
});
hist.dedup_by_key(|h| h.fetched_at);
let mut start = nominal;
let mut reset = None;
for j in 1..hist.len() {
let (a, b) = (hist[j - 1], hist[j]);
if b.utilization + RESET_FALL >= a.utilization || a.fetched_at <= start {
continue;
}
let lagging = hist[..j - 1]
.iter()
.rev()
.take_while(|c| c.fetched_at >= a.fetched_at - SOURCES_DISAGREE_FOR)
.any(|c| c.utilization <= b.utilization + RESET_FALL);
let (back_up, low): (Vec<&Reading>, Vec<&Reading>) = hist[j + 1..]
.iter()
.take_while(|c| c.fetched_at <= b.fetched_at + SOURCES_DISAGREE_FOR)
.partition(|c| c.utilization + RESET_FALL >= a.utilization);
let undone = back_up.len() > low.len();
if !lagging && !undone {
start = a.fetched_at;
reset = Some(a.fetched_at);
}
}
(start, reset)
}
#[derive(Debug, Clone, PartialEq)]
pub struct ProjectSpend {
pub project: ProjectKey,
pub name: String,
pub cost: Usd,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Share {
pub project: ProjectKey,
pub name: String,
pub cost: Usd,
pub share: f64,
pub limit_points: Pct,
}
pub fn attribute(utilization: Pct, costs: &[ProjectSpend]) -> Vec<Share> {
let total: Usd = costs.iter().map(|p| p.cost.max(Usd::ZERO)).sum();
let mut out: Vec<Share> = costs
.iter()
.map(|p| {
let c = &p.cost;
let share = if total > Usd::ZERO {
c.max(Usd::ZERO) / total
} else {
0.0
};
Share {
project: p.project.clone(),
name: p.name.clone(),
cost: *c,
share,
limit_points: utilization * share,
}
})
.collect();
out.sort_by(|a, b| b.cost.total_cmp(&a.cost));
out
}
pub fn usd_per_point(utilization: Pct, cost_until_reading: Usd) -> Option<UsdPerPct> {
UsdPerPct::measured(cost_until_reading, utilization)
}
pub fn calibrate(samples: &[UsdPerPct]) -> Option<UsdPerPct> {
let mut v: Vec<f64> = samples
.iter()
.filter(|x| x.is_usable())
.map(|x| x.get())
.collect();
v.sort_by(f64::total_cmp);
let n = v.len();
let median = match n {
0 => return None,
_ if n % 2 == 1 => v.get(n / 2).copied(),
_ => Some((v.get(n / 2 - 1)? + v.get(n / 2)?) / 2.0),
};
median.map(UsdPerPct::new)
}
pub fn estimate_now(reading: Pct, spent_since: Usd, usd_per_point: Option<UsdPerPct>) -> Pct {
match usd_per_point {
Some(r) if r.is_usable() => {
let room = (Pct::new(Level::REACHED_AT) - reading).max(Pct::ZERO);
reading + (spent_since / r).max(Pct::ZERO).min(room)
}
Some(_) | None => reading,
}
}
pub fn eta(current: Pct, pace: PctPerHour) -> Option<Duration> {
if Level::of(current.get()) == Level::Reached {
return Some(Duration::zero());
}
let hours = pace.hours_for(Pct::new(Level::REACHED_AT) - current)?;
let secs = (hours * 3600.0).round();
if !(0.0..1e12).contains(&secs) {
return None;
}
#[expect(
clippy::cast_possible_truncation,
reason = "a whole number of seconds in 0..1e12, checked above"
)]
let secs = secs as i64;
Duration::try_seconds(secs)
}
pub fn runs_out(
current: Pct,
pace: Option<PctPerHour>,
now: DateTime<Utc>,
window_end: DateTime<Utc>,
) -> Option<Duration> {
pace.and_then(|p| eta(current, p))
.filter(|d| now.checked_add_signed(*d).is_some_and(|t| t < window_end))
}
pub fn projection_at_reset(
current: Pct,
pace: Option<PctPerHour>,
now: DateTime<Utc>,
window_end: DateTime<Utc>,
) -> Option<Pct> {
pace.filter(|p| p.get() > 0.0).map(|p| {
let left = (window_end - now).num_seconds().max(0) as f64 / 3600.0;
current + p.over(left)
})
}
pub const BURN_MINUTES: i64 = 60;
pub const REFERENCE_MARGIN: f64 = 1.2;
const CALIBRATION_READINGS: usize = 12;
const CALIBRATION_STEP: f64 = 0.5;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WindowPlace {
pub window: Span,
pub manual_reset: Option<DateTime<Utc>>,
pub reset_passed: bool,
}
pub fn window_of(r: &Reading, history: &[Reading], now: DateTime<Utc>) -> WindowPlace {
let reset_passed = r.is_reset(now);
let mut window = current_window(r, now);
let mut manual_reset = None;
if !reset_passed {
let (start, reset) = effective_start(history, r);
window.start = start;
manual_reset = reset;
}
WindowPlace {
window,
manual_reset,
reset_passed,
}
}
pub fn counting_from(r: &Reading, place: &WindowPlace) -> (f64, DateTime<Utc>) {
if place.reset_passed {
(0.0, place.window.start)
} else {
(r.utilization, r.fetched_at)
}
}
pub fn calibration_points(history: &[Reading], r: &Reading) -> Vec<(Span, f64)> {
let mut history = history.to_vec();
if !history.iter().any(|h| h.fetched_at == r.fetched_at) {
history.push(*r);
}
let mut recent: Vec<Reading> = Vec::new();
for h in history.iter().rev() {
if recent
.last()
.is_none_or(|l: &Reading| (l.utilization - h.utilization).abs() >= CALIBRATION_STEP)
{
recent.push(*h);
}
if recent.len() >= CALIBRATION_READINGS {
break;
}
}
recent
.into_iter()
.map(|h| {
let (start, _) = effective_start(&history, &h);
(
Span {
start,
end: h.fetched_at,
},
h.utilization,
)
})
.collect()
}
pub fn own_ratio(points: &[(Pct, Usd)]) -> Option<UsdPerPct> {
let samples: Vec<UsdPerPct> = points
.iter()
.filter_map(|(u, c)| usd_per_point(*u, *c))
.collect();
calibrate(&samples)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PlanTier {
pub harness: crate::core::harness::Harness,
pub tier: String,
}
impl PlanTier {
pub fn of(account: &str, tier: impl Into<String>) -> PlanTier {
use crate::core::harness::Harness;
let harness = if account.starts_with("agy:") {
Harness::Agy
} else if account.starts_with("cdx:") {
Harness::Codex
} else {
Harness::Claude
};
PlanTier {
harness,
tier: tier.into(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Calibration {
pub account: String,
pub tier: PlanTier,
pub kind: WindowKind,
pub usd_per_point: UsdPerPct,
pub at: DateTime<Utc>,
}
pub fn pick_references(
c: &[Calibration],
now: DateTime<Utc>,
) -> std::collections::HashMap<(PlanTier, WindowKind), Calibration> {
let mut refs: std::collections::HashMap<(PlanTier, WindowKind), Calibration> =
std::collections::HashMap::new();
for x in c.iter().filter(|x| now - x.at <= x.kind.length()) {
let e = refs
.entry((x.tier.clone(), x.kind))
.or_insert_with(|| x.clone());
if x.usd_per_point > e.usd_per_point {
*e = x.clone();
}
}
refs
}
pub fn reference_wins(own: Option<UsdPerPct>, reference: UsdPerPct) -> bool {
own.is_none_or(|o| reference > o * REFERENCE_MARGIN)
}
pub fn curve_step(window: Span) -> Duration {
Duration::minutes(((window.end - window.start).num_minutes() / 168).max(1))
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowInputs {
pub reading: Reading,
pub place: WindowPlace,
pub own_usd_per_point: Option<UsdPerPct>,
pub spent_since: Usd,
pub cost_in_window: Usd,
pub cost_burn: Usd,
pub by_project: Vec<ProjectSpend>,
pub series: Vec<Usd>,
pub series_step: Duration,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WindowEstimate {
pub usd_per_point: Option<UsdPerPct>,
pub estimate_now: Pct,
pub visible_points: Option<Pct>,
pub outside_points: Option<Pct>,
pub burn_usd_per_hour: Usd,
pub points_per_hour: Option<PctPerHour>,
pub eta: Option<Duration>,
pub runs_out_at: Option<DateTime<Utc>>,
pub shares: Vec<Share>,
pub window_usd: Option<Usd>,
pub curve: Vec<Pct>,
}
pub fn window_estimate(
i: &WindowInputs,
reference: Option<UsdPerPct>,
now: DateTime<Utc>,
) -> WindowEstimate {
let usd_per_point = reference.or(i.own_usd_per_point);
let (base, _) = counting_from(&i.reading, &i.place);
let estimate_now = estimate_now(Pct::new(base), i.spent_since, usd_per_point);
let visible_points = usd_per_point.map(|u| (i.cost_in_window / u).min(estimate_now));
let outside_points = visible_points.map(|v| (estimate_now - v).max(Pct::ZERO));
let burn_usd_per_hour = i.cost_burn * 60.0 / BURN_MINUTES as f64;
let points_per_hour = usd_per_point.map(|u| PctPerHour::of_burn(burn_usd_per_hour, u));
let eta = runs_out(estimate_now, points_per_hour, now, i.place.window.end);
let shares = attribute(visible_points.unwrap_or(estimate_now), &i.by_project);
let total: Usd = i.series.iter().sum();
let scale = if total > Usd::ZERO {
estimate_now.get() / total.get()
} else {
0.0
};
let mut acc = Usd::ZERO;
let curve = i
.series
.iter()
.map(|c| {
acc += *c;
Pct::new(acc.get() * scale)
})
.collect();
WindowEstimate {
usd_per_point,
estimate_now,
visible_points,
outside_points,
burn_usd_per_hour,
points_per_hour,
eta,
runs_out_at: eta.map(|d| now + d),
shares,
window_usd: usd_per_point.map(|u| Pct::new(100.0) * u),
curve,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_reading_is_checked_where_it_comes_in() {
let at = ts("2026-11-20T12:00:00Z");
let reset = ts("2026-11-24T00:00:00Z");
assert!(Reading::parse(WindowKind::SevenDay, 42.0, reset, at).is_some());
assert!(Reading::parse(WindowKind::SevenDay, f64::NAN, reset, at).is_none());
assert!(Reading::parse(WindowKind::SevenDay, -1.0, reset, at).is_none());
let far = DateTime::from_timestamp(reset.timestamp() * 1000, 0)
.unwrap_or(reset + Duration::days(9999));
assert!(Reading::parse(WindowKind::SevenDay, 42.0, far, at).is_none());
assert!(Reading::parse(WindowKind::FiveHour, 104.0, at + Duration::hours(3), at).is_some());
}
#[test]
fn attribution_by_hand() {
let s = attribute(
Pct::new(77.0),
&[
spend("other", 70.0),
spend("shop", 810.0),
spend("blog", 120.0),
],
);
assert_eq!(s[0].name, "shop");
assert!((s[0].share - 0.81).abs() < 1e-12);
assert!((s[0].limit_points.get() - 62.37).abs() < 1e-9);
assert!((s[1].limit_points.get() - 9.24).abs() < 1e-9);
assert!((s[2].limit_points.get() - 5.39).abs() < 1e-9);
let total: f64 = s.iter().map(|x| x.limit_points.get()).sum();
assert!((total - 77.0).abs() < 1e-9);
}
#[test]
fn zero_spend_attributes_nothing() {
let s = attribute(Pct::new(10.0), &[spend("a", 0.0)]);
assert_eq!(s[0].limit_points, Pct::ZERO);
}
#[test]
fn calibration_and_estimate() {
let r = usd_per_point(Pct::new(50.0), Usd::new(1000.0)).unwrap();
assert_eq!(r, UsdPerPct::new(20.0));
assert_eq!(
estimate_now(Pct::new(50.0), Usd::new(100.0), Some(r)),
Pct::new(55.0)
);
let rates = |v: &[f64]| v.iter().map(|x| UsdPerPct::new(*x)).collect::<Vec<_>>();
assert_eq!(
calibrate(&rates(&[30.0, 10.0, 20.0])),
Some(UsdPerPct::new(20.0))
);
assert_eq!(calibrate(&rates(&[10.0, 20.0])), Some(UsdPerPct::new(15.0)));
assert_eq!(calibrate(&[]), None);
assert_eq!(usd_per_point(Pct::new(0.5), Usd::new(10.0)), None);
}
#[test]
fn little_visible_spend_never_makes_the_estimate_explode() {
let own = own_ratio(&[(Pct::new(40.0), Usd::new(0.004))]);
assert_eq!(own, None);
assert_eq!(
estimate_now(Pct::new(40.0), Usd::new(2.0), own),
Pct::new(40.0)
);
let tiny = Some(UsdPerPct::new(0.0001));
assert_eq!(
estimate_now(Pct::new(40.0), Usd::new(2.0), tiny),
Pct::new(100.0)
);
assert_eq!(
estimate_now(Pct::new(104.0), Usd::new(2.0), tiny),
Pct::new(104.0)
);
for spent in [0.0, 0.5, 3.0, 1e9] {
for r in [1e-12, 0.01, 5.0, 1e12] {
let e = estimate_now(Pct::new(30.0), Usd::new(spent), Some(UsdPerPct::new(r)));
assert!(e.get().is_finite() && e >= Pct::new(30.0) && e <= Pct::new(100.0));
}
}
}
#[test]
fn manual_reset_moves_the_window_start() {
let t = |h: i64| {
DateTime::parse_from_rfc3339("2026-11-20T00:00:00Z")
.unwrap()
.with_timezone(&Utc)
+ Duration::hours(h)
};
let r = |h: i64, u: f64| Reading {
kind: WindowKind::SevenDay,
utilization: u,
resets_at: t(100),
fetched_at: t(h),
};
let hist = [r(10, 40.0), r(20, 70.0), r(22, 3.0), r(30, 12.0)];
let (start, reset) = effective_start(&hist, &hist[3]);
assert_eq!(start, t(20), "the reading before the fall");
assert_eq!(reset, Some(t(20)));
assert_eq!(
effective_start(&hist, &hist[1]),
(t(100) - Duration::days(7), None)
);
let next = Reading {
kind: WindowKind::SevenDay,
utilization: 1.0,
resets_at: t(268),
fetched_at: t(101),
};
let (s2, r2) = effective_start(&[hist[3], next], &next);
assert_eq!((s2, r2), (t(100), None));
}
#[test]
fn eta_by_hand() {
assert_eq!(eta(pct(70.0), pace(3.0)), Some(Duration::hours(10)));
assert_eq!(eta(pct(100.0), pace(0.0)), Some(Duration::zero()));
assert_eq!(eta(pct(50.0), pace(0.0)), None);
for p in [f64::MIN_POSITIVE, 1e-8, 2e-8, 1e-6] {
assert!(eta(pct(50.0), pace(p)).is_none_or(|d| d > Duration::days(365)));
let (now, end) = (ts("2026-11-20T12:00:00Z"), ts("2026-11-21T00:00:00Z"));
assert_eq!(runs_out(pct(50.0), Some(pace(p)), now, end), None);
}
}
#[test]
fn levels_at_their_boundaries() {
assert_eq!(Level::of(0.0), Level::Ok);
assert_eq!(Level::of(59.99), Level::Ok);
assert_eq!(Level::of(60.0), Level::Warn);
assert_eq!(Level::of(84.99), Level::Warn);
assert_eq!(Level::of(85.0), Level::Bad);
assert_eq!(Level::of(99.99), Level::Bad);
assert_eq!(Level::of(100.0), Level::Reached);
assert_eq!(
Level::of(130.0),
Level::Reached,
"extra usage goes past 100"
);
assert!(Level::Reached > Level::Bad && Level::Bad > Level::Warn && Level::Warn > Level::Ok);
}
fn ts(s: &str) -> DateTime<Utc> {
DateTime::parse_from_rfc3339(s).unwrap().with_timezone(&Utc)
}
#[test]
fn current_window_rolls_forward() {
let r = Reading {
kind: WindowKind::SevenDay,
utilization: 40.0,
resets_at: ts("2026-11-23T00:00:00Z"),
fetched_at: ts("2026-11-20T00:00:00Z"),
};
let w = current_window(&r, ts("2026-11-22T12:00:00Z"));
assert_eq!(
(w.start, w.end),
(ts("2026-11-16T00:00:00Z"), ts("2026-11-23T00:00:00Z"))
);
let w = current_window(&r, ts("2026-11-23T00:00:00Z"));
assert_eq!(
(w.start, w.end),
(ts("2026-11-23T00:00:00Z"), ts("2026-11-30T00:00:00Z"))
);
let w = current_window(&r, ts("2026-12-01T09:00:00Z"));
assert_eq!(
(w.start, w.end),
(ts("2026-11-30T00:00:00Z"), ts("2026-12-07T00:00:00Z"))
);
let five = Reading {
kind: WindowKind::FiveHour,
resets_at: ts("2026-11-20T10:00:00Z"),
..r
};
let w = current_window(&five, ts("2026-11-20T16:30:00Z"));
assert_eq!(
(w.start, w.end),
(ts("2026-11-20T15:00:00Z"), ts("2026-11-20T20:00:00Z"))
);
}
#[test]
fn reset_is_a_start_moving_forward() {
let span = |a: &str, b: &str| Span {
start: ts(a),
end: ts(b),
};
let week = span("2026-11-16T00:00:00Z", "2026-11-23T00:00:00Z");
assert!(!was_reset(week, week));
assert!(was_reset(
week,
span("2026-11-23T00:00:00Z", "2026-11-30T00:00:00Z")
));
assert!(was_reset(
week,
span("2026-11-19T06:00:00Z", "2026-11-23T00:00:00Z")
));
}
#[test]
fn runs_out_and_projection_by_hand() {
let now = ts("2026-11-20T12:00:00Z");
let end = ts("2026-11-21T00:00:00Z"); assert_eq!(runs_out(pct(70.0), Some(pace(2.0)), now, end), None);
assert_eq!(
projection_at_reset(pct(70.0), Some(pace(2.0)), now, end),
Some(pct(94.0))
);
assert_eq!(
runs_out(pct(70.0), Some(pace(5.0)), now, end),
Some(Duration::hours(6))
);
assert_eq!(
projection_at_reset(pct(70.0), Some(pace(5.0)), now, end),
Some(pct(130.0))
);
assert_eq!(
projection_at_reset(pct(70.0), Some(pace(0.0)), now, end),
None
);
assert_eq!(projection_at_reset(pct(70.0), None, now, end), None);
assert_eq!(runs_out(pct(70.0), None, now, end), None);
assert_eq!(
runs_out(pct(100.0), Some(pace(0.0)), now, end),
Some(Duration::zero())
);
}
fn hour(h: i64) -> DateTime<Utc> {
ts("2026-11-20T00:00:00Z") + Duration::hours(h)
}
fn wk(h: i64, used: f64) -> Reading {
Reading {
kind: WindowKind::SevenDay,
utilization: used,
resets_at: hour(100),
fetched_at: hour(h),
}
}
#[test]
fn reference_wins_only_beyond_twenty_percent() {
assert!(reference_wins(None, UsdPerPct::new(0.3)));
assert!(reference_wins(
Some(UsdPerPct::new(1.0)),
UsdPerPct::new(1.3)
));
assert!(
!reference_wins(Some(UsdPerPct::new(1.0)), UsdPerPct::new(1.2)),
"exactly 20% more is noise"
);
assert!(!reference_wins(
Some(UsdPerPct::new(1.0)),
UsdPerPct::new(1.1)
));
assert!(!reference_wins(
Some(UsdPerPct::new(1.0)),
UsdPerPct::new(0.5)
));
}
#[test]
fn window_of_a_reading() {
let hist = [wk(10, 40.0), wk(20, 70.0), wk(22, 3.0)];
let p = window_of(&hist[2], &hist, hour(50));
assert_eq!(p.window.start, hour(20));
assert_eq!(p.window.end, hour(100));
assert_eq!(p.manual_reset, Some(hour(20)));
assert!(!p.reset_passed);
assert_eq!(counting_from(&hist[2], &p), (3.0, hour(22)));
let p = window_of(&hist[2], &hist, hour(120));
assert!(p.reset_passed);
assert_eq!(p.manual_reset, None);
assert_eq!((p.window.start, p.window.end), (hour(100), hour(268)));
assert_eq!(counting_from(&hist[2], &p), (0.0, hour(100)));
}
#[test]
fn calibration_points_and_own_ratio_by_hand() {
let hist = [wk(1, 10.0), wk(2, 10.3), wk(3, 11.0)];
let pts = calibration_points(&hist, &wk(4, 12.0));
let start = hour(-68);
let want: Vec<(Span, f64)> = [(4, 12.0), (3, 11.0), (2, 10.3)]
.iter()
.map(|&(h, u)| {
(
Span {
start,
end: hour(h),
},
u,
)
})
.collect();
assert_eq!(pts, want);
let many: Vec<Reading> = (1..=20).map(|h| wk(h, h as f64)).collect();
let pts = calibration_points(&many, &many[19]);
assert_eq!(pts.len(), 12);
assert_eq!(pts[11].1, 9.0);
let r = own_ratio(&[
(Pct::new(10.0), Usd::new(4.0)),
(Pct::new(30.0), Usd::new(14.0)),
(Pct::new(0.5), Usd::new(10.0)),
])
.unwrap();
assert!((r.get() - 13.0 / 30.0).abs() < 1e-12);
assert_eq!(own_ratio(&[]), None);
}
#[test]
fn curve_step_keeps_about_170_points() {
let span = |h: i64| Span {
start: hour(0),
end: hour(h),
};
assert_eq!(curve_step(span(168)), Duration::minutes(60));
assert_eq!(curve_step(span(5)), Duration::minutes(1));
}
fn spend(dir: &str, usd: f64) -> ProjectSpend {
ProjectSpend {
project: ProjectKey::from_stored(format!("/w/{dir}")),
name: dir.into(),
cost: Usd::new(usd),
}
}
fn inputs() -> WindowInputs {
let r = wk(10, 30.0);
WindowInputs {
reading: r,
place: window_of(&r, &[r], hour(76)),
own_usd_per_point: Some(UsdPerPct::new(0.5)),
spent_since: Usd::new(5.0),
cost_in_window: Usd::new(15.0),
cost_burn: Usd::new(2.0),
by_project: vec![spend("b", 5.0), spend("a", 10.0)],
series: [5.0, 0.0, 10.0].map(Usd::new).to_vec(),
series_step: Duration::hours(1),
}
}
#[test]
fn a_stale_calibration_is_no_reference() {
let now = ts("2026-11-20T12:00:00Z");
let cal = |account: &str, usd: f64, at: &str| Calibration {
account: account.into(),
tier: PlanTier::of(account, "max"),
kind: WindowKind::SevenDay,
usd_per_point: UsdPerPct::new(usd),
at: ts(at),
};
let parked = cal("parked", 40.0, "2026-10-25T00:00:00Z");
let active = cal("active", 20.0, "2026-11-20T10:00:00Z");
let refs = pick_references(&[parked.clone(), active.clone()], now);
assert_eq!(
refs[&(PlanTier::of("", "max"), WindowKind::SevenDay)].account,
"active"
);
let recent = cal("recent", 30.0, "2026-11-15T00:00:00Z");
let refs = pick_references(&[parked, active, recent], now);
assert_eq!(
refs[&(PlanTier::of("", "max"), WindowKind::SevenDay)].account,
"recent"
);
}
#[test]
fn tiers_of_two_harnesses_never_share_a_reference() {
let now = ts("2026-11-20T12:00:00Z");
let cal = |account: &str, usd: f64| Calibration {
account: account.into(),
tier: PlanTier::of(account, "team"),
kind: WindowKind::SevenDay,
usd_per_point: UsdPerPct::new(usd),
at: now,
};
let refs = pick_references(&[cal("acc-1/org", 20.0), cal("cdx:u/ws", 90.0)], now);
assert_eq!(refs.len(), 2);
assert_eq!(
refs[&(PlanTier::of("acc-1/org", "team"), WindowKind::SevenDay)].account,
"acc-1/org"
);
}
fn pct(v: f64) -> Pct {
Pct::new(v)
}
fn pace(v: f64) -> PctPerHour {
PctPerHour::new(v)
}
fn near(a: impl Into<f64>, b: f64) -> bool {
(a.into() - b).abs() < 1e-9
}
#[test]
fn window_estimate_with_its_own_ratio() {
let e = window_estimate(&inputs(), None, hour(76));
assert_eq!(e.usd_per_point, Some(UsdPerPct::new(0.5)));
assert!(near(e.estimate_now, 40.0));
assert!(near(e.visible_points.unwrap(), 30.0));
assert!(near(e.outside_points.unwrap(), 10.0));
assert!(near(e.burn_usd_per_hour, 2.0));
assert!(near(e.points_per_hour.unwrap(), 4.0));
assert_eq!(e.eta, Some(Duration::hours(15)));
assert_eq!(e.runs_out_at, Some(hour(76) + Duration::hours(15)));
assert_eq!(e.shares[0].name, "a");
assert_eq!(e.shares[0].project, ProjectKey::from_stored("/w/a"));
assert!(near(e.shares[0].limit_points, 20.0));
assert!(near(e.shares[1].limit_points, 10.0));
assert!(near(e.window_usd.unwrap(), 50.0));
assert!(near(e.curve[0], 40.0 / 3.0));
assert!(near(e.curve[1], 40.0 / 3.0));
assert!(near(e.curve[2], 40.0));
}
#[test]
fn window_estimate_with_a_tier_reference() {
let e = window_estimate(&inputs(), Some(UsdPerPct::new(1.0)), hour(76));
assert_eq!(e.usd_per_point, Some(UsdPerPct::new(1.0)));
assert!(near(e.estimate_now, 35.0));
assert!(near(e.visible_points.unwrap(), 15.0));
assert!(near(e.outside_points.unwrap(), 20.0));
assert!(near(e.points_per_hour.unwrap(), 2.0));
assert_eq!(e.eta, None);
assert!(near(e.window_usd.unwrap(), 100.0));
}
#[test]
fn window_estimate_after_the_reset() {
let r = wk(10, 30.0);
let i = WindowInputs {
place: window_of(&r, &[r], hour(120)),
own_usd_per_point: Some(UsdPerPct::new(2.0)),
spent_since: Usd::new(6.0),
cost_in_window: Usd::new(6.0),
series: Vec::new(),
..inputs()
};
let e = window_estimate(&i, None, hour(120));
assert!(near(e.estimate_now, 3.0));
assert!(near(e.visible_points.unwrap(), 3.0));
assert!(near(e.outside_points.unwrap(), 0.0));
assert!(e.curve.is_empty());
}
#[test]
fn window_estimate_with_nothing_known() {
let i = WindowInputs {
own_usd_per_point: None,
spent_since: Usd::new(0.0),
cost_in_window: Usd::new(0.0),
cost_burn: Usd::new(0.0),
by_project: Vec::new(),
series: Vec::new(),
..inputs()
};
let e = window_estimate(&i, None, hour(76));
assert!(near(e.estimate_now, 30.0));
assert_eq!(
(e.usd_per_point, e.visible_points, e.outside_points),
(None, None, None)
);
assert_eq!((e.points_per_hour, e.eta, e.window_usd), (None, None, None));
assert!(e.shares.is_empty() && e.curve.is_empty());
assert!(near(e.burn_usd_per_hour, 0.0));
}
fn seen(fetched: &str, used: f64, resets: &str) -> Reading {
let full = |t: &str| {
if t.contains('T') {
ts(&format!("{t}Z"))
} else {
ts(&format!("2026-11-22T{t}Z"))
}
};
Reading {
kind: WindowKind::SevenDay,
utilization: used,
resets_at: full(resets),
fetched_at: full(fetched),
}
}
#[test]
fn readings_of_one_window_agree_despite_jitter_in_the_reset_time() {
let a = seen("10:00:00", 40.0, "2026-11-25T14:59:59.56");
let b = seen("10:05:00", 41.0, "2026-11-25T15:00:00.13");
assert!(a.same_window(&b));
assert_eq!(a.window(), b.window());
assert_eq!(a.window_end(), ts("2026-11-25T15:00:00Z"));
assert!(!was_reset(a.window(), b.window()));
let now = ts("2026-11-22T12:00:00Z");
assert!(!was_reset(
window_of(&a, &[a], now).window,
window_of(&b, &[a, b], now).window
));
}
#[test]
fn a_reset_by_hand_seen_through_lagging_sources() {
let r = |f: &str, u: f64| seen(f, u, "2026-11-25T17:00:00");
let mut hist = vec![
seen("2026-11-20T15:44:49", 63.0, "2026-11-25T17:00:00.314"),
seen("13:59:47", 65.0, "2026-11-25T16:59:59.983"),
seen("16:12:33", 65.0, "2026-11-25T17:00:00.491"),
seen("17:05:16", 66.0, "2026-11-25T16:59:59.596"),
seen("18:07:01", 75.0, "2026-11-25T17:00:00.304"),
seen("18:45:53", 82.0, "2026-11-25T16:59:59.936"),
seen("20:44:58", 95.0, "2026-11-25T17:00:00.137"),
seen("21:04:04", 0.0, "2026-11-25T16:59:59.562"),
seen("21:05:31", 0.0, "2026-11-25T17:00:00.485"),
r("21:11:14", 1.0),
r("21:15:49", 2.0),
r("21:27:43", 3.0),
];
for m in 47..=55 {
for sec in [3, 13, 23, 33, 43, 53] {
hist.push(r(&format!("21:{m}:{sec:02}"), 2.0));
}
}
for f in ["21:47:06", "21:47:23", "21:48:13", "21:49:46", "21:54:52"] {
hist.push(r(f, 4.0));
}
hist.push(r("21:55:24", 5.0));
for (f, u) in [
("22:00:25", 5.0),
("22:11:28", 6.0),
("22:18:25", 7.0),
("22:27:12", 8.0),
("22:42:06", 10.0),
("23:00:28", 12.0),
("23:10:33", 13.0),
] {
hist.push(r(f, u));
}
hist.sort_by_key(|h| h.fetched_at);
let last_high = ts("2026-11-22T20:44:58Z");
let first_low = ts("2026-11-22T21:04:04Z");
let cur = *hist.last().unwrap();
assert_eq!(effective_start(&hist, &cur), (last_high, Some(last_high)));
for h in hist.iter().filter(|h| h.fetched_at >= first_low) {
let (start, reset) = effective_start(&hist, h);
assert_eq!(
(start, reset),
(last_high, Some(last_high)),
"at {}",
h.fetched_at
);
assert!(start < first_low);
}
let before = hist.iter().find(|h| h.fetched_at == last_high).unwrap();
assert_eq!(effective_start(&hist, before).0, ts("2026-11-18T17:00:00Z"));
}
#[test]
fn a_fall_that_does_not_hold_is_no_reset_and_a_dense_one_is() {
let r = |f: &str, u: f64| seen(f, u, "2026-11-25T17:00:00");
let hist = [
r("21:00:00", 94.0),
r("21:03:00", 95.0),
r("21:04:00", 0.0),
r("21:10:00", 1.0),
];
let t = ts("2026-11-22T21:03:00Z");
assert_eq!(effective_start(&hist, &hist[3]), (t, Some(t)));
let mut late = hist.to_vec();
late.push(r("21:12:00", 95.0));
late.push(r("21:14:00", 2.0));
assert_eq!(effective_start(&late, &late[5]), (t, Some(t)));
let rising = [
r("10:00:00", 30.0),
r("10:05:00", 40.0),
r("10:06:00", 30.0),
r("10:09:00", 41.0),
];
let nominal = ts("2026-11-18T17:00:00Z");
assert_eq!(effective_start(&rising, &rising[2]), (nominal, None));
assert_eq!(effective_start(&rising, &rising[3]), (nominal, None));
let undone = [
r("10:00:00", 50.0),
r("10:01:00", 10.0),
r("10:04:00", 50.0),
];
assert_eq!(effective_start(&undone, &undone[2]), (nominal, None));
}
}