Cotton code
98 lines · 3.6 KB · RustRaw
use super::mac_files;use crate::fixture::SELF_DEVICE_ID;use crate::shell::client::phone_tests::attached;use cotton_client::connection::{PlaceEntry, read_record, write_record};use cotton_store::storage_rules::{MacKeeps, mac_keeps};use serde_json::json;fn several(    available: bool,) -> (    tempfile::TempDir,    std::sync::Arc<crate::shell::client::ClientController>,) {    let (dir, client) = attached();    let second = "synthetic-settings-project";    let path = client.layout().replica(second);    std::fs::create_dir_all(path.parent().unwrap()).unwrap();    let db = cotton_store::test_template::create_workspace_for_test(        &path,        &cotton_store::meta::Meta {            workspace_id: second.into(),            owner_id: crate::fixture::OWNER.into(),            device_id: crate::fixture::DEVICE.into(),            last_server_seq: 0,            last_hlc: None,        },    )    .unwrap();    let text = |s: &str| rusqlite::types::Value::Text(s.to_string());    db.run("INSERT INTO cotton_workspaces (id,name,owner_user_id,created_at) VALUES (?,'Synthetic project',?,'2026-10-01')",        &[text(second), text(crate::fixture::OWNER)]).unwrap();    db.run("INSERT INTO cotton_devices (id,workspace_id,device_id,name,platform,approved_at,approved_by_device,created_at,updated_at) VALUES ('synthetic:approval',?,?, 'Synthetic Mac','mac','2026-10-01','self','2026-10-01','2026-10-01')",        &[text(second), text(SELF_DEVICE_ID)]).unwrap();    cotton_tools::test_fixtures::reconcile_all(&db);    drop(db);    let mut record = read_record(&client.layout().connection_json())        .unwrap()        .unwrap();    record.places.push(PlaceEntry {        id: second.into(),        name: Some("Synthetic project".into()),        // This is the shape a joined-database admission persists. `origin`        // marks another person's Mac; availability is the controller's        // authority to open the retained replica.        available,        unavailable_since: None,        origin: Some("https://shared.invalid".into()),        server_device_id: Some("synthetic-shared-mac".into()),        removed_at: None,        removed_by: None,        pending: false,        toolkit: None,    });    write_record(&client.layout().connection_json(), &record).unwrap();    client        .with_writer(|f| {            f.driver                .run("UPDATE cotton_devices SET platform = 'mac'", &[])                .map_err(|e| e.to_string())?;            cotton_tools::test_fixtures::reconcile(f.driver, "cotton_devices");            Ok(())        })        .unwrap();    (dir, client)}/// RULE: A machine-wide files choice validates every target before the first write; a phone or a malformed choice cannot partly change a household's storage rules.#[test]fn malformed_or_phone_files_intent_writes_nothing() {    let (_dir, client) = several(true);    assert!(mac_files(&client, &json!({"device": SELF_DEVICE_ID, "rule": "half"})).is_err());    client        .with_writer(|f| {            f.driver                .run("UPDATE cotton_devices SET platform = 'ios'", &[])                .map(|_| ())                .map_err(|e| e.to_string())        })        .unwrap();    assert!(        mac_files(            &client,            &json!({"device": SELF_DEVICE_ID, "rule": "rows_only"})        )        .is_err()    );    client        .with_file(|f| {            assert_eq!(                mac_keeps(f.driver, f.workspace_id, SELF_DEVICE_ID).unwrap(),                MacKeeps::Everything            );            assert_eq!(cotton_store::outbox::pending_count(f.driver).unwrap(), 0);            Ok(())        })        .unwrap();}