Browse Source

test: add revision tests

nathan 2 years ago
parent
commit
f5dc9ed975

+ 1 - 0
frontend/rust-lib/Cargo.lock

@@ -1076,6 +1076,7 @@ dependencies = [
  "futures-util",
  "lib-infra",
  "lib-ws",
+ "nanoid",
  "serde",
  "serde_json",
  "strum",

+ 3 - 0
frontend/rust-lib/flowy-revision/Cargo.toml

@@ -21,5 +21,8 @@ futures-util = "0.3.15"
 async-stream = "0.3.2"
 serde_json = {version = "1.0"}
 
+[dev-dependencies]
+nanoid = "0.4.0"
+
 [features]
 flowy_unit_test = []

+ 1 - 1
frontend/rust-lib/flowy-revision/src/cache/reset.rs

@@ -47,7 +47,7 @@ where
                 let _ = self.save_migrate_record()?;
             }
             Some(s) => {
-                let mut record = MigrationObjectRecord::from_str(&s)?;
+                let mut record = MigrationObjectRecord::from_str(&s).map_err(|e| FlowyError::serde().context(e))?;
                 let rev_str = self.target.default_target_rev_str()?;
                 if record.len < rev_str.len() {
                     let _ = self.reset_object().await?;

+ 1 - 0
frontend/rust-lib/flowy-revision/src/rev_manager.rs

@@ -185,6 +185,7 @@ impl<Connection: 'static> RevisionManager<Connection> {
         Ok(())
     }
 
+    /// Returns the current revision id
     pub fn rev_id(&self) -> i64 {
         self.rev_id_counter.value()
     }

+ 1 - 1
frontend/rust-lib/flowy-revision/tests/main.rs

@@ -1 +1 @@
-
+mod revision_test;

+ 2 - 0
frontend/rust-lib/flowy-revision/tests/revision_test/mod.rs

@@ -0,0 +1,2 @@
+mod revision_order_test;
+mod script;

+ 8 - 0
frontend/rust-lib/flowy-revision/tests/revision_test/revision_order_test.rs

@@ -0,0 +1,8 @@
+use crate::revision_test::script::{RevisionScript::*, RevisionTest};
+
+#[tokio::test]
+async fn test() {
+    let test = RevisionTest::new().await;
+    let scripts = vec![];
+    test.run_scripts(scripts).await;
+}

+ 139 - 0
frontend/rust-lib/flowy-revision/tests/revision_test/script.rs

@@ -0,0 +1,139 @@
+use bytes::Bytes;
+use flowy_error::{FlowyError, FlowyResult};
+use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, SyncRecord};
+use flowy_revision::{
+    RevisionCompress, RevisionManager, RevisionPersistence, RevisionSnapshotDiskCache, RevisionSnapshotInfo,
+};
+use flowy_sync::entities::revision::{Revision, RevisionRange};
+use nanoid::nanoid;
+use std::sync::Arc;
+
+pub enum RevisionScript {
+    AddLocalRevision(Revision),
+    AckRevision { rev_id: i64 },
+    AssertNextSyncRevisionId { rev_id: i64 },
+    AssertNextSyncRevision(Option<Revision>),
+}
+
+pub struct RevisionTest {
+    rev_manager: Arc<RevisionManager<RevisionConnectionMock>>,
+}
+
+impl RevisionTest {
+    pub async fn new() -> Self {
+        let user_id = nanoid!(10);
+        let object_id = nanoid!(6);
+        let persistence = RevisionPersistence::new(&user_id, &object_id, RevisionDiskCacheMock::new());
+        let compress = RevisionCompressMock {};
+        let snapshot = RevisionSnapshotMock {};
+        let rev_manager = RevisionManager::new(&user_id, &object_id, persistence, compress, snapshot);
+        Self {
+            rev_manager: Arc::new(rev_manager),
+        }
+    }
+    pub async fn run_scripts(&self, scripts: Vec<RevisionScript>) {
+        for script in scripts {
+            self.run_script(script).await;
+        }
+    }
+    pub async fn run_script(&self, script: RevisionScript) {
+        match script {
+            RevisionScript::AddLocalRevision(revision) => {
+                self.rev_manager.add_local_revision(&revision).await.unwrap();
+            }
+            RevisionScript::AckRevision { rev_id } => {
+                //
+                self.rev_manager.ack_revision(rev_id).await.unwrap()
+            }
+            RevisionScript::AssertNextSyncRevisionId { rev_id } => {
+                //
+                assert_eq!(self.rev_manager.rev_id(), rev_id)
+            }
+            RevisionScript::AssertNextSyncRevision(expected) => {
+                let next_revision = self.rev_manager.next_sync_revision().await.unwrap();
+                assert_eq!(next_revision, expected);
+            }
+        }
+    }
+}
+
+pub struct RevisionDiskCacheMock {}
+
+impl RevisionDiskCacheMock {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl RevisionDiskCache<RevisionConnectionMock> for RevisionDiskCacheMock {
+    type Error = FlowyError;
+
+    fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
+        todo!()
+    }
+
+    fn get_connection(&self) -> Result<RevisionConnectionMock, Self::Error> {
+        todo!()
+    }
+
+    fn read_revision_records(
+        &self,
+        object_id: &str,
+        rev_ids: Option<Vec<i64>>,
+    ) -> Result<Vec<SyncRecord>, Self::Error> {
+        todo!()
+    }
+
+    fn read_revision_records_with_range(
+        &self,
+        object_id: &str,
+        range: &RevisionRange,
+    ) -> Result<Vec<SyncRecord>, Self::Error> {
+        todo!()
+    }
+
+    fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()> {
+        todo!()
+    }
+
+    fn delete_revision_records(&self, object_id: &str, rev_ids: Option<Vec<i64>>) -> Result<(), Self::Error> {
+        todo!()
+    }
+
+    fn delete_and_insert_records(
+        &self,
+        object_id: &str,
+        deleted_rev_ids: Option<Vec<i64>>,
+        inserted_records: Vec<SyncRecord>,
+    ) -> Result<(), Self::Error> {
+        todo!()
+    }
+}
+
+pub struct RevisionConnectionMock {}
+
+pub struct RevisionSnapshotMock {}
+
+impl RevisionSnapshotDiskCache for RevisionSnapshotMock {
+    fn write_snapshot(&self, object_id: &str, rev_id: i64, data: Vec<u8>) -> FlowyResult<()> {
+        todo!()
+    }
+
+    fn read_snapshot(&self, object_id: &str, rev_id: i64) -> FlowyResult<RevisionSnapshotInfo> {
+        todo!()
+    }
+}
+
+pub struct RevisionCompressMock {}
+
+impl RevisionCompress for RevisionCompressMock {
+    fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
+        todo!()
+    }
+}
+
+pub struct RevisionMock {}
+
+// impl std::convert::From<RevisionMock> for Revision {
+//     fn from(_: RevisionMock) -> Self {}
+// }

