| 1 | //! Finding 7 of the native realtime audit: a command's update carries only |
| 2 | //! what its own transactions inserted and deleted (as Yjs's doc.on('update') |
| 3 | //! does), never the document's whole delete set. |
| 4 | mod support; |
| 5 | use docs_native_crdt::Editor; |
| 6 | use serde_json::{json, Value}; |
| 7 | use support::{generate, oracle, plain}; |
| 8 | use yrs::updates::decoder::Decode; |
| 9 | use yrs::updates::encoder::Encode; |
| 10 | use yrs::Update; |
| 11 | |
| 12 | fn open(bytes: &[u8], client: u64) -> Editor { |
| 13 | let editor = Editor::new("large-document", client); |
| 14 | editor.apply(bytes).unwrap(); |
| 15 | editor |
| 16 | } |
| 17 | fn deleted_units(update: &[u8]) -> u32 { |
| 18 | let update = Update::decode_v1(update).unwrap(); |
| 19 | update.delete_set().iter().map(|(_, ranges)| ranges.iter().map(|r| r.end - r.start).sum::<u32>()).sum() |
| 20 | } |
| 21 | fn middle_of_text(projection: &Value) -> u32 { |
| 22 | let (ranges, _) = oracle(projection); |
| 23 | let text = ranges.iter().filter(|r| !r.atomic && r.length > 8).nth(ranges.len() / 3).unwrap(); |
| 24 | text.start + text.length / 2 |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn keystroke_update_is_independent_of_the_documents_history() { |
| 29 | let edited = generate(10, 600, 1); |
| 30 | let mut editor = open(&edited, 41); |
| 31 | let at = middle_of_text(&editor.projection()); |
| 32 | // What the engine used to send: a state-vector diff carries every deletion. |
| 33 | 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 }; |
| 34 | let update = editor.command(&json!({"op":"display_edit","at":at,"length":0,"text":"x"})).unwrap(); |
| 35 | assert!(update.len() <= 32, "one character, {} bytes", update.len()); |
| 36 | assert_eq!(deleted_units(&update), 0); |
| 37 | 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 }; |
| 38 | assert!(legacy.len() > 10 * update.len(), "the history really is large: {} vs {}", legacy.len(), update.len()); |
| 39 | |
| 40 | let deletion = editor.command(&json!({"op":"display_edit","at":at,"length":3,"text":""})).unwrap(); |
| 41 | assert_eq!(deleted_units(&deletion), 3, "only the three deleted units"); |
| 42 | assert!(deletion.len() <= 32, "a three-unit deletion, {} bytes", deletion.len()); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn every_command_kind_converges_through_its_own_update_alone() { |
| 47 | // Each step's update is applied, alone and in order, to a peer which |
| 48 | // started from the same document: with gc on and off it must match the |
| 49 | // authority exactly. This covers multi-transaction commands (join_range, |
| 50 | // paste, rules, batch normalization) and undo/redo. |
| 51 | let edited = generate(4, 240, 2); |
| 52 | let mut authority = open(&edited, 51); |
| 53 | let peer_gc = open(&edited, 52); |
| 54 | 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 }; |
| 55 | let mut client = 60; |
| 56 | let mut step = |authority: &mut Editor, command: Value, from_source: bool| { |
| 57 | let update = if from_source { |
| 58 | client += 1; |
| 59 | let mut source = open(&authority.snapshot(), client); |
| 60 | let update = source.command(&command).unwrap_or_else(|e| panic!("{command}: {e}")); |
| 61 | authority.apply_local(&update).unwrap(); |
| 62 | update |
| 63 | } else { authority.command(&command).unwrap_or_else(|e| panic!("{command}: {e}")) }; |
| 64 | peer_gc.apply(&update).unwrap(); |
| 65 | { use yrs::Transact; peer.transact_mut().apply_update(Update::decode_v1(&update).unwrap()).unwrap(); } |
| 66 | let expected = authority.projection()["children"].clone(); |
| 67 | assert_eq!(peer_gc.projection()["children"], expected, "{command}"); |
| 68 | let reread = open(&{ use yrs::{ReadTxn, StateVector, Transact}; let bytes = peer.transact().encode_state_as_update_v1(&StateVector::default()); bytes }, 99); |
| 69 | assert_eq!(reread.projection()["children"], expected, "{command} (no gc)"); |
| 70 | update |
| 71 | }; |
| 72 | let at = middle_of_text(&authority.projection()); |
| 73 | step(&mut authority, json!({"op":"display_edit","at":at,"length":0,"text":"typed"}), true); |
| 74 | step(&mut authority, json!({"op":"display_edit","at":at,"length":2,"text":"Q"}), true); |
| 75 | step(&mut authority, json!({"op":"display_edit","at":at,"length":0,"text":"one\ntwo\u{2028}three"}), true); |
| 76 | // A cross-paragraph replacement (join_range runs several transactions). |
| 77 | step(&mut authority, json!({"op":"display_edit","at":at - 20,"length":900,"text":"joined"}), true); |
| 78 | step(&mut authority, json!({"op":"display_paste","at":10,"length":4,"blocks":[ |
| 79 | {"type":"heading","attrs":{"level":"3"},"children":[{"type":"text","runs":[{"insert":"Pasted","marks":{"strong":{}}}]}]}, |
| 80 | {"type":"bullet_list","children":[{"type":"list_item","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"Item","marks":{}}]}]}]}]} |
| 81 | ]}), true); |
| 82 | let (_, length) = oracle(&authority.projection()); |
| 83 | step(&mut authority, json!({"op":"display_edit","at":length - 1,"length":0,"text":"\n-"}), true); |
| 84 | let p = authority.projection(); |
| 85 | let (_, length) = oracle(&p); |
| 86 | step(&mut authority, json!({"op":"display_autoformat","at":length - 2,"length":1,"kind":"bullet_list","level":0}), true); |
| 87 | let p = authority.projection(); |
| 88 | let (_, length) = oracle(&p); |
| 89 | step(&mut authority, json!({"op":"display_revert_rule","at":length - 1,"kind":"bullet_list","prior_type":"paragraph","prior_attrs":{},"text":"- "}), true); |
| 90 | step(&mut authority, json!({"op":"undo"}), false); |
| 91 | step(&mut authority, json!({"op":"undo"}), false); |
| 92 | step(&mut authority, json!({"op":"redo"}), false); |
| 93 | let p = authority.projection(); |
| 94 | let first = p["children"][1]["id"].clone(); |
| 95 | let second = p["children"][2]["id"].clone(); |
| 96 | step(&mut authority, json!({"op":"batch","commands":[ |
| 97 | {"op":"block","node":first,"type":"bullet_list"}, |
| 98 | {"op":"block","node":second,"type":"bullet_list"} |
| 99 | ]}), true); |
| 100 | step(&mut authority, json!({"op":"undo"}), false); |
| 101 | assert!(!plain(&authority.projection()).is_empty()); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn a_command_that_changes_nothing_sends_an_empty_update() { |
| 106 | let mut editor = open(&generate(2, 50, 3), 70); |
| 107 | let update = editor.command(&json!({"op":"display_edit","at":3,"length":0,"text":""})).unwrap(); |
| 108 | assert_eq!(update, Update::new().encode_v1()); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn applying_updates_records_nothing_for_the_next_command() { |
| 113 | // Audit review finding 2: only a command's own transactions are kept; |
| 114 | // a peer's update or a whole state applied in between is neither encoded |
| 115 | // again nor carried by the next command's update. |
| 116 | let base = generate(3, 100, 4); |
| 117 | let mut editor = Editor::new("large-document", 80); |
| 118 | editor.apply(&base).unwrap(); |
| 119 | let mut peer = open(&base, 81); |
| 120 | for _ in 0..20 { |
| 121 | let remote = peer.command(&json!({"op":"display_edit","at":7,"length":0,"text":"peer"})).unwrap(); |
| 122 | editor.apply(&remote).unwrap(); |
| 123 | } |
| 124 | let update = editor.command(&json!({"op":"display_edit","at":3,"length":0,"text":"x"})).unwrap(); |
| 125 | assert!(update.len() <= 32, "{} bytes", update.len()); |
| 126 | let undo = editor.command(&json!({"op":"undo"})).unwrap(); |
| 127 | assert!(undo.len() <= 32, "undo carries only its own change: {} bytes", undo.len()); |
| 128 | } |
| 129 | |