//! UI commands: `ui.registerCommands` validates and replaces the frontend's //! commands, `listCommands` shows them with `ui: true`, and running one //! through the engine emits `uiCommand` for the frontend. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use lutra_engine::{Engine, Event}; use proptest::prelude::*; use serde_json::{Value, json}; fn register(engine: &mut Engine, commands: Value) -> Result { engine .execute("ui.registerCommands", json!({ "commands": commands })) .map(|o| o.value) .map_err(|e| e.code().to_string()) } fn first_commands() -> Value { json!([ {"id": "palette.open", "labelKey": "command.palette.open", "defaultShortcut": "Mod+Shift+P"}, {"id": "settings.open", "labelKey": "command.settings.open", "defaultShortcut": "Mod+,"}, {"id": "view.toggleProjectTree", "labelKey": "command.view.toggleProjectTree", "menuPath": "View/Project", "defaultShortcut": "Mod+1"}, ]) } #[test] fn registered_commands_are_listed_as_ui_commands() { let mut e = Engine::new().unwrap(); assert_eq!( register(&mut e, first_commands()).unwrap(), json!({"registered": 3}) ); let list = e.list_commands(); let ids: Vec<_> = list.iter().map(|c| c.id.as_str()).collect(); let mut sorted = ids.clone(); sorted.sort_unstable(); assert_eq!(ids, sorted, "one list in id order"); let palette = list.iter().find(|c| c.id == "palette.open").unwrap(); assert_eq!( serde_json::to_value(palette).unwrap(), json!({ "id": "palette.open", "label": "command.palette.open", "labelKey": "command.palette.open", "menuPath": null, "defaultShortcut": "Mod+Shift+P", "params": "any; passed to the frontend in uiCommand", "enabled": true, "ui": true, }) ); let tree = list .iter() .find(|c| c.id == "view.toggleProjectTree") .unwrap(); assert_eq!(tree.menu_path.as_deref(), Some("View/Project")); let quit = serde_json::to_value(list.iter().find(|c| c.id == "app.quit").unwrap()).unwrap(); assert_eq!(quit["ui"], false); assert!(quit.get("labelKey").is_none()); } #[test] fn running_a_ui_command_emits_ui_command() { let mut e = Engine::new().unwrap(); register(&mut e, first_commands()).unwrap(); let outcome = e.execute("palette.open", json!({"query": "save"})).unwrap(); assert_eq!(outcome.value, Value::Null); assert_eq!( outcome.events, vec![Event::UiCommand { id: "palette.open".into(), params: json!({"query": "save"}), }] ); let outcome = e.execute("settings.open", Value::Null).unwrap(); assert_eq!( outcome.events, vec![Event::UiCommand { id: "settings.open".into(), params: Value::Null, }] ); } #[test] fn a_new_registration_replaces_the_old_one() { let mut e = Engine::new().unwrap(); register(&mut e, first_commands()).unwrap(); register( &mut e, json!([{"id": "palette.open", "labelKey": "command.palette.open"}]), ) .unwrap(); assert_eq!( e.execute("view.toggleProjectTree", Value::Null) .unwrap_err() .code(), "unknown_command" ); let ui: Vec<_> = e .list_commands() .into_iter() .filter(|c| c.ui) .map(|c| c.id) .collect(); assert_eq!(ui, vec!["palette.open"]); register(&mut e, json!([])).unwrap(); assert!(e.list_commands().iter().all(|c| !c.ui)); } #[test] fn bad_registrations_change_nothing() { let mut e = Engine::new().unwrap(); register(&mut e, first_commands()).unwrap(); for bad in [ json!([{"id": "file.save", "labelKey": "x.y"}]), json!([{"id": "nodot", "labelKey": "x.y"}]), json!([{"id": "a.b", "labelKey": ""}]), json!([{"id": "a.b", "labelKey": "x.y", "extra": 1}]), json!([{"id": "a.b", "labelKey": "x.y"}, {"id": "a.b", "labelKey": "x.z"}]), json!([{"id": "a.b", "labelKey": "x.y", "defaultShortcut": "M".repeat(300)}]), json!({"id": "a.b"}), ] { assert_eq!( register(&mut e, bad.clone()), Err("invalid_params".into()), "{bad}" ); } let many: Vec<_> = (0..1001) .map(|i| json!({"id": format!("a.c{i}"), "labelKey": "x.y"})) .collect(); assert_eq!( register(&mut e, Value::Array(many)), Err("invalid_params".into()) ); assert_eq!(e.list_commands().iter().filter(|c| c.ui).count(), 3); } proptest! { #[test] fn arbitrary_registrations_never_panic(params in any::()) { let mut e = Engine::new().unwrap(); let value = serde_json::from_str(¶ms).unwrap_or(Value::String(params)); let _ = e.execute("ui.registerCommands", value); let _ = e.list_commands(); } #[test] fn valid_ids_round_trip(area in "[a-z][a-zA-Z0-9]{0,10}", action in "[a-z][a-zA-Z0-9]{0,10}") { let id = format!("{area}.{action}"); prop_assume!(!e_has(&id)); let mut e = Engine::new().unwrap(); register(&mut e, json!([{"id": id, "labelKey": "k.k"}])).unwrap(); let outcome = e.execute(&id, json!(1)).unwrap(); prop_assert_eq!(outcome.events, vec![Event::UiCommand { id: id.clone(), params: json!(1) }]); } } /// Whether the engine already has a command with this id. fn e_has(id: &str) -> bool { Engine::new() .unwrap() .list_commands() .iter() .any(|c| c.id == id) }