Cotton code
193 lines · 5.2 KB · RustRaw
//! What a seeded file holds, and what a second seed does to it.use super::*;use crate::meta::Meta;const WORKSPACE: &str = "11111111-1111-4111-8111-111111111111";const OWNER: &str = "owner-1";const NOW: &str = "2026-09-18T09:00:00.000Z";const NOW_MS: i64 = 1_789_729_200_000;fn a_created_file() -> Driver {    let db = Driver::open_in_memory().expect("an in-memory file");    crate::create::create_workspace(        &db,        &Meta {            workspace_id: WORKSPACE.to_string(),            owner_id: OWNER.to_string(),            device_id: "device:one".to_string(),            last_server_seq: 0,            last_hlc: None,        },    )    .expect("a created file");    db}fn seed(db: &Driver, name: Option<&str>) {    seed_workspace_rows(        db,        NOW,        NOW_MS,        &WorkspaceSeed {            workspace_id: WORKSPACE,            owner_id: OWNER,            name,            kind: "personal",            relay: None,        },        &mut crate::protocol::ulid::Monotonic::new(crate::test_support::counter_seed()),        crate::mint::Held::Served,    )    .expect("the seed");}fn count(db: &Driver, sql: &str) -> i64 {    db.get(sql, &[])        .expect("a count")        .and_then(|row| row.int("n"))        .unwrap_or(0)}#[test]fn a_seeded_file_holds_a_workspace_row_and_one_default_calendar() {    let db = a_created_file();    seed(&db, None);    let row = db        .get(            "SELECT name, owner_user_id FROM cotton_workspaces WHERE id = ?",            &[SqlValue::Text(WORKSPACE.to_string())],        )        .expect("a read")        .expect("the workspace row");    assert_eq!(row.text("name"), Some("Personal"));    assert_eq!(row.text("owner_user_id"), Some(OWNER));    assert_eq!(        count(            &db,            "SELECT COUNT(*) AS n FROM cotton_collections WHERE kind = 'calendar'"        ),        1    );}#[test]fn every_seeded_row_carries_this_file_s_own_author_rather_than_the_empty_stamp() {    // An unstamped row belongs to the `origin` author and reaches another    // device only inside a whole-file copy — so a phone that started a database    // alone would hand a Mac a life with no workspace row in it.    let db = a_created_file();    seed(&db, None);    assert_eq!(        count(            &db,            "SELECT COUNT(*) AS n FROM cotton_workspaces WHERE _hlc = ''"        ),        0    );    assert_eq!(        count(            &db,            "SELECT COUNT(*) AS n FROM cotton_collections WHERE _hlc = ''"        ),        0    );}#[test]fn seeding_twice_adds_nothing_and_a_file_that_kept_only_one_row_gets_the_other() {    let db = a_created_file();    seed(&db, None);    seed(&db, None);    assert_eq!(        count(&db, "SELECT COUNT(*) AS n FROM cotton_collections"),        1    );    // The case the separate guards exist for: a file holding a workspace row    // and no calendar — an adopted file, or one a fixture wrote itself.    db.exec("DELETE FROM cotton_collections").expect("a delete");    seed(&db, None);    assert_eq!(        count(&db, "SELECT COUNT(*) AS n FROM cotton_collections"),        1    );}#[test]fn the_name_is_the_person_s_when_they_gave_one() {    let db = a_created_file();    seed(&db, Some("Home"));    let row = db        .get(            "SELECT name FROM cotton_workspaces WHERE id = ?",            &[SqlValue::Text(WORKSPACE.to_string())],        )        .expect("a read")        .expect("the workspace row");    assert_eq!(row.text("name"), Some("Home"));}#[test]fn nothing_is_queued_for_anybody_because_this_file_is_the_one_being_asked() {    let db = a_created_file();    seed(&db, None);    assert_eq!(count(&db, "SELECT COUNT(*) AS n FROM _outbox"), 0);}/// RULE: a household's relay is written in its workspace row's BIRTH — the one/// moment it can be — and a household that names none is born with none/// (Cotton's relay is two NULLs, never a spelled default).#[test]fn a_workspace_is_born_on_the_relay_it_was_founded_with() {    let relay_of = |db: &Driver| {        let row = db            .get(                "SELECT relay_domain, relay_tunnel_host FROM cotton_workspaces WHERE id = ?",                &[SqlValue::Text(WORKSPACE.to_string())],            )            .expect("a read")            .expect("the workspace row");        (            row.text("relay_domain").map(str::to_string),            row.text("relay_tunnel_host").map(str::to_string),        )    };    let ours = a_created_file();    seed(&ours, None);    assert_eq!(relay_of(&ours), (None, None));    let theirs = a_created_file();    seed_workspace_rows(        &theirs,        NOW,        NOW_MS,        &WorkspaceSeed {            workspace_id: WORKSPACE,            owner_id: OWNER,            name: None,            kind: "personal",            relay: Some(("relay.example.org", "tunnel.example.org")),        },        &mut crate::protocol::ulid::Monotonic::new(crate::test_support::counter_seed()),        crate::mint::Held::Served,    )    .expect("the seed");    assert_eq!(        relay_of(&theirs),        (            Some("relay.example.org".to_string()),            Some("tunnel.example.org".to_string())        )    );}