Cotton code
154 lines · 6.6 KB · RustRaw
//! The vocabulary's own shape, and the four-way disagreement held as a//! baseline rather than as a surprise.use std::collections::BTreeSet;use super::*;fn set(list: &[&'static str]) -> BTreeSet<&'static str> {    list.iter().copied().collect()}#[test]fn both_lists_are_sorted_and_name_nothing_twice() {    // Sorted so a set comparison in either direction reads as a diff, and    // duplicate-free so a count is a count.    for (name, list) in [        ("PUSH_TYPES", PUSH_TYPES),        ("SERVER_PUSH_TYPES", SERVER_PUSH_TYPES),        ("UNMAPPED_BY_THE_APP", UNMAPPED_BY_THE_APP),        ("EMITTED_BY_NOBODY", EMITTED_BY_NOBODY),        ("RETIRED_PUSH_TYPES", RETIRED_PUSH_TYPES),        (            "SENT_FOR_A_ROW_THE_PHONE_ALSO_PLANS",            SENT_FOR_A_ROW_THE_PHONE_ALSO_PLANS,        ),    ] {        assert!(            list.windows(2).all(|pair| pair[0] < pair[1]),            "{name} is not sorted, or names something twice"        );    }}// Empty vocabularies cannot prove that every emitted push opens a page.#[test]fn the_notification_vocabularies_are_nonempty() {    assert!(!PUSH_TYPES.is_empty());    assert!(!SERVER_PUSH_TYPES.is_empty());}/// RULE: Every owner push opens a declared app route; coordinator notices need no phone route because they never reach the owner.#[test]fn the_two_vocabularies_disagree_in_exactly_the_places_that_are_written_down() {    // THE GUARD. Not "the two lists are equal" — they are not, and a test that    // demanded it would be red on arrival and deleted by the next person. This    // is the shape that survives: the difference must be EXACTLY the two named    // lists, so a new sender with no map entry, or a map entry with no sender,    // fails here and names itself.    let app = set(PUSH_TYPES);    let server = set(SERVER_PUSH_TYPES);    let server_only: BTreeSet<&str> = server.difference(&app).copied()        .filter(|kind| push_audience(kind) == Some(PushAudience::Owner))        .collect();    assert_eq!(        server_only,        set(UNMAPPED_BY_THE_APP),        "a server type the app maps nothing for. Either add it to the Swift table (a behaviour \         change, which needs asking) or add it to UNMAPPED_BY_THE_APP with its ledger row."    );    let app_only: BTreeSet<&str> = app.difference(&server).copied().collect();    assert_eq!(        app_only,        set(EMITTED_BY_NOBODY),        "the app maps a type nothing sends. Either land its sender, or add it to \         EMITTED_BY_NOBODY with its ledger row."    );}/// RULE: A retired notification type is gone from both the server and the app, so an old one still in the tray opens nothing but the app.#[test]fn every_retired_type_is_in_neither_list() {    // Ported from `notificationRoutes.test.ts`'s "no longer routes the call    // pushes the cell used to send", and now covering the sharing plane's five    // as well. A tap on an old notification still sitting in the tray opens    // the app and nothing else.    for retired in RETIRED_PUSH_TYPES {        assert!(            !PUSH_TYPES.contains(retired),            "{retired} is back in the app's table without its sender"        );        assert!(            !SERVER_PUSH_TYPES.contains(retired),            "{retired} is back in the server's vocabulary without its sender"        );        assert!(!app_maps_push_type(retired));    }}/// RULE: The server never pushes a notification the phone already schedules for itself, so nothing arrives twice.#[test]fn nothing_the_server_sends_is_a_row_the_phone_already_plans_for_itself() {    // THE DEDUPE, and today it is a disjointness rather than a mechanism: the    // phone schedules every time-based notification from its own copy    // (`rules::reminders`) and the server pushes only about rows a phone could    // not have known about. The two sets do not overlap, and this is what keeps    // that from being a coincidence.    //    // A type added to the list is a promise that the sender ALSO withdraws the    // phone's local twin — see the constant's own doc for how. A type added    // with no such work is what this assertion is here to stop.    assert!(        SENT_FOR_A_ROW_THE_PHONE_ALSO_PLANS.is_empty(),        "a server push now covers a row the phone plans locally: the sender owes the \         withdrawal of the local twin before this list may name it"    );    for push_type in SENT_FOR_A_ROW_THE_PHONE_ALSO_PLANS {        // Vacuous while the list is empty, and it is the half that stops the        // list drifting into a place to park a name nobody sends.        assert!(            SERVER_PUSH_TYPES.contains(push_type),            "{push_type} is not a type the server composes"        );    }}/// RULE: Tapping a notification the app has no route for does nothing, rather than guessing where to go.#[test]fn an_unmapped_type_is_a_no_op_rather_than_a_guess() {    assert!(app_maps_push_type("cotton_document_ready"));    // Retired types, asked the way the tap handler asks.    assert!(!app_maps_push_type("cotton_message"));    assert!(!app_maps_push_type("share_revoked"));    assert!(!app_maps_push_type(""));    assert!(!app_maps_push_type("cotton_document_ready "));}/// RULE: Cotton never notifies on a new WhatsApp or Instagram message, status or story (owner, browser-session sources): no push type a server composes or the app maps, and no reminder kind a phone schedules, is about a chat message; cotton_message stays retired.#[test]fn no_push_and_no_reminder_is_about_a_chat_message() {    const CHAT_WORDS: [&str; 7] = ["message", "chat", "whatsapp", "instagram", "story", "status", "dm"];    let about_chat = |name: &str| {        let name = name.to_lowercase();        CHAT_WORDS.iter().find(|word| name.split(|c: char| !c.is_ascii_alphanumeric()).any(|part| part == **word || part == format!("{word}s")))    };    for name in PUSH_TYPES.iter().chain(SERVER_PUSH_TYPES) {        assert_eq!(about_chat(name), None, "push type {name} is about a chat message");    }    for kind in crate::rules::reminders::NOTIFICATION_KINDS {        assert_eq!(about_chat(kind.as_str()), None, "reminder kind {} is about a chat message", kind.as_str());    }    assert!(RETIRED_PUSH_TYPES.contains(&"cotton_message"), "the relay's message push stays retired");    assert!(!app_maps_push_type("cotton_message"), "a tap on an old message push opens nothing");}/// RULE: Every emitted kind must declare its audience, so a new sender cannot silently interrupt the owner.#[test]fn every_sender_has_an_audience() {    for kind in SERVER_PUSH_TYPES {        assert!(push_audience(kind).is_some(), "missing audience: {kind}");    }    assert_eq!(push_audience("unknown"), None);}