Cotton code
26 lines · 1.8 KB · RustRaw
//! Snapshot transport acts run before a database file exists. Synthetic only.use cotton_ffi::{dispatch, CottonHandle};use serde_json::json;#[test]fn debug_can_offer_and_run_removal_without_opening_the_failed_copy() {    let dir=tempfile::tempdir().unwrap();    let layout=cotton_client::layout::Layout::new(dir.path());    let id="22222222-2222-4222-8222-222222222222";    std::fs::create_dir_all(layout.replica(id).parent().unwrap()).unwrap();    std::fs::write(layout.connection_json(),serde_json::to_vec(&json!({"mode":"local","baseUrl":"","workspaceId":"11111111-1111-4111-8111-111111111111","places":[{"id":id,"name":"Synthetic","available":true,"unavailableSince":null}]})).unwrap()).unwrap();    std::fs::write(layout.replica(id),b"synthetic unreadable copy").unwrap();    let phone=CottonHandle::open(serde_json::from_value(json!({"containerPath":dir.path(),"flavor":"dev","timeZone":"Etc/UTC"})).unwrap()).unwrap();    dispatch(&phone,"read.home",&json!({"database":id})).expect_err("the copy is unreadable");    let debug=dispatch(&phone,"diagnostics.debug",&json!({"database":id})).expect("Debug is outside the failed copy");    let offer=&debug["removals"][0];    assert_eq!(offer["databaseId"],id);    assert_eq!(offer["filename"],format!("{id}/replica.db"));    let request=json!({"database":id,"token":offer["token"]});    let removed=dispatch(&phone,"diagnostics.removeCopy",&request).expect("the one-shot act");    assert_eq!(removed["removed"],true);    assert!(!layout.replica(id).exists());    let kept=std::path::Path::new(removed["keptAt"].as_str().unwrap()).join("replica.db");    assert_eq!(std::fs::read(kept).unwrap(),b"synthetic unreadable copy");    assert!(dispatch(&phone,"diagnostics.removeCopy",&request).is_err(),"a token is never reused");}