//! Cotton Days' rules, over invented items. use serde_json::{json, Value}; use super::layout::{days, rows_by_day, Entry, Item, Kind, Place, Whose}; use super::words::words; use super::{day_of_key, marks, when}; use crate::js::parse_date_ms; const ZONE: &str = "Europe/London"; fn day(key: &str) -> i64 { day_of_key(key).expect("a day key") } fn at(iso: &str) -> i64 { parse_date_ms(iso).expect("an instant") } fn event(key: &str, words: &str, starts: &str, ends: Option<&str>, all_day: bool) -> Item { Item { key: key.to_string(), kind: Kind::Event, whose: Whose::Yours, words: words.to_string(), mark: None, done: false, place: Place::Event { starts: at(starts), ends: ends.map(at), all_day }, opens: json!({ "sheet": "event", "id": key }), order: 0, } } fn todo(key: &str, words: &str, on: &str, minute: Option, from: Option<&str>) -> Item { Item { key: key.to_string(), kind: Kind::Todo, whose: Whose::Yours, words: words.to_string(), mark: None, done: false, place: Place::Todo { day: day(on), minute, from: from.map(day) }, opens: json!({ "sheet": "todo", "id": key }), order: 0, } } fn summary(rows: &[super::layout::Row]) -> Vec { rows.iter() .map(|row| { let mark = match row.mark["kind"].as_str() { Some("tick") => "○".to_string(), Some("drawn") => format!("[{}]", row.mark["id"].as_str().unwrap_or("")), _ => "·".to_string(), }; let mut line = format!("{} {} {}", row.time.clone().unwrap_or_default(), mark, row.words); if let Some(suffix) = &row.suffix { line.push(' '); line.push_str(suffix); } line.trim().to_string() }) .collect() } /// RULE (DECISIONS.md, Structure): inside a day, things for the whole day first, then things at a time in order, then to-dos with no time, the ones brought forward from earlier days last; an end is a quiet "to 12:00" only where it exists, a to-do has the tick circle and an unmarked event the dot. #[test] fn a_day_reads_whole_day_then_timed_then_untimed_todos() { let mut cake = Item { key: "b".into(), kind: Kind::Birthday, whose: Whose::Birthday, words: "Akash's birthday".into(), mark: None, done: false, place: Place::Birthday { day: day("2026-11-20") }, opens: json!({ "person": "p" }), order: 0 }; cake.order = 1; let mut dinner = event("dinner", "Dinner at Rose's", "2026-11-20T19:00:00.000Z", None, false); dinner.mark = Some("fork-knife".into()); let items = vec![ todo("invoice", "Send the invoice", "2026-11-20", None, Some("2026-11-17")), dinner, todo("form", "Renew passport form", "2026-11-20", None, None), event("bank", "Call with the bank", "2026-11-20T11:30:00.000Z", Some("2026-11-20T12:00:00.000Z"), false), cake, todo("pages", "Morning pages", "2026-11-20", Some(9 * 60), None), event("rose", "Rose in town", "2026-11-20T00:00:00.000Z", Some("2026-11-20T23:59:59.999Z"), true), ]; let today = day("2026-11-20"); let rows = rows_by_day(&items, today, today, today, ZONE, words("en")).expect("lays out"); assert_eq!( summary(&rows[&today]), [ "· Rose in town", "[cake] Akash's birthday", "9:00 ○ Morning pages", "11:30 · Call with the bank to 12:00", "19:00 [fork-knife] Dinner at Rose's", "○ Renew passport form", "○ Send the invoice from Tue", ] ); assert_eq!(rows[&today][3].end.as_deref(), Some("12:00")); } /// RULE (DECISIONS.md, Structure): a many-day thing is a row on each day it covers: at its start time on its first day, a whole-day row in between, and a whole-day row with "to " on its last day; an end at midnight belongs to the evening before. #[test] fn a_many_day_event_is_a_row_on_each_day_it_covers() { let items = vec![ event("visit", "Rose in town", "2026-11-20T18:00:00.000Z", Some("2026-11-23T10:00:00.000Z"), false), event("late", "Late film", "2026-11-25T21:00:00.000Z", Some("2026-11-26T00:00:00.000Z"), false), ]; let today = day("2026-11-20"); let rows = rows_by_day(&items, today, day("2026-11-27"), today, ZONE, words("en")).expect("lays out"); let line = |key: &str| summary(&rows[&day(key)]); assert_eq!(line("2026-11-20"), ["18:00 · Rose in town"]); assert_eq!(line("2026-11-21"), ["· Rose in town"]); assert_eq!(line("2026-11-22"), ["· Rose in town"]); assert_eq!(line("2026-11-23"), ["· Rose in town to 10:00"]); assert_eq!(line("2026-11-25"), ["21:00 · Late film"]); assert!(!rows.contains_key(&day("2026-11-26")), "midnight is the evening's end, not a row on the next day"); } /// RULE (DECISIONS.md, Structure, 20 Nov 2026): every empty day that is not today folds, one alone too ("27 · a quiet day"), a run into one thin line ("22 – 25 · four quiet days"); an empty today is never folded, so every other date has a row under it. #[test] fn quiet_days_fold_and_today_never_does() { let items = vec![ event("dentist", "Dentist", "2026-11-21T10:00:00.000Z", Some("2026-11-21T10:45:00.000Z"), false), event("flight", "Flight to Hyderabad", "2026-11-26T06:40:00.000Z", None, false), event("eve", "Eve", "2026-11-28T09:00:00.000Z", None, false), ]; let entries = days(&items, day("2026-11-19"), day("2026-12-03"), day("2026-11-20"), ZONE, words("en")).expect("lays out"); let seen: Vec = entries .iter() .map(|entry| match entry { Entry::Day(d) => format!("{} {}", d.day, d.rows.len()), Entry::Fold { fold } => fold.words.clone(), }) .collect(); assert_eq!( seen, [ "19 · a quiet day", "2026-11-20 0", "2026-11-21 1", "22 – 25 · four quiet days", "2026-11-26 1", "27 · a quiet day", "2026-11-28 1", "29 Nov – 3 Dec · five quiet days", ] ); let Entry::Day(today) = &entries[1] else { panic!("today is a day") }; assert!(today.today); assert_eq!((today.number.as_str(), today.weekday.as_str(), today.month.as_str()), ("20", "Friday", "November")); let as_json = serde_json::to_value(&entries[3]).expect("serialises"); assert_eq!(as_json["fold"]["count"], json!(4)); } /// RULE (DECISIONS.md, Structure): a to-do brought onto today says "from Tue" within the last week and "from 12 Nov" further back. #[test] fn a_to_do_from_long_ago_names_its_date() { let items = vec![todo("old", "Old one", "2026-11-20", None, Some("2026-11-02"))]; let today = day("2026-11-20"); let rows = rows_by_day(&items, today, today, today, ZONE, words("en")).expect("lays out"); assert_eq!(rows[&today][0].suffix.as_deref(), Some("from 2 Nov")); } fn understood(line: &str) -> (String, Option, Option, Option, bool) { let today = day("2026-11-20"); // a Friday let got = when::read(line, today, "en"); ( got.title, got.day.map(crate::zoned_clock::day_key), got.time.map(super::layout::clock), got.end.map(super::layout::clock), got.someday, ) } /// RULE (DECISIONS.md: adding is one line): the day and time are read out of the words and the rest is the title; a weekday is the next one on or after today, "next" never today; a bare hour after the day or "at" is a time, 1 to 7 in the afternoon or evening; "someday" is no day. #[test] fn one_line_reads_the_day_and_the_time() { let s = |v: &str| Some(v.to_string()); assert_eq!(understood("Dinner with Rose Thursday 7pm"), ("Dinner with Rose".into(), s("2026-11-26"), s("19:00"), None, false)); assert_eq!(understood("dentist next Tue 3"), ("dentist".into(), s("2026-11-24"), s("15:00"), None, false)); assert_eq!(understood("someday learn the guitar"), ("learn the guitar".into(), None, None, None, true)); assert_eq!(understood("Call mum tomorrow at 10:30"), ("Call mum".into(), s("2026-11-21"), s("10:30"), None, false)); assert_eq!(understood("Standup friday 9:15am"), ("Standup".into(), s("2026-11-20"), s("9:15"), None, false)); assert_eq!(understood("Standup next friday"), ("Standup".into(), s("2026-11-27"), None, None, false)); assert_eq!(understood("Buy 3 lemons"), ("Buy 3 lemons".into(), None, None, None, false)); assert_eq!(understood("Party on 26 Dec 7-11pm"), ("Party".into(), s("2026-12-26"), s("19:00"), s("23:00"), false)); assert_eq!(understood("Pay rent 1/12"), ("Pay rent".into(), s("2026-12-01"), None, None, false)); assert_eq!(understood("Renew passport March 3rd"), ("Renew passport".into(), s("2027-03-03"), None, None, false)); assert_eq!(understood("Lunch noon"), ("Lunch".into(), None, s("12:00"), None, false)); assert_eq!(understood("Gym tonight at 19:00"), ("Gym".into(), s("2026-11-20"), s("19:00"), None, false)); } /// RULE (DECISIONS: when an event ends, 22 Nov 2026): an end is read from "3:30 to 4:30" and from a length after the time ("for an hour", "for 90 minutes", "for 2h", "for half an hour", "for an hour and a half"); "for" with no length stays in the title. #[test] fn one_line_reads_an_end_or_a_length() { let s = |v: &str| Some(v.to_string()); assert_eq!(understood("Massage Saturday 3:30 to 4:30"), ("Massage".into(), s("2026-11-21"), s("15:30"), s("16:30"), false)); assert_eq!(understood("Call Mum Saturday 6pm for an hour"), ("Call Mum".into(), s("2026-11-21"), s("18:00"), s("19:00"), false)); assert_eq!(understood("Run tomorrow 7am for 90 minutes"), ("Run".into(), s("2026-11-21"), s("7:00"), s("8:30"), false)); assert_eq!(understood("Workshop Monday 10 for 2h"), ("Workshop".into(), s("2026-11-23"), s("10:00"), s("12:00"), false)); assert_eq!(understood("Tea at 4pm for half an hour"), ("Tea".into(), None, s("16:00"), s("16:30"), false)); assert_eq!(understood("Film tonight 9pm for an hour and a half"), ("Film".into(), s("2026-11-20"), s("21:00"), s("22:30"), false)); assert_eq!(understood("Shopping for dinner tomorrow 5pm"), ("Shopping for dinner".into(), s("2026-11-21"), s("17:00"), None, false)); } /// RULE: what was read as the day and the time is marked on the line, in characters, so the phone can underline it. #[test] fn one_line_says_where_it_read_the_day_and_the_time() { let got = when::read("Dinner with Rose Thursday 7pm", day("2026-11-20"), "en"); let said: Vec = got.said.iter().map(|one| json!([one.from, one.to, one.what])).collect(); assert_eq!(said, [json!([17, 25, "day"]), json!([26, 29, "time"])]); let words = words("en"); assert_eq!(when::pill(got.day, got.time, got.end, words), "Thu 26 Nov · 19:00"); assert_eq!(when::pill(None, None, None, words), "Sometime"); } /// RULE (DECISIONS.md, Marks): the marks whose words match what was typed are offered, best first, in any language the catalogue holds, offline; nothing typed that means nothing offers nothing. #[test] fn typed_words_find_their_marks() { assert_eq!(marks::find("Dinner with Rose", "en", 4), ["fork-knife"]); assert_eq!(marks::find("Dentist", "en", 4), ["tooth"]); assert_eq!(marks::find("Flight to Hyderabad", "en", 4), ["plane"]); assert_eq!(marks::find("Akash's birthday", "en", 4), ["cake"]); assert_eq!(marks::find("cumpleaños de Ana", "es", 4), ["cake"]); assert_eq!(marks::find("गिटार", "hi", 4), ["guitar"]); assert_eq!(marks::find("Café with Jo", "en", 4), ["cup"]); assert_eq!(marks::find("dent", "en", 4), ["tooth"], "the last word may still be being typed"); assert_eq!(marks::find("Renew the form for Priya", "en", 4), ["pen"]); assert!(marks::find("xyzzy", "en", 4).is_empty()); assert!(marks::catalogue().len() >= 40); let ids: std::collections::BTreeSet<&str> = marks::catalogue().iter().map(|m| m.id.as_str()).collect(); assert_eq!(ids.len(), marks::catalogue().len(), "every id once"); } /// RULE (DECISIONS.md, Structure, 20 Nov 2026): a timed event shorter than a day that ends after midnight is one row on the day it starts, "to 2:00"; a longer one is still a row on each day it covers. #[test] fn an_evening_past_midnight_is_one_row_on_its_first_day() { let items = vec![ event("party", "Party at Rose's", "2026-11-21T22:00:00.000Z", Some("2026-11-22T02:00:00.000Z"), false), event("shift", "Night shift", "2026-11-23T20:00:00.000Z", Some("2026-11-25T08:00:00.000Z"), false), ]; let rows = rows_by_day(&items, day("2026-11-21"), day("2026-11-26"), day("2026-11-20"), ZONE, words("en")).expect("lays out"); let line = |key: &str| summary(rows.get(&day(key)).map(Vec::as_slice).unwrap_or_default()); assert_eq!(line("2026-11-21"), ["22:00 · Party at Rose's to 2:00"]); assert_eq!(rows[&day("2026-11-21")][0].end.as_deref(), Some("2:00")); assert!(line("2026-11-22").is_empty(), "the party is not on the next day"); assert_eq!(line("2026-11-23"), ["20:00 · Night shift"]); assert_eq!(line("2026-11-25"), ["· Night shift to 8:00"]); } /// RULE (DECISIONS.md, Structure, 20 Nov 2026): one event held twice (the same words, start and end, as from two shared calendars) is one row; two events at one time with other words are two. #[test] fn an_event_held_twice_is_one_row() { let mut twice = event("b", "Team sync", "2026-11-23T10:00:00.000Z", Some("2026-11-23T10:30:00.000Z"), false); twice.whose = Whose::Google; let items = vec![ event("a", "Team sync", "2026-11-23T10:00:00.000Z", Some("2026-11-23T10:30:00.000Z"), false), twice, event("c", "Lunch", "2026-11-23T10:00:00.000Z", Some("2026-11-23T10:30:00.000Z"), false), ]; let rows = rows_by_day(&items, day("2026-11-23"), day("2026-11-23"), day("2026-11-20"), ZONE, words("en")).expect("lays out"); assert_eq!(summary(&rows[&day("2026-11-23")]), ["10:00 · Lunch to 10:30", "10:00 · Team sync to 10:30"]); assert_eq!(rows[&day("2026-11-23")][1].key, "a", "the first read stands"); } /// RULE (DECISIONS.md, Marks, 20 Nov 2026): every mark of the catalogue is drawn, as 5 to 8 strokes of absolute M and C on the 24 square; the picker offers only drawn marks, the ones the words find first, then the rest. #[test] fn every_mark_is_drawn_and_the_picker_offers_matches_first() { for mark in marks::catalogue() { assert!((5..=8).contains(&mark.strokes.len()), "{} has {} strokes", mark.id, mark.strokes.len()); for stroke in &mark.strokes { assert!(stroke.starts_with('M') && stroke.chars().all(|c| "MC .-0123456789".contains(c)), "{}: {stroke}", mark.id); } assert!(marks::drawn(&mark.id)); } let offered: Vec<&str> = marks::offered("Dinner with Rose", "en").iter().map(|mark| mark.id.as_str()).collect(); assert_eq!(offered[0], "fork-knife"); assert_eq!(offered.len(), marks::catalogue().len()); assert_eq!(marks::offered("", "en")[0].id, marks::catalogue()[0].id); } /// RULE (DECISIONS.md, adding is one line): "10pm to 2am" reads an end after midnight; "11-1" is still the afternoon's. #[test] fn an_evening_said_to_end_after_midnight_keeps_its_end() { let got = when::read("Party at Rose's Saturday 10pm to 2am", day("2026-11-20"), "en"); assert_eq!((got.title.as_str(), got.time, got.end), ("Party at Rose's", Some(22 * 60), Some(2 * 60))); let got = when::read("Lunch Saturday 11-1", day("2026-11-20"), "en"); assert_eq!((got.time, got.end), (Some(11 * 60), Some(13 * 60))); } /// RULE (DECISIONS.md, Show: Repeating, 21 Nov 2026): a repeating item's rhythm is said in plain words, its first day naming what the rule leaves out. #[test] fn a_rhythm_is_said_in_words() { use super::rhythm::rhythm; let en = words("en"); let monday = day("2026-11-02"); for (rule, said) in [ ("FREQ=DAILY", "Every day"), ("FREQ=DAILY;INTERVAL=3", "Every three days"), ("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR", "Every weekday"), ("FREQ=WEEKLY", "Every Monday"), ("FREQ=WEEKLY;BYDAY=TU,TH", "Every Tue and Thu"), ("FREQ=WEEKLY;BYDAY=MO,WE,FR", "Every Mon, Wed and Fri"), ("FREQ=WEEKLY;INTERVAL=2;BYDAY=FR", "Every two weeks on Friday"), ("FREQ=MONTHLY", "Every month on the 2nd"), ("FREQ=MONTHLY;BYMONTHDAY=21", "Every month on the 21st"), ("FREQ=MONTHLY;BYDAY=2TU", "Every month"), ("FREQ=YEARLY", "Every year on 2 Nov"), ("FREQ=HOURLY", "Repeats"), ] { assert_eq!(rhythm(rule, monday, en), said, "{rule}"); } } /// RULE (DECISIONS.md, search, 21 Nov 2026): search folds case and Latin accents in any script, and every typed word must be found; a Devanagari vowel sign stays part of its letter. #[test] fn search_folds_case_and_accents_and_finds_every_word() { use super::rhythm::{fold, holds_all}; let asked = |typed: &str| fold(typed).split_whitespace().map(str::to_string).collect::>(); assert!(holds_all(&fold("Café with Rose"), &asked("CAFE rose"))); assert!(!holds_all(&fold("Café with Rose"), &asked("cafe akash"))); assert!(holds_all(&fold("दाँत का डॉक्टर"), &asked("डॉक्टर"))); assert!(!holds_all(&fold("दाँत का डॉक्टर"), &asked("डक्टर"))); assert!(holds_all(&fold("Москва ЗАВТРА"), &asked("завтра"))); assert!(!holds_all(&fold("anything"), &asked(""))); } /// RULE (DECISIONS.md, never two folds in a row, 21 Nov 2026): a fold across a year says each end's year. #[test] fn a_fold_across_a_year_says_its_years() { use super::layout::fold_of; assert_eq!(fold_of(day("2026-12-30"), day("2027-01-02"), words("en")).words, "30 Dec 2026 – 2 Jan 2027 · four quiet days"); assert_eq!(fold_of(day("2026-11-30"), day("2026-12-02"), words("en")).words, "30 Nov – 2 Dec · three quiet days"); }