127 lines · 5.9 KB · RustRaw
//! The `.ics` Days writes, read back through the calendar reader every door//! uses (`import::ical`), over invented events and to-dos.use super::*;use crate::import::ical;const ZONE: &str = "Europe/London";/// 2026-11-20T11:30:00Z.const BANK: i64 = 1_795_174_200_000;const NOW: i64 = 1_795_000_000_000;fn day(y: i64, m: i64, d: i64) -> i64 { crate::cotton_rules::zoned_clock::day_number(y, m, d)}fn iso(ms: i64) -> String { cotton_store::protocol::js::to_iso_string(ms).expect("an instant")}#[test]fn events_come_back_as_they_went_in() { let events = vec![ Event { uid: "ev-bank@cottondb.com".into(), title: "Call with the bank; bring the forms, both".into(), notes: Some("Line one\nLine two \\ back".into()), place: Some("High Street, Bath".into()), start: Some(When::Instant(BANK)), end: Some(When::Instant(BANK + 30 * 60_000)), made: Some(NOW), ..Event::default() }, Event { uid: "ev-visit@cottondb.com".into(), title: "Rose in town".into(), start: Some(When::Day(day(2026, 11, 20))), end: Some(When::Day(day(2026, 11, 22))), ..Event::default() }, ]; let file = calendar("Days", &events, &[], NOW); assert!(file.starts_with("BEGIN:VCALENDAR\r\nVERSION:2.0\r\n"), "{file}"); assert!(file.ends_with("END:VCALENDAR\r\n")); assert!(file.contains("X-WR-CALNAME:Days\r\n")); let read = ical::parse(&file); assert_eq!(read.len(), 2); let bank = &read[0]; assert_eq!(bank.uid, "ev-bank@cottondb.com"); assert_eq!(bank.summary.as_deref(), Some("Call with the bank; bring the forms, both")); assert_eq!(bank.description.as_deref(), Some("Line one\nLine two \\ back")); assert_eq!(bank.location.as_deref(), Some("High Street, Bath")); assert_eq!(ical::instant_of(bank.start.as_ref().unwrap(), ZONE).as_deref(), Some(iso(BANK).as_str())); assert_eq!(ical::instant_of(bank.end.as_ref().unwrap(), ZONE).as_deref(), Some(iso(BANK + 30 * 60_000).as_str())); let visit = &read[1]; let start = visit.start.as_ref().unwrap(); assert!(start.is_date); assert_eq!(start.value, "20261120"); assert_eq!(visit.end.as_ref().unwrap().value, "20261122", "an all-day end is the day after the last");}#[test]fn a_series_keeps_its_rule_its_hour_its_gaps_and_its_changed_day() { // Every Monday at 19:00 in London, from 2 Nov 2026 (19:00Z, before and after the clocks change alike). let first = 1_793_646_000_000; // 2026-11-02T19:00:00Z let week = 7 * 86_400_000; let zoned = |ms: i64| When::Zoned { ms, zone: ZONE.into() }; let events = vec![ Event { uid: "choir@cottondb.com".into(), title: "Choir".into(), start: Some(zoned(first)), end: Some(zoned(first + 2 * 3_600_000)), rule: Some("FREQ=WEEKLY;BYDAY=MO".into()), taken_out: vec![zoned(first + week)], ..Event::default() }, Event { uid: "choir@cottondb.com".into(), title: "Choir, in the hall".into(), replaces: Some(zoned(first + 2 * week)), start: Some(zoned(first + 2 * week + 3_600_000)), end: Some(zoned(first + 2 * week + 3 * 3_600_000)), ..Event::default() }, ]; let file = calendar("Days", &events, &[], NOW); assert!(file.contains("DTSTART;TZID=Europe/London:20261102T190000\r\n"), "{file}"); let read = ical::parse(&file); assert_eq!(read.len(), 2); assert_eq!(ical::rrule_from_recurrence(Some(&read[0].recurrence)).as_deref(), Some("FREQ=WEEKLY;BYDAY=MO")); assert_eq!(ical::exdates_from_recurrence(Some(&read[0].recurrence), ZONE), Some(vec![iso(first + week)])); assert_eq!(read[1].uid, read[0].uid, "a changed occurrence is the series'"); assert_eq!(ical::instant_of(read[1].recurrence_id.as_ref().unwrap(), ZONE).as_deref(), Some(iso(first + 2 * week).as_str())); assert_eq!(read[1].summary.as_deref(), Some("Choir, in the hall"));}#[test]fn to_dos_say_when_they_are_due_whether_done_and_their_group() { let todos = vec![ Todo { uid: "t-gate@cottondb.com".into(), title: "Fix the gate".into(), due: Some(When::Day(day(2026, 11, 21))), groups: vec!["Home, garden".into()], ..Todo::default() }, Todo { uid: "t-call@cottondb.com".into(), title: "Ring Asha".into(), due: Some(When::Instant(BANK)), status: Status::Done, done_at: Some(BANK + 60_000), ..Todo::default() }, Todo { uid: "t-drop@cottondb.com".into(), title: "Old idea".into(), status: Status::Dropped, details: Some("kept for later".into()), ..Todo::default() }, ]; let file = calendar("Home", &[], &todos, NOW); let blocks: Vec<&str> = file.split("BEGIN:VTODO\r\n").skip(1).collect(); assert_eq!(blocks.len(), 3, "{file}"); assert!(blocks[0].contains("SUMMARY:Fix the gate\r\n")); assert!(blocks[0].contains("DUE;VALUE=DATE:20261121\r\n")); assert!(blocks[0].contains("STATUS:NEEDS-ACTION\r\n")); assert!(blocks[0].contains("CATEGORIES:Home\\, garden\r\n")); assert!(blocks[1].contains("DUE:20261120T113000Z\r\n")); assert!(blocks[1].contains("STATUS:COMPLETED\r\nCOMPLETED:20261120T113100Z\r\n")); assert!(blocks[2].contains("STATUS:CANCELLED\r\n")); assert!(blocks[2].contains("DESCRIPTION:kept for later\r\n")); assert!(!blocks[2].contains("COMPLETED:")); assert!(ical::parse(&file).is_empty(), "no to-do reads as an event");}#[test]fn a_long_line_folds_at_75_octets_without_splitting_a_character() { let title = "Écrire à grand-mère — ".repeat(8); let file = calendar("Days", &[Event { uid: "long@cottondb.com".into(), title: title.clone(), start: Some(When::Instant(BANK)), ..Event::default() }], &[], NOW); for line in file.split("\r\n") { assert!(line.len() <= 75, "{} octets: {line}", line.len()); } assert_eq!(ical::parse(&file)[0].summary.as_deref(), Some(title.as_str()));}