//! A scene change must fit one live update (the final review's call 3), //! the twin of fixtures/scene-update.test.mjs. Data within 960 KiB of //! canonical JSON can still encode past the 1 MiB a posted update may be //! (internal/slides/live.go `maxUpdateBytes`): a number takes nine bytes //! in the update and four in JSON. So a batch carrying `add_scene` or //! `set_scene` is refused, having written nothing, when the update it //! would make is over 1 MiB. use serde_json::{json, Value}; use slides_core::{apply_ops, new_doc, view_json, CoreError}; use yrs::Doc; const CODE: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; const MAX_UPDATE: usize = 1 << 20; fn deck() -> Doc { let d = new_doc("scene-update", 1); apply( &d, json!([ {"op": "add_scene", "id": "worldsceneaa", "name": "World", "code": CODE, "states": [{"id": "a"}], "data": {"n": 1}}, {"op": "add_slide", "id": "s1", "layout": "flow"} ]), ) .expect("the deck"); d } fn apply(d: &Doc, ops: Value) -> Result, CoreError> { apply_ops(d, &ops.to_string()) } fn tenths(n: usize) -> Value { Value::Array(vec![json!(0.1); n]) } fn refusal(r: Result, CoreError>) -> String { match r { Err(CoreError::Ops(m)) => m, Err(e) => panic!("refused as {e}, want an ops refusal"), Ok(u) => panic!("taken, a {}-byte update", u.len()), } } #[test] fn tenths_past_one_update_are_refused() { let d = deck(); let before = view_json(&d); let m = refusal(apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(150_000)}]), )); assert!( m.starts_with("op 0: scene \"worldsceneaa\" would make an update larger than 1 MiB"), "{m}" ); assert_eq!(view_json(&d), before); let m = refusal(apply( &d, json!([ {"op": "add_slide", "id": "s2", "layout": "flow"}, {"op": "add_scene", "id": "secondscene2", "name": "Second", "code": CODE, "states": [{"id": "a"}], "data": tenths(150_000)} ]), )); assert!(m.starts_with("op 1: scene \"secondscene2\" "), "{m}"); assert_eq!(view_json(&d), before); } #[test] fn just_under_the_cap_is_taken() { let d = deck(); let update = apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(116_000)}]), ) .expect("taken"); assert!( update.len() > MAX_UPDATE - 8 * 1024 && update.len() <= MAX_UPDATE, "the update is {} bytes", update.len() ); let v: Value = serde_json::from_str(&view_json(&d)).unwrap(); assert_eq!( v["scenes"]["worldsceneaa"]["data"].as_array().map(Vec::len), Some(116_000) ); } #[test] fn two_scene_changes_in_one_batch_are_measured_together() { let d = deck(); apply(&d, json!([{"op": "add_scene", "id": "secondscene2", "name": "Second", "code": CODE, "states": [{"id": "a"}]}])).unwrap(); let m = refusal(apply( &d, json!([ {"op": "set_scene", "id": "worldsceneaa", "data": tenths(70_000)}, {"op": "set_scene", "id": "secondscene2", "data": tenths(70_000)} ]), )); assert!(m.starts_with("op 0: "), "{m}"); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(70_000)}]), ) .unwrap(); apply( &d, json!([{"op": "set_scene", "id": "secondscene2", "data": tenths(70_000)}]), ) .unwrap(); } #[test] fn a_batch_with_no_scene_op_is_not_measured() { let d = deck(); apply( &d, json!([{"op": "add_block", "slide": "s1", "id": "b1", "type": "text", "region": "body", "markdown": "x".repeat(1_100_000)}]), ) .expect("a long text is not a scene change"); } /// The exact boundary, on a deck whose own client has a long history (a /// clock past 16,384): an update of exactly 1 MiB is taken and one of a /// byte more refused. The copy the batch is measured on must write as /// this client, at its clock, or the measure is of another update. #[test] fn exactly_one_mib_is_taken_and_one_byte_more_is_refused() { let long = || { let d = deck(); apply( &d, json!([{"op": "add_block", "slide": "s1", "id": "b1", "type": "text", "region": "body", "markdown": "y".repeat(20_000)}]), ) .unwrap(); d }; let data = |s: usize| { let mut v = vec![json!("x".repeat(s))]; v.extend(std::iter::repeat_n(json!(0.1), 20_000)); Value::Array(v) }; let size_of = |s: usize| { apply( &long(), json!([{"op": "set_scene", "id": "worldsceneaa", "data": data(s)}]), ) .expect("under the cap") .len() }; let probe = 800_000; let at = probe + MAX_UPDATE - size_of(probe); assert_eq!(size_of(at), MAX_UPDATE); let d = long(); let before = view_json(&d); let m = refusal(apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "data": data(at + 1)}]), )); assert!(m.contains("larger than 1 MiB"), "{m}"); assert_eq!(view_json(&d), before); }