Cotton code
96 lines · 4.8 KB · RustRaw
//! GENESIS KITS 2 (the independent review, 26 Nov 2026): the kit-own tables laid at an open never stand in the way of//! the open, only tables on `cotton_core::kit::KIT_OWN_TABLES` are ever laid, and the lay holds for a kit other than//! Sites (Post's `email_account`).use serde_json::json;use crate::handle::CottonHandle;/// A Post phone over the container at `path` (an invented phone, no sign-in); `fresh` starts its copy.fn post_phone_in(path: &std::path::Path, flavor: cotton_core::flavor::Flavor, fresh: bool) -> CottonHandle {    let manifest = json!({        "genesis": "personal", "flavour": "mail", "name": "Cotton Post", "databases": "single", "database": "personal",        "fan": {"home": false, "tools": false, "opens": "mail"}, "kit": "mail",    });    let handle = CottonHandle::open(crate::handle::OpenConfig {        mode: crate::handle::OpenMode::Phone { container_path: path.to_string_lossy().to_string(), previous_container_path: None },        app_version: None,        time_zone: Some("Asia/Kolkata".to_string()),        device_secret: Some(crate::fixture::paired_secret()),        vault_secret: None,        flavor,        manifest: Some(manifest),    })    .expect("an open core");    if fresh {        crate::methods::dispatch(&handle, "workspace.startLocal", &json!({"deviceName": "An invented phone"})).expect("started");    }    handle}fn post_phone() -> (tempfile::TempDir, CottonHandle) {    let dir = tempfile::tempdir().expect("a temp container");    let handle = post_phone_in(dir.path(), cotton_core::flavor::Flavor::Real, true);    (dir, handle)}fn defined(handle: &crate::handle::CottonHandle, id: &str) -> bool {    handle        .client()        .with_file(|file| Ok(file.driver.get("SELECT id FROM cotton_table_definitions WHERE id = ?", &[rusqlite::types::Value::Text(id.to_string())]).map_err(|e| e.to_string())?.is_some()))        .unwrap()}/// RULE: a lay that is refused (a copy with no identity yet, or a kit that does not read) answers a note to write/// down and never an error, so the open carries on; a kit that names no kit-own table lays nothing.#[test]fn a_refused_lay_is_a_note_and_never_stops_the_open() {    let root = tempfile::tempdir().unwrap();    let now = chrono::Utc::now();    let bare = cotton_store::Driver::open_in_memory().expect("a database with no identity");    assert_eq!(crate::shell::client::lay_kit_tables_at_open(&bare, root.path(), now), None, "no kit kept: nothing to lay");    std::fs::write(root.path().join("app_kit.json"), json!({ "id": "sites", "tools": [], "tables": ["site"] }).to_string()).unwrap();    let note = crate::shell::client::lay_kit_tables_at_open(&bare, root.path(), now).expect("refused, said");    assert!(note.contains("next open tries again"), "{note}");    std::fs::write(root.path().join("app_kit.json"), b"{ not a kit").unwrap();    let note = crate::shell::client::lay_kit_tables_at_open(&bare, root.path(), now).expect("a kit that does not read, said");    assert!(note.contains("did not read"), "{note}");    std::fs::write(root.path().join("app_kit.json"), json!({ "id": "books", "tools": [], "tables": ["book", "author"] }).to_string()).unwrap();    assert_eq!(crate::shell::client::lay_kit_tables_at_open(&bare, root.path(), now), None, "no kit-own table named: nothing tried");}/// RULE: only a kit-own table is laid; a table a tool's package carries (Books' `book`) is never made by this path,/// even when it is asked for by name.#[test]fn only_a_kit_own_table_is_ever_laid() {    let (_dir, handle) = post_phone();    assert!(!defined(&handle, "book"));    let laid = handle        .client()        .with_writer(|file| {            cotton_tools::kit::lay_kit_tables(&file.driver, chrono::Utc::now(), std::sync::Arc::new(cotton_tools::random::SystemRandom), None, &["book".to_string(), "not_a_table".to_string()])                .map_err(|failure| failure.0)        })        .expect("the lay answers");    assert!(laid.is_empty(), "{laid:?}");    assert!(!defined(&handle, "book"), "a package's table is its tool's to lay");}/// RULE: a kit other than Sites: a Post copy holds `email_account`, and a copy that lost its definition gains it again/// at its next open.#[test]fn a_post_copy_holds_email_account_and_gains_it_again_at_its_next_open() {    let dir = tempfile::tempdir().unwrap();    let handle = post_phone_in(dir.path(), cotton_core::flavor::Flavor::Real, true);    assert!(defined(&handle, "email_account"), "a fresh Post copy holds it");    handle        .client()        .with_writer(|file| file.driver.run("DELETE FROM cotton_table_definitions WHERE id = 'email_account'", &[]).map(|_| ()).map_err(|e| e.to_string()))        .unwrap();    drop(handle);    let handle = post_phone_in(dir.path(), cotton_core::flavor::Flavor::Real, false);    assert!(defined(&handle, "email_account"), "laid again at the open");}