177 lines · 6.8 KB · RustRaw
//! A record move waits for the real replica's receipt queues before its signed act.use crate::shell::client::{phone_tests::attached, ClientController};use cotton_store::protocol::types::{ExchangeRequest, ExchangeResponse, PushRejection};use serde_json::json;use std::sync::{ atomic::{AtomicUsize, Ordering}, Arc,};struct HoldsPush;impl cotton_store::exchange::SyncTransport for HoldsPush { fn exchange(&mut self, request: &ExchangeRequest) -> Result<ExchangeResponse, String> { let mut answer:ExchangeResponse=serde_json::from_value(json!({ "workspaceId":request.workspace_id,"ops":[],"cursors":{},"hasMore":false, "schemaVersion":cotton_store::migrate::SCHEMA_VERSION,"appliedRows":{},"rejected":[],"seen":{} })).map_err(|e|e.to_string())?; answer.rejected = request .ops .iter() .map(|op| { PushRejection::held( op.op_id.clone(), "Keep this receipt until it can be sent", "phone_behind", ) }) .collect(); Ok(answer) }}impl cotton_client::transport::RefusalAware for HoldsPush { fn last_refusal(&self) -> Option<cotton_client::transport::ServerRefusal> { None }}fn hold_pushes(client: &ClientController) { client.set_transport(Some(Arc::new(|| { Ok(Box::new(HoldsPush) as Box<dyn cotton_client::transport::SyncingTransport>) })));}fn attempts(client: &ClientController) { let calls = AtomicUsize::new(0); let send = || { calls.fetch_add(1, Ordering::SeqCst); Ok(json!({"moved":true})) }; let request = json!({"id":"todo","to":crate::fixture::WORKSPACE,"database":crate::fixture::WORKSPACE}); let error = super::record_moves::over(client, &request, send).unwrap_err(); assert_eq!(error.code, "unsent_write"); assert_eq!(calls.load(Ordering::SeqCst), 0);}/// RULE: A move sends a phone's real edits first; when the Mac holds that push, the signed transfer is not sent and the edit remains on the phone.#[test]fn a_held_push_prevents_the_move() { let (_dir, client) = attached(); hold_pushes(&client); client .write( "create_todo", json!({"title":"An edit that has not reached the Mac"}) .as_object() .unwrap(), ) .unwrap(); attempts(&client); let remaining = client .with_file(|file| { cotton_store::runner_change::claim_of( file.driver, file.workspace_id, file.device_id, ) .map(|c| c.unsent) .map_err(|e| e.to_string()) }) .unwrap(); assert!(remaining > 0); assert!(client .read_tool("list_todos", &serde_json::Map::new()) .unwrap() .to_string() .contains("An edit that has not reached the Mac"));}/// RULE: A parked push is an unsent write even when the ordinary outbox is empty; a move may not strand the only receipt this phone holds.#[test]fn a_parked_push_alone_prevents_the_move() { let (_dir, client) = attached(); hold_pushes(&client); client.with_writer(|file|{ use cotton_store::{actor::{Actor,JobName},mint::Change,rejected::{park,Direction,Class}}; let change=Change::create("cotton_todos","parked-todo",serde_json::from_value(json!({"id":"parked-todo","workspace_id":file.workspace_id,"title":"Keep this parked edit","created_at":"2026-09-08T10:00:00.000Z","updated_at":"2026-09-08T10:00:00.000Z"})).unwrap()); let actor=Actor::device_job(JobName::named("transfer-fixture").unwrap()); let device=file.device_id; let op=cotton_store::wire::op_of_change(file.driver,&change,&format!("1788861600000:0000:{device}"),device,&actor,"parked-transfer-edit".into()).map_err(|e|e.to_string())?; park(file.driver,&op,Direction::Push,Class::Conflict,"Not sent yet","2026-09-08T10:00:00.000Z").map_err(|e|e.to_string())?; assert_eq!(cotton_store::outbox::pending_count(file.driver).unwrap(),0); assert_eq!(cotton_store::mint::clock::held_count(file.driver).unwrap(),0); Ok(()) }).unwrap(); attempts(&client); assert!( client .with_file( |file| cotton_store::rejected::count_parked(file.driver).map_err(|e| e.to_string()) ) .unwrap() > 0 );}struct TakesPush(Arc<AtomicUsize>);impl cotton_store::exchange::SyncTransport for TakesPush { fn exchange(&mut self, request: &ExchangeRequest) -> Result<ExchangeResponse, String> { self.0.fetch_add(request.ops.len(), Ordering::SeqCst); let mut seen = std::collections::BTreeMap::<String, String>::new(); for op in &request.ops { let at = seen.entry(op.device_id.clone()).or_default(); if op.hlc > *at { at.clone_from(&op.hlc); } } serde_json::from_value(json!({ "workspaceId":request.workspace_id,"ops":[],"cursors":{},"hasMore":false, "schemaVersion":cotton_store::migrate::SCHEMA_VERSION,"appliedRows":{},"rejected":[],"seen":seen })).map_err(|e|e.to_string()) }}impl cotton_client::transport::RefusalAware for TakesPush { fn last_refusal(&self) -> Option<cotton_client::transport::ServerRefusal> { None }}/// RULE: The signed move follows the Mac's acknowledgment of the phone's edits; a completed push clears the local debt before the transfer is sent.#[test]fn an_acknowledged_edit_is_sent_before_the_move() { let (_dir, client) = attached(); let pushed = Arc::new(AtomicUsize::new(0)); let counted = pushed.clone(); client.set_transport(Some(Arc::new(move || { Ok(Box::new(TakesPush(counted.clone())) as Box<dyn cotton_client::transport::SyncingTransport>) }))); client .write( "create_todo", json!({"title":"Send this edit first"}).as_object().unwrap(), ) .unwrap(); let calls = AtomicUsize::new(0); super::record_moves::over( &client, &json!({"id":"todo","to":crate::fixture::WORKSPACE,"database":crate::fixture::WORKSPACE}), || { assert!(pushed.load(Ordering::SeqCst) > 0); assert_eq!( client .with_file(|file| cotton_store::runner_change::claim_of( file.driver, file.workspace_id, file.device_id ) .map(|c| c.unsent) .map_err(|e| e.to_string())) .unwrap(), 0 ); calls.fetch_add(1, Ordering::SeqCst); Ok(json!({"moved":true})) }, ) .unwrap(); assert_eq!(calls.load(Ordering::SeqCst), 1);}