| 1 | //! The undo-kept GC repro (oficina/docs #145; native/crdt/tests/gc_undo_kept.rs) |
| 2 | //! through the shipped WASM module and the bounded host, as CRDTReplica runs |
| 3 | //! it: before the fix the module read freed memory here, so a run could pass by |
| 4 | //! luck, trap, or write a snapshot a fresh sandbox cannot integrate. |
| 5 | use docs_native_host::Host; |
| 6 | use serde_json::{json, Value}; |
| 7 | |
| 8 | fn open(client: u64, state: &[u8]) -> Host { |
| 9 | let mut host = Host::new().unwrap(); |
| 10 | host.call(&serde_json::to_vec(&json!({"op":"open","guid":"keep","client":client})).unwrap()).unwrap(); |
| 11 | if !state.is_empty() { host.call(&[&[255u8][..], state].concat()).unwrap(); } |
| 12 | host |
| 13 | } |
| 14 | fn call(host: &mut Host, command: Value) -> Vec<u8> { host.call(&serde_json::to_vec(&command).unwrap()).unwrap().to_vec() } |
| 15 | fn project(host: &mut Host) -> Value { serde_json::from_slice::<Value>(&call(host, json!({"op":"project"}))).unwrap()["children"].clone() } |
| 16 | fn whole(host: &mut Host) -> bool { serde_json::from_slice(&call(host, json!({"op":"whole"}))).unwrap() } |
| 17 | |
| 18 | #[test] |
| 19 | fn a_kept_word_under_a_paragraph_a_peer_deletes_snapshots_whole_in_the_sandbox() { |
| 20 | let mut author = open(11, &[]); |
| 21 | call(&mut author, json!({"op":"insert_element","at":0,"type":"paragraph"})); |
| 22 | call(&mut author, json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})); |
| 23 | let base = call(&mut author, json!({"op":"snapshot"})); |
| 24 | let mut editor = open(12, &base); |
| 25 | let mut peer = open(13, &base); |
| 26 | for command in [ |
| 27 | json!({"op":"display_edit","at":4,"length":5,"text":""}), |
| 28 | json!({"op":"display_edit","at":6,"length":0,"text":"Q"}), |
| 29 | json!({"op":"undo"}), |
| 30 | json!({"op":"display_edit","at":0,"length":0,"text":"x"}), |
| 31 | ] { |
| 32 | let update = call(&mut editor, command); |
| 33 | peer.call(&[&[255u8][..], &update].concat()).unwrap(); |
| 34 | } |
| 35 | let remote = call(&mut peer, json!({"op":"display_edit","at":2,"length":12,"text":""})); |
| 36 | editor.call(&[&[255u8][..], &remote].concat()).unwrap(); |
| 37 | let snapshot = call(&mut editor, json!({"op":"snapshot"})); |
| 38 | let mut fresh = open(99, &snapshot); |
| 39 | assert!(whole(&mut fresh), "a fresh sandbox parks part of the snapshot"); |
| 40 | assert_eq!(project(&mut fresh), project(&mut editor)); |
| 41 | assert_eq!(project(&mut editor), project(&mut peer)); |
| 42 | } |
| 43 | |