//! Every case of `packages/core/__tests__/engine/series.test.ts`, plus the //! Rust form of `expansionCallers.test.ts`. //! //! Every shape below is one the owner's own Google calendar had on 2026-09-04, //! because those are the shapes that were drawing wrong: weekly "Vicky" //! (`FREQ=WEEKLY`, 12:00 Asia/Kolkata on a Friday) with its 4 Sep occurrence //! moved to 8 Sep and its 11 Sep occurrence moved to 12 Sep, and "Call with //! Bella" with its 23 Sep occurrence cancelled. //! //! The zone is EXPLICIT in every call. The host runs `TZ=UTC` and the phone //! runs Asia/Kolkata; an expansion that read the runtime's zone would put the //! same occurrence at two instants, and an occurrence's identity here IS its //! instant. use super::*; const ZONE: &str = "Asia/Kolkata"; fn row(id: &str, starts_at: &str, ends_at: Option<&str>, rrule: Option<&str>) -> SeriesRow { SeriesRow { id: id.to_string(), starts_at: starts_at.to_string(), ends_at: ends_at.map(str::to_string), rrule: rrule.map(str::to_string), ..SeriesRow::default() } } /// Weekly "Vicky": Fridays, 12:00–13:00 in Asia/Kolkata. fn vicky() -> SeriesRow { row( "ev-vicky", "2026-09-04T06:30:00.000Z", Some("2026-09-04T07:30:00.000Z"), Some("FREQ=WEEKLY"), ) } /// The 4 Sep occurrence, moved to Tuesday 8 Sep at the same clock time. fn moved_to_8() -> SeriesRow { SeriesRow { series_id: Some("ev-vicky".to_string()), original_starts_at: Some("2026-09-04T06:30:00.000Z".to_string()), ..row( "ev-vicky-0904", "2026-09-08T06:30:00.000Z", Some("2026-09-08T07:30:00.000Z"), None, ) } } fn range(from: &str, to: &str) -> DateRange { DateRange { from: cotton_store::protocol::js::parse_date_ms(from).expect("an ISO instant"), to: cotton_store::protocol::js::parse_date_ms(to).expect("an ISO instant"), } } fn september() -> DateRange { range("2026-09-01T00:00:00.000Z", "2026-09-30T23:59:59.999Z") } fn starts_in(occurrences: &[SeriesOccurrence]) -> Vec<&str> { occurrences.iter().map(|o| o.starts_at.as_str()).collect() } fn expand(master: &SeriesRow, exceptions: &[SeriesRow], range: DateRange) -> Vec { expand_series(master, exceptions, range, ZONE).expect("a valid series") } // ── expand_series ──────────────────────────────────────────────────────────── #[test] fn draws_every_friday_when_nothing_has_been_changed() { let out = expand(&vicky(), &[], september()); assert_eq!( starts_in(&out), [ "2026-09-04T06:30:00.000Z", "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); assert!(out .iter() .all(|o| o.series_id.as_deref() == Some("ev-vicky") && !o.is_exception)); // Each occurrence keeps the series' duration. assert_eq!(out[0].ends_at.as_deref(), Some("2026-09-04T07:30:00.000Z")); } #[test] fn keeps_twelve_hundred_on_the_wall_clock_across_a_rule_read_in_its_own_zone() { // A weekly rule anchored at 12:00 IST reads 12:00 IST at every occurrence, // whatever the reader's own zone is. (Asia/Kolkata has no DST, which is why // the instants are identical; the property under test is that the ZONE // argument is what the engine used.) let london = expand_series(&vicky(), &[], september(), "Europe/London").expect("a valid series"); assert_eq!( starts_in(&london), starts_in(&expand(&vicky(), &[], september())) ); } #[test] fn drops_an_instant_the_master_skips_the_cancelled_call_with_bella() { let bella = SeriesRow { id: "ev-bella".to_string(), starts_at: "2026-09-09T09:00:00.000Z".to_string(), ends_at: Some("2026-09-09T09:30:00.000Z".to_string()), exdates: Some(vec!["2026-09-23T09:00:00.000Z".to_string()]), ..vicky() }; assert_eq!( starts_in(&expand(&bella, &[], september())), [ "2026-09-09T09:00:00.000Z", "2026-09-16T09:00:00.000Z", "2026-09-30T09:00:00.000Z" ] ); } #[test] fn moves_an_occurrence_rather_than_drawing_it_twice() { let out = expand(&vicky(), &[moved_to_8()], september()); assert_eq!( starts_in(&out), [ "2026-09-08T06:30:00.000Z", "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); let moved = &out[0]; assert_eq!(moved.event_id, "ev-vicky-0904"); assert!(moved.is_exception); // The ADDRESS is the instant it replaces, not the time it moved to — that // is what an assistant passes back as `occurrenceAt` and what Google's // instance id is derived from. assert_eq!(moved.occurrence_at, "2026-09-04T06:30:00.000Z"); } #[test] fn empties_the_day_an_occurrence_left_even_when_the_window_cannot_see_where_it_went() { let fourth = range("2026-09-04T00:00:00.000Z", "2026-09-04T23:59:59.999Z"); assert!(expand(&vicky(), &[moved_to_8()], fourth).is_empty()); } #[test] fn draws_an_occurrence_on_the_day_it_moved_to_even_when_the_instant_it_left_is_out_of_the_window() { let eighth = range("2026-09-08T00:00:00.000Z", "2026-09-08T23:59:59.999Z"); assert_eq!( starts_in(&expand(&vicky(), &[moved_to_8()], eighth)), ["2026-09-08T06:30:00.000Z"] ); } #[test] fn takes_two_moved_occurrences_at_once() { let moved_to_12 = SeriesRow { id: "ev-vicky-0911".to_string(), starts_at: "2026-09-12T06:30:00.000Z".to_string(), ends_at: Some("2026-09-12T07:30:00.000Z".to_string()), original_starts_at: Some("2026-09-11T06:30:00.000Z".to_string()), ..moved_to_8() }; assert_eq!( starts_in(&expand(&vicky(), &[moved_to_8(), moved_to_12], september())), [ "2026-09-08T06:30:00.000Z", "2026-09-12T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } #[test] fn draws_nothing_at_all_for_a_series_whose_master_is_in_the_bin() { let trashed = SeriesRow { trashed_at: Some("2026-09-04T10:00:00.000Z".to_string()), ..vicky() }; assert!(expand(&trashed, &[moved_to_8()], september()).is_empty()); } #[test] fn does_not_put_the_original_instant_back_when_a_moved_occurrence_goes_in_the_bin() { // A trashed exception is on its way out of Google, not on its way back into // the series. Restoring 4 Sep here would put an appointment back on a day // the person already emptied. let binned = SeriesRow { trashed_at: Some("2026-09-05T10:00:00.000Z".to_string()), ..moved_to_8() }; assert_eq!( starts_in(&expand(&vicky(), &[binned], september())), [ "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } #[test] fn draws_an_exception_whose_claimed_instant_the_rule_never_produced() { // Google allows one, and a row the person can see beats a row silently // swallowed. let odd = SeriesRow { original_starts_at: Some("2026-09-05T06:30:00.000Z".to_string()), ..moved_to_8() }; assert_eq!( starts_in(&expand(&vicky(), &[odd], september())), [ "2026-09-04T06:30:00.000Z", "2026-09-08T06:30:00.000Z", "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } #[test] fn treats_a_one_off_event_as_a_series_of_one_belonging_to_no_series() { let dinner = row("ev-dinner", "2026-09-05T14:00:00.000Z", None, None); assert_eq!( expand(&dinner, &[], september()), vec![SeriesOccurrence { event_id: "ev-dinner".to_string(), series_id: None, occurrence_at: "2026-09-05T14:00:00.000Z".to_string(), starts_at: "2026-09-05T14:00:00.000Z".to_string(), ends_at: None, is_exception: false, }] ); } #[test] fn draws_nothing_for_a_one_off_event_in_the_bin() { let binned = SeriesRow { trashed_at: Some("2026-09-05T15:00:00.000Z".to_string()), ..row("x", "2026-09-05T14:00:00.000Z", None, None) }; assert!(expand(&binned, &[], september()).is_empty()); } #[test] fn honours_count_and_until_through_the_underlying_rule() { let counted = SeriesRow { rrule: Some("FREQ=WEEKLY;COUNT=2".to_string()), ..vicky() }; assert_eq!( starts_in(&expand(&counted, &[], september())), ["2026-09-04T06:30:00.000Z", "2026-09-11T06:30:00.000Z"] ); let until = SeriesRow { rrule: Some("FREQ=WEEKLY;UNTIL=20260915T000000Z".to_string()), ..vicky() }; assert_eq!( starts_in(&expand(&until, &[], september())), ["2026-09-04T06:30:00.000Z", "2026-09-11T06:30:00.000Z"] ); } #[test] fn ignores_a_malformed_exdate_rather_than_losing_the_whole_series() { let odd = SeriesRow { exdates: Some(vec![ "not a date".to_string(), "2026-09-11T06:30:00.000Z".to_string(), ]), ..vicky() }; assert_eq!( starts_in(&expand(&odd, &[], september())), [ "2026-09-04T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } #[test] fn matches_an_exdate_by_instant_not_by_how_it_was_written() { let shorter = SeriesRow { exdates: Some(vec!["2026-09-11T06:30:00Z".to_string()]), ..vicky() }; assert!(!starts_in(&expand(&shorter, &[], september())).contains(&"2026-09-11T06:30:00.000Z")); } // ── expand_calendar ────────────────────────────────────────────────────────── #[test] fn groups_an_exception_under_its_master_rather_than_drawing_it_on_its_own() { let out = expand_calendar(&[vicky(), moved_to_8()], september(), ZONE).expect("a valid calendar"); assert_eq!( starts_in(&out), [ "2026-09-08T06:30:00.000Z", "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } #[test] fn draws_an_orphan_exception_as_an_ordinary_event_rather_than_losing_it() { let orphan = SeriesRow { series_id: Some("ev-that-was-never-pulled".to_string()), ..moved_to_8() }; let out = expand_calendar(&[orphan], september(), ZONE).expect("a valid calendar"); assert_eq!(starts_in(&out), ["2026-09-08T06:30:00.000Z"]); assert!(!out[0].is_exception); } #[test] fn returns_every_calendars_occurrences_in_one_chronological_list() { let dinner = row("ev-dinner", "2026-09-05T14:00:00.000Z", None, None); let out = expand_calendar(&[vicky(), dinner], september(), ZONE).expect("a valid calendar"); assert_eq!( starts_in(&out), [ "2026-09-04T06:30:00.000Z", "2026-09-05T14:00:00.000Z", "2026-09-11T06:30:00.000Z", "2026-09-18T06:30:00.000Z", "2026-09-25T06:30:00.000Z" ] ); } // ── with_exdate / has_exdate ───────────────────────────────────────────────── #[test] fn adds_an_instant_once_however_many_times_the_sweep_re_runs() { let once = with_exdate(None, "2026-09-23T09:00:00.000Z").expect("an instant"); assert_eq!(once, ["2026-09-23T09:00:00.000Z"]); assert_eq!( with_exdate(Some(&once), "2026-09-23T09:00:00.000Z").expect("an instant"), once ); // Same moment, written shorter — still the same entry. assert_eq!( with_exdate(Some(&once), "2026-09-23T09:00:00Z").expect("an instant"), once ); } #[test] fn keeps_the_list_sorted_so_two_devices_converge_on_the_same_json() { assert_eq!( with_exdate( Some(&["2026-09-30T09:00:00.000Z".to_string()]), "2026-09-23T09:00:00.000Z" ) .expect("an instant"), ["2026-09-23T09:00:00.000Z", "2026-09-30T09:00:00.000Z"] ); } #[test] fn refuses_something_that_is_not_an_instant_rather_than_writing_it() { let refusal = with_exdate(None, "next Tuesday").expect_err("not an instant"); assert!(refusal.to_string().contains("not an instant")); assert_eq!(refusal.to_string(), "not an instant: \"next Tuesday\""); } #[test] fn answers_whether_an_instant_is_already_cancelled_by_instant() { assert!(has_exdate( Some(&["2026-09-23T09:00:00.000Z".to_string()]), "2026-09-23T09:00:00Z" )); assert!(!has_exdate(None, "2026-09-23T09:00:00Z")); // An unreadable instant is never present — `NaN !== NaN`. assert!(!has_exdate(Some(&["not a date".to_string()]), "not a date")); } // ───────────────────────────────────────────────────────────────────────────── // `expansionCallers.test.ts`, in Rust. // // ONE reader of the raw rule, and it is this module. `expand_event` answers // "when does this rule fire" and knows nothing about the three facts schema v32 // added: the instants a master SKIPS, the occurrences a row has CLAIMED, and // the row that is in the BIN. A caller that reaches past `expand_series` for it // therefore draws a moved occurrence on two days and a cancelled one for ever — // the exact bug the owner was looking at on 2026-09-04, and one no type can // prevent, because the wrong call compiles. // // So it is asserted structurally, over the crate's own source. A deliberate new // caller changes the list below and says why in the commit. // ───────────────────────────────────────────────────────────────────────────── #[test] fn event_expansion_has_one_way_in() { let src = cotton_store::manifest_dir().join("src"); /// The files allowed to name `expand_event`: this module is the one /// caller (cotton-rules' `rrule.rs` declares it). const ALLOWED: &[&str] = &["rules/series.rs"]; let mut scanned = 0usize; let mut offenders: Vec = Vec::new(); let mut stack = vec![src.clone()]; while let Some(dir) = stack.pop() { let entries = std::fs::read_dir(&dir) .unwrap_or_else(|error| panic!("could not read {} ({error})", dir.display())); for entry in entries.flatten() { let path = entry.path(); if path.is_dir() { stack.push(path); continue; } let name = path.file_name().and_then(|n| n.to_str()).unwrap_or(""); if !name.ends_with(".rs") { continue; } // Tests may call the engine directly — the TypeScript guard skips // `__tests__/` for the same reason. This crate keeps its tests in // `*_tests.rs` beside the source, so that is the exclusion. if name.ends_with("_tests.rs") { continue; } scanned += 1; let source = std::fs::read_to_string(&path) .unwrap_or_else(|error| panic!("could not read {} ({error})", path.display())); // A CALL, not a mention: the module headers explain the rule in // prose, and a guard that failed on those is a guard people delete. if !source.contains("expand_event(") { continue; } let relative = path .strip_prefix(&src) .unwrap_or(&path) .to_string_lossy() .replace('\\', "/"); if !ALLOWED.contains(&relative.as_str()) { offenders.push(relative); } } } // The guard must not pass by finding nothing — the failure mode the // TypeScript file names in its own second case. assert!( scanned >= 5, "scanned only {scanned} files; the walk is broken" ); assert!( offenders.is_empty(), "these files call expand_event directly and will draw a moved occurrence twice — go through expand_series/expand_calendar: {offenders:?}" ); }