//! The planner's cases are the shared vectors //! (`crates/cotton-rules/vectors/todo_recurrence.json`); the rrule edges it //! leans on are here and in `rrule_tests.rs`. use super::*; use crate::rrule::next_occurrence_after; use crate::js::{parse_date_ms, to_iso_string}; fn at(iso: &str) -> i64 { parse_date_ms(iso).expect("an ISO instant") } fn isos(instants: &[i64]) -> Vec { instants .iter() .map(|ms| to_iso_string(*ms).expect("a renderable instant")) .collect() } // ── next_occurrence_after, through the planner's own eyes ──────────────────── #[test] fn returns_the_first_occurrence_strictly_after_the_given_instant() { let next = next_occurrence_after( "FREQ=MONTHLY", at("2026-09-02T09:00:00Z"), at("2026-09-02T09:00:00Z"), "UTC", ) .expect("a valid rule") .and_then(to_iso_string); assert_eq!(next.as_deref(), Some("2026-10-02T09:00:00.000Z")); } #[test] fn returns_dtstart_itself_when_after_precedes_it() { let next = next_occurrence_after( "FREQ=MONTHLY", at("2026-09-02T09:00:00Z"), at("2026-08-16T00:00:00Z"), "UTC", ) .expect("a valid rule") .and_then(to_iso_string); assert_eq!(next.as_deref(), Some("2026-09-02T09:00:00.000Z")); } #[test] fn is_none_once_count_is_exhausted() { assert_eq!( next_occurrence_after( "FREQ=DAILY;COUNT=3", at("2026-08-01T09:00:00Z"), // All three occurrences are past. at("2026-08-03T10:00:00Z"), "UTC", ), Ok(None) ); } #[test] fn is_none_past_until() { assert_eq!( next_occurrence_after( "FREQ=DAILY;UNTIL=20260810T000000Z", at("2026-08-01T09:00:00Z"), at("2026-08-09T10:00:00Z"), "UTC", ), Ok(None) ); } #[test] fn skips_short_months_for_a_monthly_rule_on_the_thirty_first() { // September has no 31st — skipped, never rolled (the iCal rule). Read in a // DST zone on purpose: the engine is wall-clock-preserving, so the UTC // instant shifts by the offset delta and the local hour does not. let zone = "Europe/London"; let dtstart = crate::zoned_clock::instant_from_zoned( crate::zoned_clock::ZonedParts { year: 2026, month: 8, day: 31, hour: 9, minute: 0, second: 0, }, 0, zone, ) .expect("a known zone"); let next = next_occurrence_after("FREQ=MONTHLY", dtstart, dtstart, zone) .expect("a valid rule") .expect("a next occurrence"); let parts = crate::zoned_clock::zoned_parts(next, zone).expect("a known zone"); assert_eq!((parts.month, parts.day, parts.hour), (10, 31, 9)); } // ── plan_materialization: the shared vectors ──────────────────────────────── /// One case of `crates/cotton-rules/vectors/todo_recurrence.json`. #[derive(serde::Deserialize)] struct PlanVector { name: String, rrule: String, dtstart: String, zone: String, now: String, latest: Option, mark: Option, paused: bool, #[serde(default)] want: Vec, #[serde(default)] refused: bool, } #[derive(serde::Deserialize)] struct LatestVector { at: String, finished: bool, } /// RULE: a series owes what the shared vectors say, on the phone and on the /// Worker alike (the Worker's recurrence.test.ts reads the same file). #[test] fn plan_materialization_answers_the_shared_vectors() { #[derive(serde::Deserialize)] struct File { cases: Vec, } let file: File = serde_json::from_str(include_str!("../vectors/todo_recurrence.json")).expect("the vectors parse"); assert!(file.cases.len() >= 10); for vector in &file.cases { let planned = plan_materialization( &TodoRecurrencePlanInput { rrule: vector.rrule.clone(), dtstart: at(&vector.dtstart), materialized_until: vector.mark.as_deref().map(at), latest: vector.latest.as_ref().map(|l| LatestOccurrence { at: at(&l.at), finished: l.finished }), paused: vector.paused, timezone: vector.zone.clone(), }, at(&vector.now), ); if vector.refused { assert!(planned.is_err(), "{}", vector.name); continue; } let planned = planned.unwrap_or_else(|error| panic!("{}: {error}", vector.name)); assert_eq!(isos(&planned.occurrences), vector.want, "{}", vector.name); assert_eq!(planned.advance_mark_to.map(|ms| to_iso_string(ms).expect("renderable")), vector.want.last().cloned(), "{}", vector.name); } } /// RULE: A stack of late open copies left by the old rule collapses to its newest: every earlier open one is missed, none is deleted, and one open copy is no stack. #[test] fn a_stack_of_late_copies_keeps_its_newest_and_misses_the_rest() { use crate::todo_recurrence::{collapse_stack, OpenOccurrence}; let one = |id: &str, iso: &str| OpenOccurrence { id: id.to_string(), at: at(iso) }; let stack = [ one("wed", "2026-08-12T09:00:00.000Z"), one("mon", "2026-08-10T09:00:00.000Z"), one("fri", "2026-08-14T09:00:00.000Z"), one("tue", "2026-08-11T09:00:00.000Z"), ]; assert_eq!(collapse_stack(&stack), vec!["mon", "tue", "wed"]); assert!(collapse_stack(&stack[..1]).is_empty()); assert!(collapse_stack(&[]).is_empty()); }