Cotton code
119 lines · 5.5 KB · RustRaw
use super::*;#[test]fn every_route_is_an_absolute_path_and_none_is_named_twice() {    let mut seen = std::collections::BTreeSet::new();    for &route in ROUTES {        assert!(route.starts_with('/'), "{route} is not an absolute path");        assert!(            !route.ends_with('/') || route == "/",            "{route} carries a trailing slash — the strings are wire protocol and a link \             another process holds would not match"        );        assert!(seen.insert(route), "{route} is in the table twice");    }}#[test]fn the_table_is_sorted_so_a_diff_reads_as_one_line() {    let mut sorted = ROUTES.to_vec();    sorted.sort_unstable();    assert_eq!(        ROUTES.to_vec(),        sorted,        "ROUTES is not sorted; a route added in the middle would show as a whole-block diff"    );}#[test]fn a_route_is_matched_exactly_and_never_by_prefix() {    assert!(is_route("/todos/done"));    assert!(!is_route("/todos/anything"));    assert!(!is_route("/todos/"));}#[test]fn the_entry_points_other_processes_deep_link_into_are_all_here() {    // These four are addressed from OUTSIDE the app — the share extension, the    // Android keyboard and a notification — so losing one is not a    // missing screen, it is a link somebody else's process holds going dead.    for entry in ["/capture", "/take", "/keyboard-take", "/search"] {        assert!(is_route(entry), "{entry} is a documented entry point");    }}/// RULE: A table's list_route is a screen the app has (ROUTES), so a definition can never send a list to a path nothing draws./// Copy (c4246b3a0) deliberately opens the generic table list: its concrete/// `/lists/copy` is served by `/lists/[table]`, with the definition's own id.#[test]fn every_list_route_is_a_declared_screen() {    for definition in cotton_core::definitions::builtin_table_definitions() {        if let Some(route) = &definition.list_route {            let generic_list = route == &format!("/lists/{}", definition.id)                && ROUTES.contains(&"/lists/[table]");            assert!(ROUTES.contains(&route.as_str()) || generic_list, "{}'s list_route {route} is not a declared screen", definition.id);        }    }}fn builtin(id: &str) -> cotton_core::definitions::TableDefinition {    cotton_core::definitions::builtin_table_definitions().iter().find(|table| table.id == id).unwrap_or_else(|| panic!("the kit ships {id}")).clone()}fn none(_: &str) -> Option<cotton_core::definitions::TableDefinition> {    None}/// RULE: where a record opens is read from its definition, never a table/// name: a table renamed keeps its record_route and its screen, a table/// naming none opens on the generic list's record, and every path answered/// is a declared route filled in.#[test]fn a_record_opens_where_its_definition_says_even_renamed() {    let empty = serde_json::Map::new();    let mut jotting = builtin("note");    jotting.id = "jotting".into();    let opens = opens(&jotting, "n1", &empty, "", &none);    assert_eq!(opens["path"], "/notes/note/n1");    assert_eq!((opens["table"].as_str(), opens["id"].as_str()), (Some("jotting"), Some("n1")));    assert_eq!(opens["screen"], "outside", "a note at rest opens on its own route screen: {opens}");    jotting.record_route = None;    let opens = super::opens(&jotting, "n1", &empty, "", &none);    assert_eq!((opens["path"].as_str(), opens["screen"].as_str()), (Some("/lists/jotting/n1"), Some("deck")));    let mut gallery = builtin("photo");    gallery.id = "snapshot".into();    let opens = super::opens(&gallery, "p1", &empty, "", &none);    assert_eq!((opens["path"].as_str(), opens["screen"].as_str(), opens["opening"].as_str()), (Some("/photos/photo/p1"), Some("viewer"), Some("viewer")));    let mut chat = builtin("thread");    chat.id = "conversation_renamed".into();    let opens = super::opens(&chat, "t1", &empty, "", &none);    assert_eq!((opens["path"].as_str(), opens["screen"].as_str()), (Some("/messages/t1"), Some("chat")));    let opens = super::opens(&builtin("todo"), "x", &empty, "group", &none);    assert_eq!(opens["screen"], "group");    for table in cotton_core::definitions::builtin_table_definitions() {        let opens = super::opens(table, "r1", &empty, "", &none);        assert!(SCREENS.contains(&opens["screen"].as_str().unwrap()), "{opens}");        if let Some(route) = &table.record_route { assert!(is_route(route), "{}'s record_route {route}", table.id) }    }}/// RULE: a row standing for another record (`opens`) opens THAT record,/// by the table it names or the table its `table_column` holds.#[test]fn a_row_standing_for_another_opens_that_record() {    let mut pointer = builtin("note");    pointer.id = "pointer".into();    pointer.record_route = None;    pointer.opens = Some(cotton_core::definitions::RecordOpens { column: "record_id".into(), table: None, table_column: Some("kind".into()) });    let record = serde_json::json!({"record_id": "ph1", "kind": "picture"}).as_object().unwrap().clone();    let tables = |id: &str| (id == "picture").then(|| { let mut photo = builtin("photo"); photo.id = "picture".into(); photo });    let opens = opens(&pointer, "row1", &record, "", &tables);    assert_eq!((opens["table"].as_str(), opens["id"].as_str(), opens["path"].as_str(), opens["screen"].as_str()), (Some("picture"), Some("ph1"), Some("/photos/photo/ph1"), Some("viewer")));    let blank = serde_json::Map::new();    assert_eq!(super::opens(&pointer, "row1", &blank, "", &tables)["path"], "/lists/pointer/row1", "nothing to stand for: the row itself");}