| 1 | //! Scenes in the document (scenes and clips spec §2, plan task 2): the |
| 2 | //! deck-level `scenes` map and its three ops, the `scene` and `clip` |
| 3 | //! block types, and a scene's version (plan call R1) against the shared |
| 4 | //! vectors in fixtures/vectors/scene-version.json, which |
| 5 | //! fixtures/scenes.test.mjs and internal/slides/deck read too. |
| 6 | |
| 7 | use serde_json::{json, Value}; |
| 8 | use slides_core::{apply_ops, new_doc, scene_canonical, scene_version, view_json}; |
| 9 | use yrs::Doc; |
| 10 | |
| 11 | const CODE: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; |
| 12 | const CODE2: &str = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"; |
| 13 | const STILL: &str = "1111111111111111111111111111111111111111111111111111111111111111"; |
| 14 | /// The data limit: 960 KiB of canonical JSON. A scene change must also |
| 15 | /// fit one live update (1 MiB), which scene_update.rs is about. |
| 16 | const LIMIT: usize = 960 * 1024; |
| 17 | |
| 18 | fn doc(n: u64) -> Doc { |
| 19 | new_doc(&format!("scenes-{n}"), n) |
| 20 | } |
| 21 | |
| 22 | fn view(doc: &Doc) -> Value { |
| 23 | serde_json::from_str(&view_json(doc)).expect("view is JSON") |
| 24 | } |
| 25 | |
| 26 | fn apply(d: &Doc, ops: Value) -> Result<Vec<u8>, slides_core::CoreError> { |
| 27 | apply_ops(d, &ops.to_string()) |
| 28 | } |
| 29 | |
| 30 | fn world() -> Value { |
| 31 | json!({"op": "add_scene", "id": "worldsceneaa", "name": "The world", "code": CODE, |
| 32 | "states": [ |
| 33 | {"id": "a", "name": "Start", "description": "An empty field"}, |
| 34 | {"id": "b", "name": "Town", "description": "A town appears"}, |
| 35 | {"id": "c", "name": "City", "description": "The town grows"} |
| 36 | ], |
| 37 | "words": {"title": "Vienna", "sign": "HalfStack"}, |
| 38 | "data": {"pieces": [{"id": "p1", "x": 0.5}], "n": 3}}) |
| 39 | } |
| 40 | |
| 41 | /// A deck with the world scene and one flow slide. |
| 42 | fn deck(n: u64) -> Doc { |
| 43 | let d = doc(n); |
| 44 | apply( |
| 45 | &d, |
| 46 | json!([world(), {"op": "add_slide", "id": "s1", "layout": "flow"}]), |
| 47 | ) |
| 48 | .expect("the scene and a slide apply"); |
| 49 | d |
| 50 | } |
| 51 | |
| 52 | fn scene_block(id: &str, props: Value) -> Value { |
| 53 | json!({"op": "add_block", "slide": "s1", "id": id, "type": "scene", "props": props}) |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn a_deck_without_scenes_has_no_scenes_key() { |
| 58 | let d = doc(1); |
| 59 | apply( |
| 60 | &d, |
| 61 | json!([{"op": "add_slide", "id": "s1", "layout": "flow"}]), |
| 62 | ) |
| 63 | .unwrap(); |
| 64 | assert!(view(&d).get("scenes").is_none(), "{}", view(&d)); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn add_scene_round_trips() { |
| 69 | let d = deck(2); |
| 70 | assert_eq!( |
| 71 | view(&d)["scenes"], |
| 72 | json!({"worldsceneaa": { |
| 73 | "name": "The world", "author": "", "code": CODE, |
| 74 | "states": [ |
| 75 | {"id": "a", "name": "Start", "description": "An empty field"}, |
| 76 | {"id": "b", "name": "Town", "description": "A town appears"}, |
| 77 | {"id": "c", "name": "City", "description": "The town grows"} |
| 78 | ], |
| 79 | "words": {"title": "Vienna", "sign": "HalfStack"}, |
| 80 | "data": {"pieces": [{"id": "p1", "x": 0.5}], "n": 3}, |
| 81 | "stills": {} |
| 82 | }}) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn add_scene_defaults_words_data_and_state_names() { |
| 88 | let d = doc(3); |
| 89 | apply( |
| 90 | &d, |
| 91 | json!([{"op": "add_scene", "id": "wwwwwwwwwwww", "name": "", "code": CODE, "states": [{"id": "only"}]}]), |
| 92 | ) |
| 93 | .unwrap(); |
| 94 | assert_eq!( |
| 95 | view(&d)["scenes"]["wwwwwwwwwwww"], |
| 96 | json!({"name": "", "author": "", "code": CODE, |
| 97 | "states": [{"id": "only", "name": "", "description": ""}], |
| 98 | "words": {}, "data": null, "stills": {}}) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn set_scene_patches_words_per_key_replaces_data_and_sets_stills() { |
| 104 | let d = deck(4); |
| 105 | apply( |
| 106 | &d, |
| 107 | json!([{"op": "set_scene", "id": "worldsceneaa", |
| 108 | "words": {"sign": null, "title": "Wien", "new": "Neu"}, |
| 109 | "data": [1, 2], |
| 110 | "stills": {"a": {"ref": STILL, "made": CODE2}, "b": {"ref": STILL, "made": CODE2}}}]), |
| 111 | ) |
| 112 | .expect("the patch applies"); |
| 113 | let s = &view(&d)["scenes"]["worldsceneaa"]; |
| 114 | assert_eq!(s["words"], json!({"title": "Wien", "new": "Neu"})); |
| 115 | assert_eq!(s["data"], json!([1, 2])); |
| 116 | assert_eq!(s["name"], "The world", "an absent name is left alone"); |
| 117 | assert_eq!( |
| 118 | s["stills"], |
| 119 | json!({"a": {"ref": STILL, "made": CODE2}, "b": {"ref": STILL, "made": CODE2}}) |
| 120 | ); |
| 121 | |
| 122 | apply( |
| 123 | &d, |
| 124 | json!([{"op": "set_scene", "id": "worldsceneaa", "name": "World", "code": CODE2, |
| 125 | "stills": {"a": null}, "data": null, |
| 126 | "states": [{"id": "c", "name": "City", "description": "d"}, {"id": "b"}]}]), |
| 127 | ) |
| 128 | .unwrap(); |
| 129 | let s = &view(&d)["scenes"]["worldsceneaa"]; |
| 130 | assert_eq!(s["name"], "World"); |
| 131 | assert_eq!(s["code"], CODE2); |
| 132 | assert_eq!( |
| 133 | s["data"], |
| 134 | Value::Null, |
| 135 | "data: null replaces the whole value" |
| 136 | ); |
| 137 | assert_eq!(s["stills"], json!({"b": {"ref": STILL, "made": CODE2}})); |
| 138 | assert_eq!( |
| 139 | s["states"], |
| 140 | json!([{"id": "c", "name": "City", "description": "d"}, {"id": "b", "name": "", "description": ""}]) |
| 141 | ); |
| 142 | assert_eq!( |
| 143 | s["words"], |
| 144 | json!({"title": "Wien", "new": "Neu"}), |
| 145 | "absent words leave them" |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn author_is_written_when_the_op_carries_one() { |
| 151 | // The server stamps it (internal/slides, from the acting principal); |
| 152 | // the core writes what it is given. |
| 153 | let d = doc(5); |
| 154 | let mut add = world(); |
| 155 | add["author"] = json!("agent:7"); |
| 156 | apply(&d, json!([add])).unwrap(); |
| 157 | assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "agent:7"); |
| 158 | apply( |
| 159 | &d, |
| 160 | json!([{"op": "set_scene", "id": "worldsceneaa", "code": CODE2, "author": "person:1"}]), |
| 161 | ) |
| 162 | .unwrap(); |
| 163 | assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "person:1"); |
| 164 | apply( |
| 165 | &d, |
| 166 | json!([{"op": "set_scene", "id": "worldsceneaa", "name": "x"}]), |
| 167 | ) |
| 168 | .unwrap(); |
| 169 | assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "person:1"); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn remove_scene_drops_it_and_an_empty_map_leaves_the_view() { |
| 174 | let d = deck(6); |
| 175 | apply(&d, json!([{"op": "remove_scene", "id": "worldsceneaa"}])).unwrap(); |
| 176 | assert!(view(&d).get("scenes").is_none(), "{}", view(&d)); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn scene_refusals_apply_nothing() { |
| 181 | let big = "x".repeat(LIMIT - 1); // its canonical JSON is the limit + 1 byte |
| 182 | let with = |f: &dyn Fn(&mut Value)| { |
| 183 | let mut op = world(); |
| 184 | op["id"] = json!("secondscenea"); |
| 185 | f(&mut op); |
| 186 | json!([op]) |
| 187 | }; |
| 188 | let bad: Vec<Value> = vec![ |
| 189 | json!([world()]), // already exists |
| 190 | with(&|o| o["id"] = json!("")), |
| 191 | with(&|o| o["code"] = json!("abc")), |
| 192 | with(&|o| o["code"] = json!(CODE.to_uppercase())), |
| 193 | with(&|o| o["states"] = json!([])), |
| 194 | with(&|o| o["states"] = json!("a")), |
| 195 | with(&|o| o["states"] = json!(["a"])), |
| 196 | with(&|o| o["states"] = json!([{"name": "x"}])), |
| 197 | with(&|o| o["states"] = json!([{"id": ""}])), |
| 198 | with(&|o| o["states"] = json!([{"id": 3}])), |
| 199 | with(&|o| o["states"] = json!([{"id": "a", "name": 3}])), |
| 200 | with(&|o| o["states"] = json!([{"id": "a", "description": []}])), |
| 201 | with(&|o| o["states"] = json!([{"id": "a"}, {"id": "b"}, {"id": "a"}])), |
| 202 | with(&|o| o["words"] = json!(["a"])), |
| 203 | with(&|o| o["words"] = json!({"k": 1})), |
| 204 | with(&|o| o["words"] = json!({"k": null})), |
| 205 | with(&|o| o["data"] = json!(big)), |
| 206 | json!([{"op": "set_scene", "id": "nope", "name": "x"}]), |
| 207 | json!([{"op": "set_scene", "id": "worldsceneaa", "code": "x"}]), |
| 208 | json!([{"op": "set_scene", "id": "worldsceneaa", "states": []}]), |
| 209 | json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}, {"id": "a"}]}]), |
| 210 | json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"k": 2}}]), |
| 211 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": {"s": big}}]), |
| 212 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"zz": {"ref": STILL, "made": CODE}}}]), |
| 213 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": "x", "made": CODE}}}]), |
| 214 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": STILL, "made": "x"}}}]), |
| 215 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": STILL}}}]), |
| 216 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": "x"}}]), |
| 217 | json!([{"op": "set_scene", "id": "worldsceneaa", "stills": [1]}]), |
| 218 | // a still for a state the same op removes |
| 219 | json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}], |
| 220 | "stills": {"b": {"ref": STILL, "made": CODE}}}]), |
| 221 | json!([{"op": "remove_scene", "id": "nope"}]), |
| 222 | json!([{"op": "remove_scene", "id": "worldsceneaa"}, {"op": "remove_scene", "id": "worldsceneaa"}]), |
| 223 | ]; |
| 224 | let d = deck(7); |
| 225 | let before = view_json(&d); |
| 226 | for b in bad { |
| 227 | let err = apply(&d, b.clone()).expect_err(&b.to_string()[..200.min(b.to_string().len())]); |
| 228 | assert!(err.to_string().starts_with("ops: op "), "{err}"); |
| 229 | assert_eq!(view_json(&d), before); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn data_up_to_960_kib_of_canonical_json_is_taken() { |
| 235 | let d = deck(8); |
| 236 | // The string plus two quotes is exactly the limit. |
| 237 | let edge = "x".repeat(LIMIT - 2); |
| 238 | apply( |
| 239 | &d, |
| 240 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": edge}]), |
| 241 | ) |
| 242 | .expect("960 KiB exactly"); |
| 243 | // Measured on canonical JSON, not as sent: whitespace does not count. |
| 244 | let spaced = format!( |
| 245 | r#"[{{"op": "set_scene", "id": "worldsceneaa", "data": [ "{}" ]}}]"#, |
| 246 | "x".repeat(LIMIT - 4) |
| 247 | ); |
| 248 | apply_ops(&d, &spaced).expect("spaces in the op are not the data's size"); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn data_numbers_keep_every_digit_an_agent_sent() { |
| 253 | // An agent works a scene's version out from the data it sent (docs/api.md), |
| 254 | // so the document must hold exactly those numbers: a double that takes |
| 255 | // seventeen digits to write reads back as the same double, not its |
| 256 | // neighbour. |
| 257 | let d = deck(17); |
| 258 | let sent = |
| 259 | "[[427.20000000000005,-15.599999999999994],[54.400000000000006,0.30000000000000004]]"; |
| 260 | apply_ops( |
| 261 | &d, |
| 262 | &format!(r#"[{{"op": "set_scene", "id": "worldsceneaa", "data": {sent}}}]"#), |
| 263 | ) |
| 264 | .expect("the data applies"); |
| 265 | assert_eq!(view(&d)["scenes"]["worldsceneaa"]["data"].to_string(), sent); |
| 266 | assert!( |
| 267 | view_json(&d).contains(sent), |
| 268 | "the view as written: {}", |
| 269 | view_json(&d) |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn a_scene_block_names_a_scene_and_two_of_its_states() { |
| 275 | let d = deck(9); |
| 276 | apply( |
| 277 | &d, |
| 278 | json!([scene_block( |
| 279 | "sb", |
| 280 | json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) |
| 281 | )]), |
| 282 | ) |
| 283 | .expect("a good scene block"); |
| 284 | apply( |
| 285 | &d, |
| 286 | json!([scene_block( |
| 287 | "eq", |
| 288 | json!({"scene": "worldsceneaa", "from": "b", "to": "b"}) |
| 289 | )]), |
| 290 | ) |
| 291 | .expect("from equal to to"); |
| 292 | apply(&d, json!([scene_block("empty", json!({}))])) |
| 293 | .expect("a scene block not yet pointed at a scene"); |
| 294 | let b = &view(&d)["slides"][0]["blocks"][0]; |
| 295 | assert_eq!(b["type"], "scene"); |
| 296 | assert_eq!( |
| 297 | b["props"], |
| 298 | json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) |
| 299 | ); |
| 300 | |
| 301 | let before = view_json(&d); |
| 302 | for props in [ |
| 303 | json!({"scene": "nope", "from": "a", "to": "c"}), |
| 304 | json!({"scene": "worldsceneaa", "from": "a", "to": "zz"}), |
| 305 | json!({"scene": "worldsceneaa", "from": "zz", "to": "a"}), |
| 306 | json!({"scene": "worldsceneaa", "from": "c", "to": "a"}), |
| 307 | json!({"scene": 5}), |
| 308 | json!({"scene": "worldsceneaa", "from": 1}), |
| 309 | json!({"from": "a", "to": "b"}), |
| 310 | ] { |
| 311 | apply(&d, json!([scene_block("x", props.clone())])).expect_err(&props.to_string()); |
| 312 | assert_eq!(view_json(&d), before, "{props}"); |
| 313 | } |
| 314 | for props in [ |
| 315 | json!({"to": "zz"}), |
| 316 | json!({"from": "b", "to": "a"}), |
| 317 | json!({"scene": "nope"}), |
| 318 | ] { |
| 319 | apply( |
| 320 | &d, |
| 321 | json!([{"op": "set_block", "slide": "s1", "id": "sb", "props": props}]), |
| 322 | ) |
| 323 | .expect_err(&props.to_string()); |
| 324 | assert_eq!(view_json(&d), before, "{props}"); |
| 325 | } |
| 326 | apply( |
| 327 | &d, |
| 328 | json!([{"op": "set_block", "slide": "s1", "id": "sb", "props": {"from": "b"}}]), |
| 329 | ) |
| 330 | .expect("moving from within the range"); |
| 331 | } |
| 332 | |
| 333 | #[test] |
| 334 | fn a_removed_scene_or_state_leaves_the_block_as_it_was() { |
| 335 | // Review Focus 3: the block stays valid; the renderer draws the |
| 336 | // placeholder and presses clamp (task 4). |
| 337 | let d = deck(10); |
| 338 | apply( |
| 339 | &d, |
| 340 | json!([ |
| 341 | scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "c"})), |
| 342 | {"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}, {"id": "b"}]} |
| 343 | ]), |
| 344 | ) |
| 345 | .expect("dropping a state a block names is fine"); |
| 346 | apply(&d, json!([{"op": "remove_scene", "id": "worldsceneaa"}])) |
| 347 | .expect("removing a scene in use is fine"); |
| 348 | let v = view(&d); |
| 349 | assert!(v.get("scenes").is_none()); |
| 350 | assert_eq!( |
| 351 | v["slides"][0]["blocks"][0]["props"], |
| 352 | json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) |
| 353 | ); |
| 354 | // The block's other settings still take, and props that do not touch |
| 355 | // the scene's names are not re-checked. |
| 356 | apply( |
| 357 | &d, |
| 358 | json!([{"op": "set_block", "slide": "s1", "id": "sb", "step": 2, "props": {"alt": "x"}}]), |
| 359 | ) |
| 360 | .expect("a step on a block whose scene is gone"); |
| 361 | // A batch that removes a scene and then points a block at it is refused. |
| 362 | let d = deck(11); |
| 363 | apply( |
| 364 | &d, |
| 365 | json!([{"op": "remove_scene", "id": "worldsceneaa"}, |
| 366 | scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "b"}))]), |
| 367 | ) |
| 368 | .expect_err("the model sees the removal"); |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn a_clip_block_plays_on_a_press_in_a_loop_or_once() { |
| 373 | let d = deck(12); |
| 374 | for (id, play) in [("c1", "press"), ("c2", "loop"), ("c3", "once")] { |
| 375 | apply( |
| 376 | &d, |
| 377 | json!([{"op": "add_block", "slide": "s1", "id": id, "type": "clip", |
| 378 | "props": {"asset": "v1", "play": play}}]), |
| 379 | ) |
| 380 | .expect(play); |
| 381 | } |
| 382 | let before = view_json(&d); |
| 383 | for op in [ |
| 384 | json!({"op": "add_block", "slide": "s1", "id": "c4", "type": "clip", "props": {"asset": "v1", "play": "bounce"}}), |
| 385 | json!({"op": "add_block", "slide": "s1", "id": "c4", "type": "clip", "props": {"asset": 3}}), |
| 386 | json!({"op": "set_block", "slide": "s1", "id": "c1", "props": {"play": "forever"}}), |
| 387 | ] { |
| 388 | apply(&d, json!([op])).expect_err(&op.to_string()); |
| 389 | assert_eq!(view_json(&d), before); |
| 390 | } |
| 391 | apply( |
| 392 | &d, |
| 393 | json!([{"op": "set_block", "slide": "s1", "id": "c1", "props": {"play": "loop"}}]), |
| 394 | ) |
| 395 | .unwrap(); |
| 396 | assert_eq!(view(&d)["slides"][0]["blocks"][0]["props"]["play"], "loop"); |
| 397 | } |
| 398 | |
| 399 | /// A clip's video is an asset with a poster (the ref of the picture of |
| 400 | /// its first frame) and its length in whole milliseconds; a picture has |
| 401 | /// neither, and its record in the view is what it always was. |
| 402 | #[test] |
| 403 | fn a_video_asset_carries_its_poster_and_duration() { |
| 404 | let d = doc(13); |
| 405 | apply( |
| 406 | &d, |
| 407 | json!([ |
| 408 | {"op": "register_asset", "id": "v1", "ref": "videoref", "name": "clip.webm", |
| 409 | "width": 1280, "height": 720, "alt": "A screen recording", |
| 410 | "poster": "posterref", "duration": 4250}, |
| 411 | {"op": "register_asset", "id": "p1", "ref": "posterref", "name": "clip.png", |
| 412 | "width": 1280, "height": 720}, |
| 413 | {"op": "register_asset", "id": "p2", "ref": "plain", "poster": "", "duration": 0} |
| 414 | ]), |
| 415 | ) |
| 416 | .expect("the assets apply"); |
| 417 | let v = view(&d); |
| 418 | assert_eq!( |
| 419 | v["assets"], |
| 420 | json!({ |
| 421 | "v1": {"ref": "videoref", "name": "clip.webm", "width": 1280, "height": 720, |
| 422 | "alt": "A screen recording", "poster": "posterref", "duration": 4250}, |
| 423 | "p1": {"ref": "posterref", "name": "clip.png", "width": 1280, "height": 720, "alt": ""}, |
| 424 | "p2": {"ref": "plain", "name": "", "width": 0, "height": 0, "alt": ""} |
| 425 | }) |
| 426 | ); |
| 427 | for bad in [ |
| 428 | json!({"op": "register_asset", "id": "v2", "ref": "r", "duration": -1}), |
| 429 | json!({"op": "register_asset", "id": "v2", "ref": "r", "duration": 1.5}), |
| 430 | json!({"op": "register_asset", "id": "v2", "ref": "r", "poster": 3}), |
| 431 | ] { |
| 432 | apply(&d, json!([bad])).expect_err(&bad.to_string()); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | #[test] |
| 437 | fn duplicate_slide_keeps_a_scene_block_checkable() { |
| 438 | let d = deck(13); |
| 439 | apply( |
| 440 | &d, |
| 441 | json!([ |
| 442 | scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "b"})), |
| 443 | {"op": "duplicate_slide", "id": "s1", "new_id": "s2", "new_block_ids": ["sb2"]}, |
| 444 | {"op": "set_block", "slide": "s2", "id": "sb2", "props": {"to": "c"}} |
| 445 | ]), |
| 446 | ) |
| 447 | .expect("the copy is a scene block too"); |
| 448 | apply( |
| 449 | &d, |
| 450 | json!([{"op": "set_block", "slide": "s2", "id": "sb2", "props": {"from": "zz"}}]), |
| 451 | ) |
| 452 | .expect_err("and is checked like one"); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn scene_version_vectors() { |
| 457 | let path = concat!( |
| 458 | env!("CARGO_MANIFEST_DIR"), |
| 459 | "/../fixtures/vectors/scene-version.json" |
| 460 | ); |
| 461 | let cases: Vec<Value> = |
| 462 | serde_json::from_str(&std::fs::read_to_string(path).expect("vectors")).expect("JSON"); |
| 463 | assert!(cases.len() >= 5); |
| 464 | let mut seen = std::collections::HashMap::new(); |
| 465 | for c in cases { |
| 466 | let name = c["name"].as_str().unwrap(); |
| 467 | // Distinct scenes never share a version (the collision cases). |
| 468 | if let Some(other) = seen.insert(c["version"].clone(), c["canonical"].clone()) { |
| 469 | assert_eq!( |
| 470 | other, c["canonical"], |
| 471 | "{name} shares a version with a different scene" |
| 472 | ); |
| 473 | } |
| 474 | assert_eq!( |
| 475 | scene_canonical(&c["scene"]), |
| 476 | c["canonical"].as_str().unwrap(), |
| 477 | "{name}" |
| 478 | ); |
| 479 | assert_eq!( |
| 480 | scene_version(&c["scene"]), |
| 481 | c["version"].as_str().unwrap(), |
| 482 | "{name}" |
| 483 | ); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn scene_version_changes_with_code_data_and_words_only() { |
| 489 | let d = deck(14); |
| 490 | let v0 = scene_version(&view(&d)["scenes"]["worldsceneaa"]); |
| 491 | apply( |
| 492 | &d, |
| 493 | json!([{"op": "set_scene", "id": "worldsceneaa", "name": "Renamed", |
| 494 | "stills": {"a": {"ref": STILL, "made": v0}}}]), |
| 495 | ) |
| 496 | .unwrap(); |
| 497 | assert_eq!(scene_version(&view(&d)["scenes"]["worldsceneaa"]), v0); |
| 498 | for op in [ |
| 499 | json!({"op": "set_scene", "id": "worldsceneaa", "code": CODE2}), |
| 500 | json!({"op": "set_scene", "id": "worldsceneaa", "data": {"n": 4}}), |
| 501 | json!({"op": "set_scene", "id": "worldsceneaa", "words": {"title": "Wien"}}), |
| 502 | ] { |
| 503 | let before = scene_version(&view(&d)["scenes"]["worldsceneaa"]); |
| 504 | apply(&d, json!([op])).unwrap(); |
| 505 | assert_ne!( |
| 506 | scene_version(&view(&d)["scenes"]["worldsceneaa"]), |
| 507 | before, |
| 508 | "{op}" |
| 509 | ); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn a_scene_id_is_twelve_lowercase_base32_characters() { |
| 515 | let d = doc(15); |
| 516 | let before = view_json(&d); |
| 517 | for id in [ |
| 518 | "", |
| 519 | "World", |
| 520 | "short", |
| 521 | "toolongsceneid", |
| 522 | "scene0000001", |
| 523 | "__proto__", |
| 524 | "SCENEAAAAAAA", |
| 525 | ] { |
| 526 | let mut op = world(); |
| 527 | op["id"] = json!(id); |
| 528 | let err = apply(&d, json!([op])).expect_err(id); |
| 529 | assert!(err.to_string().contains("base32"), "{id:?}: {err}"); |
| 530 | assert_eq!(view_json(&d), before); |
| 531 | } |
| 532 | let mut op = world(); |
| 533 | op["id"] = json!("abcdefghij27"); |
| 534 | apply(&d, json!([op])).expect("a well-formed id"); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn proto_is_refused_as_a_state_id_a_word_key_and_anywhere_in_data() { |
| 539 | // JavaScript's decoder (lib0) loses a plain object's "__proto__" key, |
| 540 | // so a scene may not depend on one (scenes review, finding 4). |
| 541 | let d = deck(16); |
| 542 | let before = view_json(&d); |
| 543 | let with = |f: &dyn Fn(&mut Value)| { |
| 544 | let mut op = world(); |
| 545 | op["id"] = json!("secondscenea"); |
| 546 | f(&mut op); |
| 547 | json!([op]) |
| 548 | }; |
| 549 | for (bad, what) in [ |
| 550 | ( |
| 551 | with(&|o| o["states"] = json!([{"id": "a"}, {"id": "__proto__"}])), |
| 552 | "states", |
| 553 | ), |
| 554 | (with(&|o| o["words"] = json!({"__proto__": "x"})), "words"), |
| 555 | (with(&|o| o["data"] = json!({"__proto__": 1})), "data"), |
| 556 | ( |
| 557 | with(&|o| o["data"] = json!([1, {"a": {"b": [{"__proto__": null}]}}])), |
| 558 | "data", |
| 559 | ), |
| 560 | ( |
| 561 | json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "__proto__"}]}]), |
| 562 | "states", |
| 563 | ), |
| 564 | ( |
| 565 | json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": "x"}}]), |
| 566 | "words", |
| 567 | ), |
| 568 | ( |
| 569 | json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": null}}]), |
| 570 | "words", |
| 571 | ), |
| 572 | ( |
| 573 | json!([{"op": "set_scene", "id": "worldsceneaa", "data": {"x": {"__proto__": []}}}]), |
| 574 | "data", |
| 575 | ), |
| 576 | ] { |
| 577 | let err = apply(&d, bad.clone()).expect_err(&bad.to_string()); |
| 578 | assert!( |
| 579 | err.to_string().contains("__proto__") && err.to_string().contains(what), |
| 580 | "{bad}: {err}" |
| 581 | ); |
| 582 | assert_eq!(view_json(&d), before); |
| 583 | } |
| 584 | // As text it is only text. |
| 585 | apply( |
| 586 | &d, |
| 587 | json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"k": "__proto__"}, "data": ["__proto__", {"v": "__proto__"}], |
| 588 | "states": [{"id": "a", "name": "__proto__", "description": "__proto__"}]}]), |
| 589 | ) |
| 590 | .expect("__proto__ as a value"); |
| 591 | } |
| 592 | |