Cotton code
232 lines · 6.0 KB · RustRaw
use super::*;fn last(linked: &[Option<&str>], sessions: &[Option<&str>]) -> Option<String> {    project_last_activity(&ProjectActivityInput {        linked_updated_at: linked,        session_last_active_at: sessions,    })    .map(str::to_string)}#[test]fn is_none_for_a_project_nothing_has_happened_to() {    assert_eq!(last(&[], &[]), None);}/// A memory's `updated_at` rides in `linked_updated_at` since v34 made it a/// link kind — there is no third stream any more.#[test]fn takes_the_newest_instant_across_both_streams() {    assert_eq!(        last(            &[                Some("2026-08-20T09:00:00.000Z"),                Some("2026-08-21T09:00:00.000Z")            ],            &[Some("2026-08-22T09:00:00.000Z")]        )        .as_deref(),        Some("2026-08-22T09:00:00.000Z")    );}#[test]fn reads_a_linked_records_own_moment_when_it_is_the_newest() {    assert_eq!(        last(            &[                Some("2026-08-25T09:00:00.000Z"),                Some("2026-08-01T09:00:00.000Z")            ],            &[Some("2026-08-22T09:00:00.000Z")]        )        .as_deref(),        Some("2026-08-25T09:00:00.000Z")    );}#[test]fn steps_over_a_null_instant_rather_than_treating_it_as_the_epoch() {    assert_eq!(        last(&[None, Some("2026-08-20T09:00:00.000Z"), None], &[None]).as_deref(),        Some("2026-08-20T09:00:00.000Z")    );}/// `2026-08-21T04:00+05:30` IS `2026-08-20T22:30Z` — half an hour EARLIER than/// the Z-spelled instant, though it sorts later as a string. A lexical/// comparison returns the `+05:30` string; only a parsed one is right.#[test]fn ranks_an_offset_bearing_instant_by_the_moment_it_denotes() {    assert_eq!(        last(            &[Some("2026-08-20T23:00:00.000Z")],            &[Some("2026-08-21T04:00:00+05:30")]        )        .as_deref(),        Some("2026-08-20T23:00:00.000Z")    );}#[test]fn returns_the_winning_instant_exactly_as_the_row_spells_it() {    assert_eq!(        last(            &[Some("2026-08-20T09:00:00.000Z")],            &[Some("2026-08-21T04:00:00+05:30")]        )        .as_deref(),        Some("2026-08-21T04:00:00+05:30")    );}#[test]fn ignores_an_unparseable_instant_instead_of_poisoning_the_answer() {    assert_eq!(        last(&[Some("not a date"), Some("2026-08-20T09:00:00.000Z")], &[]).as_deref(),        Some("2026-08-20T09:00:00.000Z")    );}// ── projectOrder ─────────────────────────────────────────────────────────────//// The TypeScript ships no test for this half; the app's list and the Mac// window's sidebar each test what they draw. These pin the comparator itself,// which is what the two of them now share.#[derive(Debug, Clone, PartialEq, Eq)]struct Project {    id: &'static str,    name: &'static str,}impl OrderedProject for Project {    fn id(&self) -> &str {        self.id    }    fn name(&self) -> &str {        self.name    }}fn ordered(projects: &[Project], overviews: &[(&str, Option<&str>)]) -> Vec<&'static str> {    let map: HashMap<String, Option<String>> = overviews        .iter()        .map(|(id, at)| ((*id).to_string(), at.map(str::to_string)))        .collect();    let mut sorted = projects.to_vec();    sorted.sort_by(|a, b| project_order(a, b, &map));    sorted.into_iter().map(|p| p.id).collect()}#[test]fn orders_projects_by_last_activity_newest_first() {    let projects = [        Project {            id: "old",            name: "Old",        },        Project {            id: "new",            name: "New",        },        Project {            id: "middle",            name: "Middle",        },    ];    assert_eq!(        ordered(            &projects,            &[                ("old", Some("2026-08-01T00:00:00.000Z")),                ("new", Some("2026-08-30T00:00:00.000Z")),                ("middle", Some("2026-08-15T00:00:00.000Z")),            ]        ),        ["new", "middle", "old"]    );}/// A project with no instant sorts to the BOTTOM, never the top — and two of/// them fall to the alphabet, which is the order an archive wants anyway.#[test]fn sinks_a_project_with_no_activity_and_alphabetises_the_ones_that_tie() {    let projects = [        Project {            id: "zed",            name: "Zed",        },        Project {            id: "amy",            name: "Amy",        },        Project {            id: "live",            name: "Live",        },    ];    assert_eq!(        ordered(            &projects,            &[                ("live", Some("2026-08-30T00:00:00.000Z")),                ("zed", None),                // `amy` is not in the record at all, which is the same thing.            ]        ),        ["live", "amy", "zed"]    );}/// Two projects that moved at the same moment fall to the name, so the order is/// TOTAL — the point of the tiebreak.#[test]fn breaks_an_exact_tie_by_name() {    let projects = [        Project {            id: "b",            name: "Beta",        },        Project {            id: "a",            name: "Alpha",        },    ];    assert_eq!(        ordered(            &projects,            &[                ("a", Some("2026-08-30T00:00:00.000Z")),                ("b", Some("2026-08-30T00:00:00.000Z")),            ]        ),        ["a", "b"]    );}/// An instant that does not parse is "no instant", the same as absent — the/// alternative is NaN deciding an order.#[test]fn treats_an_unparseable_overview_instant_as_no_activity() {    let projects = [        Project {            id: "broken",            name: "Broken",        },        Project {            id: "fine",            name: "Fine",        },    ];    assert_eq!(        ordered(            &projects,            &[                ("broken", Some("not a date")),                ("fine", Some("2026-01-01T00:00:00.000Z")),            ]        ),        ["fine", "broken"]    );}