#![allow(unexpected_cfgs)]
use serial_test::parallel;
use loro::event::{Diff, DiffBatch};
use loro::json::{JsonChange, JsonOp, JsonOpContent, JsonSchema, MapOp};
use loro::{CommitOptions, Container, ContainerID, ContainerType, LoroDoc, LoroList, ID};
use loro::{Frontiers, LoroValue};
#[test]
#[parallel]
#[should_panic(
expected = "The container does not exist in the document. Use `try_get_text` or `get_container` to check for existence."
)]
fn get_text_with_nonexistent_container_id_panics() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Text);
let _text = doc.get_text(id);
}
#[test]
#[parallel]
#[should_panic(
expected = "The container does not exist in the document. Use `try_get_list` or `get_container` to check for existence."
)]
fn get_list_with_nonexistent_container_id_panics() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::List);
let _list = doc.get_list(id);
}
#[test]
#[parallel]
#[should_panic(
expected = "The container does not exist in the document. Use `try_get_map` or `get_container` to check for existence."
)]
fn get_map_with_nonexistent_container_id_panics() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Map);
let _map = doc.get_map(id);
}
#[test]
#[parallel]
#[should_panic(
expected = "The container does not exist in the document. Use `try_get_tree` or `get_container` to check for existence."
)]
fn get_tree_with_nonexistent_container_id_panics() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Tree);
let _tree = doc.get_tree(id);
}
#[test]
#[parallel]
fn try_get_text_returns_none_for_missing_container() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Text);
assert!(doc.try_get_text(id).is_none());
}
#[test]
#[parallel]
fn try_get_list_returns_none_for_missing_container() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::List);
assert!(doc.try_get_list(id).is_none());
}
#[test]
#[parallel]
fn try_get_map_returns_none_for_missing_container() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Map);
assert!(doc.try_get_map(id).is_none());
}
#[test]
#[parallel]
fn try_get_tree_returns_none_for_missing_container() {
let doc = LoroDoc::new();
let id = ContainerID::new_normal(ID::new(0, 0), ContainerType::Tree);
assert!(doc.try_get_tree(id).is_none());
}
#[test]
#[parallel]
fn detached_list_insert_out_of_bounds_returns_error() {
let list = LoroList::new();
let err = list.insert(10, "x").unwrap_err();
assert!(matches!(err, loro::LoroError::OutOfBound { .. }));
}
#[test]
#[parallel]
fn nested_transaction_now_returns_error() {
let doc = LoroDoc::new();
let err = doc.inner().txn().unwrap_err();
assert!(matches!(err, loro::LoroError::DuplicatedTransactionError));
}
#[test]
#[parallel]
fn commit_with_immediate_renew_on_detached_doc_no_longer_panics() {
let doc = LoroDoc::new();
doc.get_text("text").insert(0, "hello").unwrap();
doc.set_detached_editing(true);
doc.detach();
doc.set_detached_editing(false);
doc.commit_with(CommitOptions::new().immediate_renew(true));
}
#[test]
#[parallel]
fn tree_mov_after_deleted_node_returns_error() {
let doc = LoroDoc::new();
let tree = doc.get_tree("root");
let a = tree.create(None).unwrap();
let b = tree.create(None).unwrap();
tree.delete(b).unwrap();
let err = tree.mov_after(a, b).unwrap_err();
assert!(matches!(
err,
loro::LoroError::TreeError(loro::LoroTreeError::TreeNodeDeletedOrNotExist(_))
));
}
#[test]
#[parallel]
fn tree_mov_before_deleted_node_returns_error() {
let doc = LoroDoc::new();
let tree = doc.get_tree("root");
let a = tree.create(None).unwrap();
let b = tree.create(None).unwrap();
tree.delete(b).unwrap();
let err = tree.mov_before(a, b).unwrap_err();
assert!(matches!(
err,
loro::LoroError::TreeError(loro::LoroTreeError::TreeNodeDeletedOrNotExist(_))
));
}
#[test]
#[parallel]
#[should_panic(expected = "Cannot create a detached container of type Unknown")]
fn container_new_unknown_panics() {
let _container = Container::new(ContainerType::Unknown(0));
}
#[test]
#[parallel]
fn apply_diff_with_wrong_type_returns_error() {
let doc = LoroDoc::new();
let mut batch = DiffBatch::default();
let map_id = ContainerID::new_root("map", ContainerType::Map);
batch.push(map_id, Diff::Text(vec![])).unwrap();
let err = doc.apply_diff(batch).unwrap_err();
assert!(matches!(err, loro::LoroError::DecodeError(..)));
}
#[test]
#[parallel]
fn import_json_updates_with_short_peers_array_no_longer_panics() {
let doc = LoroDoc::new();
let schema = JsonSchema {
schema_version: 1,
start_version: Frontiers::default(),
peers: Some(vec![1u64]),
changes: vec![JsonChange {
id: ID::new(5u64, 0),
timestamp: 0,
deps: vec![],
lamport: 0,
msg: None,
ops: vec![JsonOp {
content: JsonOpContent::Map(MapOp::Insert {
key: "x".into(),
value: LoroValue::Null,
}),
container: ContainerID::new_root("map", ContainerType::Map),
counter: 0,
}],
}],
};
let _ = doc.import_json_updates(schema);
}
use loro::LoroTree;
#[test]
#[parallel]
fn detached_tree_is_fractional_index_enabled_reports_enabled() {
let tree = LoroTree::new();
assert!(tree.is_fractional_index_enabled());
}
#[test]
#[parallel]
fn detached_tree_enable_disable_fractional_index_does_not_panic() {
let tree = LoroTree::new();
tree.enable_fractional_index(1);
tree.disable_fractional_index();
}