2021-08-05 07:05:20 +00:00
|
|
|
use derive_more::Display;
|
2021-08-06 00:40:45 +00:00
|
|
|
use flowy_ot::{client::Document, core::*};
|
2021-07-31 12:53:45 +00:00
|
|
|
use rand::{prelude::*, Rng as WrappedRng};
|
2021-08-10 03:22:57 +00:00
|
|
|
use std::{sync::Once, thread::sleep_ms, time::Duration};
|
2021-07-31 12:53:45 +00:00
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
#[derive(Clone, Debug, Display)]
|
2021-08-04 07:09:04 +00:00
|
|
|
pub enum TestOp {
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "Insert")]
|
2021-08-03 15:13:20 +00:00
|
|
|
Insert(usize, &'static str, usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
// delta_i, s, start, length,
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "InsertBold")]
|
2021-08-02 10:35:25 +00:00
|
|
|
InsertBold(usize, &'static str, Interval),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
// delta_i, start, length, enable
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "Bold")]
|
2021-08-02 10:35:25 +00:00
|
|
|
Bold(usize, Interval, bool),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "Delete")]
|
2021-08-03 08:20:25 +00:00
|
|
|
Delete(usize, Interval),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
#[display(fmt = "Replace")]
|
|
|
|
Replace(usize, Interval, &'static str),
|
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "Italic")]
|
2021-08-02 15:08:24 +00:00
|
|
|
Italic(usize, Interval, bool),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
|
|
|
#[display(fmt = "Transform")]
|
2021-08-02 10:35:25 +00:00
|
|
|
Transform(usize, usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
// invert the delta_a base on the delta_b
|
2021-08-05 14:52:19 +00:00
|
|
|
#[display(fmt = "Invert")]
|
|
|
|
Invert(usize, usize),
|
|
|
|
|
2021-08-05 11:55:28 +00:00
|
|
|
#[display(fmt = "Undo")]
|
2021-08-05 14:52:19 +00:00
|
|
|
Undo(usize),
|
|
|
|
|
|
|
|
#[display(fmt = "Redo")]
|
|
|
|
Redo(usize),
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-10 03:22:57 +00:00
|
|
|
#[display(fmt = "Wait")]
|
|
|
|
Wait(usize),
|
|
|
|
|
2021-08-06 14:25:09 +00:00
|
|
|
#[display(fmt = "AssertStr")]
|
|
|
|
AssertStr(usize, &'static str),
|
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
#[display(fmt = "AssertOpsJson")]
|
2021-08-02 10:35:25 +00:00
|
|
|
AssertOpsJson(usize, &'static str),
|
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub struct OpTester {
|
2021-08-05 12:05:40 +00:00
|
|
|
documents: Vec<Document>,
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
impl OpTester {
|
2021-08-02 10:35:25 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
2021-08-08 14:29:16 +00:00
|
|
|
color_eyre::install().unwrap();
|
2021-08-10 06:24:41 +00:00
|
|
|
std::env::set_var("RUST_LOG", "info");
|
2021-08-02 10:35:25 +00:00
|
|
|
env_logger::init();
|
|
|
|
});
|
|
|
|
|
2021-08-05 12:05:40 +00:00
|
|
|
let mut documents = Vec::with_capacity(2);
|
2021-08-02 10:35:25 +00:00
|
|
|
for _ in 0..2 {
|
2021-08-05 12:05:40 +00:00
|
|
|
documents.push(Document::new());
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
2021-08-05 12:05:40 +00:00
|
|
|
Self { documents }
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub fn run_op(&mut self, op: &TestOp) {
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("***************** 😈{} *******************", &op);
|
2021-08-02 10:35:25 +00:00
|
|
|
match op {
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Insert(delta_i, s, index) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-08-06 00:40:45 +00:00
|
|
|
document.edit(*index, s).unwrap();
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Delete(delta_i, interval) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-08-10 03:22:57 +00:00
|
|
|
document.replace(*interval, "").unwrap();
|
|
|
|
},
|
|
|
|
TestOp::Replace(delta_i, interval, s) => {
|
|
|
|
let document = &mut self.documents[*delta_i];
|
|
|
|
document.replace(*interval, s).unwrap();
|
2021-08-03 08:20:25 +00:00
|
|
|
},
|
2021-08-05 12:05:40 +00:00
|
|
|
TestOp::InsertBold(delta_i, s, interval) => {
|
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-08-06 00:40:45 +00:00
|
|
|
document.edit(interval.start, s).unwrap();
|
|
|
|
document.format(*interval, Attribute::Bold, true).unwrap();
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Bold(delta_i, interval, enable) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-08-06 00:40:45 +00:00
|
|
|
document
|
|
|
|
.format(*interval, Attribute::Bold, *enable)
|
|
|
|
.unwrap();
|
2021-08-02 15:08:24 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Italic(delta_i, interval, enable) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let document = &mut self.documents[*delta_i];
|
2021-08-06 00:40:45 +00:00
|
|
|
document
|
|
|
|
.format(*interval, Attribute::Italic, *enable)
|
|
|
|
.unwrap();
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::Transform(delta_a_i, delta_b_i) => {
|
2021-08-06 00:40:45 +00:00
|
|
|
let (a_prime, b_prime) = self.documents[*delta_a_i]
|
|
|
|
.data()
|
|
|
|
.transform(&self.documents[*delta_b_i].data())
|
|
|
|
.unwrap();
|
|
|
|
log::trace!("a:{:?},b:{:?}", a_prime, b_prime);
|
|
|
|
|
|
|
|
let data_left = self.documents[*delta_a_i].data().compose(&b_prime).unwrap();
|
|
|
|
let data_right = self.documents[*delta_b_i].data().compose(&a_prime).unwrap();
|
|
|
|
|
|
|
|
self.documents[*delta_a_i].set_data(data_left);
|
|
|
|
self.documents[*delta_b_i].set_data(data_right);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-05 14:52:19 +00:00
|
|
|
TestOp::Invert(delta_a_i, delta_b_i) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let delta_a = &self.documents[*delta_a_i].data();
|
|
|
|
let delta_b = &self.documents[*delta_b_i].data();
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("Invert: ");
|
|
|
|
log::debug!("a: {}", delta_a.to_json());
|
|
|
|
log::debug!("b: {}", delta_b.to_json());
|
2021-08-04 08:59:21 +00:00
|
|
|
|
|
|
|
let (_, b_prime) = delta_a.transform(delta_b).unwrap();
|
2021-08-06 14:25:09 +00:00
|
|
|
let undo = b_prime.invert(&delta_a);
|
2021-08-05 07:05:20 +00:00
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
let new_delta = delta_a.compose(&b_prime).unwrap();
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("new delta: {}", new_delta.to_json());
|
|
|
|
log::debug!("undo delta: {}", undo.to_json());
|
|
|
|
|
2021-08-04 08:59:21 +00:00
|
|
|
let new_delta_after_undo = new_delta.compose(&undo).unwrap();
|
|
|
|
|
2021-08-05 07:05:20 +00:00
|
|
|
log::debug!("inverted delta a: {}", new_delta_after_undo.to_string());
|
|
|
|
|
2021-08-05 12:05:40 +00:00
|
|
|
assert_eq!(delta_a, &&new_delta_after_undo);
|
2021-08-04 08:59:21 +00:00
|
|
|
|
2021-08-05 12:05:40 +00:00
|
|
|
self.documents[*delta_a_i].set_data(new_delta_after_undo);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
2021-08-05 14:52:19 +00:00
|
|
|
TestOp::Undo(delta_i) => {
|
2021-08-06 00:40:45 +00:00
|
|
|
self.documents[*delta_i].undo().unwrap();
|
2021-08-05 14:52:19 +00:00
|
|
|
},
|
|
|
|
TestOp::Redo(delta_i) => {
|
2021-08-06 00:40:45 +00:00
|
|
|
self.documents[*delta_i].redo().unwrap();
|
2021-08-05 14:52:19 +00:00
|
|
|
},
|
2021-08-10 03:22:57 +00:00
|
|
|
TestOp::Wait(mills_sec) => {
|
|
|
|
std::thread::sleep(Duration::from_millis(*mills_sec as u64));
|
|
|
|
},
|
2021-08-06 14:25:09 +00:00
|
|
|
TestOp::AssertStr(delta_i, expected) => {
|
|
|
|
assert_eq!(&self.documents[*delta_i].to_string(), expected);
|
|
|
|
},
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
TestOp::AssertOpsJson(delta_i, expected) => {
|
2021-08-05 12:05:40 +00:00
|
|
|
let delta_i_json = self.documents[*delta_i].to_json();
|
2021-08-02 15:08:24 +00:00
|
|
|
|
2021-08-03 08:20:25 +00:00
|
|
|
let expected_delta: Delta = serde_json::from_str(expected).unwrap();
|
|
|
|
let target_delta: Delta = serde_json::from_str(&delta_i_json).unwrap();
|
|
|
|
|
|
|
|
if expected_delta != target_delta {
|
2021-08-03 15:13:20 +00:00
|
|
|
log::error!("✅ expect: {}", expected,);
|
|
|
|
log::error!("❌ receive: {}", delta_i_json);
|
2021-08-02 15:08:24 +00:00
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
assert_eq!(target_delta, expected_delta);
|
2021-08-02 10:35:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 07:09:04 +00:00
|
|
|
pub fn run_script(&mut self, script: Vec<TestOp>) {
|
2021-08-03 08:20:25 +00:00
|
|
|
for (_i, op) in script.iter().enumerate() {
|
2021-08-02 10:35:25 +00:00
|
|
|
self.run_op(op);
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 10:35:25 +00:00
|
|
|
pub fn debug_print_delta(delta: &Delta) {
|
2021-08-04 07:09:04 +00:00
|
|
|
eprintln!("😁 {}", serde_json::to_string(delta).unwrap());
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Rng(StdRng);
|
|
|
|
|
|
|
|
impl Default for Rng {
|
|
|
|
fn default() -> Self { Rng(StdRng::from_rng(thread_rng()).unwrap()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng {
|
|
|
|
pub fn from_seed(seed: [u8; 32]) -> Self { Rng(StdRng::from_seed(seed)) }
|
|
|
|
|
|
|
|
pub fn gen_string(&mut self, len: usize) -> String {
|
|
|
|
(0..len).map(|_| self.0.gen::<char>()).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_delta(&mut self, s: &str) -> Delta {
|
|
|
|
let mut delta = Delta::default();
|
|
|
|
loop {
|
2021-08-04 07:09:04 +00:00
|
|
|
let left = s.chars().count() - delta.base_len;
|
2021-08-03 08:20:25 +00:00
|
|
|
if left == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let i = if left == 1 {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
1 + self.0.gen_range(0, std::cmp::min(left - 1, 20))
|
|
|
|
};
|
|
|
|
match self.0.gen_range(0.0, 1.0) {
|
|
|
|
f if f < 0.2 => {
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert(&self.gen_string(i), Attributes::default());
|
2021-08-03 08:20:25 +00:00
|
|
|
},
|
|
|
|
f if f < 0.4 => {
|
2021-08-06 15:06:27 +00:00
|
|
|
delta.delete(i);
|
2021-08-03 08:20:25 +00:00
|
|
|
},
|
|
|
|
_ => {
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.retain(i, Attributes::Follow);
|
2021-08-03 08:20:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.0.gen_range(0.0, 1.0) < 0.3 {
|
2021-08-10 06:24:41 +00:00
|
|
|
delta.insert(
|
|
|
|
&("1".to_owned() + &self.gen_string(10)),
|
|
|
|
Attributes::default(),
|
|
|
|
);
|
2021-08-03 08:20:25 +00:00
|
|
|
}
|
|
|
|
delta
|
|
|
|
}
|
2021-08-02 10:35:25 +00:00
|
|
|
}
|