Splitting side-node crate into lib/bin for integration tests

This commit is contained in:
Dave Hrycyszyn
2024-06-17 15:25:22 +01:00
parent 8fa0eebe2b
commit d38721e1a0
6 changed files with 113 additions and 75 deletions

View File

@@ -1,13 +1,9 @@
use bft_crdt_derive::add_crdt_fields;
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode};
use bft_json_crdt::json_crdt::{BaseCrdt, CrdtNode, IntoCrdtNode, SignedOp};
use bft_json_crdt::op::ROOT_ID;
use bft_json_crdt::{keypair::make_keypair, list_crdt::ListCrdt};
#[add_crdt_fields]
#[derive(Clone, CrdtNode)]
struct ListExample {
list: ListCrdt<char>,
}
use serde_json::Value;
use side_node::crdt::{Transaction, TransactionList};
// case 1 - send valid updates
#[test]
@@ -15,15 +11,35 @@ 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
let keypair1 = make_keypair();
let mut crdt1 = BaseCrdt::<ListExample>::new(&keypair1);
let _a = crdt1.doc.list.insert(ROOT_ID, 'a').sign(&keypair1);
let _b = crdt1.doc.list.insert(_a.id(), 'b').sign(&keypair1);
let _c = crdt1.doc.list.insert(_b.id(), 'c').sign(&keypair1);
let mut crdt1 = BaseCrdt::<TransactionList>::new(&keypair1);
assert_eq!(crdt1.doc.list.view(), vec!['a', 'b', 'c']);
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"));
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())]
);
let keypair2 = make_keypair();
let mut crdt2 = BaseCrdt::<ListExample>::new(&keypair2);
let mut crdt2 = BaseCrdt::<TransactionList>::new(&keypair2);
crdt2.apply(_a.clone());
crdt2.apply(_b);
crdt2.apply(_c.clone());
@@ -43,3 +59,20 @@ fn test_valid_updates() {
"views are still equal after repeated applies"
);
}
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()
// }