mod common;
use common::{at, berlin, day, day_of};
use flexibility::jiff::civil::date;
use flexibility::jiff::tz::TimeZone;
use flexibility::{Error, Grid, IspDuration, Series};
#[test]
fn an_ordinary_day_has_96_quarter_hours() {
let grid = day();
assert_eq!(grid.count(), 96);
assert_eq!(grid.start(), at(0, 0));
assert_eq!(grid.interval(69).unwrap().from(), at(17, 0));
assert_eq!(grid.interval(69).unwrap().until(), at(17, 15));
assert_eq!(grid.isp_at(at(17, 14)), Some(69));
}
#[test]
fn dst_days_have_92_and_100() {
assert_eq!(day_of(date(2026, 3, 29)).count(), 92);
assert_eq!(day_of(date(2026, 10, 25)).count(), 100);
}
#[test]
fn every_day_of_2026_and_2027() {
let mut d = date(2026, 1, 1);
let mut days = 0;
while d.year() < 2028 {
let n = day_of(d).count();
assert!(matches!(n, 92 | 96 | 100), "{d}: {n}");
let short = (d == date(2026, 3, 29)) || (d == date(2027, 3, 28));
let long = (d == date(2026, 10, 25)) || (d == date(2027, 10, 31));
assert_eq!(n == 92, short, "{d}");
assert_eq!(n == 100, long, "{d}");
d = d.tomorrow().unwrap();
days += 1;
}
assert_eq!(days, 730);
}
#[test]
fn isp_durations_must_divide_a_day() {
assert_eq!(
IspDuration::from_minutes(7).unwrap_err(),
Error::IspDuration { minutes: 7 }
);
assert!(IspDuration::from_minutes(0).is_err());
for m in [1, 5, 15, 30, 60] {
assert_eq!(IspDuration::from_minutes(m).unwrap().minutes(), m);
}
}
#[test]
fn a_half_hour_dst_shift_with_hour_isps_is_refused() {
let zone = TimeZone::get("Australia/Lord_Howe").unwrap();
let hour = IspDuration::from_minutes(60).unwrap();
let err = Grid::day(date(2026, 4, 5), &zone, hour).unwrap_err();
assert!(matches!(err, Error::Day { .. }), "{err}");
assert!(Grid::day(date(2026, 4, 5), &zone, IspDuration::FIFTEEN_MINUTES).is_ok());
}
#[test]
fn instants_outside_the_grid_map_to_no_isp() {
let grid = day();
assert_eq!(grid.isp_at(grid.end()), None);
assert_eq!(
grid.isp_at(
at(0, 0)
.checked_sub(flexibility::jiff::SignedDuration::from_secs(1))
.unwrap()
),
None
);
assert_eq!(grid.interval(0), None);
assert_eq!(grid.interval(97), None);
}
#[test]
fn grid_sizes_are_bounded() {
let isp = IspDuration::FIFTEEN_MINUTES;
assert_eq!(
Grid::new(at(0, 0), isp, 0, TimeZone::UTC).unwrap_err(),
Error::GridSize { count: 0 }
);
assert!(Grid::new(at(0, 0), isp, flexibility::MAX_ISPS, TimeZone::UTC).is_ok());
}
#[test]
fn series_need_one_value_per_isp() {
let err = Series::new(day(), vec![0_i64; 95]).unwrap_err();
assert_eq!(
err,
Error::SeriesLength {
expected: 96,
actual: 95
}
);
assert!(err.to_string().contains("96"));
let s = Series::new(day(), (1..=96).collect::<Vec<i64>>()).unwrap();
assert_eq!(s.get(1), Some(&1));
assert_eq!(s.get(96), Some(&96));
assert_eq!(s.get(0), None);
assert_eq!(s.iter().nth(68), Some((69, &69)));
}
#[test]
fn the_named_zone_is_used_not_a_default() {
let utc = Grid::day(
date(2026, 12, 1),
&TimeZone::UTC,
IspDuration::FIFTEEN_MINUTES,
)
.unwrap();
assert_ne!(utc, day());
assert!(TimeZone::get("Europe/Nowhere").is_err());
let _ = berlin();
}
use flexibility::jiff::civil::time;
#[test]
fn a_local_window_spans_its_isps() {
let grid = day();
let evening = grid.window(time(17, 0, 0, 0), time(19, 0, 0, 0)).unwrap();
assert_eq!(grid.isps(evening), Some(69..=76));
assert_eq!(evening.from(), at(17, 0));
assert_eq!(grid.local(time(17, 0, 0, 0)).unwrap(), at(17, 0));
}
#[test]
fn a_local_window_may_cross_midnight() {
let grid = day();
let night = grid.window(time(22, 0, 0, 0), time(2, 0, 0, 0)).unwrap();
assert_eq!(grid.isps(night), Some(89..=96), "the part on this day");
assert_eq!(
night.until().as_second() - night.from().as_second(),
4 * 3_600
);
}
#[test]
fn skipped_and_repeated_local_times_are_refused() {
let spring = day_of(date(2026, 3, 29));
let err = spring
.window(time(2, 30, 0, 0), time(4, 0, 0, 0))
.unwrap_err();
assert!(
matches!(err, Error::LocalTime { reason, .. } if reason.starts_with("skipped")),
"{err}"
);
let fall = day_of(date(2026, 10, 25));
let err = fall.local(time(2, 30, 0, 0)).unwrap_err();
assert!(
matches!(err, Error::LocalTime { reason, .. } if reason.starts_with("ambiguous")),
"{err}"
);
assert!(
err.to_string()
.contains("2026-10-25 02:30:00 in Europe/Berlin"),
"{err}"
);
assert!(err.hint().is_some());
}
#[test]
fn labels_show_local_time_and_offset() {
let grid = day();
assert_eq!(
grid.label(69).unwrap().to_string(),
"ISP 69 (17:00–17:15 +01:00)"
);
assert_eq!(grid.label(0), None);
let fall = day_of(date(2026, 10, 25));
assert_eq!(
fall.label(9).unwrap().to_string(),
"ISP 9 (02:00–02:15 +02:00)"
);
assert_eq!(
fall.label(13).unwrap().to_string(),
"ISP 13 (02:00–02:15 +01:00)"
);
}
#[test]
fn the_zone_labels_but_does_not_change_the_isps() {
let grid = day();
let utc = grid.clone().with_zone(TimeZone::UTC);
assert_eq!(utc, grid, "same ISPs");
assert_eq!(
utc.label(69).unwrap().to_string(),
"ISP 69 (16:00–16:15 +00:00)"
);
assert_eq!(grid.zone(), &berlin());
}
#[test]
fn a_slice_keeps_the_days_isp_numbers() {
let grid = day();
let evening = grid.slice(69..=76).unwrap();
assert_eq!((evening.first(), evening.numbers()), (69, 69..=76));
assert_eq!(evening.start(), at(17, 0));
assert_eq!(
evening.label(69).unwrap().to_string(),
"ISP 69 (17:00–17:15 +01:00)"
);
assert_eq!(evening.label(1), None);
assert_eq!(evening.isp_at(at(18, 59)), Some(76));
let s = Series::new(grid.clone(), (1..=96).collect::<Vec<i64>>()).unwrap();
let cut = s.slice(69..=76).unwrap();
assert_eq!(cut.get(69), Some(&69));
assert_eq!(cut.iter().next(), Some((69, &69)));
assert_ne!(
evening,
Grid::new(at(17, 0), IspDuration::FIFTEEN_MINUTES, 8, berlin()).unwrap()
);
assert!(matches!(grid.slice(0..=3), Err(Error::IspRange { .. })));
assert!(matches!(grid.slice(90..=97), Err(Error::IspRange { .. })));
assert!(evening.slice(68..=70).is_err());
assert_eq!(
evening.to_string(),
"ISPs 69–76 of PT15M from 2026-12-01T16:00:00Z (Europe/Berlin)"
);
}