Cotton code
73 lines · 2.7 KB · RustRaw
use super::*;use crate::meta::{seed_meta, Meta};use crate::protocol::derived_id::{derived_id, memory_project_id, repo_id};use crate::schema::ddl::SCHEMA_VERSION;use crate::test_support::{empty_file, fold_create, SEED_AUTHOR, SEED_MS};const NEW_FILE: &str = "22222222-2222-4222-8222-222222222222";const OLD_FILE: &str = "11111111-1111-4111-8111-111111111111";/// A file whose own workspace row carries `namespace` (or none).fn file_with(namespace: Option<&str>) -> Driver {    let db = empty_file();    seed_meta(        &db,        &Meta {            workspace_id: NEW_FILE.to_string(),            owner_id: "owner-1".to_string(),            device_id: "device:test".to_string(),            last_server_seq: 0,            last_hlc: None,        },        SCHEMA_VERSION,    )    .expect("seeds the identity");    let mut row = serde_json::json!({        "id": NEW_FILE,        "name": "Cotton",        "owner_user_id": "owner-1",        "created_at": "2026-10-03T10:30:00.000Z",        "kind": "shared",    });    if let Some(namespace) = namespace {        row["namespace"] = serde_json::Value::from(namespace);    }    fold_create(&db, "cotton_workspaces", NEW_FILE, row, SEED_AUTHOR, SEED_MS);    db}#[test]fn database_made_later_derives_under_its_own_id() {    // A database made after the stop has no older file whose ids it must keep,    // so its row carries no namespace and it derives under its own id.    let file = read_file_identity(&file_with(None))        .expect("reads")        .expect("the file is seeded");    assert_eq!(file.id().as_str(), NEW_FILE);    assert_eq!(file.namespace().as_str(), NEW_FILE);    assert_eq!(        memory_project_id(file.namespace(), "cotton"),        derived_id("cotton_memory_projects", &[NEW_FILE, "cotton"]),    );}#[test]fn producer_in_a_database_with_a_namespace_derives_under_the_namespace() {    // A database made at the stop carries records derived under the OLD file's    // id. A producer writing there must reach the same project id every device    // already holds, or the slug `cotton` becomes two projects — while the    // repository id, which names a clone on disk, stays the new file's own.    let file = read_file_identity(&file_with(Some(OLD_FILE)))        .expect("reads")        .expect("the file is seeded");    assert_eq!(file.id().as_str(), NEW_FILE);    assert_eq!(file.namespace().as_str(), OLD_FILE);    assert_eq!(        memory_project_id(file.namespace(), "cotton"),        derived_id("cotton_memory_projects", &[OLD_FILE, "cotton"]),    );    assert_eq!(        repo_id(file.id(), "git.example.com/owner/name"),        derived_id("cotton_repos", &[NEW_FILE, "git.example.com/owner/name"]),    );}