//! Real damaged snapshots (oficina/docs #147, finding 1). Claude Fable's review //! wrote them with the undo-kept GC fix reverted, through the WASM sandbox: a //! fuzz of editors with undo, stopped at the first snapshot a fresh sandbox //! could not integrate whole. Each `damaged--.bin` is that //! snapshot and `good--.bin` the server's state at the same step. //! //! The recovery DocsModel runs on such a replica: catch up from the start, //! then rebuild from its own snapshot. Applying the server's state alone is //! not enough: yrs checks a parked struct's dependencies before noticing it //! is already integrated, so the damaged duplicate stays parked (21 of 29). //! The rebuild drops it, since a snapshot merges parked structs with //! integrated ones and the duplicate goes. use docs_native_crdt::Editor; use std::path::Path; fn open(bytes: &[u8], client: u64) -> Editor { let e = Editor::new("keep", client); e.apply(bytes).unwrap(); e } fn pairs() -> Vec<(String, Vec, Vec)> { let directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/damaged-snapshots"); let mut names: Vec = std::fs::read_dir(&directory).unwrap() .map(|e| e.unwrap().file_name().into_string().unwrap()) .filter(|n| n.starts_with("damaged-")).collect(); names.sort(); names.into_iter().map(|name| { let damaged = std::fs::read(directory.join(&name)).unwrap(); let good = std::fs::read(directory.join(name.replace("damaged", "good"))).unwrap(); (name, damaged, good) }).collect() } #[test] fn every_damaged_snapshot_heals_by_catching_up_from_the_start_and_rebuilding() { let pairs = pairs(); assert_eq!(pairs.len(), 29); let mut parked_after_catch_up = 0; for (name, damaged, good) in pairs { let server = open(&good, 97); let replica = open(&damaged, 99); assert!(!replica.is_whole() && !replica.parked().is_empty(), "{name}: reads whole"); // Catching up from the start: the server's whole state. replica.apply(&good).unwrap(); assert_eq!(replica.projection()["children"], server.projection()["children"], "{name}: shows other than the server"); if !replica.is_whole() { parked_after_catch_up += 1; } // Rebuilt from its own snapshot, it parks only what the server does. let rebuilt = open(&replica.snapshot(), 98); assert_eq!(rebuilt.parked(), server.parked(), "{name}: still parks the damaged struct"); assert_eq!(rebuilt.projection()["children"], server.projection()["children"], "{name}"); // DocsReplicaCache merges the damaged stored state with it: the // record it stores reopens as the rebuilt replica does. let merger = open(&damaged, 96); merger.apply(&rebuilt.snapshot()).unwrap(); let stored = open(&merger.snapshot(), 95); assert_eq!(stored.parked(), server.parked(), "{name}: the stored record parks"); assert_eq!(stored.projection()["children"], server.projection()["children"], "{name}"); } // The case the rebuild exists for (21 of 29 when the review found it). assert!(parked_after_catch_up > 0); } /// What a replica parks is described the same way after a rebuild from its /// own snapshot, so accepted parking (the server's own) stays accepted. #[test] fn parked_content_reads_the_same_after_a_rebuild() { let mut writer = Editor::new("keep", 11); let paragraph = writer.command(&serde_json::json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap(); let typed = writer.command(&serde_json::json!({"op":"display_edit","at":0,"length":0,"text":"Parked"})).unwrap(); let parked = open(&typed, 12); assert!(!parked.is_whole()); assert_eq!(open(&parked.snapshot(), 13).parked(), parked.parked()); assert_eq!(open(&open(&parked.snapshot(), 14).snapshot(), 15).parked(), parked.parked()); parked.apply(¶graph).unwrap(); assert!(parked.is_whole()); assert_eq!(parked.parked(), ""); } /// An edit typed on a damaged replica (before it settles) carries only the /// edit: the server, holding its whole state, parks nothing from it. #[test] fn an_edit_on_a_damaged_replica_does_not_carry_what_it_parks() { for (name, damaged, good) in pairs() { let mut replica = open(&damaged, 99); let update = replica.command(&serde_json::json!({"op":"display_edit","at":0,"length":0,"text":"LOCAL"})).unwrap(); let server = open(&good, 97); let parked_before = server.parked(); server.apply(&update).unwrap(); assert_eq!(server.parked(), parked_before, "{name}: the server parks what the edit carried"); } }