2024-06-13 11:35:07 +01:00
|
|
|
use bft_crdt_derive::add_crdt_fields;
|
2024-06-17 15:25:22 +01:00
|
|
|
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode, SignedOp};
|
2024-06-13 11:35:07 +01:00
|
|
|
use bft_json_crdt::op::ROOT_ID;
|
|
|
|
|
use bft_json_crdt::{keypair::make_keypair, list_crdt::ListCrdt};
|
2024-06-17 15:25:22 +01:00
|
|
|
use serde_json::Value;
|
|
|
|
|
use side_node::crdt::{Transaction, TransactionList};
|
2024-06-13 11:35:07 +01:00
|
|
|
|
|
|
|
|
// case 1 - send valid updates
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_valid_updates() {
|
|
|
|
|
// Insert to crdt.doc on local node, test applying the same operation to a remote node
|
|
|
|
|
// and check that the view is the same
|
2024-06-13 11:36:51 +01:00
|
|
|
let keypair1 = make_keypair();
|
2024-06-17 15:25:22 +01:00
|
|
|
let mut crdt1 = BaseCrdt::<TransactionList>::new(&keypair1);
|
|
|
|
|
|
|
|
|
|
let val_a = side_node::utils::fake_transaction_json(String::from("a"));
|
|
|
|
|
let val_b = side_node::utils::fake_transaction_json(String::from("b"));
|
|
|
|
|
let val_c = side_node::utils::fake_transaction_json(String::from("c"));
|
2024-06-13 11:35:07 +01:00
|
|
|
|
2024-06-17 15:25:22 +01:00
|
|
|
let _a = crdt1
|
|
|
|
|
.doc
|
|
|
|
|
.list
|
|
|
|
|
.insert(ROOT_ID, val_a.clone())
|
|
|
|
|
.sign(&keypair1);
|
|
|
|
|
let _b = crdt1
|
|
|
|
|
.doc
|
|
|
|
|
.list
|
|
|
|
|
.insert(_a.id(), val_b.clone())
|
|
|
|
|
.sign(&keypair1);
|
|
|
|
|
let _c = crdt1
|
|
|
|
|
.doc
|
|
|
|
|
.list
|
|
|
|
|
.insert(_b.id(), val_c.clone())
|
|
|
|
|
.sign(&keypair1);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
crdt1.doc.list.view(),
|
|
|
|
|
vec![to_tx(_a.clone()), to_tx(_b.clone()), to_tx(_c.clone())]
|
|
|
|
|
);
|
2024-06-13 11:35:07 +01:00
|
|
|
|
2024-06-13 11:36:51 +01:00
|
|
|
let keypair2 = make_keypair();
|
2024-06-17 15:25:22 +01:00
|
|
|
let mut crdt2 = BaseCrdt::<TransactionList>::new(&keypair2);
|
2024-06-13 11:35:07 +01:00
|
|
|
crdt2.apply(_a.clone());
|
|
|
|
|
crdt2.apply(_b);
|
|
|
|
|
crdt2.apply(_c.clone());
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
crdt2.doc.list.view(),
|
2024-06-13 11:36:51 +01:00
|
|
|
crdt1.doc.list.view(),
|
2024-06-13 11:35:07 +01:00
|
|
|
"views should be equal"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
crdt2.apply(_a.clone());
|
|
|
|
|
crdt2.apply(_a);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-06-13 11:36:51 +01:00
|
|
|
crdt1.doc.list.view(),
|
2024-06-13 11:35:07 +01:00
|
|
|
crdt2.doc.list.view(),
|
|
|
|
|
"views are still equal after repeated applies"
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-17 15:25:22 +01:00
|
|
|
|
|
|
|
|
fn to_tx(op: SignedOp) -> Transaction {
|
|
|
|
|
let val = op.inner.content;
|
|
|
|
|
serde_json::from_value(val.into()).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pub fn iter(&self) -> impl Iterator<Item = &Transaction> {
|
|
|
|
|
// self.ops
|
|
|
|
|
// .iter()
|
|
|
|
|
// .filter(|op| !op.is_deleted && op.content.is_some())
|
|
|
|
|
// .map(|op| op.content.as_ref().unwrap())
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// /// Convenience function to get a vector of visible list elements
|
|
|
|
|
// pub fn view(&self) -> Vec<T> {
|
|
|
|
|
// self.iter().map(|i| i.to_owned()).collect()
|
|
|
|
|
// }
|