Cotton code
69 lines · 2.7 KB · RustRaw
use super::*;fn chat(id: &str, unread: bool, at: Option<&str>, channels: &[&str]) -> StreamChat {    StreamChat { id: id.into(), unread, at: at.map(str::to_string), channels: channels.iter().map(|c| c.to_string()).collect() }}fn ids(chats: &[StreamChat]) -> Vec<&str> {    chats.iter().map(|chat| chat.id.as_str()).collect()}#[test]fn unread_first_then_newest_across_every_source() {    let read = vec![        chat("wa-old", false, Some("2026-11-01T09:00:00Z"), &["whatsapp"]),        chat("cotton-unread-old", true, Some("2026-10-01T09:00:00Z"), &["cotton"]),        chat("sms-new", false, Some("2026-11-14T09:00:00Z"), &["sms"]),        chat("ig-unread-new", true, Some("2026-11-13T09:00:00Z"), &["instagram"]),        chat("empty", false, None, &[]),    ];    let stream = ordered(read);    assert_eq!(ids(&stream), ["ig-unread-new", "cotton-unread-old", "sms-new", "wa-old", "empty"]);    assert_eq!(opens(&stream), Some("ig-unread-new"));}#[test]fn with_nothing_unread_it_opens_on_the_newest() {    let stream = ordered(vec![        chat("a", false, Some("2026-11-01T09:00:00Z"), &["cotton"]),        chat("b", false, Some("2026-11-02T09:00:00Z"), &["cotton"]),    ]);    assert_eq!(opens(&stream), Some("b"));    assert_eq!(unread_count(&stream), 0);}#[test]fn the_count_is_the_unread_chats() {    let stream = vec![chat("a", true, None, &[]), chat("b", false, None, &[]), chat("c", true, None, &[])];    assert_eq!(unread_count(&stream), 2);}#[test]fn ties_keep_the_order_read() {    let stream = ordered(vec![        chat("first", false, Some("2026-11-01T09:00:00Z"), &[]),        chat("second", false, Some("2026-11-01T09:00:00Z"), &[]),    ]);    assert_eq!(ids(&stream), ["first", "second"]);}#[test]fn a_chat_read_while_standing_keeps_its_place_and_an_arrival_goes_in_front() {    // Stood on [u1, u2, r1]; u1 was read since, and a new chat arrived unread.    let fresh = ordered(vec![        chat("u1", false, Some("2026-11-10T09:00:00Z"), &["cotton"]),        chat("u2", true, Some("2026-11-09T09:00:00Z"), &["cotton"]),        chat("r1", false, Some("2026-11-12T09:00:00Z"), &["whatsapp"]),        chat("new", true, Some("2026-11-14T09:00:00Z"), &["cotton"]),    ]);    let standing = ["u1".to_string(), "u2".to_string(), "r1".to_string(), "gone".to_string()];    assert_eq!(ids(&standing_order(fresh.clone(), &standing)), ["new", "u1", "u2", "r1"]);    // Standing on nothing (a launch, a search): the stream's own order.    assert_eq!(ids(&standing_order(fresh, &[])), ["new", "u2", "r1", "u1"]);}#[test]fn the_mark_is_the_first_source() {    assert_eq!(mark(&chat("a", false, None, &["whatsapp", "sms"])), Some("whatsapp"));    assert_eq!(mark(&chat("a", false, None, &[])), None);}