| 1 | //! Finding 2(c) of the native realtime audit: after the first projection the |
| 2 | //! authority reports only the top-level blocks that changed. Replaying the |
| 3 | //! deltas onto the first projection must give exactly the whole projection |
| 4 | //! after any mix of local commands, peers' updates, undo and redo. |
| 5 | mod support; |
| 6 | use docs_native_crdt::Editor; |
| 7 | use serde_json::{json, Value}; |
| 8 | use support::{generate, legacy_projection, oracle, Random}; |
| 9 | |
| 10 | fn open(bytes: &[u8], client: u64) -> Editor { |
| 11 | let editor = Editor::new("large-document", client); |
| 12 | editor.apply(bytes).unwrap(); |
| 13 | editor |
| 14 | } |
| 15 | fn replay(children: &mut Vec<Value>, delta: &Value) -> usize { |
| 16 | assert_eq!(delta["base"].as_u64().unwrap() as usize, children.len()); |
| 17 | let mut next = Vec::new(); let mut at = 0; let mut projected = 0; |
| 18 | for op in delta["ops"].as_array().unwrap() { |
| 19 | if let Some(n) = op["keep"].as_u64() { next.extend_from_slice(&children[at..at + n as usize]); at += n as usize; } |
| 20 | else if let Some(n) = op["remove"].as_u64() { at += n as usize; } |
| 21 | else { let nodes = op["insert"].as_array().unwrap(); projected += nodes.len(); next.extend(nodes.iter().cloned()); } |
| 22 | } |
| 23 | assert_eq!(at, children.len(), "every previous block is kept or removed"); |
| 24 | assert_eq!(next.len(), delta["count"].as_u64().unwrap() as usize); |
| 25 | *children = next; |
| 26 | projected |
| 27 | } |
| 28 | /// The current projection without touching the editor's delta base. |
| 29 | fn truth(editor: &Editor) -> Value { legacy_projection(&editor.snapshot())["children"].clone() } |
| 30 | |
| 31 | #[test] |
| 32 | fn typing_reprojects_only_the_block_typed_in() { |
| 33 | let mut editor = open(&generate(20, 300, 21), 501); |
| 34 | let mut children = editor.projection()["children"].as_array().unwrap().clone(); |
| 35 | let (ranges, _) = oracle(&json!({"children": children})); |
| 36 | let at = ranges[ranges.len() / 2].start + 3; |
| 37 | for _ in 0..5 { |
| 38 | editor.command(&json!({"op":"display_edit","at":at,"length":0,"text":"k"})).unwrap(); |
| 39 | let delta = editor.projection_delta(); |
| 40 | assert_eq!(replay(&mut children, &delta), 1, "one block re-projected: {}", delta["ops"]); |
| 41 | assert_eq!(delta["ops"].as_array().unwrap().len(), 4, "keep, insert, remove, keep: {}", delta["ops"]); |
| 42 | } |
| 43 | assert_eq!(Value::Array(children), truth(&editor)); |
| 44 | let unchanged = editor.projection_delta(); |
| 45 | assert_eq!(unchanged["ops"], json!([{"keep": unchanged["count"]}])); |
| 46 | } |
| 47 | |
| 48 | /// Seeds: a bounded run in the gate; DOCS_DELTA_SEEDS=1..100 locally. |
| 49 | fn seeds() -> std::ops::RangeInclusive<u64> { |
| 50 | match std::env::var("DOCS_DELTA_SEEDS") { |
| 51 | Ok(v) => match v.split_once("..") { Some((a, b)) => a.parse().unwrap()..=b.parse().unwrap(), None => { let s = v.parse().unwrap(); s..=s } }, |
| 52 | Err(_) => 1..=4, |
| 53 | } |
| 54 | } |
| 55 | #[test] |
| 56 | fn deltas_replay_to_the_whole_projection_through_every_kind_of_change() { |
| 57 | for seed in seeds() { deltas_replay(seed); } |
| 58 | } |
| 59 | fn deltas_replay(seed: u64) { |
| 60 | let base = generate(6, 400, 20 + seed); |
| 61 | let mut authority = open(&base, 601); |
| 62 | let peers: Vec<Editor> = (0..3).map(|i| open(&base, 610 + i)).collect(); |
| 63 | let mut peers = peers; |
| 64 | let mut children = authority.projection()["children"].as_array().unwrap().clone(); |
| 65 | let mut random = Random::new(seed * 7 + 2); |
| 66 | let mut applied = std::collections::HashMap::<String, usize>::new(); |
| 67 | for step in 0..160 { |
| 68 | let p = json!({"children": children.clone()}); |
| 69 | let (ranges, total) = oracle(&p); |
| 70 | let texts: Vec<_> = ranges.iter().filter(|r| !r.atomic).collect(); |
| 71 | let span = texts[random.below(texts.len() as u64) as usize]; |
| 72 | let at = span.start + random.below(span.length as u64 + 1) as u32; |
| 73 | let command = match step % 11 { |
| 74 | 0 => json!({"op":"display_edit","at":at,"length":0,"text":"\nnew block"}), |
| 75 | 1 => json!({"op":"display_edit","at":at.min(total.saturating_sub(200)),"length":150,"text":"J"}), |
| 76 | 2 => json!({"op":"display_paste","at":at,"length":0,"blocks":[ |
| 77 | {"type":"paragraph","children":[{"type":"text","runs":[{"insert":"pasted","marks":{"em":{}}}]}]}, |
| 78 | {"type":"horizontal_rule"}, |
| 79 | {"type":"bullet_list","children":[{"type":"list_item","children":[{"type":"paragraph","children":[{"type":"text","runs":[{"insert":"item","marks":{}}]}]}]}]}]}), |
| 80 | 3 => json!({"op":"undo"}), |
| 81 | 4 => json!({"op":"redo"}), |
| 82 | 5 => json!({"op":"block","node":children[random.below(children.len() as u64) as usize]["id"],"type":"heading","level":2}), |
| 83 | 6 => json!({"op":"attribute","node":children[random.below(children.len() as u64) as usize]["id"],"name":"align","value":"center"}), |
| 84 | _ => json!({"op":"display_edit","at":at,"length":if step % 3 == 0 { span.length.min(2) } else { 0 },"text":"typed"}), |
| 85 | }; |
| 86 | let local = authority.command(&command); |
| 87 | if local.is_ok() { *applied.entry(command["op"].as_str().unwrap().to_owned()).or_default() += 1; } |
| 88 | // Peers edit concurrently; their updates arrive in between. |
| 89 | let peer = &mut peers[step % 3]; |
| 90 | let peer_p = peer.projection(); |
| 91 | let (peer_ranges, _) = oracle(&peer_p); |
| 92 | let peer_texts: Vec<_> = peer_ranges.iter().filter(|r| !r.atomic && r.length > 0).collect(); |
| 93 | let peer_span = peer_texts[random.below(peer_texts.len() as u64) as usize]; |
| 94 | // A position inside an emoji is refused; the peer then edits at the |
| 95 | // text's start, which is always a boundary. |
| 96 | let remote = peer.command(&json!({"op":"display_edit","at":peer_span.start + 1,"length":if step % 4 == 0 { 1 } else { 0 },"text":"p"})) |
| 97 | .or_else(|_| peer.command(&json!({"op":"display_edit","at":peer_span.start,"length":0,"text":"p"}))).unwrap(); |
| 98 | authority.apply(&remote).unwrap(); |
| 99 | if let Ok(update) = local { for peer in &peers { peer.apply(&update).unwrap(); } } |
| 100 | for other in peers.iter().enumerate().filter(|(i, _)| *i != step % 3).map(|(_, p)| p) { other.apply(&remote).unwrap(); } |
| 101 | if step % 5 != 4 { |
| 102 | replay(&mut children, &authority.projection_delta()); |
| 103 | assert_eq!(Value::Array(children.clone()), truth(&authority), "seed {seed} step {step}: {command}"); |
| 104 | } |
| 105 | } |
| 106 | for op in ["display_edit", "display_paste", "undo", "redo", "block", "attribute"] { assert!(applied.get(op).copied().unwrap_or(0) >= 3, "seed {seed}: {op} ran: {applied:?}"); } |
| 107 | let delta = authority.projection_delta(); |
| 108 | assert_eq!(delta["canUndo"], authority.projection()["canUndo"]); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn a_full_projection_resets_the_delta_base() { |
| 113 | let mut editor = open(&generate(2, 0, 23), 701); |
| 114 | let _ = editor.projection(); |
| 115 | editor.command(&json!({"op":"display_edit","at":5,"length":0,"text":"a"})).unwrap(); |
| 116 | let mut children = editor.projection()["children"].as_array().unwrap().clone(); |
| 117 | let delta = editor.projection_delta(); |
| 118 | assert_eq!(delta["ops"], json!([{"keep": children.len()}])); |
| 119 | replay(&mut children, &delta); |
| 120 | } |
| 121 | |