Cotton code
54 lines · 3.0 KB · RustRaw
//! Home's tile prints the owner's face, and before anyone is chosen as them —//! a database just founded, on a phone or a Mac — or in a business app, which//! has no "you", it prints the picture Home's board names. A Home naming none//! drew a blank cream square in the fan (homepicture lane, 22 Oct 2026).use serde_json::Value;/// The imagesets the app ships, by name.fn ships(asset: &str) -> bool {    let media = cotton_store::manifest_dir().join("../../apps/loom/Sources/CottonPaper/Media.xcassets");    assert!(media.is_dir(), "{} is missing — the imagesets the app ships", media.display());    media.join(format!("{asset}.imageset")).is_dir()}/// Every manifest's Home, by flavour.fn manifest_homes() -> Vec<(String, Value)> {    let dir = cotton_store::manifest_dir().join("../../apps/cotton/App/Flavours");    assert!(dir.is_dir(), "{} is missing — the flavour manifests", dir.display());    // The manifests kept as fixtures since their apps retired: Cotton Project's carries a Home (1711bdc57d).    let kept = cotton_store::manifest_dir().join("../../apps/cotton/Tests/Fixtures/Flavours");    let shipped = std::fs::read_dir(&dir).expect("the flavours").map(|entry| entry.expect("an entry").path().join("flavour.json"));    let fixtures = std::fs::read_dir(&kept).expect("the kept manifests").map(|entry| entry.expect("an entry").path());    let mut homes: Vec<(String, Value)> = shipped        .chain(fixtures)        .filter_map(|path| {            let manifest: Value = serde_json::from_str(&std::fs::read_to_string(&path).ok()?).expect("a manifest reads");            Some((manifest["flavour"].as_str()?.to_string(), manifest.get("home")?.clone()))        })        .collect();    homes.sort_by(|a, b| a.0.cmp(&b.0));    homes}/// RULE: the shipped Home and every manifest's Home name a picture for Home's tile, and it is an imageset the app ships — without one the tile is a blank square wherever nobody is chosen as you.#[test]fn every_home_names_a_picture_the_app_ships() {    let shipped = cotton_core::definitions::board::BoardDefinition::builtin("home").expect("the shipped Home");    let mut homes = vec![("shipped".to_string(), serde_json::to_value(&shipped).expect("Home serialises"))];    homes.extend(manifest_homes());    assert!(homes.len() > 1, "at least one manifest names its Home");    for (whose, home) in homes {        let picture = home["picture"].as_str().unwrap_or_else(|| panic!("{whose}'s Home names no picture"));        assert!(ships(picture), "{whose}'s Home names {picture}, which the app does not ship");    }}/// RULE: a manifest's Home that names no picture is refused when the app declares it, so no business app draws a blank Home tile.#[test]fn a_manifest_home_with_no_picture_is_refused() {    let (_, mut home) = manifest_homes().into_iter().next().expect("a manifest's Home");    home.as_object_mut().expect("a board").remove("picture");    let refused = cotton_tools::presentation::declare_home(&home).expect_err("a Home with no picture is refused");    assert!(refused.contains("picture"), "{refused}");}