| 1 | //! A scene change must fit one live update (the final review's call 3), |
| 2 | //! the twin of fixtures/scene-update.test.mjs. Data within 960 KiB of |
| 3 | //! canonical JSON can still encode past the 1 MiB a posted update may be |
| 4 | //! (internal/slides/live.go `maxUpdateBytes`): a number takes nine bytes |
| 5 | //! in the update and four in JSON. So a batch carrying `add_scene` or |
| 6 | //! `set_scene` is refused, having written nothing, when the update it |
| 7 | //! would make is over 1 MiB. |
| 8 | |
| 9 | use serde_json::{json, Value}; |
| 10 | use slides_core::{apply_ops, new_doc, view_json, CoreError}; |
| 11 | use yrs::Doc; |
| 12 | |
| 13 | const CODE: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 14 | const MAX_UPDATE: usize = 1 << 20; |
| 15 | |
| 16 | fn deck() -> Doc { |
| 17 | let d = new_doc("scene-update", 1); |
| 18 | apply( |
| 19 | &d, |
| 20 | json!([ |
| 21 | {"op": "add_scene", "id": "worldsceneaa", "name": "World", "code": CODE, "states": [{"id": "a"}], "data": {"n": 1}}, |
| 22 | {"op": "add_slide", "id": "s1", "layout": "flow"} |
| 23 | ]), |
| 24 | ) |
| 25 | .expect("the deck"); |
| 26 | d |
| 27 | } |
| 28 | |
| 29 | fn apply(d: &Doc, ops: Value) -> Result<Vec<u8>, CoreError> { |
| 30 | apply_ops(d, &ops.to_string()) |
| 31 | } |
| 32 | |
| 33 | fn tenths(n: usize) -> Value { |
| 34 | Value::Array(vec![json!(0.1); n]) |
| 35 | } |
| 36 | |
| 37 | fn refusal(r: Result<Vec<u8>, CoreError>) -> String { |
| 38 | match r { |
| 39 | Err(CoreError::Ops(m)) => m, |
| 40 | Err(e) => panic!("refused as {e}, want an ops refusal"), |
| 41 | Ok(u) => panic!("taken, a {}-byte update", u.len()), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn tenths_past_one_update_are_refused() { |
| 47 | let d = deck(); |
| 48 | let before = view_json(&d); |
| 49 | let m = refusal(apply( |
| 50 | &d, |
| 51 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(150_000)}]), |
| 52 | )); |
| 53 | assert!( |
| 54 | m.starts_with("op 0: scene \"worldsceneaa\" would make an update larger than 1 MiB"), |
| 55 | "{m}" |
| 56 | ); |
| 57 | assert_eq!(view_json(&d), before); |
| 58 | let m = refusal(apply( |
| 59 | &d, |
| 60 | json!([ |
| 61 | {"op": "add_slide", "id": "s2", "layout": "flow"}, |
| 62 | {"op": "add_scene", "id": "secondscene2", "name": "Second", "code": CODE, "states": [{"id": "a"}], "data": tenths(150_000)} |
| 63 | ]), |
| 64 | )); |
| 65 | assert!(m.starts_with("op 1: scene \"secondscene2\" "), "{m}"); |
| 66 | assert_eq!(view_json(&d), before); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn just_under_the_cap_is_taken() { |
| 71 | let d = deck(); |
| 72 | let update = apply( |
| 73 | &d, |
| 74 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(116_000)}]), |
| 75 | ) |
| 76 | .expect("taken"); |
| 77 | assert!( |
| 78 | update.len() > MAX_UPDATE - 8 * 1024 && update.len() <= MAX_UPDATE, |
| 79 | "the update is {} bytes", |
| 80 | update.len() |
| 81 | ); |
| 82 | let v: Value = serde_json::from_str(&view_json(&d)).unwrap(); |
| 83 | assert_eq!( |
| 84 | v["scenes"]["worldsceneaa"]["data"].as_array().map(Vec::len), |
| 85 | Some(116_000) |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn two_scene_changes_in_one_batch_are_measured_together() { |
| 91 | let d = deck(); |
| 92 | apply(&d, json!([{"op": "add_scene", "id": "secondscene2", "name": "Second", "code": CODE, "states": [{"id": "a"}]}])).unwrap(); |
| 93 | let m = refusal(apply( |
| 94 | &d, |
| 95 | json!([ |
| 96 | {"op": "set_scene", "id": "worldsceneaa", "data": tenths(70_000)}, |
| 97 | {"op": "set_scene", "id": "secondscene2", "data": tenths(70_000)} |
| 98 | ]), |
| 99 | )); |
| 100 | assert!(m.starts_with("op 0: "), "{m}"); |
| 101 | apply( |
| 102 | &d, |
| 103 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": tenths(70_000)}]), |
| 104 | ) |
| 105 | .unwrap(); |
| 106 | apply( |
| 107 | &d, |
| 108 | json!([{"op": "set_scene", "id": "secondscene2", "data": tenths(70_000)}]), |
| 109 | ) |
| 110 | .unwrap(); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn a_batch_with_no_scene_op_is_not_measured() { |
| 115 | let d = deck(); |
| 116 | apply( |
| 117 | &d, |
| 118 | json!([{"op": "add_block", "slide": "s1", "id": "b1", "type": "text", "region": "body", "markdown": "x".repeat(1_100_000)}]), |
| 119 | ) |
| 120 | .expect("a long text is not a scene change"); |
| 121 | } |
| 122 | |
| 123 | /// The exact boundary, on a deck whose own client has a long history (a |
| 124 | /// clock past 16,384): an update of exactly 1 MiB is taken and one of a |
| 125 | /// byte more refused. The copy the batch is measured on must write as |
| 126 | /// this client, at its clock, or the measure is of another update. |
| 127 | #[test] |
| 128 | fn exactly_one_mib_is_taken_and_one_byte_more_is_refused() { |
| 129 | let long = || { |
| 130 | let d = deck(); |
| 131 | apply( |
| 132 | &d, |
| 133 | json!([{"op": "add_block", "slide": "s1", "id": "b1", "type": "text", "region": "body", "markdown": "y".repeat(20_000)}]), |
| 134 | ) |
| 135 | .unwrap(); |
| 136 | d |
| 137 | }; |
| 138 | let data = |s: usize| { |
| 139 | let mut v = vec![json!("x".repeat(s))]; |
| 140 | v.extend(std::iter::repeat_n(json!(0.1), 20_000)); |
| 141 | Value::Array(v) |
| 142 | }; |
| 143 | let size_of = |s: usize| { |
| 144 | apply( |
| 145 | &long(), |
| 146 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": data(s)}]), |
| 147 | ) |
| 148 | .expect("under the cap") |
| 149 | .len() |
| 150 | }; |
| 151 | let probe = 800_000; |
| 152 | let at = probe + MAX_UPDATE - size_of(probe); |
| 153 | assert_eq!(size_of(at), MAX_UPDATE); |
| 154 | let d = long(); |
| 155 | let before = view_json(&d); |
| 156 | let m = refusal(apply( |
| 157 | &d, |
| 158 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": data(at + 1)}]), |
| 159 | )); |
| 160 | assert!(m.contains("larger than 1 MiB"), "{m}"); |
| 161 | assert_eq!(view_json(&d), before); |
| 162 | } |
| 163 | |