Cotton code
461 lines · 13.4 KB · RustRaw
//! Ported case-for-case from `apps/cotton/__tests__/lib/homeToday.test.ts`.//!//! **The zone is the zone its TypeScript ran in.**//! `homeToday.test.ts` sets no `process.env.TZ`: every instant in it is a//! zoneless local string and `NOW` is built the same way, so the file is//! self-consistent in whatever zone the machine stands in. `parse_date_ms` is//! this crate's `new Date` and reads a zoneless date-time as UTC, so naming//! `UTC` here reproduces that run exactly — the same literals, the same//! answers.//!//! Every fixture is invented. The names are the ones the rest of this crate's//! people fixtures use, in place of the TypeScript's.use super::*;// ── homeToday.test.ts ────────────────────────────────────────────────────────//// Home's glance band. Wall clock is frozen at 2026-08-08 10:00 local so every// "today" boundary in these cases is explicit rather than ambient.const ZONE: &str = "UTC";const NOW: &str = "2026-08-08T10:00:00";const NO_TODOS: &[BandTodo] = &[];const NO_EVENTS: &[BandOccurrence] = &[];fn now() -> i64 {    parse_date_ms(NOW).expect("NOW parses")}fn todo() -> BandTodo {    BandTodo {        id: "t1".to_string(),        status: "open".to_string(),        due_at: None,        snoozed_until: None,    }}fn occurrence() -> BandOccurrence {    BandOccurrence {        id: "e1".to_string(),        all_day: false,        starts_at: "2026-08-08T14:00:00".to_string(),        ends_at: Some("2026-08-08T15:00:00".to_string()),    }}fn picked_todos(todos: &[BandTodo]) -> Vec<TodayTodo<BandTodo>> {    select_today_todos(todos, now(), ZONE).expect("the zone is known")}fn picked_events(occurrences: &[BandOccurrence]) -> Vec<TodayEvent<BandOccurrence>> {    select_today_events(occurrences, now(), ZONE).expect("the zone is known")}fn band(    todos: &[BandTodo],    occurrences: &[BandOccurrence],    now_ms: i64,    timezone: &str,) -> TodayBand<BandTodo, BandOccurrence> {    select_today_band(&TodayBandInput {        todos,        occurrences,        now_ms,        timezone,    })    .expect("the zone is known")}/// `"todo:id"` / `"event:id"`, in render order — the thing the band promises.fn order(band: &TodayBand<BandTodo, BandOccurrence>) -> Vec<String> {    band.entries        .iter()        .map(|entry| match entry {            TodayEntry::Todo(row) => format!("todo:{}", row.todo.id),            TodayEntry::Event(row) => format!("event:{}", row.occurrence.id),        })        .collect()}// ── selectTodayTodos ─────────────────────────────────────────────────────────#[test]fn keeps_open_todos_due_today() {    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-08T17:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked.len(), 1);    assert_eq!(picked[0].state, TodayTodoState::Today);}#[test]fn marks_a_todo_due_before_today_as_overdue_and_puts_it_first() {    let picked = picked_todos(&[        BandTodo {            id: "due-today".to_string(),            due_at: Some("2026-08-08T09:00:00".to_string()),            ..todo()        },        BandTodo {            id: "late".to_string(),            due_at: Some("2026-08-05T09:00:00".to_string()),            ..todo()        },    ]);    assert_eq!(        picked            .iter()            .map(|p| p.todo.id.as_str())            .collect::<Vec<_>>(),        ["late", "due-today"]    );    assert_eq!(picked[0].state, TodayTodoState::Overdue);}#[test]fn hides_a_todo_snoozed_beyond_today_that_is_what_snooze_means() {    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-01T09:00:00".to_string()),        snoozed_until: Some("2026-08-20T09:00:00".to_string()),        ..todo()    }]);    assert!(picked.is_empty());}#[test]fn shows_a_todo_arriving_today_off_a_snooze() {    let picked = picked_todos(&[BandTodo {        due_at: None,        snoozed_until: Some("2026-08-08T18:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked.len(), 1);    assert_eq!(picked[0].state, TodayTodoState::Snoozed);}#[test]fn leaves_undated_todos_in_their_lists() {    assert!(picked_todos(&[BandTodo {        due_at: None,        ..todo()    }])    .is_empty());}#[test]fn ignores_todos_that_are_done_or_dropped() {    let done = BandTodo {        id: "d".to_string(),        status: "done".to_string(),        due_at: Some("2026-08-08T09:00:00".to_string()),        ..todo()    };    let dropped = BandTodo {        id: "x".to_string(),        status: "dropped".to_string(),        due_at: Some("2026-08-08T09:00:00".to_string()),        ..todo()    };    assert!(picked_todos(&[done, dropped]).is_empty());}#[test]fn ignores_todos_due_after_today() {    assert!(picked_todos(&[BandTodo {        due_at: Some("2026-08-09T09:00:00".to_string()),        ..todo()    }])    .is_empty());}// ── selectTodayTodos › due earlier today ─────────────────────────────────────#[test]fn due_earlier_today_marks_a_passed_clock_time_late() {    // (the band read it like an evening row)    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-08T09:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked[0].state, TodayTodoState::Late);}#[test]fn due_earlier_today_leaves_a_still_ahead_time_alone() {    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-08T15:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked[0].state, TodayTodoState::Today);}#[test]fn due_earlier_today_never_marks_a_date_only_todo_late() {    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-08T00:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked[0].state, TodayTodoState::Today);}#[test]fn due_earlier_today_keeps_a_previous_day_as_overdue() {    let picked = picked_todos(&[BandTodo {        due_at: Some("2026-08-07T09:00:00".to_string()),        ..todo()    }]);    assert_eq!(picked[0].state, TodayTodoState::Overdue);}// ── readsLate ────────────────────────────────────────────────────────────────#[test]fn reads_late_is_true_for_both_debt_states_only() {    assert!(reads_late(TodayTodoState::Overdue));    assert!(reads_late(TodayTodoState::Late));    assert!(!reads_late(TodayTodoState::Today));    assert!(!reads_late(TodayTodoState::Snoozed));}// ── selectTodayEvents ────────────────────────────────────────────────────────#[test]fn keeps_an_occurrence_still_ahead_today() {    assert_eq!(picked_events(&[occurrence()]).len(), 1);}#[test]fn drops_an_occurrence_that_already_finished() {    let done = BandOccurrence {        starts_at: "2026-08-08T08:00:00".to_string(),        ends_at: Some("2026-08-08T09:00:00".to_string()),        ..occurrence()    };    assert!(picked_events(&[done]).is_empty());}#[test]fn keeps_an_occurrence_in_progress_right_now() {    let running = BandOccurrence {        starts_at: "2026-08-08T09:30:00".to_string(),        ends_at: Some("2026-08-08T11:00:00".to_string()),        ..occurrence()    };    assert_eq!(picked_events(&[running]).len(), 1);}#[test]fn treats_an_all_day_event_as_present_for_the_whole_day_and_sorts_it_first() {    let all_day = BandOccurrence {        id: "all".to_string(),        all_day: true,        starts_at: "2026-08-08T00:00:00".to_string(),        ends_at: None,    };    let picked = picked_events(&[occurrence(), all_day]);    assert_eq!(        picked            .iter()            .map(|p| p.occurrence.id.as_str())            .collect::<Vec<_>>(),        ["all", "e1"]    );}#[test]fn ignores_occurrences_on_another_day() {    let tomorrow = BandOccurrence {        starts_at: "2026-08-09T14:00:00".to_string(),        ends_at: Some("2026-08-09T15:00:00".to_string()),        ..occurrence()    };    assert!(picked_events(&[tomorrow]).is_empty());}// ── selectTodayBand ──────────────────────────────────────────────────────────#[test]fn reports_a_clear_day_when_nothing_is_left() {    let band = band(NO_TODOS, NO_EVENTS, now(), ZONE);    assert!(band.is_clear);    assert!(band.entries.is_empty());}#[test]fn is_not_clear_when_anything_remains() {    let band = band(        &[BandTodo {            due_at: Some("2026-08-08T17:00:00".to_string()),            ..todo()        }],        NO_EVENTS,        now(),        ZONE,    );    assert!(!band.is_clear);    assert_eq!(band.entries.len(), 1);}#[test]fn interleaves_todos_and_events_by_time_one_timeline_not_two_lists() {    // The defect this replaces: every todo rendered above every event, so a    // 6:00 pm todo sat above a 3:30 pm meeting inside one time-sorted-looking    // list.    let band = band(        &[            BandTodo {                id: "evening-todo".to_string(),                due_at: Some("2026-08-08T18:00:00".to_string()),                ..todo()            },            BandTodo {                id: "noon-todo".to_string(),                due_at: Some("2026-08-08T12:00:00".to_string()),                ..todo()            },        ],        &[BandOccurrence {            id: "afternoon-event".to_string(),            starts_at: "2026-08-08T15:30:00".to_string(),            ends_at: Some("2026-08-08T16:00:00".to_string()),            ..occurrence()        }],        now(),        ZONE,    );    assert_eq!(        order(&band),        [            "todo:noon-todo",            "event:afternoon-event",            "todo:evening-todo"        ]    );}#[test]fn holds_overdue_todos_at_the_top_oldest_debt_first_ahead_of_the_timeline() {    let band = band(        &[            BandTodo {                id: "late-recent".to_string(),                due_at: Some("2026-08-07T09:00:00".to_string()),                ..todo()            },            BandTodo {                id: "late-old".to_string(),                due_at: Some("2026-08-02T09:00:00".to_string()),                ..todo()            },            BandTodo {                id: "later-today".to_string(),                due_at: Some("2026-08-08T17:00:00".to_string()),                ..todo()            },        ],        &[BandOccurrence {            id: "soon".to_string(),            starts_at: "2026-08-08T11:00:00".to_string(),            ends_at: Some("2026-08-08T11:30:00".to_string()),            ..occurrence()        }],        now(),        ZONE,    );    assert_eq!(        order(&band),        [            "todo:late-old",            "todo:late-recent",            "event:soon",            "todo:later-today"        ]    );}#[test]fn sorts_a_date_only_todo_at_the_start_of_its_day() {    // Midnight due instant = no clock time; it opens the day rather than    // landing at whatever hour it was created.    let band = band(        &[BandTodo {            id: "dateless".to_string(),            due_at: Some("2026-08-08T00:00:00".to_string()),            ..todo()        }],        &[BandOccurrence {            id: "morning".to_string(),            starts_at: "2026-08-08T11:00:00".to_string(),            ends_at: Some("2026-08-08T12:00:00".to_string()),            ..occurrence()        }],        now(),        ZONE,    );    assert_eq!(order(&band), ["todo:dateless", "event:morning"]);}#[test]fn puts_an_all_day_event_ahead_of_a_todo_anchored_to_the_same_instant() {    // An all-day event frames the day the other rows sit inside.    let band = band(        &[BandTodo {            id: "midnight-todo".to_string(),            due_at: Some("2026-08-08T00:00:00".to_string()),            ..todo()        }],        &[BandOccurrence {            id: "all-day".to_string(),            all_day: true,            starts_at: "2026-08-08T00:00:00".to_string(),            ends_at: None,        }],        now(),        ZONE,    );    assert_eq!(order(&band), ["event:all-day", "todo:midnight-todo"]);}#[test]fn marks_late_todos_as_overdue_so_the_band_can_say_so() {    let band = band(        &[BandTodo {            id: "late".to_string(),            due_at: Some("2026-08-05T09:00:00".to_string()),            ..todo()        }],        NO_EVENTS,        now(),        ZONE,    );    let TodayEntry::Todo(entry) = &band.entries[0] else {        panic!("the first entry is a todo");    };    assert_eq!(entry.state, TodayTodoState::Overdue);}#[test]fn keeps_a_snoozed_into_today_todo_in_the_timeline_at_its_arrival_time() {    let band = band(        &[BandTodo {            id: "snoozed".to_string(),            due_at: None,            snoozed_until: Some("2026-08-08T20:00:00".to_string()),            ..todo()        }],        &[BandOccurrence {            id: "earlier".to_string(),            starts_at: "2026-08-08T14:00:00".to_string(),            ends_at: Some("2026-08-08T15:00:00".to_string()),            ..occurrence()        }],        now(),        ZONE,    );    assert_eq!(order(&band), ["event:earlier", "todo:snoozed"]);}