Cotton code
181 lines · 6.2 KB · RustRaw
//! The list's pure half: what one answer changes, and that absence removes//! nothing.use super::*;use crate::connection::{ConnectionMode, ConnectionRecord, StoredConnection};const MINE: &str = "11111111-1111-4111-8111-111111111111";const PLACE: &str = "22222222-2222-4222-8222-222222222222";const OTHER: &str = "33333333-3333-4333-8333-333333333333";fn listed(ids: &[&str]) -> Listing {    Listing {        databases: ids            .iter()            .map(|id| ListedDatabase {                id: (*id).to_string(),                name: "A synthetic database".to_string(),                kind: "shared".to_string(),                schema_version: 1,            })            .collect(),    }}fn place(id: &str, available: bool) -> PlaceEntry {    PlaceEntry {        id: id.to_string(),        name: Some("A synthetic place".to_string()),        available,        unavailable_since: None,        origin: None,        server_device_id: None,        removed_at: None,        removed_by: None,        pending: false,        toolkit: None,    }}#[test]fn a_missing_place_is_gone_a_returning_one_is_back_and_an_unknown_one_is_new() {    let change = reconcile(        Some(MINE),        &[place(PLACE, true), place(OTHER, false)],        &listed(&[MINE, OTHER, "44444444-4444-4444-8444-444444444444"]),    );    assert!(change.mine_listed);    assert_eq!(change.gone, vec![PLACE.to_string()]);    assert_eq!(change.back, vec![OTHER.to_string()]);    assert_eq!(        change            .new            .iter()            .map(|db| db.id.as_str())            .collect::<Vec<_>>(),        vec!["44444444-4444-4444-8444-444444444444"]    );}#[test]fn mine_missing_from_the_list_is_said_and_nothing_else_is_decided_about_it() {    let change = reconcile(Some(MINE), &[], &listed(&[PLACE]));    assert!(!change.mine_listed);    assert!(change.gone.is_empty());}#[test]fn a_place_absent_from_the_list_stays_in_the_record_flagged_unavailable() {    let dir = tempfile::tempdir().expect("a temp dir");    let path = dir.path().join("connection.json");    crate::connection::write_record(        &path,        &ConnectionRecord {            left_databases: Vec::new(),            pending_leaves: Vec::new(),            mine: StoredConnection {            device_scope: None,            takes_placements: false,                mode: ConnectionMode::Remote,                base_url: "https://example.invalid".to_string(),                workspace_id: Some(MINE.to_string()),                owner_id: None,                connected_at: None,            },            places: vec![place(PLACE, true)],        },    )    .expect("the record");    let file = ConnectionFile::at(&path);    let change = file        .apply_listing(&listed(&[MINE]), "2026-09-08T10:00:00.000Z")        .expect("written");    assert_eq!(change.gone, vec![PLACE.to_string()]);    // Read back from a fresh handle: the flag is on DISK, and the place is    // still there.    let record = ConnectionFile::at(&path).read_record().expect("a record");    assert_eq!(record.places.len(), 1);    assert_eq!(record.places[0].id, PLACE);    assert!(!record.places[0].available);    assert_eq!(        record.places[0].unavailable_since.as_deref(),        Some("2026-09-08T10:00:00.000Z")    );    file.apply_listing(&listed(&[MINE, PLACE]), "2026-09-08T10:01:00.000Z")        .expect("written");    let record = ConnectionFile::at(&path).read_record().expect("a record");    assert!(record.places[0].available);    assert_eq!(record.places[0].unavailable_since, None);}#[test]fn the_list_is_unlisted_until_the_first_answer_arrives() {    let list = DatabaseList::new(Arc::new(crate::seams::FixedClock::default_start()));    assert!(!list.is_listed(MINE));    let dir = tempfile::tempdir().expect("a temp dir");    let file = ConnectionFile::at(dir.path().join("connection.json"));    list.observe(listed(&[MINE]), &file);    assert!(list.is_listed(MINE));    assert!(!list.is_listed(PLACE));}// RULE: A database made on this device is never flagged gone by a listing that does not name it yet; the first listing that names it marks it taken and clears its pending flag on disk.#[test]fn a_database_made_here_is_never_gone_and_is_taken_when_first_listed() {    let made = PlaceEntry {        pending: true,        toolkit: Some("family".to_string()),        ..place(PLACE, true)    };    let unlisted = reconcile(Some(MINE), std::slice::from_ref(&made), &listed(&[MINE]));    assert!(unlisted.gone.is_empty(), "{unlisted:?}");    assert!(unlisted.taken.is_empty(), "{unlisted:?}");    let listed_once = reconcile(Some(MINE), std::slice::from_ref(&made), &listed(&[MINE, PLACE]));    assert_eq!(listed_once.taken, vec![PLACE.to_string()]);    assert!(listed_once.new.is_empty(), "a place held here is not new");    assert!(listed_once.back.is_empty());    let dir = tempfile::tempdir().expect("a temp dir");    let path = dir.path().join("connection.json");    crate::connection::write_record(        &path,        &ConnectionRecord {            left_databases: Vec::new(),            pending_leaves: Vec::new(),            mine: StoredConnection {            device_scope: None,            takes_placements: false,                mode: ConnectionMode::Remote,                base_url: "https://example.invalid".to_string(),                workspace_id: Some(MINE.to_string()),                owner_id: None,                connected_at: None,            },            places: vec![made],        },    )    .expect("the record");    let file = ConnectionFile::at(&path);    file.apply_listing(&listed(&[MINE]), "2026-10-04T09:00:00.000Z")        .expect("written");    let record = ConnectionFile::at(&path).read_record().expect("a record");    assert!(record.places[0].pending, "an unlisted made place stays pending");    assert!(record.places[0].available);    let change = file        .apply_listing(&listed(&[MINE, PLACE]), "2026-10-04T09:01:00.000Z")        .expect("written");    assert_eq!(change.taken, vec![PLACE.to_string()]);    let record = ConnectionFile::at(&path).read_record().expect("a record");    assert!(!record.places[0].pending);    assert!(record.places[0].available);    assert_eq!(record.places[0].toolkit.as_deref(), Some("family"));}