//! Finding 7 of the native realtime audit: a command's update carries only //! what its own transactions inserted and deleted (as Yjs's doc.on('update') //! does), never the document's whole delete set. mod support; use docs_native_crdt::Editor; use serde_json::{json, Value}; use support::{generate, oracle, plain}; use yrs::updates::decoder::Decode; use yrs::updates::encoder::Encode; use yrs::Update; fn open(bytes: &[u8], client: u64) -> Editor { let editor = Editor::new("large-document", client); editor.apply(bytes).unwrap(); editor } fn deleted_units(update: &[u8]) -> u32 { let update = Update::decode_v1(update).unwrap(); update.delete_set().iter().map(|(_, ranges)| ranges.iter().map(|r| r.end - r.start).sum::()).sum() } fn middle_of_text(projection: &Value) -> u32 { let (ranges, _) = oracle(projection); let text = ranges.iter().filter(|r| !r.atomic && r.length > 8).nth(ranges.len() / 3).unwrap(); text.start + text.length / 2 } #[test] fn keystroke_update_is_independent_of_the_documents_history() { let edited = generate(10, 600, 1); let mut editor = open(&edited, 41); let at = middle_of_text(&editor.projection()); // What the engine used to send: a state-vector diff carries every deletion. let before = { use yrs::{ReadTxn, Transact}; let doc = yrs::Doc::new(); doc.transact_mut().apply_update(Update::decode_v1(&edited).unwrap()).unwrap(); let sv = doc.transact().state_vector(); sv }; let update = editor.command(&json!({"op":"display_edit","at":at,"length":0,"text":"x"})).unwrap(); assert!(update.len() <= 32, "one character, {} bytes", update.len()); assert_eq!(deleted_units(&update), 0); let legacy = { use yrs::{ReadTxn, Transact}; let doc = yrs::Doc::new(); doc.transact_mut().apply_update(Update::decode_v1(&editor.snapshot()).unwrap()).unwrap(); let bytes = doc.transact().encode_state_as_update_v1(&before); bytes }; assert!(legacy.len() > 10 * update.len(), "the history really is large: {} vs {}", legacy.len(), update.len()); let deletion = editor.command(&json!({"op":"display_edit","at":at,"length":3,"text":""})).unwrap(); assert_eq!(deleted_units(&deletion), 3, "only the three deleted units"); assert!(deletion.len() <= 32, "a three-unit deletion, {} bytes", deletion.len()); } #[test] fn every_command_kind_converges_through_its_own_update_alone() { // Each step's update is applied, alone and in order, to a peer which // started from the same document: with gc on and off it must match the // authority exactly. This covers multi-transaction commands (join_range, // paste, rules, batch normalization) and undo/redo. let edited = generate(4, 240, 2); let mut authority = open(&edited, 51); let peer_gc = open(&edited, 52); let peer = { let mut options = yrs::Options::with_guid_and_client_id("large-document".into(), 53); options.skip_gc = true; options.offset_kind = yrs::OffsetKind::Utf16; let doc = yrs::Doc::with_options(options); use yrs::Transact; doc.transact_mut().apply_update(Update::decode_v1(&edited).unwrap()).unwrap(); doc }; let mut client = 60; let mut step = |authority: &mut Editor, command: Value, from_source: bool| { let update = if from_source { client += 1; let mut source = open(&authority.snapshot(), client); let update = source.command(&command).unwrap_or_else(|e| panic!("{command}: {e}")); authority.apply_local(&update).unwrap(); update } else { authority.command(&command).unwrap_or_else(|e| panic!("{command}: {e}")) }; peer_gc.apply(&update).unwrap(); { use yrs::Transact; peer.transact_mut().apply_update(Update::decode_v1(&update).unwrap()).unwrap(); } let expected = authority.projection()["children"].clone(); assert_eq!(peer_gc.projection()["children"], expected, "{command}"); let reread = open(&{ use yrs::{ReadTxn, StateVector, Transact}; let bytes = peer.transact().encode_state_as_update_v1(&StateVector::default()); bytes }, 99); assert_eq!(reread.projection()["children"], expected, "{command} (no gc)"); update }; let at = middle_of_text(&authority.projection()); step(&mut authority, json!({"op":"display_edit","at":at,"length":0,"text":"typed"}), true); step(&mut authority, json!({"op":"display_edit","at":at,"length":2,"text":"Q"}), true); step(&mut authority, json!({"op":"display_edit","at":at,"length":0,"text":"one\ntwo\u{2028}three"}), true); // A cross-paragraph replacement (join_range runs several transactions). step(&mut authority, json!({"op":"display_edit","at":at - 20,"length":900,"text":"joined"}), true); step(&mut authority, json!({"op":"display_paste","at":10,"length":4,"blocks":[ {"type":"heading","attrs":{"level":"3"},"children":[{"type":"text","runs":[{"insert":"Pasted","marks":{"strong":{}}}]}]}, {"type":"bullet_list","children":[{"type":"list_item","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"Item","marks":{}}]}]}]}]} ]}), true); let (_, length) = oracle(&authority.projection()); step(&mut authority, json!({"op":"display_edit","at":length - 1,"length":0,"text":"\n-"}), true); let p = authority.projection(); let (_, length) = oracle(&p); step(&mut authority, json!({"op":"display_autoformat","at":length - 2,"length":1,"kind":"bullet_list","level":0}), true); let p = authority.projection(); let (_, length) = oracle(&p); step(&mut authority, json!({"op":"display_revert_rule","at":length - 1,"kind":"bullet_list","prior_type":"paragraph","prior_attrs":{},"text":"- "}), true); step(&mut authority, json!({"op":"undo"}), false); step(&mut authority, json!({"op":"undo"}), false); step(&mut authority, json!({"op":"redo"}), false); let p = authority.projection(); let first = p["children"][1]["id"].clone(); let second = p["children"][2]["id"].clone(); step(&mut authority, json!({"op":"batch","commands":[ {"op":"block","node":first,"type":"bullet_list"}, {"op":"block","node":second,"type":"bullet_list"} ]}), true); step(&mut authority, json!({"op":"undo"}), false); assert!(!plain(&authority.projection()).is_empty()); } #[test] fn a_command_that_changes_nothing_sends_an_empty_update() { let mut editor = open(&generate(2, 50, 3), 70); let update = editor.command(&json!({"op":"display_edit","at":3,"length":0,"text":""})).unwrap(); assert_eq!(update, Update::new().encode_v1()); } #[test] fn applying_updates_records_nothing_for_the_next_command() { // Audit review finding 2: only a command's own transactions are kept; // a peer's update or a whole state applied in between is neither encoded // again nor carried by the next command's update. let base = generate(3, 100, 4); let mut editor = Editor::new("large-document", 80); editor.apply(&base).unwrap(); let mut peer = open(&base, 81); for _ in 0..20 { let remote = peer.command(&json!({"op":"display_edit","at":7,"length":0,"text":"peer"})).unwrap(); editor.apply(&remote).unwrap(); } let update = editor.command(&json!({"op":"display_edit","at":3,"length":0,"text":"x"})).unwrap(); assert!(update.len() <= 32, "{} bytes", update.len()); let undo = editor.command(&json!({"op":"undo"})).unwrap(); assert!(undo.len() <= 32, "undo carries only its own change: {} bytes", undo.len()); }