Cotton code
284 lines · 8.1 KB · RustRaw
//! Every case of `packages/core/__tests__/engine/todoDue.test.ts`.//!//! The property under test is agreement with the phone's contract: local//! midnight IS "no time", and the instant this module writes for a day is the//! one the phone writes for the same day.use super::*;const KOLKATA: &str = "Asia/Kolkata";const NEW_YORK: &str = "America/New_York";fn due(date: &str, time: Option<&str>) -> MomentSplit {    MomentSplit {        date: date.to_string(),        time: time.map(str::to_string),    }}// ── split_moment ──────────────────────────────────────────────────────────────#[test]fn reads_local_midnight_as_a_date_with_no_time() {    // 18:30 UTC on the 3rd is 00:00 on the 4th in Kolkata.    assert_eq!(        split_moment("2026-09-03T18:30:00.000Z", KOLKATA),        Ok(due("2026-09-04", None))    );}#[test]fn reads_any_other_clock_as_a_stated_time_in_the_zone() {    assert_eq!(        split_moment("2026-09-04T03:30:00.000Z", KOLKATA),        Ok(due("2026-09-04", Some("09:00")))    );    // The same instant is the evening before in New York.    assert_eq!(        split_moment("2026-09-04T03:30:00.000Z", NEW_YORK),        Ok(due("2026-09-03", Some("23:30")))    );}#[test]fn refuses_an_instant_that_does_not_parse() {    let refusal = split_moment("yesterday", KOLKATA).expect_err("not an instant");    assert!(refusal.to_string().contains("not an instant"));    assert_eq!(        refusal.to_string(),        "cotton: \"yesterday\" is not an instant"    );}#[test]fn todo_has_clock_time_is_the_same_reading_as_a_yes_or_no() {    assert_eq!(        states_clock_time("2026-09-03T18:30:00.000Z", KOLKATA),        Ok(false)    );    assert_eq!(        states_clock_time("2026-09-04T03:30:00.000Z", KOLKATA),        Ok(true)    );}// ── join_moment ─────────────────────────────────────────────────────────#[test]fn writes_a_day_as_its_local_midnight_and_round_trips() {    let iso = join_moment(&due("2026-09-04", None), KOLKATA).expect("a writable due");    assert_eq!(iso, "2026-09-03T18:30:00.000Z");    assert_eq!(split_moment(&iso, KOLKATA), Ok(due("2026-09-04", None)));}#[test]fn writes_a_day_and_a_clock_as_that_instant_in_the_zone() {    let iso = join_moment(&due("2026-09-04", Some("09:00")), KOLKATA).expect("a writable due");    assert_eq!(iso, "2026-09-04T03:30:00.000Z");    assert_eq!(        split_moment(&iso, KOLKATA),        Ok(due("2026-09-04", Some("09:00")))    );}#[test]fn refuses_a_day_or_clock_it_cannot_store() {    assert_eq!(        join_moment(&due("2026-02-30", None), KOLKATA)            .expect_err("not a day")            .to_string(),        "cotton: \"2026-02-30\" is not a calendar day"    );    assert_eq!(        join_moment(&due("2026-09-04", Some("24:00")), KOLKATA)            .expect_err("not a clock")            .to_string(),        "cotton: \"24:00\" is not a clock time"    );}// ── the shapes ───────────────────────────────────────────────────────────────#[test]fn accept_real_days_and_twenty_four_hour_clocks_only() {    assert!(is_calendar_day("2026-09-04"));    assert!(!is_calendar_day("2026-13-04"));    assert!(!is_calendar_day("2026-02-29"));    assert!(is_calendar_day("2028-02-29"));    assert!(!is_calendar_day("4 Sep 2026"));    assert!(is_clock_time("00:00"));    assert!(is_clock_time("23:59"));    assert!(!is_clock_time("9:00"));    assert!(!is_clock_time("24:00"));    assert!(!is_clock_time("9am"));}/// The two shapes' remaining edges, so a rewritten matcher cannot loosen them.#[test]fn the_shapes_have_no_slack_in_them() {    for bad in [        "2026-00-04",        "2026-09-00",        "2026-09-31",        "2026-9-04",        "20260904",        "2026-09-04T00:00:00Z",        "",        " 2026-09-04",    ] {        assert!(!is_calendar_day(bad), "{bad}");    }    assert!(is_calendar_day("2026-04-30"));    assert!(!is_calendar_day("2026-04-31"));    for bad in ["0:00", "00:60", "24:01", "0000", "00:0", "1:2", " 00:00"] {        assert!(!is_clock_time(bad), "{bad}");    }}// ── next_moment ────────────────────────────────────────────────────────────/// 4 Sep, 09:00 Kolkata.const TIMED: &str = "2026-09-04T03:30:00.000Z";/// 4 Sep, no time.const DATE_ONLY: &str = "2026-09-03T18:30:00.000Z";#[test]fn drops_the_time_and_keeps_the_day_the_change_that_had_no_spelling() {    assert_eq!(        next_moment(            Some(TIMED),            PartChange::Unchanged,            PartChange::Cleared,            KOLKATA        ),        Ok(Some(DATE_ONLY.to_string()))    );}#[test]fn moves_the_day_and_keeps_the_hour() {    assert_eq!(        next_moment(            Some(TIMED),            PartChange::Set("2026-09-10"),            PartChange::Unchanged,            KOLKATA        ),        Ok(Some("2026-09-10T03:30:00.000Z".to_string()))    );}#[test]fn moves_a_date_only_todo_to_another_day_still_date_only() {    assert_eq!(        next_moment(            Some(DATE_ONLY),            PartChange::Set("2026-09-10"),            PartChange::Unchanged,            KOLKATA        ),        Ok(Some("2026-09-09T18:30:00.000Z".to_string()))    );}#[test]fn sets_an_hour_on_a_date_only_todo() {    assert_eq!(        next_moment(            Some(DATE_ONLY),            PartChange::Unchanged,            PartChange::Set("18:00"),            KOLKATA        ),        Ok(Some("2026-09-04T12:30:00.000Z".to_string()))    );}#[test]fn gives_an_undated_todo_a_day_with_or_without_an_hour() {    assert_eq!(        next_moment(            None,            PartChange::Set("2026-09-04"),            PartChange::Unchanged,            KOLKATA        ),        Ok(Some(DATE_ONLY.to_string()))    );    assert_eq!(        next_moment(            None,            PartChange::Set("2026-09-04"),            PartChange::Set("09:00"),            KOLKATA        ),        Ok(Some(TIMED.to_string()))    );}#[test]fn a_null_day_clears_everything_whatever_else_was_said() {    assert_eq!(        next_moment(            Some(TIMED),            PartChange::Cleared,            PartChange::Set("09:00"),            KOLKATA        ),        Ok(None)    );}#[test]fn dropping_the_time_of_an_undated_todo_changes_nothing() {    assert_eq!(        next_moment(None, PartChange::Unchanged, PartChange::Cleared, KOLKATA),        Ok(None)    );}#[test]fn refuses_an_hour_on_a_todo_with_no_day() {    let refusal = next_moment(None, PartChange::Unchanged, PartChange::Set("09:00"), KOLKATA)        .expect_err("a time needs a day");    assert!(refusal.to_string().contains("a time needs a day"));}/// The round trip that ties the two halves together, in four zones and across/// both 2026 transitions: a day written and read back is the same day, and a/// day plus a clock is the same clock.#[test]fn every_day_and_clock_round_trips_in_every_zone() {    for zone in [        KOLKATA,        NEW_YORK,        "Europe/London",        "Australia/Sydney",        "UTC",    ] {        for date in [            "2026-01-01",            "2026-03-29",            "2026-10-25",            "2026-11-01",            "2026-04-05",            "2026-12-31",        ] {            for time in [                None,                Some("00:01"),                Some("09:00"),                Some("13:45"),                Some("23:59"),            ] {                let value = due(date, time);                let iso = join_moment(&value, zone).expect("a writable due");                assert_eq!(                    split_moment(&iso, zone),                    Ok(value.clone()),                    "{zone} / {date} / {time:?}"                );            }        }    }}