//! Scenes in the document (scenes and clips spec ยง2, plan task 2): the //! deck-level `scenes` map and its three ops, the `scene` and `clip` //! block types, and a scene's version (plan call R1) against the shared //! vectors in fixtures/vectors/scene-version.json, which //! fixtures/scenes.test.mjs and internal/slides/deck read too. use serde_json::{json, Value}; use slides_core::{apply_ops, new_doc, scene_canonical, scene_version, view_json}; use yrs::Doc; const CODE: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; const CODE2: &str = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"; const STILL: &str = "1111111111111111111111111111111111111111111111111111111111111111"; /// The data limit: 960 KiB of canonical JSON. A scene change must also /// fit one live update (1 MiB), which scene_update.rs is about. const LIMIT: usize = 960 * 1024; fn doc(n: u64) -> Doc { new_doc(&format!("scenes-{n}"), n) } fn view(doc: &Doc) -> Value { serde_json::from_str(&view_json(doc)).expect("view is JSON") } fn apply(d: &Doc, ops: Value) -> Result, slides_core::CoreError> { apply_ops(d, &ops.to_string()) } fn world() -> Value { json!({"op": "add_scene", "id": "worldsceneaa", "name": "The world", "code": CODE, "states": [ {"id": "a", "name": "Start", "description": "An empty field"}, {"id": "b", "name": "Town", "description": "A town appears"}, {"id": "c", "name": "City", "description": "The town grows"} ], "words": {"title": "Vienna", "sign": "HalfStack"}, "data": {"pieces": [{"id": "p1", "x": 0.5}], "n": 3}}) } /// A deck with the world scene and one flow slide. fn deck(n: u64) -> Doc { let d = doc(n); apply( &d, json!([world(), {"op": "add_slide", "id": "s1", "layout": "flow"}]), ) .expect("the scene and a slide apply"); d } fn scene_block(id: &str, props: Value) -> Value { json!({"op": "add_block", "slide": "s1", "id": id, "type": "scene", "props": props}) } #[test] fn a_deck_without_scenes_has_no_scenes_key() { let d = doc(1); apply( &d, json!([{"op": "add_slide", "id": "s1", "layout": "flow"}]), ) .unwrap(); assert!(view(&d).get("scenes").is_none(), "{}", view(&d)); } #[test] fn add_scene_round_trips() { let d = deck(2); assert_eq!( view(&d)["scenes"], json!({"worldsceneaa": { "name": "The world", "author": "", "code": CODE, "states": [ {"id": "a", "name": "Start", "description": "An empty field"}, {"id": "b", "name": "Town", "description": "A town appears"}, {"id": "c", "name": "City", "description": "The town grows"} ], "words": {"title": "Vienna", "sign": "HalfStack"}, "data": {"pieces": [{"id": "p1", "x": 0.5}], "n": 3}, "stills": {} }}) ); } #[test] fn add_scene_defaults_words_data_and_state_names() { let d = doc(3); apply( &d, json!([{"op": "add_scene", "id": "wwwwwwwwwwww", "name": "", "code": CODE, "states": [{"id": "only"}]}]), ) .unwrap(); assert_eq!( view(&d)["scenes"]["wwwwwwwwwwww"], json!({"name": "", "author": "", "code": CODE, "states": [{"id": "only", "name": "", "description": ""}], "words": {}, "data": null, "stills": {}}) ); } #[test] fn set_scene_patches_words_per_key_replaces_data_and_sets_stills() { let d = deck(4); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"sign": null, "title": "Wien", "new": "Neu"}, "data": [1, 2], "stills": {"a": {"ref": STILL, "made": CODE2}, "b": {"ref": STILL, "made": CODE2}}}]), ) .expect("the patch applies"); let s = &view(&d)["scenes"]["worldsceneaa"]; assert_eq!(s["words"], json!({"title": "Wien", "new": "Neu"})); assert_eq!(s["data"], json!([1, 2])); assert_eq!(s["name"], "The world", "an absent name is left alone"); assert_eq!( s["stills"], json!({"a": {"ref": STILL, "made": CODE2}, "b": {"ref": STILL, "made": CODE2}}) ); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "name": "World", "code": CODE2, "stills": {"a": null}, "data": null, "states": [{"id": "c", "name": "City", "description": "d"}, {"id": "b"}]}]), ) .unwrap(); let s = &view(&d)["scenes"]["worldsceneaa"]; assert_eq!(s["name"], "World"); assert_eq!(s["code"], CODE2); assert_eq!( s["data"], Value::Null, "data: null replaces the whole value" ); assert_eq!(s["stills"], json!({"b": {"ref": STILL, "made": CODE2}})); assert_eq!( s["states"], json!([{"id": "c", "name": "City", "description": "d"}, {"id": "b", "name": "", "description": ""}]) ); assert_eq!( s["words"], json!({"title": "Wien", "new": "Neu"}), "absent words leave them" ); } #[test] fn author_is_written_when_the_op_carries_one() { // The server stamps it (internal/slides, from the acting principal); // the core writes what it is given. let d = doc(5); let mut add = world(); add["author"] = json!("agent:7"); apply(&d, json!([add])).unwrap(); assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "agent:7"); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "code": CODE2, "author": "person:1"}]), ) .unwrap(); assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "person:1"); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "name": "x"}]), ) .unwrap(); assert_eq!(view(&d)["scenes"]["worldsceneaa"]["author"], "person:1"); } #[test] fn remove_scene_drops_it_and_an_empty_map_leaves_the_view() { let d = deck(6); apply(&d, json!([{"op": "remove_scene", "id": "worldsceneaa"}])).unwrap(); assert!(view(&d).get("scenes").is_none(), "{}", view(&d)); } #[test] fn scene_refusals_apply_nothing() { let big = "x".repeat(LIMIT - 1); // its canonical JSON is the limit + 1 byte let with = |f: &dyn Fn(&mut Value)| { let mut op = world(); op["id"] = json!("secondscenea"); f(&mut op); json!([op]) }; let bad: Vec = vec![ json!([world()]), // already exists with(&|o| o["id"] = json!("")), with(&|o| o["code"] = json!("abc")), with(&|o| o["code"] = json!(CODE.to_uppercase())), with(&|o| o["states"] = json!([])), with(&|o| o["states"] = json!("a")), with(&|o| o["states"] = json!(["a"])), with(&|o| o["states"] = json!([{"name": "x"}])), with(&|o| o["states"] = json!([{"id": ""}])), with(&|o| o["states"] = json!([{"id": 3}])), with(&|o| o["states"] = json!([{"id": "a", "name": 3}])), with(&|o| o["states"] = json!([{"id": "a", "description": []}])), with(&|o| o["states"] = json!([{"id": "a"}, {"id": "b"}, {"id": "a"}])), with(&|o| o["words"] = json!(["a"])), with(&|o| o["words"] = json!({"k": 1})), with(&|o| o["words"] = json!({"k": null})), with(&|o| o["data"] = json!(big)), json!([{"op": "set_scene", "id": "nope", "name": "x"}]), json!([{"op": "set_scene", "id": "worldsceneaa", "code": "x"}]), json!([{"op": "set_scene", "id": "worldsceneaa", "states": []}]), json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}, {"id": "a"}]}]), json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"k": 2}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "data": {"s": big}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"zz": {"ref": STILL, "made": CODE}}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": "x", "made": CODE}}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": STILL, "made": "x"}}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": {"ref": STILL}}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": {"a": "x"}}]), json!([{"op": "set_scene", "id": "worldsceneaa", "stills": [1]}]), // a still for a state the same op removes json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}], "stills": {"b": {"ref": STILL, "made": CODE}}}]), json!([{"op": "remove_scene", "id": "nope"}]), json!([{"op": "remove_scene", "id": "worldsceneaa"}, {"op": "remove_scene", "id": "worldsceneaa"}]), ]; let d = deck(7); let before = view_json(&d); for b in bad { let err = apply(&d, b.clone()).expect_err(&b.to_string()[..200.min(b.to_string().len())]); assert!(err.to_string().starts_with("ops: op "), "{err}"); assert_eq!(view_json(&d), before); } } #[test] fn data_up_to_960_kib_of_canonical_json_is_taken() { let d = deck(8); // The string plus two quotes is exactly the limit. let edge = "x".repeat(LIMIT - 2); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "data": edge}]), ) .expect("960 KiB exactly"); // Measured on canonical JSON, not as sent: whitespace does not count. let spaced = format!( r#"[{{"op": "set_scene", "id": "worldsceneaa", "data": [ "{}" ]}}]"#, "x".repeat(LIMIT - 4) ); apply_ops(&d, &spaced).expect("spaces in the op are not the data's size"); } #[test] fn data_numbers_keep_every_digit_an_agent_sent() { // An agent works a scene's version out from the data it sent (docs/api.md), // so the document must hold exactly those numbers: a double that takes // seventeen digits to write reads back as the same double, not its // neighbour. let d = deck(17); let sent = "[[427.20000000000005,-15.599999999999994],[54.400000000000006,0.30000000000000004]]"; apply_ops( &d, &format!(r#"[{{"op": "set_scene", "id": "worldsceneaa", "data": {sent}}}]"#), ) .expect("the data applies"); assert_eq!(view(&d)["scenes"]["worldsceneaa"]["data"].to_string(), sent); assert!( view_json(&d).contains(sent), "the view as written: {}", view_json(&d) ); } #[test] fn a_scene_block_names_a_scene_and_two_of_its_states() { let d = deck(9); apply( &d, json!([scene_block( "sb", json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) )]), ) .expect("a good scene block"); apply( &d, json!([scene_block( "eq", json!({"scene": "worldsceneaa", "from": "b", "to": "b"}) )]), ) .expect("from equal to to"); apply(&d, json!([scene_block("empty", json!({}))])) .expect("a scene block not yet pointed at a scene"); let b = &view(&d)["slides"][0]["blocks"][0]; assert_eq!(b["type"], "scene"); assert_eq!( b["props"], json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) ); let before = view_json(&d); for props in [ json!({"scene": "nope", "from": "a", "to": "c"}), json!({"scene": "worldsceneaa", "from": "a", "to": "zz"}), json!({"scene": "worldsceneaa", "from": "zz", "to": "a"}), json!({"scene": "worldsceneaa", "from": "c", "to": "a"}), json!({"scene": 5}), json!({"scene": "worldsceneaa", "from": 1}), json!({"from": "a", "to": "b"}), ] { apply(&d, json!([scene_block("x", props.clone())])).expect_err(&props.to_string()); assert_eq!(view_json(&d), before, "{props}"); } for props in [ json!({"to": "zz"}), json!({"from": "b", "to": "a"}), json!({"scene": "nope"}), ] { apply( &d, json!([{"op": "set_block", "slide": "s1", "id": "sb", "props": props}]), ) .expect_err(&props.to_string()); assert_eq!(view_json(&d), before, "{props}"); } apply( &d, json!([{"op": "set_block", "slide": "s1", "id": "sb", "props": {"from": "b"}}]), ) .expect("moving from within the range"); } #[test] fn a_removed_scene_or_state_leaves_the_block_as_it_was() { // Review Focus 3: the block stays valid; the renderer draws the // placeholder and presses clamp (task 4). let d = deck(10); apply( &d, json!([ scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "c"})), {"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "a"}, {"id": "b"}]} ]), ) .expect("dropping a state a block names is fine"); apply(&d, json!([{"op": "remove_scene", "id": "worldsceneaa"}])) .expect("removing a scene in use is fine"); let v = view(&d); assert!(v.get("scenes").is_none()); assert_eq!( v["slides"][0]["blocks"][0]["props"], json!({"scene": "worldsceneaa", "from": "a", "to": "c"}) ); // The block's other settings still take, and props that do not touch // the scene's names are not re-checked. apply( &d, json!([{"op": "set_block", "slide": "s1", "id": "sb", "step": 2, "props": {"alt": "x"}}]), ) .expect("a step on a block whose scene is gone"); // A batch that removes a scene and then points a block at it is refused. let d = deck(11); apply( &d, json!([{"op": "remove_scene", "id": "worldsceneaa"}, scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "b"}))]), ) .expect_err("the model sees the removal"); } #[test] fn a_clip_block_plays_on_a_press_in_a_loop_or_once() { let d = deck(12); for (id, play) in [("c1", "press"), ("c2", "loop"), ("c3", "once")] { apply( &d, json!([{"op": "add_block", "slide": "s1", "id": id, "type": "clip", "props": {"asset": "v1", "play": play}}]), ) .expect(play); } let before = view_json(&d); for op in [ json!({"op": "add_block", "slide": "s1", "id": "c4", "type": "clip", "props": {"asset": "v1", "play": "bounce"}}), json!({"op": "add_block", "slide": "s1", "id": "c4", "type": "clip", "props": {"asset": 3}}), json!({"op": "set_block", "slide": "s1", "id": "c1", "props": {"play": "forever"}}), ] { apply(&d, json!([op])).expect_err(&op.to_string()); assert_eq!(view_json(&d), before); } apply( &d, json!([{"op": "set_block", "slide": "s1", "id": "c1", "props": {"play": "loop"}}]), ) .unwrap(); assert_eq!(view(&d)["slides"][0]["blocks"][0]["props"]["play"], "loop"); } /// A clip's video is an asset with a poster (the ref of the picture of /// its first frame) and its length in whole milliseconds; a picture has /// neither, and its record in the view is what it always was. #[test] fn a_video_asset_carries_its_poster_and_duration() { let d = doc(13); apply( &d, json!([ {"op": "register_asset", "id": "v1", "ref": "videoref", "name": "clip.webm", "width": 1280, "height": 720, "alt": "A screen recording", "poster": "posterref", "duration": 4250}, {"op": "register_asset", "id": "p1", "ref": "posterref", "name": "clip.png", "width": 1280, "height": 720}, {"op": "register_asset", "id": "p2", "ref": "plain", "poster": "", "duration": 0} ]), ) .expect("the assets apply"); let v = view(&d); assert_eq!( v["assets"], json!({ "v1": {"ref": "videoref", "name": "clip.webm", "width": 1280, "height": 720, "alt": "A screen recording", "poster": "posterref", "duration": 4250}, "p1": {"ref": "posterref", "name": "clip.png", "width": 1280, "height": 720, "alt": ""}, "p2": {"ref": "plain", "name": "", "width": 0, "height": 0, "alt": ""} }) ); for bad in [ json!({"op": "register_asset", "id": "v2", "ref": "r", "duration": -1}), json!({"op": "register_asset", "id": "v2", "ref": "r", "duration": 1.5}), json!({"op": "register_asset", "id": "v2", "ref": "r", "poster": 3}), ] { apply(&d, json!([bad])).expect_err(&bad.to_string()); } } #[test] fn duplicate_slide_keeps_a_scene_block_checkable() { let d = deck(13); apply( &d, json!([ scene_block("sb", json!({"scene": "worldsceneaa", "from": "a", "to": "b"})), {"op": "duplicate_slide", "id": "s1", "new_id": "s2", "new_block_ids": ["sb2"]}, {"op": "set_block", "slide": "s2", "id": "sb2", "props": {"to": "c"}} ]), ) .expect("the copy is a scene block too"); apply( &d, json!([{"op": "set_block", "slide": "s2", "id": "sb2", "props": {"from": "zz"}}]), ) .expect_err("and is checked like one"); } #[test] fn scene_version_vectors() { let path = concat!( env!("CARGO_MANIFEST_DIR"), "/../fixtures/vectors/scene-version.json" ); let cases: Vec = serde_json::from_str(&std::fs::read_to_string(path).expect("vectors")).expect("JSON"); assert!(cases.len() >= 5); let mut seen = std::collections::HashMap::new(); for c in cases { let name = c["name"].as_str().unwrap(); // Distinct scenes never share a version (the collision cases). if let Some(other) = seen.insert(c["version"].clone(), c["canonical"].clone()) { assert_eq!( other, c["canonical"], "{name} shares a version with a different scene" ); } assert_eq!( scene_canonical(&c["scene"]), c["canonical"].as_str().unwrap(), "{name}" ); assert_eq!( scene_version(&c["scene"]), c["version"].as_str().unwrap(), "{name}" ); } } #[test] fn scene_version_changes_with_code_data_and_words_only() { let d = deck(14); let v0 = scene_version(&view(&d)["scenes"]["worldsceneaa"]); apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "name": "Renamed", "stills": {"a": {"ref": STILL, "made": v0}}}]), ) .unwrap(); assert_eq!(scene_version(&view(&d)["scenes"]["worldsceneaa"]), v0); for op in [ json!({"op": "set_scene", "id": "worldsceneaa", "code": CODE2}), json!({"op": "set_scene", "id": "worldsceneaa", "data": {"n": 4}}), json!({"op": "set_scene", "id": "worldsceneaa", "words": {"title": "Wien"}}), ] { let before = scene_version(&view(&d)["scenes"]["worldsceneaa"]); apply(&d, json!([op])).unwrap(); assert_ne!( scene_version(&view(&d)["scenes"]["worldsceneaa"]), before, "{op}" ); } } #[test] fn a_scene_id_is_twelve_lowercase_base32_characters() { let d = doc(15); let before = view_json(&d); for id in [ "", "World", "short", "toolongsceneid", "scene0000001", "__proto__", "SCENEAAAAAAA", ] { let mut op = world(); op["id"] = json!(id); let err = apply(&d, json!([op])).expect_err(id); assert!(err.to_string().contains("base32"), "{id:?}: {err}"); assert_eq!(view_json(&d), before); } let mut op = world(); op["id"] = json!("abcdefghij27"); apply(&d, json!([op])).expect("a well-formed id"); } #[test] fn proto_is_refused_as_a_state_id_a_word_key_and_anywhere_in_data() { // JavaScript's decoder (lib0) loses a plain object's "__proto__" key, // so a scene may not depend on one (scenes review, finding 4). let d = deck(16); let before = view_json(&d); let with = |f: &dyn Fn(&mut Value)| { let mut op = world(); op["id"] = json!("secondscenea"); f(&mut op); json!([op]) }; for (bad, what) in [ ( with(&|o| o["states"] = json!([{"id": "a"}, {"id": "__proto__"}])), "states", ), (with(&|o| o["words"] = json!({"__proto__": "x"})), "words"), (with(&|o| o["data"] = json!({"__proto__": 1})), "data"), ( with(&|o| o["data"] = json!([1, {"a": {"b": [{"__proto__": null}]}}])), "data", ), ( json!([{"op": "set_scene", "id": "worldsceneaa", "states": [{"id": "__proto__"}]}]), "states", ), ( json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": "x"}}]), "words", ), ( json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"__proto__": null}}]), "words", ), ( json!([{"op": "set_scene", "id": "worldsceneaa", "data": {"x": {"__proto__": []}}}]), "data", ), ] { let err = apply(&d, bad.clone()).expect_err(&bad.to_string()); assert!( err.to_string().contains("__proto__") && err.to_string().contains(what), "{bad}: {err}" ); assert_eq!(view_json(&d), before); } // As text it is only text. apply( &d, json!([{"op": "set_scene", "id": "worldsceneaa", "words": {"k": "__proto__"}, "data": ["__proto__", {"v": "__proto__"}], "states": [{"id": "a", "name": "__proto__", "description": "__proto__"}]}]), ) .expect("__proto__ as a value"); }