93 lines
2.9 KiB
Rust
93 lines
2.9 KiB
Rust
/*
|
|
* Copyright (C) 2026 Mateusz Gruszczyński @linuxiarz.pl
|
|
* Source-Available Code / Dual-Licensed.
|
|
*
|
|
* Free for non-commercial and evaluation use under terms of BSL/GPLv3.
|
|
* Commercial or production use requires a valid paid license.
|
|
* See LICENSE file in repository root for details.
|
|
*/
|
|
|
|
use super::*;
|
|
|
|
fn operation(components: Vec<OperationComponent>) -> TextOperation {
|
|
TextOperation { components }
|
|
}
|
|
|
|
#[test]
|
|
fn concurrent_insertions_have_stable_order() {
|
|
let left = operation(vec![
|
|
OperationComponent::Retain { count: 1 },
|
|
OperationComponent::Insert {
|
|
text: "X".into(),
|
|
owners: Vec::new(),
|
|
},
|
|
OperationComponent::Retain { count: 1 },
|
|
]);
|
|
let right = operation(vec![
|
|
OperationComponent::Retain { count: 1 },
|
|
OperationComponent::Insert {
|
|
text: "Y".into(),
|
|
owners: Vec::new(),
|
|
},
|
|
OperationComponent::Retain { count: 1 },
|
|
]);
|
|
let left_prime = transform_operation(&left, &right, true).unwrap();
|
|
let right_prime = transform_operation(&right, &left, false).unwrap();
|
|
let after_right = apply_operation_to_document("aYb", "[]", &left_prime, &[])
|
|
.unwrap()
|
|
.0;
|
|
let after_left = apply_operation_to_document("aXb", "[]", &right_prime, &[])
|
|
.unwrap()
|
|
.0;
|
|
assert_eq!(after_right, "aXYb");
|
|
assert_eq!(after_left, "aXYb");
|
|
}
|
|
|
|
#[test]
|
|
fn utf16_offsets_support_emoji() {
|
|
let operation = operation(vec![
|
|
OperationComponent::Retain { count: 3 },
|
|
OperationComponent::Insert {
|
|
text: "x".into(),
|
|
owners: Vec::new(),
|
|
},
|
|
OperationComponent::Retain { count: 1 },
|
|
]);
|
|
let result = apply_operation_to_document("A😀B", "[]", &operation, &[])
|
|
.unwrap()
|
|
.0;
|
|
assert_eq!(result, "A😀xB");
|
|
}
|
|
|
|
#[test]
|
|
fn operation_from_edit_preserves_utf16_boundaries() {
|
|
let operation = operation_from_edit("A😀B", "A😀xB", "[]");
|
|
let result = apply_operation_to_document("A😀B", "[]", &operation, &[])
|
|
.unwrap()
|
|
.0;
|
|
assert_eq!(result, "A😀xB");
|
|
}
|
|
|
|
#[test]
|
|
fn acknowledgements_survive_history_compaction() {
|
|
let mut document = CollaborativeDocument::new(String::new(), "[]".into(), 0);
|
|
for update_id in 1..=MAX_OPERATION_HISTORY as u64 + 8 {
|
|
let base_revision_id = document.revision_id;
|
|
let revision_id = base_revision_id + 1;
|
|
document.revision_id = revision_id;
|
|
document.record(AppliedOperation {
|
|
base_revision_id,
|
|
revision_id,
|
|
client_id: "client-123".into(),
|
|
update_id,
|
|
operation: TextOperation::default(),
|
|
owner_replacements: Vec::new(),
|
|
});
|
|
}
|
|
assert!(document.has_applied_update("client-123", 1));
|
|
assert_eq!(
|
|
document.acknowledged_updates("client-123"),
|
|
vec![MAX_OPERATION_HISTORY as u64 + 8]
|
|
);
|
|
}
|