oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1//! Real damaged snapshots (oficina/docs #147, finding 1). Claude Fable's review
2//! wrote them with the undo-kept GC fix reverted, through the WASM sandbox: a
3//! fuzz of editors with undo, stopped at the first snapshot a fresh sandbox
4//! could not integrate whole. Each `damaged-<seed>-<step>.bin` is that
5//! snapshot and `good-<seed>-<step>.bin` the server's state at the same step.
6//!
7//! The recovery DocsModel runs on such a replica: catch up from the start,
8//! then rebuild from its own snapshot. Applying the server's state alone is
9//! not enough: yrs checks a parked struct's dependencies before noticing it
10//! is already integrated, so the damaged duplicate stays parked (21 of 29).
11//! The rebuild drops it, since a snapshot merges parked structs with
12//! integrated ones and the duplicate goes.
13use docs_native_crdt::Editor;
14use std::path::Path;
15
16fn open(bytes: &[u8], client: u64) -> Editor { let e = Editor::new("keep", client); e.apply(bytes).unwrap(); e }
17
18fn pairs() -> Vec<(String, Vec<u8>, Vec<u8>)> {
19 let directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/damaged-snapshots");
20 let mut names: Vec<String> = std::fs::read_dir(&directory).unwrap()
21 .map(|e| e.unwrap().file_name().into_string().unwrap())
22 .filter(|n| n.starts_with("damaged-")).collect();
23 names.sort();
24 names.into_iter().map(|name| {
25 let damaged = std::fs::read(directory.join(&name)).unwrap();
26 let good = std::fs::read(directory.join(name.replace("damaged", "good"))).unwrap();
27 (name, damaged, good)
28 }).collect()
29}
30
31#[test]
32fn every_damaged_snapshot_heals_by_catching_up_from_the_start_and_rebuilding() {
33 let pairs = pairs();
34 assert_eq!(pairs.len(), 29);
35 let mut parked_after_catch_up = 0;
36 for (name, damaged, good) in pairs {
37 let server = open(&good, 97);
38 let replica = open(&damaged, 99);
39 assert!(!replica.is_whole() && !replica.parked().is_empty(), "{name}: reads whole");
40 // Catching up from the start: the server's whole state.
41 replica.apply(&good).unwrap();
42 assert_eq!(replica.projection()["children"], server.projection()["children"], "{name}: shows other than the server");
43 if !replica.is_whole() { parked_after_catch_up += 1; }
44 // Rebuilt from its own snapshot, it parks only what the server does.
45 let rebuilt = open(&replica.snapshot(), 98);
46 assert_eq!(rebuilt.parked(), server.parked(), "{name}: still parks the damaged struct");
47 assert_eq!(rebuilt.projection()["children"], server.projection()["children"], "{name}");
48 // DocsReplicaCache merges the damaged stored state with it: the
49 // record it stores reopens as the rebuilt replica does.
50 let merger = open(&damaged, 96);
51 merger.apply(&rebuilt.snapshot()).unwrap();
52 let stored = open(&merger.snapshot(), 95);
53 assert_eq!(stored.parked(), server.parked(), "{name}: the stored record parks");
54 assert_eq!(stored.projection()["children"], server.projection()["children"], "{name}");
55 }
56 // The case the rebuild exists for (21 of 29 when the review found it).
57 assert!(parked_after_catch_up > 0);
58}
59
60/// What a replica parks is described the same way after a rebuild from its
61/// own snapshot, so accepted parking (the server's own) stays accepted.
62#[test]
63fn parked_content_reads_the_same_after_a_rebuild() {
64 let mut writer = Editor::new("keep", 11);
65 let paragraph = writer.command(&serde_json::json!({"op":"insert_element","at":0,"type":"paragraph"})).unwrap();
66 let typed = writer.command(&serde_json::json!({"op":"display_edit","at":0,"length":0,"text":"Parked"})).unwrap();
67 let parked = open(&typed, 12);
68 assert!(!parked.is_whole());
69 assert_eq!(open(&parked.snapshot(), 13).parked(), parked.parked());
70 assert_eq!(open(&open(&parked.snapshot(), 14).snapshot(), 15).parked(), parked.parked());
71 parked.apply(&paragraph).unwrap();
72 assert!(parked.is_whole());
73 assert_eq!(parked.parked(), "");
74}
75
76/// An edit typed on a damaged replica (before it settles) carries only the
77/// edit: the server, holding its whole state, parks nothing from it.
78#[test]
79fn an_edit_on_a_damaged_replica_does_not_carry_what_it_parks() {
80 for (name, damaged, good) in pairs() {
81 let mut replica = open(&damaged, 99);
82 let update = replica.command(&serde_json::json!({"op":"display_edit","at":0,"length":0,"text":"LOCAL"})).unwrap();
83 let server = open(&good, 97);
84 let parked_before = server.parked();
85 server.apply(&update).unwrap();
86 assert_eq!(server.parked(), parked_before, "{name}: the server parks what the edit carried");
87 }
88}
89