oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1//! Wire fixture for the undo-kept GC fix (oficina/docs #145; tests/gc_undo_kept.rs).
2//! Writes the native update stream of the repro (delete a word, type, undo,
3//! type, a peer deletes the paragraph, then undo and redo) with the editor's
4//! snapshot and projection after each step, for native/scripts/check-gc-undo-kept-wire.mjs
5//! to replay in Yjs. With `--check`, reads the Yjs states that script wrote
6//! back and checks a fresh native editor shows the same as the editor did.
7use docs_native_crdt::Editor;
8use serde_json::{json, Value};
9use std::path::Path;
10
11fn main() {
12 let mut args = std::env::args().skip(1);
13 let directory = args.next().expect("usage: gc_undo_kept_probe DIRECTORY [--check]");
14 let directory = Path::new(&directory);
15 if args.next().as_deref() == Some("--check") { return check(directory); }
16 std::fs::create_dir_all(directory).unwrap();
17 let mut author = Editor::new("gc-undo-kept", 11);
18 author.command(&json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap();
19 author.command(&json!({"op":"display_edit","at":0,"length":0,"text":"aaa\nhello world\nzzz"})).unwrap();
20 let base = author.snapshot();
21 let mut editor = Editor::new("gc-undo-kept", 12);
22 editor.apply(&base).unwrap();
23 let mut peer = Editor::new("gc-undo-kept", 13);
24 peer.apply(&base).unwrap();
25 let mut stream = vec![base];
26 let mut record = |stream: &Vec<Vec<u8>>, editor: &Editor| {
27 let step = stream.len() - 1;
28 std::fs::write(directory.join(format!("{step}.bin")), stream.last().unwrap()).unwrap();
29 std::fs::write(directory.join(format!("{step}.snapshot")), editor.snapshot()).unwrap();
30 std::fs::write(directory.join(format!("{step}.json")), editor.projection()["children"].to_string()).unwrap();
31 };
32 record(&stream, &editor);
33 for command in [
34 json!({"op":"display_edit","at":4,"length":5,"text":""}), // delete "hello"
35 json!({"op":"display_edit","at":6,"length":0,"text":"Q"}),
36 json!({"op":"undo"}),
37 json!({"op":"display_edit","at":0,"length":0,"text":"x"}), // clears the redo stack
38 ] {
39 let update = editor.command(&command).unwrap();
40 peer.apply(&update).unwrap();
41 stream.push(update);
42 record(&stream, &editor);
43 }
44 let remote = peer.command(&json!({"op":"display_edit","at":2,"length":12,"text":""})).unwrap();
45 editor.apply(&remote).unwrap();
46 stream.push(remote);
47 record(&stream, &editor);
48 for op in ["undo", "undo", "undo", "redo", "redo", "redo"] {
49 let update = editor.command(&json!({"op":op})).unwrap();
50 peer.apply(&update).unwrap();
51 stream.push(update);
52 record(&stream, &editor);
53 }
54 assert_eq!(editor.projection()["children"], peer.projection()["children"]);
55 std::fs::write(directory.join("steps"), (stream.len() - 1).to_string()).unwrap();
56}
57
58fn check(directory: &Path) {
59 let steps: usize = std::fs::read_to_string(directory.join("steps")).unwrap().parse().unwrap();
60 let mut checked = 0;
61 for gc in [true, false] {
62 for step in 0..=steps {
63 let state = std::fs::read(directory.join(format!("browser-{gc}-{step}.bin"))).unwrap();
64 let fresh = Editor::new("gc-undo-kept", 99);
65 fresh.apply(&state).unwrap();
66 assert!(fresh.is_whole(), "gc={gc} step={step}: the browser's state does not integrate whole");
67 let expected: Value = serde_json::from_slice(&std::fs::read(directory.join(format!("{step}.json"))).unwrap()).unwrap();
68 assert_eq!(fresh.projection()["children"], expected, "gc={gc} step={step}");
69 checked += 1;
70 }
71 }
72 println!("GC undo-kept wire: {checked} browser states read natively as the editor showed them.");
73}
74