Skip to content

Commit f5dc9ed

Browse files
committed
test: add revision tests
1 parent 608a08e commit f5dc9ed

File tree

10 files changed

+163
-15
lines changed

10 files changed

+163
-15
lines changed

frontend/rust-lib/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/rust-lib/flowy-revision/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ futures-util = "0.3.15"
2121
async-stream = "0.3.2"
2222
serde_json = {version = "1.0"}
2323

24+
[dev-dependencies]
25+
nanoid = "0.4.0"
26+
2427
[features]
2528
flowy_unit_test = []

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ where
4747
let _ = self.save_migrate_record()?;
4848
}
4949
Some(s) => {
50-
let mut record = MigrationObjectRecord::from_str(&s)?;
50+
let mut record = MigrationObjectRecord::from_str(&s).map_err(|e| FlowyError::serde().context(e))?;
5151
let rev_str = self.target.default_target_rev_str()?;
5252
if record.len < rev_str.len() {
5353
let _ = self.reset_object().await?;

frontend/rust-lib/flowy-revision/src/rev_manager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl<Connection: 'static> RevisionManager<Connection> {
185185
Ok(())
186186
}
187187

188+
/// Returns the current revision id
188189
pub fn rev_id(&self) -> i64 {
189190
self.rev_id_counter.value()
190191
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
mod revision_test;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod revision_order_test;
2+
mod script;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::revision_test::script::{RevisionScript::*, RevisionTest};
2+
3+
#[tokio::test]
4+
async fn test() {
5+
let test = RevisionTest::new().await;
6+
let scripts = vec![];
7+
test.run_scripts(scripts).await;
8+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
use bytes::Bytes;
2+
use flowy_error::{FlowyError, FlowyResult};
3+
use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, SyncRecord};
4+
use flowy_revision::{
5+
RevisionCompress, RevisionManager, RevisionPersistence, RevisionSnapshotDiskCache, RevisionSnapshotInfo,
6+
};
7+
use flowy_sync::entities::revision::{Revision, RevisionRange};
8+
use nanoid::nanoid;
9+
use std::sync::Arc;
10+
11+
pub enum RevisionScript {
12+
AddLocalRevision(Revision),
13+
AckRevision { rev_id: i64 },
14+
AssertNextSyncRevisionId { rev_id: i64 },
15+
AssertNextSyncRevision(Option<Revision>),
16+
}
17+
18+
pub struct RevisionTest {
19+
rev_manager: Arc<RevisionManager<RevisionConnectionMock>>,
20+
}
21+
22+
impl RevisionTest {
23+
pub async fn new() -> Self {
24+
let user_id = nanoid!(10);
25+
let object_id = nanoid!(6);
26+
let persistence = RevisionPersistence::new(&user_id, &object_id, RevisionDiskCacheMock::new());
27+
let compress = RevisionCompressMock {};
28+
let snapshot = RevisionSnapshotMock {};
29+
let rev_manager = RevisionManager::new(&user_id, &object_id, persistence, compress, snapshot);
30+
Self {
31+
rev_manager: Arc::new(rev_manager),
32+
}
33+
}
34+
pub async fn run_scripts(&self, scripts: Vec<RevisionScript>) {
35+
for script in scripts {
36+
self.run_script(script).await;
37+
}
38+
}
39+
pub async fn run_script(&self, script: RevisionScript) {
40+
match script {
41+
RevisionScript::AddLocalRevision(revision) => {
42+
self.rev_manager.add_local_revision(&revision).await.unwrap();
43+
}
44+
RevisionScript::AckRevision { rev_id } => {
45+
//
46+
self.rev_manager.ack_revision(rev_id).await.unwrap()
47+
}
48+
RevisionScript::AssertNextSyncRevisionId { rev_id } => {
49+
//
50+
assert_eq!(self.rev_manager.rev_id(), rev_id)
51+
}
52+
RevisionScript::AssertNextSyncRevision(expected) => {
53+
let next_revision = self.rev_manager.next_sync_revision().await.unwrap();
54+
assert_eq!(next_revision, expected);
55+
}
56+
}
57+
}
58+
}
59+
60+
pub struct RevisionDiskCacheMock {}
61+
62+
impl RevisionDiskCacheMock {
63+
pub fn new() -> Self {
64+
Self {}
65+
}
66+
}
67+
68+
impl RevisionDiskCache<RevisionConnectionMock> for RevisionDiskCacheMock {
69+
type Error = FlowyError;
70+
71+
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
72+
todo!()
73+
}
74+
75+
fn get_connection(&self) -> Result<RevisionConnectionMock, Self::Error> {
76+
todo!()
77+
}
78+
79+
fn read_revision_records(
80+
&self,
81+
object_id: &str,
82+
rev_ids: Option<Vec<i64>>,
83+
) -> Result<Vec<SyncRecord>, Self::Error> {
84+
todo!()
85+
}
86+
87+
fn read_revision_records_with_range(
88+
&self,
89+
object_id: &str,
90+
range: &RevisionRange,
91+
) -> Result<Vec<SyncRecord>, Self::Error> {
92+
todo!()
93+
}
94+
95+
fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()> {
96+
todo!()
97+
}
98+
99+
fn delete_revision_records(&self, object_id: &str, rev_ids: Option<Vec<i64>>) -> Result<(), Self::Error> {
100+
todo!()
101+
}
102+
103+
fn delete_and_insert_records(
104+
&self,
105+
object_id: &str,
106+
deleted_rev_ids: Option<Vec<i64>>,
107+
inserted_records: Vec<SyncRecord>,
108+
) -> Result<(), Self::Error> {
109+
todo!()
110+
}
111+
}
112+
113+
pub struct RevisionConnectionMock {}
114+
115+
pub struct RevisionSnapshotMock {}
116+
117+
impl RevisionSnapshotDiskCache for RevisionSnapshotMock {
118+
fn write_snapshot(&self, object_id: &str, rev_id: i64, data: Vec<u8>) -> FlowyResult<()> {
119+
todo!()
120+
}
121+
122+
fn read_snapshot(&self, object_id: &str, rev_id: i64) -> FlowyResult<RevisionSnapshotInfo> {
123+
todo!()
124+
}
125+
}
126+
127+
pub struct RevisionCompressMock {}
128+
129+
impl RevisionCompress for RevisionCompressMock {
130+
fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
131+
todo!()
132+
}
133+
}
134+
135+
pub struct RevisionMock {}
136+
137+
// impl std::convert::From<RevisionMock> for Revision {
138+
// fn from(_: RevisionMock) -> Self {}
139+
// }

