//! Wire fixture for the undo-kept GC fix (oficina/docs #145; tests/gc_undo_kept.rs). //! Writes the native update stream of the repro (delete a word, type, undo, //! type, a peer deletes the paragraph, then undo and redo) with the editor's //! snapshot and projection after each step, for native/scripts/check-gc-undo-kept-wire.mjs //! to replay in Yjs. With `--check`, reads the Yjs states that script wrote //! back and checks a fresh native editor shows the same as the editor did. use docs_native_crdt::Editor; use serde_json::{json, Value}; use std::path::Path; fn main() { let mut args = std::env::args().skip(1); let directory = args.next().expect("usage: gc_undo_kept_probe DIRECTORY [--check]"); let directory = Path::new(&directory); if args.next().as_deref() == Some("--check") { return check(directory); } std::fs::create_dir_all(directory).unwrap(); let mut author = Editor::new("gc-undo-kept", 11); author.command(&json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap(); author.command(&json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})).unwrap(); let base = author.snapshot(); let mut editor = Editor::new("gc-undo-kept", 12); editor.apply(&base).unwrap(); let mut peer = Editor::new("gc-undo-kept", 13); peer.apply(&base).unwrap(); let mut stream = vec![base]; let mut record = |stream: &Vec>, editor: &Editor| { let step = stream.len() - 1; std::fs::write(directory.join(format!("{step}.bin")), stream.last().unwrap()).unwrap(); std::fs::write(directory.join(format!("{step}.snapshot")), editor.snapshot()).unwrap(); std::fs::write(directory.join(format!("{step}.json")), editor.projection()["children"].to_string()).unwrap(); }; record(&stream, &editor); for command in [ json!({"op":"display_edit","at":4,"length":5,"text":""}), // delete "hello" json!({"op":"display_edit","at":6,"length":0,"text":"Q"}), json!({"op":"undo"}), json!({"op":"display_edit","at":0,"length":0,"text":"x"}), // clears the redo stack ] { let update = editor.command(&command).unwrap(); peer.apply(&update).unwrap(); stream.push(update); record(&stream, &editor); } let remote = peer.command(&json!({"op":"display_edit","at":2,"length":12,"text":""})).unwrap(); editor.apply(&remote).unwrap(); stream.push(remote); record(&stream, &editor); for op in ["undo", "undo", "undo", "redo", "redo", "redo"] { let update = editor.command(&json!({"op":op})).unwrap(); peer.apply(&update).unwrap(); stream.push(update); record(&stream, &editor); } assert_eq!(editor.projection()["children"], peer.projection()["children"]); std::fs::write(directory.join("steps"), (stream.len() - 1).to_string()).unwrap(); } fn check(directory: &Path) { let steps: usize = std::fs::read_to_string(directory.join("steps")).unwrap().parse().unwrap(); let mut checked = 0; for gc in [true, false] { for step in 0..=steps { let state = std::fs::read(directory.join(format!("browser-{gc}-{step}.bin"))).unwrap(); let fresh = Editor::new("gc-undo-kept", 99); fresh.apply(&state).unwrap(); assert!(fresh.is_whole(), "gc={gc} step={step}: the browser's state does not integrate whole"); let expected: Value = serde_json::from_slice(&std::fs::read(directory.join(format!("{step}.json"))).unwrap()).unwrap(); assert_eq!(fresh.projection()["children"], expected, "gc={gc} step={step}"); checked += 1; } } println!("GC undo-kept wire: {checked} browser states read natively as the editor showed them."); }