From ae84e3fef4b3317ba0fc5bcef152ce2ed9392f9c Mon Sep 17 00:00:00 2001 From: yoriswind Date: Wed, 4 Nov 2026 19:36:45 -0800 Subject: [PATCH] fix(actions): preserve unrelated shortcuts when replacing definitions --- crates/engine/src/cmd/actions_edit_cmds.rs | 9 +++++++-- crates/engine/src/cmd/atn_cmds/files.rs | 6 ++++-- crates/engine/tests/it/actions_clear_all.rs | 8 ++++++++ crates/engine/tests/it/actions_replace.rs | 12 +++++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/actions_edit_cmds.rs b/crates/engine/src/cmd/actions_edit_cmds.rs index d2b25687395..95598cec829 100644 --- a/crates/engine/src/cmd/actions_edit_cmds.rs +++ b/crates/engine/src/cmd/actions_edit_cmds.rs @@ -294,14 +294,19 @@ fn clear_all(s: &mut Session, p: &Value) -> Result { return Err(bad(cmd, "clearing all actions requires `confirm`: true")); } let count = s.actions.list.len(); + if count == 0 && s.actions.sets.is_empty() { + return Ok(json!({"cleared":0})); + } + let removed_keys: std::collections::HashSet<_> = + s.actions.list.iter().map(|action| format!("{}{}", crate::actions_cmds::PLAY_SHORTCUT_PREFIX, action.name)).collect(); let mut proposed = crate::actions_cmds::library::definition_copy(&s.actions); proposed.list.clear(); let names = proposed.sets.iter().cloned().collect(); crate::atn_cmds::sidecar::retire_sets(&mut proposed, &names); let mut shortcuts = s.prefs().shortcuts.clone(); let mut alternate = s.prefs().alternate_shortcuts.clone(); - shortcuts.retain(|key, _| !key.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX)); - alternate.retain(|key, _| !key.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX)); + shortcuts.retain(|key, _| !removed_keys.contains(key)); + alternate.retain(|key, _| !removed_keys.contains(key)); crate::actions_cmds::library::check_definition(&proposed, &shortcuts).map_err(|error| bad(cmd, error))?; s.actions.list = proposed.list; s.actions.sets = proposed.sets; diff --git a/crates/engine/src/cmd/atn_cmds/files.rs b/crates/engine/src/cmd/atn_cmds/files.rs index 301c36a9331..186dbf66055 100644 --- a/crates/engine/src/cmd/atn_cmds/files.rs +++ b/crates/engine/src/cmd/atn_cmds/files.rs @@ -143,8 +143,10 @@ fn import_actions(s: &mut Session, p: &Value, whole: bool) -> Result { let mut shortcuts = s.prefs().shortcuts.clone(); let mut alternate = s.prefs().alternate_shortcuts.clone(); if whole { - shortcuts.retain(|key, _| !key.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX)); - alternate.retain(|key, _| !key.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX)); + let removed_keys: std::collections::HashSet<_> = + s.actions.list.iter().map(|action| format!("{}{}", crate::actions_cmds::PLAY_SHORTCUT_PREFIX, action.name)).collect(); + shortcuts.retain(|key, _| !removed_keys.contains(key)); + alternate.retain(|key, _| !removed_keys.contains(key)); } for (name, key) in &keys { shortcuts.retain(|id, value| !(id.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX) && value == key)); diff --git a/crates/engine/tests/it/actions_clear_all.rs b/crates/engine/tests/it/actions_clear_all.rs index 66fda210c36..dd83f900b57 100644 --- a/crates/engine/tests/it/actions_clear_all.rs +++ b/crates/engine/tests/it/actions_clear_all.rs @@ -15,18 +15,21 @@ fn confirmed_clear_is_atomic_for_definitions_shortcuts_and_recording() { s.prefs.edit(|p| { for (id, key) in [("actions.play:A", "F5"), ("actions.play:B", "F6"), ("actions.play:Unloaded", "F7"), ("view.zoomIn", "Cmd+0")] { p.shortcuts.insert(id.into(), key.into()); + p.alternate_shortcuts.insert(id.into(), vec![format!("Shift+{key}")]); } }); let definitions = s.actions.list.clone(); s.execute("actions.newSet", json!({"name": "Empty"})).unwrap(); let sets = s.actions.sets.clone(); let shortcuts = s.prefs().shortcuts.clone(); + let alternate = s.prefs().alternate_shortcuts.clone(); let rev = s.actions.rev; for params in [json!({}), json!({"confirm": false}), json!({"confirm": null}), json!({"confirm": "yes"})] { assert!(s.execute("actions.clearAll", params.clone()).is_err(), "{params}"); assert_eq!(s.actions.list, definitions); assert_eq!(s.actions.sets, sets); assert_eq!(s.prefs().shortcuts, shortcuts); + assert_eq!(s.prefs().alternate_shortcuts, alternate); assert_eq!(s.actions.rev, rev); } s.execute("actions.record", json!({"action": "A"})).unwrap(); @@ -51,12 +54,17 @@ fn confirmed_clear_is_atomic_for_definitions_shortcuts_and_recording() { assert!(!s.prefs().shortcuts.contains_key("actions.play:B")); assert_eq!(s.prefs().shortcuts.get("actions.play:Unloaded"), shortcuts.get("actions.play:Unloaded")); assert_eq!(s.prefs().shortcuts.get("view.zoomIn"), shortcuts.get("view.zoomIn")); + assert!(!s.prefs().alternate_shortcuts.contains_key("actions.play:A")); + assert!(!s.prefs().alternate_shortcuts.contains_key("actions.play:B")); + assert_eq!(s.prefs().alternate_shortcuts.get("actions.play:Unloaded"), alternate.get("actions.play:Unloaded")); + assert_eq!(s.prefs().alternate_shortcuts.get("view.zoomIn"), alternate.get("view.zoomIn")); assert_eq!(s.active().unwrap().revision, revision); assert_eq!(s.active().unwrap().history.entries(), history); assert_eq!(s.journal, journal); let rev = s.actions.rev; assert_eq!(s.execute("actions.clearAll", json!({"confirm": true})).unwrap()["cleared"], 0); assert_eq!(s.actions.rev, rev); + assert_eq!(s.prefs().alternate_shortcuts.get("actions.play:Unloaded"), alternate.get("actions.play:Unloaded")); s.execute("actions.newSet", json!({"name": "Only Empty"})).unwrap(); let rev = s.actions.rev; s.execute("actions.clearAll", json!({"confirm": true})).unwrap(); diff --git a/crates/engine/tests/it/actions_replace.rs b/crates/engine/tests/it/actions_replace.rs index cb44796737e..d56c45e7507 100644 --- a/crates/engine/tests/it/actions_replace.rs +++ b/crates/engine/tests/it/actions_replace.rs @@ -25,6 +25,7 @@ fn session() -> Session { s.prefs.edit(|p| { for (id, key) in [("actions.play:OldA", "F5"), ("actions.play:OldB", "F6"), ("actions.play:Unloaded", "F9"), ("view.zoomIn", "Cmd+0")] { p.shortcuts.insert(id.into(), key.into()); + p.alternate_shortcuts.insert(id.into(), vec![format!("Shift+{key}")]); } }); s @@ -44,7 +45,7 @@ fn replace_preserves_imported_flags_references_and_keys_without_inheriting_old_b let result = s.execute("actions.replace", json!({"data": data, "confirm": true})).unwrap(); assert_eq!(result["actions"], json!(["Caller", "NewChild", "OldA"])); assert_eq!(s.actions.list.len(), 3); - assert!(s.actions.sets.is_empty(), "old empty folders are replaced too"); + assert_eq!(s.actions.sets, ["Incoming"], "only incoming physical folders remain; old empty folders are replaced too"); assert!(s.actions.list.iter().all(|a| a.set.as_deref() == Some("Incoming"))); assert_eq!(s.actions.list[0].disabled.iter().copied().collect::>(), [1]); assert_eq!(s.actions.list[0].dialogs.iter().copied().collect::>(), [0]); @@ -53,6 +54,10 @@ fn replace_preserves_imported_flags_references_and_keys_without_inheriting_old_b assert_eq!(s.prefs().shortcuts["actions.play:NewChild"], "F8"); assert_eq!(s.prefs().shortcuts["actions.play:Unloaded"], "F9"); assert_eq!(s.prefs().shortcuts["view.zoomIn"], "Cmd+0"); + assert!(!s.prefs().alternate_shortcuts.contains_key("actions.play:OldA")); + assert!(!s.prefs().alternate_shortcuts.contains_key("actions.play:OldB")); + assert_eq!(s.prefs().alternate_shortcuts["actions.play:Unloaded"], ["Shift+F9"]); + assert_eq!(s.prefs().alternate_shortcuts["view.zoomIn"], ["Shift+Cmd+0"]); assert_eq!(s.active().unwrap().revision, revision); assert_eq!(s.active().unwrap().history.entries(), history); assert_eq!(s.journal, journal); @@ -65,6 +70,8 @@ fn replace_preserves_imported_flags_references_and_keys_without_inheriting_old_b assert!(s.actions.list.is_empty()); assert_eq!(s.actions.sets, ["Empty"]); assert!(!s.prefs().shortcuts.contains_key("actions.play:NewChild")); + assert_eq!(s.prefs().shortcuts["actions.play:Unloaded"], "F9"); + assert_eq!(s.prefs().alternate_shortcuts["actions.play:Unloaded"], ["Shift+F9"]); } /// Rejected input, absent consent and active recording/playback never clear or flush definitions. @@ -75,6 +82,7 @@ fn invalid_or_unconfirmed_replacement_preserves_the_library_and_preferences() { let list = s.actions.list.clone(); let sets = s.actions.sets.clone(); let shortcuts = s.prefs().shortcuts.clone(); + let alternate = s.prefs().alternate_shortcuts.clone(); let revision = s.active().unwrap().revision; let rev = s.actions.rev; for params in [ @@ -92,6 +100,7 @@ fn invalid_or_unconfirmed_replacement_preserves_the_library_and_preferences() { assert_eq!(s.actions.list, list); assert_eq!(s.actions.sets, sets); assert_eq!(s.prefs().shortcuts, shortcuts); + assert_eq!(s.prefs().alternate_shortcuts, alternate); assert_eq!(s.actions.rev, rev); assert_eq!(s.active().unwrap().revision, revision); } @@ -108,4 +117,5 @@ fn invalid_or_unconfirmed_replacement_preserves_the_library_and_preferences() { assert!(result["failed"].is_object(), "{result}"); assert_eq!(s.actions.list, list); assert_eq!(s.prefs().shortcuts, shortcuts); + assert_eq!(s.prefs().alternate_shortcuts, alternate); }