//! A served row pulled again, saying more (27 Oct 2026): a copy that gained a //! column the object fills pulls the rows it held again, and the receipt of //! the same version — at the same stamp — now names that column too. One row //! keeps one receipt per stamp, so the new one replaces the one it says all //! of; one that says something else at that stamp, or a re-date that does, //! replaces nothing. //! //! Every row is synthetic and every value invented. use std::collections::BTreeMap; use cotton_store::actor::{Actor, JobName}; use cotton_store::exchange::fold_incoming; use cotton_store::migrate::migrate_store; use cotton_store::mint::{served, ServedRow}; use cotton_store::protocol::types::CottonOp; use cotton_store::Driver; use rusqlite::types::Value; use serde_json::{json, Value as JsonValue}; const WORKSPACE: &str = "11111111-1111-4111-8111-111111111111"; const NOW: &str = "2026-10-27T09:00:00.000Z"; const AUTHOR: &str = "object"; const STAMP: &str = "1792486800000:0004:object"; fn file() -> Driver { let db = Driver::open_in_memory().expect("opens"); migrate_store(&db).expect("migrates"); db.run( "INSERT INTO cotton_workspaces (id, name, owner_user_id, created_at) VALUES (?,?,?,?)", &[Value::Text(WORKSPACE.into()), Value::Text("A workspace".into()), Value::Text("owner-1".into()), Value::Text(NOW.into())], ) .expect("the workspace row"); db } fn actor() -> Actor { Actor::device_job(JobName::named("object-sync").expect("a job name")) } /// The object's author fact, as a copy folds it first. fn author() -> CottonOp { let stamp = format!("{:013}:0000:{AUTHOR}", 0); let fields: BTreeMap = [ ("workspace_id", json!(WORKSPACE)), ("device_id", json!(AUTHOR)), ("device_name", JsonValue::Null), ("minted_at", json!("1970-01-01T00:00:00.000Z")), ("reason", json!("first")), ("first_stamp", json!(stamp)), ] .into_iter() .map(|(key, value)| (key.to_string(), value)) .collect(); served("01JAUTHOR00000000000000000".into(), "cotton_authors", AUTHOR, &stamp, &actor(), ServedRow::Live(fields), None).expect("the fact") } fn todo(op_id: &str, stamp: &str, title: &str, notes: Option<&str>, replaces: Option<&str>) -> CottonOp { let mut fields: BTreeMap = [ ("workspace_id", json!(WORKSPACE)), ("title", json!(title)), ("status", json!("open")), ("priority", json!(0)), ("created_at", json!(NOW)), ("updated_at", json!(NOW)), ] .into_iter() .map(|(key, value)| (key.to_string(), value)) .collect(); if let Some(notes) = notes { fields.insert("notes".into(), json!(notes)); } served(op_id.into(), "cotton_todos", "t-1", stamp, &actor(), ServedRow::Live(fields), replaces).expect("a receipt") } fn held(db: &Driver) -> Vec { db.all("SELECT op_id FROM _writes WHERE tbl = 'cotton_todos' ORDER BY op_id", &[]) .expect("reads") .iter() .filter_map(|row| row.text("op_id").map(str::to_string)) .collect() } fn row(db: &Driver) -> (Option, Option) { db.get("SELECT title, notes FROM cotton_todos WHERE id = 't-1'", &[]) .expect("reads") .map(|row| (row.text("title").map(str::to_string), row.text("notes").map(str::to_string))) .expect("the row") } /// RULE: at its own stamp a served receipt replaces the one it names when it says every field and value of it and more, so a copy that gained a column takes the value on a row it held; one that says another value there, or a re-date that adds a field, replaces nothing and parks. #[test] fn a_served_receipt_saying_more_at_its_own_stamp_replaces_the_one_it_names() { let db = file(); fold_incoming(&db, &[author(), todo("01JTODOFIRST00000000000000", STAMP, "Ring the bank", None, None)]).expect("folds"); assert_eq!(row(&db), (Some("Ring the bank".into()), None)); let more = todo("01JTODOMORE000000000000000", STAMP, "Ring the bank", Some("before noon"), Some("01JTODOFIRST00000000000000")); let folded = fold_incoming(&db, &[more]).expect("folds"); assert!(folded.rejected.is_empty(), "{:?}", folded.rejected); assert_eq!(row(&db), (Some("Ring the bank".into()), Some("before noon".into())), "the value lands on the row held"); assert_eq!(held(&db), vec!["01JTODOMORE000000000000000".to_string()], "one receipt per stamp: the one said all of is gone"); // Another value at the same stamp is another write: nothing is dropped. let other = todo("01JTODOOTHER00000000000000", STAMP, "Ring the plumber", Some("before noon"), Some("01JTODOMORE000000000000000")); fold_incoming(&db, &[other]).expect("folds"); assert_eq!(held(&db), vec!["01JTODOMORE000000000000000".to_string()]); assert_eq!(row(&db), (Some("Ring the bank".into()), Some("before noon".into()))); // A re-date keeps every field it names: one that adds a field is refused. let redated = todo("01JTODOREDATE0000000000000", "1792486800001:0004:object", "Ring the bank", Some("before noon, today"), Some("01JTODOMORE000000000000000")); fold_incoming(&db, &[redated]).expect("folds"); assert_eq!(held(&db), vec!["01JTODOMORE000000000000000".to_string()]); let parked = db.all("SELECT reason FROM _parked", &[]).expect("reads"); assert!(!parked.is_empty(), "the refused writes park"); assert!(parked.iter().all(|row| row.text("reason").is_some_and(|reason| reason.contains("not the same write"))), "{parked:?}"); } /// A receipt the fold kept but could not assemble a row from: held, its row absent. fn hold_unassembled(db: &Driver, op: &CottonOp) { let receipt = cotton_store::canonical::decode(op.receipt.as_bytes()).expect("decodes"); db.run( "INSERT INTO _writes (tbl, row_id, hlc, author, op_id, kind, actor, bytes) VALUES (?,?,?,?,?,?,?,?)", &[ Value::Text(op.table.clone()), Value::Text(op.row_id.clone()), Value::Text(receipt.stamp.clone()), Value::Text(receipt.author.clone()), Value::Text(op.op_id.clone()), Value::Text("create".into()), Value::Text(receipt.actor.clone()), Value::Blob(op.receipt.as_bytes().to_vec()), ], ) .expect("held"); } /// RULE (walk, 27 Oct 2026): a receipt an earlier build minted naming null in a column that cannot hold null — the object's nothing, which the copy now holds as the column's default — is said all of by the receipt of the same version naming that default at its own stamp, so the row the earlier one could never assemble lands; a null over a column that may hold null is still a value said, and replaces nothing. #[test] fn a_served_receipt_naming_a_default_replaces_one_that_named_null_where_null_cannot_stand() { let db = file(); fold_incoming(&db, &[author()]).expect("the fact folds"); let mut nothing = todo("01JTODONULL000000000000000", STAMP, "Ring the bank", None, None); { let mut receipt = cotton_store::canonical::decode(nothing.receipt.as_bytes()).expect("decodes"); receipt.fields.insert("priority".into(), JsonValue::Null); nothing = served(nothing.op_id.clone(), "cotton_todos", "t-1", STAMP, &actor(), ServedRow::Live(receipt.fields), None).expect("a receipt"); } hold_unassembled(&db, ¬hing); assert!(db.get("SELECT 1 FROM cotton_todos WHERE id = 't-1'", &[]).expect("reads").is_none(), "no row stands on a null priority"); let defaulted = todo("01JTODODEFAULT000000000000", STAMP, "Ring the bank", None, Some("01JTODONULL000000000000000")); let folded = fold_incoming(&db, &[defaulted]).expect("folds"); assert!(folded.rejected.is_empty(), "{:?}", folded.rejected); assert_eq!(row(&db), (Some("Ring the bank".into()), None), "the row lands"); assert_eq!(held(&db), vec!["01JTODODEFAULT000000000000".to_string()], "one receipt per stamp"); // notes may hold null: a null said there is a value, and a receipt // naming another value at the same stamp is another write. let db = file(); let mut said_null = todo("01JTODONOTESNULL0000000000", STAMP, "Ring the bank", None, None); { let mut receipt = cotton_store::canonical::decode(said_null.receipt.as_bytes()).expect("decodes"); receipt.fields.insert("notes".into(), JsonValue::Null); said_null = served(said_null.op_id.clone(), "cotton_todos", "t-1", STAMP, &actor(), ServedRow::Live(receipt.fields), None).expect("a receipt"); } fold_incoming(&db, &[author(), said_null]).expect("folds"); let noted = todo("01JTODONOTED00000000000000", STAMP, "Ring the bank", Some("before noon"), Some("01JTODONOTESNULL0000000000")); fold_incoming(&db, &[noted]).expect("folds"); assert_eq!(held(&db), vec!["01JTODONOTESNULL0000000000".to_string()], "a null notes was said: nothing is dropped"); assert_eq!(row(&db), (Some("Ring the bank".into()), None)); } /// RULE (walk, 27 Oct 2026): the receipt a re-stamp replaced, offered again at its own stamp (a park of it retried), is that write already — stale, never a second receipt at the stamp and never a park that waits for ever on a unique clash. #[test] fn a_replaced_receipt_offered_again_at_its_stamp_is_stale() { let db = file(); fold_incoming(&db, &[author()]).expect("the fact folds"); let mut nothing = todo("01JTODONULL000000000000000", STAMP, "Ring the bank", None, None); { let mut receipt = cotton_store::canonical::decode(nothing.receipt.as_bytes()).expect("decodes"); receipt.fields.insert("priority".into(), JsonValue::Null); nothing = served(nothing.op_id.clone(), "cotton_todos", "t-1", STAMP, &actor(), ServedRow::Live(receipt.fields), None).expect("a receipt"); } let defaulted = todo("01JTODODEFAULT000000000000", STAMP, "Ring the bank", None, None); fold_incoming(&db, &[defaulted]).expect("folds"); let again = fold_incoming(&db, &[nothing]).expect("folds"); assert!(again.rejected.is_empty(), "{:?}", again.rejected); assert_eq!(held(&db), vec!["01JTODODEFAULT000000000000".to_string()], "one receipt per stamp"); assert!(db.all("SELECT 1 FROM _parked", &[]).expect("reads").is_empty(), "nothing parks"); assert_eq!(row(&db), (Some("Ring the bank".into()), None)); // Another value at the stamp is another write: nothing is dropped. let other = todo("01JTODOOTHER00000000000000", STAMP, "Ring the plumber", None, None); fold_incoming(&db, &[other]).expect("folds"); assert_eq!(held(&db), vec!["01JTODODEFAULT000000000000".to_string()]); }