Cotton code
134 lines · 7.6 KB · RustRaw
use super::*;const MARK: &str = "photo:aa30ee64d799b856e6219185544162815159ad9493bfb3e181c24898c805d0ec";fn tool(id: &str, tables: &[&str], tile: Option<Value>) -> ToolDefinition {    let mut tool = json!({"id": id, "name": id, "tables": tables, "instructions": "", "automations": [],        "author": {"device": "test", "name": "Test"}, "signature": null});    if let Some(tile) = tile { tool["fan_tile"] = tile; }    serde_json::from_value(tool).unwrap()}fn table(id: &str) -> TableDefinition {    let mut table = cotton_core::definitions::builtin_table_definitions()[0].clone();    table.id = id.into();    table.tool = None;    table}// RULE: the tile on a tier's answer is the tile of the tool owning its table; a table no tool with a tile owns answers as it did, and its acts and doors carry no pictogram.#[test]fn a_tier_wears_the_tile_of_the_tool_owning_its_table() {    let tile = json!({"stock": {"photo": MARK}, "type": "letterboard", "pictures": {"new_list": {"photo": MARK}},        "acts": {"play": {"photo": MARK}, "group": {"photo": MARK}}, "doors": {"files": {"photo": MARK}}});    let tools = vec![tool("cinemas", &["film", "film_episode"], Some(tile)), tool("books", &["book"], None)];    let owned = of_tools(&table("film_episode"), &tools).expect("the episode's tool names a tile");    let mut opened = json!({"acts": [{"id": "play"}, {"id": "group"}, {"id": "mark_watched"}]});    dress_acts(Some(&owned), &mut opened, "acts");    assert_eq!(opened["tile"], json!({"stock": MARK, "type": "letterboard", "pictures": {"new_list": MARK}}));    assert_eq!(opened["acts"][0]["pictogram"], MARK);    assert_eq!(opened["acts"][1]["pictogram"], MARK, "the shell's own Group act, by its id");    assert!(opened["acts"][2].get("pictogram").is_none(), "an act the tile names nothing for carries none");    let mut plus = json!({"does": "doors", "doors": [{"id": "files"}, {"id": "find"}]});    dress_doors(Some(&owned), &mut plus);    assert_eq!(plus["doors"][0]["pictogram"], MARK);    assert!(plus["doors"][1].get("pictogram").is_none());    assert!(of_tools(&table("book"), &tools).is_none());    let mut plain = json!({"actions": [{"id": "play"}]});    dress_acts(None, &mut plain, "actions");    assert_eq!(plain, json!({"actions": [{"id": "play"}]}), "a tool with no tile answers as it did");}fn made(client: &ClientController, table: &str, data: Value) -> String {    let made = crate::phone::records::call(client, "records.create", &json!({"table": table, "data": data})).unwrap_or_else(|why| panic!("a {table}: {why:?}"));    ["/record/id", "/id", "/after/id", "/change/after/id"]        .iter()        .find_map(|pointer| made.pointer(pointer).and_then(Value::as_str))        .unwrap_or_else(|| panic!("the {table}'s id: {made}"))        .to_string()}fn cinemas(client: &ClientController) -> FanTile {    let film = crate::phone::card_actions::read_definition(client, "film", "test").unwrap();    of_table(client, &film, "test").unwrap().expect("Cinemas names its fan tile")}// RULE (owner, 6 Nov 2026): every tile the Cinemas fan deals carries its pictogram — each held film's, series', season's and episode's act, the shell's own among them, and every Plus door — on the tool's letterboard stock.#[test]fn every_cinemas_act_and_door_carries_a_pictogram() {    let (_dir, client) = crate::shell::client::phone_tests::attached();    let tile = cinemas(&client);    assert_eq!(tile.kind, cotton_core::definitions::fan_tile::TileType::Letterboard);    assert!(tile.stock.is_some());    for role in cotton_core::definitions::fan_tile::TILE_ROLES {        assert!(tile.role(role).is_some(), "the {role} role");    }    let film = made(&client, "film", json!({"title": "Synthetic Test Pattern", "kind": "film"}));    let watched = made(&client, "film", json!({"title": "Synthetic Colour Bars", "kind": "film", "status": "watched", "position": 42}));    let series = made(&client, "film", json!({"title": "Synthetic Serial", "kind": "series"}));    let season = made(&client, "film_season", json!({"title": "Season 1", "film": series, "number": 1}));    let episode = made(&client, "film_episode", json!({"title": "Pilot", "season": season, "series": series, "number": 1}));    let mut seen = std::collections::BTreeSet::new();    for (table, id) in [("film", &film), ("film", &watched), ("film", &series), ("film_season", &season), ("film_episode", &episode)] {        let answer = crate::phone::opened::call(&client, &json!({"table": table, "id": id})).unwrap();        assert_eq!(answer["tile"], tile.look(), "{table}: the tile is the tool's");        for act in answer["acts"].as_array().unwrap() {            seen.insert(act["id"].as_str().unwrap().to_string());            assert!(act["pictogram"].as_str().is_some_and(|photo| photo.starts_with("photo:")), "{table} {}: no pictogram in {answer}", act["id"]);        }    }    for act in ["play", "play_from_start", "mark_watched", "mark_unwatched", "find_details", "when", "group", "bin"] {        assert!(seen.contains(act), "{act} is dealt: {seen:?}");    }    // Every command of the tool's tables, dealt here or only once a file is    // held (Keep on this phone, Keep series, Keep season, Remove download).    for table in ["film", "film_season", "film_episode"] {        let definition = crate::phone::card_actions::read_definition(&client, table, "test").unwrap();        for command in definition.commands.iter().flatten() {            assert!(tile.act(&command.id).is_some(), "{table}'s {} act", command.id);        }    }    // The tool's own acts, wherever a view offers them.    let tools = {        let mut tools = Vec::new();        client.read_rows("test", &mut |ctx| {            tools = cotton_tools::definitions::tool_definitions(ctx).map_err(|e| e.to_string())?.definitions;            Ok(Value::Null)        }).unwrap();        tools    };    let tool = tools.iter().find(|tool| tool.id == "cinemas").unwrap();    for act in &tool.acts {        assert!(tile.act(&act.id).is_some(), "the tool's {} act", act.id);    }    for act in ["list_set_picture", "list_remove_picture", "list_rename", "list_move_earlier", "list_move_later", "list_remove", "list_member_earlier", "list_member_later", "list_member_remove", "bin"] {        assert!(tile.act(act).is_some(), "the shell's {act}");    }    let definition = crate::phone::card_actions::read_definition(&client, "film", "test").unwrap();    let mut plus = crate::methods::shell_deck::plus_of(&definition);    dress_doors(Some(&tile), &mut plus);    let doors = plus["doors"].as_array().unwrap();    assert_eq!(doors.len(), 3);    for door in doors {        assert!(door["pictogram"].as_str().is_some(), "door {}", door["id"]);    }}// RULE: a tier's tile carries the words' ink and baselines the tool's tile names, as given; a tile naming none carries no words.#[test]fn a_tier_s_tile_carries_its_words_as_given() {    let words = json!({"ink": "#211a12", "name_baseline": 430, "count_baseline_one": 516, "count_baseline_two": 574});    let tools = vec![        tool("keys", &["passkey"], Some(json!({"stock": {"photo": MARK}, "type": "letterboard", "words": words}))),        tool("cinemas", &["film"], Some(json!({"stock": {"photo": MARK}, "type": "letterboard"}))),    ];    let mut opened = json!({"acts": []});    dress_acts(of_tools(&table("passkey"), &tools).as_ref(), &mut opened, "acts");    assert_eq!(opened["tile"]["words"], words);    let mut plain = json!({"acts": []});    dress_acts(of_tools(&table("film"), &tools).as_ref(), &mut plain, "acts");    assert!(plain["tile"].get("words").is_none(), "the letterboard naming no words keeps its own");}