//! The undo-kept GC repro (oficina/docs #145; native/crdt/tests/gc_undo_kept.rs) //! through the shipped WASM module and the bounded host, as CRDTReplica runs //! it: before the fix the module read freed memory here, so a run could pass by //! luck, trap, or write a snapshot a fresh sandbox cannot integrate. use docs_native_host::Host; use serde_json::{json, Value}; fn open(client: u64, state: &[u8]) -> Host { let mut host = Host::new().unwrap(); host.call(&serde_json::to_vec(&json!({"op":"open","guid":"keep","client":client})).unwrap()).unwrap(); if !state.is_empty() { host.call(&[&[255u8][..], state].concat()).unwrap(); } host } fn call(host: &mut Host, command: Value) -> Vec { host.call(&serde_json::to_vec(&command).unwrap()).unwrap().to_vec() } fn project(host: &mut Host) -> Value { serde_json::from_slice::(&call(host, json!({"op":"project"}))).unwrap()["children"].clone() } fn whole(host: &mut Host) -> bool { serde_json::from_slice(&call(host, json!({"op":"whole"}))).unwrap() } #[test] fn a_kept_word_under_a_paragraph_a_peer_deletes_snapshots_whole_in_the_sandbox() { let mut author = open(11, &[]); call(&mut author, json!({"op":"insert_element","at":0,"type":"paragraph"})); call(&mut author, json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})); let base = call(&mut author, json!({"op":"snapshot"})); let mut editor = open(12, &base); let mut peer = open(13, &base); for command in [ json!({"op":"display_edit","at":4,"length":5,"text":""}), json!({"op":"display_edit","at":6,"length":0,"text":"Q"}), json!({"op":"undo"}), json!({"op":"display_edit","at":0,"length":0,"text":"x"}), ] { let update = call(&mut editor, command); peer.call(&[&[255u8][..], &update].concat()).unwrap(); } let remote = call(&mut peer, json!({"op":"display_edit","at":2,"length":12,"text":""})); editor.call(&[&[255u8][..], &remote].concat()).unwrap(); let snapshot = call(&mut editor, json!({"op":"snapshot"})); let mut fresh = open(99, &snapshot); assert!(whole(&mut fresh), "a fresh sandbox parks part of the snapshot"); assert_eq!(project(&mut fresh), project(&mut editor)); assert_eq!(project(&mut editor), project(&mut peer)); }