Cotton code
88 lines · 6.6 KB · RustRaw
//! `days.export` over invented days, signed out: nobody here is real.use serde_json::json;use tempfile::TempDir;use super::super::tests::{ask, phone, refused, sql, ZONE};/// The file the answer names, read, and the answer.fn exported(handle: &crate::handle::CottonHandle, request: serde_json::Value) -> (String, serde_json::Value) {    let answer = ask(handle, "days.export", request);    let text = std::fs::read_to_string(answer["path"].as_str().expect("a path")).expect("the file");    (text, answer)}#[test]fn everything_goes_as_one_calendar_file_the_reader_takes_back() {    let dir = TempDir::new().unwrap();    let handle = phone(&dir);    sql(&handle, "INSERT INTO cotton_events (id, title, notes, starts_at, ends_at, all_day, created_at, updated_at) VALUES        ('ev-visit', 'Rose in town', NULL, '2026-11-20T00:00:00.000Z', '2026-11-21T23:59:59.999Z', 1, '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z'),        ('ev-bank', 'Call with the bank', 'Bring the forms', '2026-11-20T11:30:00.000Z', '2026-11-20T12:00:00.000Z', 0, '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z'),        ('ev-gone', 'Binned', NULL, '2026-11-22T09:00:00.000Z', NULL, 0, '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z')");    sql(&handle, "UPDATE cotton_events SET trashed_at = '2026-11-02T00:00:00.000Z' WHERE id = 'ev-gone'");    sql(&handle, "INSERT INTO cotton_events (id, title, starts_at, ends_at, all_day, rrule, exdates, created_at, updated_at) VALUES        ('ev-choir', 'Choir', '2026-11-02T19:00:00.000Z', '2026-11-02T21:00:00.000Z', 0, 'FREQ=WEEKLY;BYDAY=MO', '[\"2026-11-09T19:00:00.000Z\"]', '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z')");    sql(&handle, "INSERT INTO cotton_events (id, title, starts_at, ends_at, all_day, series_id, original_starts_at, created_at, updated_at) VALUES        ('ev-choir-hall', 'Choir, in the hall', '2026-11-16T20:00:00.000Z', '2026-11-16T22:00:00.000Z', 0, 'ev-choir', '2026-11-16T19:00:00.000Z', '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z')");    let gate = ask(&handle, "days.add", json!({ "words": "Fix the gate Saturday", "timezone": ZONE, "today": super::super::tests::TODAY, "where": "days" }));    let gate_id = gate["id"].as_str().or_else(|| gate["item"]["id"].as_str()).map(str::to_string);    let (text, answer) = exported(&handle, json!({ "timezone": ZONE }));    assert_eq!(answer["file_name"], "Days.ics");    assert_eq!(answer["mime"], "text/calendar");    assert_eq!(answer["count"], 5, "four events (one a changed occurrence) and the to-do; the binned one stays out: {text}");    assert!(text.contains("X-WR-CALNAME:Days\r\n"));    let read = cotton_core::import::ical::parse(&text);    let titles: Vec<&str> = read.iter().filter_map(|e| e.summary.as_deref()).collect();    assert_eq!(titles, ["Choir", "Choir, in the hall", "Rose in town", "Call with the bank"]);    let visit = read.iter().find(|e| e.uid == "ev-visit@cottondb.com").expect("the visit");    assert_eq!((visit.start.as_ref().unwrap().value.as_str(), visit.end.as_ref().unwrap().value.as_str()), ("20261120", "20261122"));    let choir = read.iter().find(|e| e.summary.as_deref() == Some("Choir")).unwrap();    assert_eq!(cotton_core::import::ical::rrule_from_recurrence(Some(&choir.recurrence)).as_deref(), Some("FREQ=WEEKLY;BYDAY=MO"));    assert_eq!(cotton_core::import::ical::exdates_from_recurrence(Some(&choir.recurrence), ZONE), Some(vec!["2026-11-09T19:00:00.000Z".to_string()]));    let hall = read.iter().find(|e| e.summary.as_deref() == Some("Choir, in the hall")).unwrap();    assert_eq!(hall.uid, choir.uid, "a changed occurrence is filed under its series");    assert_eq!(cotton_core::import::ical::instant_of(hall.recurrence_id.as_ref().unwrap(), ZONE).as_deref(), Some("2026-11-16T19:00:00.000Z"));    assert_eq!(read.iter().find(|e| e.uid == "ev-bank@cottondb.com").unwrap().description.as_deref(), Some("Bring the forms"));    assert!(text.contains("BEGIN:VTODO\r\n"), "{text}");    assert!(text.contains("SUMMARY:Fix the gate\r\n"), "{text}");    assert!(text.contains("DUE;VALUE=DATE:20261121\r\n"), "Saturday, a day with no time: {text}");    if let Some(id) = gate_id {        assert!(text.contains(&format!("UID:{id}@cottondb.com\r\n")));    }}#[test]fn one_group_goes_as_its_own_to_dos_under_its_name() {    let dir = TempDir::new().unwrap();    let handle = phone(&dir);    sql(&handle, "INSERT INTO cotton_events (id, title, starts_at, all_day, created_at, updated_at) VALUES        ('ev-bank', 'Call with the bank', '2026-11-20T11:30:00.000Z', 0, '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z')");    sql(&handle, "INSERT INTO cotton_todos (id, workspace_id, title, status, created_at, updated_at) VALUES        ('td-gate', '$WS', 'Fix the gate', 'open', '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z'),        ('td-milk', '$WS', 'Buy milk', 'done', '2026-11-01T00:00:01.000Z', '2026-11-01T00:00:01.000Z'),        ('td-call', '$WS', 'Ring Asha', 'open', '2026-11-01T00:00:02.000Z', '2026-11-01T00:00:02.000Z')");    sql(&handle, "UPDATE cotton_todos SET completed_at = '2026-11-19T08:00:00.000Z' WHERE id = 'td-milk'");    let home = ask(&handle, "groups.create", json!({ "table": "todo", "name": "Home: house/garden", "records": ["td-gate", "td-milk"] }));    let home_id = home["id"].as_str().or_else(|| home["group"]["id"].as_str()).expect("the group's id").to_string();    let (text, answer) = exported(&handle, json!({ "timezone": ZONE, "group": home_id }));    assert_eq!(answer["file_name"], "Home house garden.ics", "a name a folder takes");    assert_eq!(answer["count"], 2);    assert!(text.contains("X-WR-CALNAME:Home: house/garden\r\n"), "{text}");    assert!(!text.contains("BEGIN:VEVENT"), "a group holds to-dos only");    assert!(text.contains("SUMMARY:Fix the gate\r\n") && text.contains("SUMMARY:Buy milk\r\n") && !text.contains("Ring Asha"));    assert!(text.contains("STATUS:COMPLETED\r\nCOMPLETED:20261119T080000Z\r\n"), "{text}");    assert!(text.contains("CATEGORIES:Home: house/garden\r\n"));    // Everything carries the group on its to-dos too.    let (all, _) = exported(&handle, json!({ "timezone": ZONE }));    assert_eq!(all.matches("CATEGORIES:Home: house/garden\r\n").count(), 2);    assert!(all.contains("SUMMARY:Ring Asha\r\n") && all.contains("SUMMARY:Call with the bank\r\n"));}#[test]fn nothing_to_export_is_said_and_an_unknown_group_refused() {    let dir = TempDir::new().unwrap();    let handle = phone(&dir);    assert_eq!(refused(&handle, "days.export", json!({ "timezone": ZONE })), ("refused".to_string(), "There is nothing to export.".to_string()));    assert_eq!(refused(&handle, "days.export", json!({ "timezone": ZONE, "group": "no-such-group" })).0, "not_found");}