shared-lib/flowy-sync/src/entities/revision.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ pub struct Revision {
2121

2222
#[pb(index = 5)]
2323
pub object_id: String,
24-
25-
#[pb(index = 6)]
26-
ty: RevType, // Deprecated
27-
28-
#[pb(index = 7)]
29-
pub user_id: String,
24+
// #[pb(index = 6)]
25+
// ty: RevType, // Deprecated
26+
//
27+
// #[pb(index = 7)]
28+
// pub user_id: String,
3029
}
3130

3231
impl std::convert::From<Vec<u8>> for Revision {
@@ -42,10 +41,9 @@ impl Revision {
4241
base_rev_id: i64,
4342
rev_id: i64,
4443
bytes: Bytes,
45-
user_id: &str,
44+
_user_id: &str,
4645
md5: T,
4746
) -> Revision {
48-
let user_id = user_id.to_owned();
4947
let object_id = object_id.to_owned();
5048
let bytes = bytes.to_vec();
5149
let base_rev_id = base_rev_id;
@@ -61,8 +59,6 @@ impl Revision {
6159
bytes,
6260
md5: md5.into(),
6361
object_id,
64-
ty: RevType::DeprecatedLocal,
65-
user_id,
6662
}
6763
}
6864
pub fn is_empty(&self) -> bool {

shared-lib/lib-ot/tests/node/serde_test.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use lib_ot::core::{
2-
AttributeBuilder, Changeset, NodeData, NodeDataBuilder, NodeOperation, NodeTree, NodeTreeContext, Path,
3-
};
1+
use lib_ot::core::{AttributeBuilder, Changeset, NodeData, NodeDataBuilder, NodeOperation, NodeTree, Path};
42
use lib_ot::text_delta::DeltaTextOperationBuilder;
53

64
#[test]

0 commit comments

Comments
 (0)