oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1use docs_native_host::Host;
2use serde_json::json;
3#[test]
4fn interpreter_executes_native_crdt_without_host_imports() {
5 let mut host = Host::new().unwrap();
6 host.call(br#"{"op":"open","guid":"fixture","client":123}"#).unwrap();
7 host.call(br#"{"op":"insert_element","at":0,"type":"paragraph"}"#).unwrap();
8 let p: serde_json::Value = serde_json::from_slice(host.call(br#"{"op":"project"}"#).unwrap()).unwrap();
9 assert_eq!(p["children"][0]["type"], "paragraph");
10 let node = p["children"][0]["id"].as_str().unwrap();
11 host.call(&serde_json::to_vec(&json!({"op":"insert_text","node":node,"at":0,"text":"Sandbox πŸ˜€"})).unwrap()).unwrap();
12 let p: serde_json::Value = serde_json::from_slice(host.call(br#"{"op":"project"}"#).unwrap()).unwrap();
13 assert_eq!(p["children"][0]["children"][0]["runs"][0]["insert"], "Sandbox πŸ˜€");
14}
15#[test]
16fn failed_replica_cannot_be_reused() {
17 let mut host = Host::new().unwrap();
18 host.call(br#"{"op":"open","guid":"fixture","client":456}"#).unwrap();
19 assert!(host.call(br#"{"op":"apply","bytes":[255,255,255]}"#).is_err());
20 assert!(host.call(br#"{"op":"project"}"#).is_err());
21 let mut clean = Host::new().unwrap();
22 assert!(clean.call(br#"{"op":"open","guid":"fixture","client":789}"#).is_ok());
23}
24
25#[test]
26fn selection_anchor_call_does_not_poison_source_interpreter() {
27 let mut host = Host::new().unwrap();
28 host.call(br#"{"op":"open","guid":"anchor","client":4000000000}"#).unwrap();
29 host.call(br#"{"op":"insert_element","at":0,"type":"paragraph"}"#).unwrap();
30 host.call(br#"{"op":"display_edit","at":0,"length":0,"text":"N"}"#).unwrap();
31 let anchored = host.call(br#"{"op":"anchor","at":1}"#);
32 assert!(anchored.is_ok(), "anchor must succeed: {:?}", anchored.err());
33 assert!(host.call(br#"{"op":"snapshot"}"#).is_ok());
34 assert!(host.call(br#"{"op":"display_edit","at":1,"length":0,"text":"a"}"#).is_ok());
35 host.call("{\"op\":\"display_edit\",\"at\":2,\"length\":0,\"text\":\"πŸ˜€\"}".as_bytes()).unwrap();
36 let anchor: serde_json::Value = serde_json::from_slice(host.call(br#"{"op":"anchor","at":4}"#).unwrap()).unwrap();
37 let resolved = host.call(&serde_json::to_vec(&json!({"op":"resolve","anchor":anchor})).unwrap()).unwrap();
38 assert_eq!(resolved,b"4");
39 assert!(host.call(br#"{"op":"anchor","at":999}"#).is_ok());
40 assert!(host.call(br#"{"op":"snapshot"}"#).is_ok());
41}
42
43#[test]
44fn generated_source_delta_applies_to_authority_with_protocol_client_ids() {
45 let mut authority = Host::new().unwrap();
46 authority.call(br#"{"op":"open","guid":"generation","client":3088656600}"#).unwrap();
47 authority.call(br#"{"op":"insert_element","at":0,"type":"paragraph"}"#).unwrap();
48 let snapshot = authority.call(br#"{"op":"snapshot"}"#).unwrap().to_vec();
49 let mut source = Host::new().unwrap();
50 source.call(br#"{"op":"open","guid":"generation","client":4088656600}"#).unwrap();
51 let mut imported = vec![255]; imported.extend(snapshot); source.call(&imported).unwrap();
52 let update = source.call(br#"{"op":"display_edit","at":0,"length":0,"text":"N"}"#).unwrap().to_vec();
53 let mut applied = vec![254]; applied.extend(update); authority.call(&applied).unwrap();
54 let p: serde_json::Value = serde_json::from_slice(authority.call(br#"{"op":"project"}"#).unwrap()).unwrap();
55 assert_eq!(p["children"][0]["children"][0]["runs"][0]["insert"], "N");
56}
57
58#[test]
59fn native_boundary_rejects_client_ids_that_v1_decoder_would_truncate() {
60 let mut host = Host::new().unwrap();
61 assert!(host.call(br#"{"op":"open","guid":"generation","client":1088656600138213}"#).is_err());
62}
63
64#[test]
65fn fuel_budget_grows_with_the_instance_but_stays_bounded() {
66 use docs_native_host::{fuel_budget, MAX_FUEL};
67 // A small call keeps the old floor; a bigger instance or request earns more.
68 assert_eq!(fuel_budget(0, 0), 40_000_000);
69 assert!(fuel_budget(16 << 20, 0) > fuel_budget(1 << 20, 0));
70 assert!(fuel_budget(1 << 20, 1 << 20) > fuel_budget(1 << 20, 0));
71 // Memory and requests are capped, so the budget is too, whatever is asked.
72 assert_eq!(fuel_budget(usize::MAX, usize::MAX), MAX_FUEL);
73 assert!(MAX_FUEL < 20_000_000_000, "the ceiling is what bounds a runaway call: {MAX_FUEL}");
74 // The production allowance itself, pinned: 40M, plus 128 per byte of
75 // memory, plus 1,024 per request byte; capped at 64 MiB and 8 MiB + 1.
76 assert_eq!(fuel_budget(1 << 20, 0), 40_000_000 + 128 * (1 << 20));
77 assert_eq!(fuel_budget(1 << 20, 64), 40_000_000 + 128 * (1 << 20) + 1_024 * 64);
78 assert_eq!(fuel_budget(16 << 20, 1 << 20), 40_000_000 + 128 * (16 << 20) + 1_024 * (1 << 20));
79 assert_eq!(fuel_budget(64 << 20, 8 << 20), 40_000_000 + 128 * (64 << 20) + 1_024 * (8 << 20));
80 assert_eq!(fuel_budget(1 << 30, 0), fuel_budget(64 << 20, 0), "memory past the cap earns nothing");
81 assert_eq!(MAX_FUEL, 40_000_000 + 128 * (64 << 20) + 1_024 * ((8 << 20) + 1));
82}
83
84#[test]
85fn running_out_of_fuel_is_a_clean_failure_that_discards_the_instance() {
86 let mut host = Host::new().unwrap();
87 host.call(br#"{"op":"open","guid":"fuel","client":321}"#).unwrap();
88 for at in 0..40 { host.call(format!(r#"{{"op":"insert_element","at":{at},"type":"paragraph"}}"#).as_bytes()).unwrap(); }
89 let text = "fuel ".repeat(600);
90 host.call(&serde_json::to_vec(&json!({"op":"display_edit","at":0,"length":0,"text":text})).unwrap()).unwrap();
91 // The same call succeeds with its budget, so only fuel can fail it below.
92 assert!(host.call(br#"{"op":"project"}"#).is_ok());
93 host.limit_fuel_for_tests(100_000);
94 assert_eq!(host.call(br#"{"op":"project"}"#).err(), Some("module_trap"));
95 assert!(host.call(br#"{"op":"snapshot"}"#).is_err(), "an instance that ran out must not be reused");
96}
97
98/// The native gate's long-document check: a 200-page document with 10,000
99/// edits from twenty clients (fixtures/generate-large-doc.mjs) opens and takes
100/// typing through the calls CRDTReplica makes, on the production budget. The
101/// old fixed 40M budget failed this at the first keystroke and could not open
102/// it at all. check-native.sh generates the document and runs this.
103#[test]
104#[ignore = "needs DOCS_LARGE_DOCUMENT; native/scripts/check-native.sh runs it"]
105fn long_edited_document_opens_and_types() {
106 let path = std::env::var("DOCS_LARGE_DOCUMENT").expect("DOCS_LARGE_DOCUMENT names the generated document");
107 let document = std::fs::read(path).unwrap();
108 assert!(document.len() > 800_000, "expected the 200-page edited document");
109 // Open (CRDTReplica.open): the authority imports the state, then snapshots and projects.
110 let mut authority = Host::new().unwrap();
111 authority.call(br#"{"op":"open","guid":"long","client":3088656600}"#).unwrap();
112 let mut imported = vec![255]; imported.extend(&document); authority.call(&imported).unwrap();
113 authority.call(br#"{"op":"clients"}"#).unwrap();
114 let snapshot = authority.call(br#"{"op":"snapshot"}"#).unwrap().to_vec();
115 let projection: serde_json::Value = serde_json::from_slice(authority.call(br#"{"op":"project"}"#).unwrap()).unwrap();
116 assert!(projection["children"].as_array().unwrap().len() > 1000);
117 // Presentation: a fresh source sandbox from the snapshot.
118 let mut source = Host::new().unwrap();
119 source.call(br#"{"op":"open","guid":"long","client":4088656600}"#).unwrap();
120 let mut imported = vec![255]; imported.extend(&snapshot); source.call(&imported).unwrap();
121 source.call(br#"{"op":"clients"}"#).unwrap();
122 // Typing, as CRDTReplica does it now: the command on the displayed
123 // source, its update on the authority, the selection's two anchors
124 // resolved, and a presentation that re-projects only the touched block.
125 // No snapshot and no whole projection per key.
126 let at = 300_000u32;
127 let mut tail: Vec<Vec<u8>> = Vec::new();
128 for (i, ch) in "Typed".chars().enumerate() {
129 let at = at + i as u32;
130 let update = source.call(&serde_json::to_vec(&json!({"op":"display_edit","at":at,"length":0,"text":ch.to_string()})).unwrap()).unwrap().to_vec();
131 assert!(update.len() < 64, "a keystroke's update carries only its change: {} B", update.len());
132 let mut applied = vec![254]; applied.extend(&update); authority.call(&applied).unwrap();
133 tail.push(update);
134 for end in [at + 1, at + 1] {
135 let anchor: serde_json::Value = serde_json::from_slice(source.call(&serde_json::to_vec(&json!({"op":"anchor","at":end})).unwrap()).unwrap()).unwrap();
136 let resolved = authority.call(&serde_json::to_vec(&json!({"op":"resolve","anchor":anchor})).unwrap()).unwrap();
137 assert_eq!(resolved, end.to_string().as_bytes());
138 }
139 let delta: serde_json::Value = serde_json::from_slice(authority.call(br#"{"op":"project_delta"}"#).unwrap()).unwrap();
140 let inserted: usize = delta["ops"].as_array().unwrap().iter().filter_map(|op| op["insert"].as_array()).map(Vec::len).sum();
141 assert_eq!(inserted, 1, "one block re-projected");
142 }
143 // A peer's change, then a new source from the base and the updates since
144 // (two envelopes), and the occasional compaction snapshot.
145 let mut peer = Host::new().unwrap();
146 peer.call(br#"{"op":"open","guid":"long","client":2088656600}"#).unwrap();
147 let mut imported = vec![255]; imported.extend(&snapshot); peer.call(&imported).unwrap();
148 let remote = peer.call(br#"{"op":"display_edit","at":1000,"length":0,"text":"peer"}"#).unwrap().to_vec();
149 let mut applied = vec![255]; applied.extend(&remote); authority.call(&applied).unwrap();
150 tail.push(remote);
151 let mut fresh = Host::new().unwrap();
152 fresh.call(br#"{"op":"open","guid":"long","client":1088656600}"#).unwrap();
153 let mut imported = vec![255]; imported.extend(&snapshot); fresh.call(&imported).unwrap();
154 let mut rest = vec![255]; for update in &tail { rest.extend(update); } fresh.call(&rest).unwrap();
155 authority.call(br#"{"op":"project_delta"}"#).unwrap();
156 authority.call(br#"{"op":"snapshot"}"#).unwrap();
157 let a: serde_json::Value = serde_json::from_slice(authority.call(br#"{"op":"project"}"#).unwrap()).unwrap();
158 let b: serde_json::Value = serde_json::from_slice(fresh.call(br#"{"op":"project"}"#).unwrap()).unwrap();
159 assert_eq!(a["children"], b["children"], "a source from base plus tail shows what the authority does");
160}
161
162#[test]
163fn hosts_share_the_compiled_module_but_never_state() {
164 // The module is compiled once per process; every host is still its own
165 // instance, memory and fuel budget, on any thread.
166 let mut first = Host::new().unwrap();
167 first.call(br#"{"op":"open","guid":"shared","client":11}"#).unwrap();
168 first.call(br#"{"op":"insert_element","at":0,"type":"paragraph"}"#).unwrap();
169 let mut second = Host::new().unwrap();
170 assert!(second.call(br#"{"op":"project"}"#).is_err(), "a new host starts with no editor");
171 let threads: Vec<_> = (0..4u32).map(|n| std::thread::spawn(move || {
172 for round in 0..3u32 {
173 let mut host = Host::new().unwrap();
174 host.call(format!(r#"{{"op":"open","guid":"thread","client":{}}}"#, 100 + n * 10 + round).as_bytes()).unwrap();
175 host.call(br#"{"op":"insert_element","at":0,"type":"paragraph"}"#).unwrap();
176 host.call(format!(r#"{{"op":"display_edit","at":0,"length":0,"text":"t{n}"}}"#).as_bytes()).unwrap();
177 let p: serde_json::Value = serde_json::from_slice(host.call(br#"{"op":"project"}"#).unwrap()).unwrap();
178 assert_eq!(p["children"].as_array().unwrap().len(), 1);
179 assert_eq!(p["children"][0]["children"][0]["runs"][0]["insert"], format!("t{n}"));
180 }
181 })).collect();
182 for thread in threads { thread.join().unwrap(); }
183 let p: serde_json::Value = serde_json::from_slice(first.call(br#"{"op":"project"}"#).unwrap()).unwrap();
184 assert_eq!(p["children"].as_array().unwrap().len(), 1);
185}
186