//! When an event ends (CONTRACT.md section 14), over invented events: the //! ENDS row, set by length, read back with the end; the list keeps "to". use serde_json::{json, Value}; use tempfile::TempDir; use super::tests::{ask, lines, phone, span, zone, TODAY, ZONE}; fn add(handle: &crate::handle::CottonHandle, words: &str) -> String { let made = ask(handle, "days.add", json!({ "words": words, "timezone": ZONE, "today": TODAY, "where": "days" })); made["id"].as_str().expect("an id").to_string() } fn sheet(handle: &crate::handle::CottonHandle, id: &str) -> Value { ask(handle, "days.item", json!({ "table": "event", "id": id, "timezone": ZONE, "today": TODAY })) } fn on(ends: &Value) -> Vec { ends["choices"].as_array().expect("choices").iter().filter(|c| c["on"] == json!(true)).map(|c| c["key"].as_str().unwrap().to_string()).collect() } /// RULE (DECISIONS: when an event ends): typed "3:30 to 4:30" sets the end, the list says "to 16:30", and the sheet reads back "16:30 · 1 hour" with 1 hour on. #[test] fn a_typed_end_is_kept_and_read_back_with_its_length() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); zone(&handle); let id = add(&handle, "Massage Saturday 3:30pm to 4:30pm"); assert_eq!(lines(&span(&handle, "2026-11-21", "2026-11-21")), ["2026-11-21: 15:30 · Massage to 16:30"]); let ends = &sheet(&handle, &id)["ends"]; assert_eq!(ends["title"], json!("Ends")); assert_eq!(ends["says"], json!("16:30 · 1 hour")); assert_eq!(on(ends), ["1h"]); let labels: Vec<&str> = ends["choices"].as_array().unwrap().iter().map(|c| c["label"].as_str().unwrap()).collect(); assert_eq!(labels, ["30 min", "1 hour", "2 hours", "Pick a time", "No end"]); } /// RULE: "for an hour" is read as the length, and with nothing said a typed event is one hour. #[test] fn for_an_hour_and_nothing_said_are_one_hour() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); zone(&handle); let pill = ask(&handle, "days.read_line", json!({ "words": "Call Mum Saturday 6pm for an hour", "timezone": ZONE, "today": TODAY, "where": "days" })); assert_eq!((pill["time"].clone(), pill["end"].clone()), (json!("18:00"), json!("19:00"))); let a = add(&handle, "Call Mum Saturday 6pm for an hour"); let b = add(&handle, "Haircut Saturday 9am"); assert_eq!(sheet(&handle, &a)["ends"]["says"], json!("19:00 · 1 hour")); assert_eq!(sheet(&handle, &b)["ends"]["says"], json!("10:00 · 1 hour")); } /// RULE: a chip sets the length; changing the start keeps the length; No end clears it and the list drops "to"; Pick a time is any clock. #[test] fn a_length_chip_sets_the_end_and_a_new_start_keeps_the_length() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); zone(&handle); let id = add(&handle, "Massage Saturday 3:30pm to 4:30pm"); ask(&handle, "days.change", json!({ "table": "event", "id": id, "length": 120, "timezone": ZONE })); assert_eq!(sheet(&handle, &id)["ends"]["says"], json!("17:30 · 2 hours")); ask(&handle, "days.change", json!({ "table": "event", "id": id, "time": "9:00", "timezone": ZONE })); assert_eq!(lines(&span(&handle, "2026-11-21", "2026-11-21")), ["2026-11-21: 9:00 · Massage to 11:00"]); ask(&handle, "days.change", json!({ "table": "event", "id": id, "end": "10:15", "timezone": ZONE })); let ends = sheet(&handle, &id)["ends"].clone(); assert_eq!((ends["says"].clone(), on(&ends)), (json!("10:15 · 1 hour 15 min"), vec!["pick".to_string()])); ask(&handle, "days.change", json!({ "table": "event", "id": id, "length": "none", "timezone": ZONE })); let ends = sheet(&handle, &id)["ends"].clone(); assert_eq!((ends["says"].clone(), on(&ends)), (json!("No end"), vec!["none".to_string()])); assert_eq!(lines(&span(&handle, "2026-11-21", "2026-11-21")), ["2026-11-21: 9:00 · Massage"]); ask(&handle, "days.change", json!({ "table": "event", "id": id, "length": 30, "timezone": ZONE })); assert_eq!(sheet(&handle, &id)["ends"]["says"], json!("9:30 · 30 min")); } /// RULE: an end at or before the start's clock is the next day, said plainly: "tomorrow 0:30"; the event stays one row on its first day. #[test] fn an_end_past_midnight_says_tomorrow() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); zone(&handle); let id = add(&handle, "Party Saturday 10pm"); ask(&handle, "days.change", json!({ "table": "event", "id": id, "end": "0:30", "timezone": ZONE })); assert_eq!(sheet(&handle, &id)["ends"]["says"], json!("tomorrow 0:30 · 2 hours 30 min")); assert_eq!(lines(&span(&handle, "2026-11-21", "2026-11-22")), ["2026-11-21: 22:00 · Party to 0:30"]); assert_eq!(super::tests::refused(&handle, "days.change", json!({ "table": "event", "id": id, "length": 0, "timezone": ZONE })).0, "bad_request"); } /// RULE: all-day events and to-dos have no ENDS row. #[test] fn whole_days_and_todos_have_no_ends_row() { let dir = TempDir::new().unwrap(); let handle = phone(&dir); zone(&handle); let todo = ask(&handle, "days.add", json!({ "words": "Renew passport form", "timezone": ZONE, "today": TODAY, "where": "days" })); let sheet_todo = ask(&handle, "days.item", json!({ "table": "todo", "id": todo["id"], "timezone": ZONE, "today": TODAY })); assert!(sheet_todo.get("ends").is_none()); super::tests::sql(&handle, "INSERT INTO cotton_events (id, title, starts_at, ends_at, all_day, created_at, updated_at) VALUES ('00000000-0000-4000-8000-0000000000e1', 'Rose in town', '2026-11-21T00:00:00.000Z', '2026-11-21T23:59:59.999Z', 1, '2026-11-01T00:00:00.000Z', '2026-11-01T00:00:00.000Z')"); assert_eq!(sheet(&handle, "00000000-0000-4000-8000-0000000000e1")["ends"], Value::Null); }