+ 6 - 10
shared-lib/flowy-sync/src/entities/revision.rs

@@ -21,12 +21,11 @@ pub struct Revision {
 
     #[pb(index = 5)]
     pub object_id: String,
-
-    #[pb(index = 6)]
-    ty: RevType, // Deprecated
-
-    #[pb(index = 7)]
-    pub user_id: String,
+    // #[pb(index = 6)]
+    // ty: RevType, // Deprecated
+    //
+    // #[pb(index = 7)]
+    // pub user_id: String,
 }
 
 impl std::convert::From<Vec<u8>> for Revision {
@@ -42,10 +41,9 @@ impl Revision {
         base_rev_id: i64,
         rev_id: i64,
         bytes: Bytes,
-        user_id: &str,
+        _user_id: &str,
         md5: T,
     ) -> Revision {
-        let user_id = user_id.to_owned();
         let object_id = object_id.to_owned();
         let bytes = bytes.to_vec();
         let base_rev_id = base_rev_id;
@@ -61,8 +59,6 @@ impl Revision {
             bytes,
             md5: md5.into(),
             object_id,
-            ty: RevType::DeprecatedLocal,
-            user_id,
         }
     }
     pub fn is_empty(&self) -> bool {

+ 1 - 3
shared-lib/lib-ot/tests/node/serde_test.rs

@@ -1,6 +1,4 @@
-use lib_ot::core::{
-    AttributeBuilder, Changeset, NodeData, NodeDataBuilder, NodeOperation, NodeTree, NodeTreeContext, Path,
-};
+use lib_ot::core::{AttributeBuilder, Changeset, NodeData, NodeDataBuilder, NodeOperation, NodeTree, Path};
 use lib_ot::text_delta::DeltaTextOperationBuilder;
 
 #[test]