From 747db484e0c0d54e243175f7e7efc144b253d65f Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Wed, 29 Apr 2026 14:56:03 +0200 Subject: [PATCH] fix(dock_area): cli call load_profile has restore_baseline kwarg --- bec_widgets/cli/client.py | 16 ++++++-- .../widgets/containers/dock_area/dock_area.py | 12 +++++- tests/unit_tests/test_dock_area.py | 37 +++++++++++++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/bec_widgets/cli/client.py b/bec_widgets/cli/client.py index f54773b5..7ba3248b 100644 --- a/bec_widgets/cli/client.py +++ b/bec_widgets/cli/client.py @@ -358,15 +358,19 @@ class BECDockArea(RPCBase): @rpc_timeout(None) @rpc_call - def load_profile(self, name: "str | None" = None): + def load_profile(self, name: "str | None" = None, restore_baseline: "bool" = False): """ Load a workspace profile. Before switching, persist the current profile to the runtime copy. - Prefer loading the runtime copy; fall back to the baseline copy. + Prefer loading the runtime copy; fall back to the baseline copy. When + ``restore_baseline`` is True, first overwrite the runtime copy with the + baseline profile and then load it. Args: name (str | None): The name of the profile to load. If None, prompts the user. + restore_baseline (bool): If True, restore the runtime copy from the + baseline before loading. Defaults to False. """ @rpc_timeout(None) @@ -1378,15 +1382,19 @@ class DockAreaView(RPCBase): @rpc_timeout(None) @rpc_call - def load_profile(self, name: "str | None" = None): + def load_profile(self, name: "str | None" = None, restore_baseline: "bool" = False): """ Load a workspace profile. Before switching, persist the current profile to the runtime copy. - Prefer loading the runtime copy; fall back to the baseline copy. + Prefer loading the runtime copy; fall back to the baseline copy. When + ``restore_baseline`` is True, first overwrite the runtime copy with the + baseline profile and then load it. Args: name (str | None): The name of the profile to load. If None, prompts the user. + restore_baseline (bool): If True, restore the runtime copy from the + baseline before loading. Defaults to False. """ @rpc_timeout(None) diff --git a/bec_widgets/widgets/containers/dock_area/dock_area.py b/bec_widgets/widgets/containers/dock_area/dock_area.py index ef3865f9..36474438 100644 --- a/bec_widgets/widgets/containers/dock_area/dock_area.py +++ b/bec_widgets/widgets/containers/dock_area/dock_area.py @@ -820,16 +820,21 @@ class BECDockArea(DockAreaWidget): @SafeSlot() @SafeSlot(str) + @SafeSlot(str, bool) @rpc_timeout(None) - def load_profile(self, name: str | None = None): + def load_profile(self, name: str | None = None, restore_baseline: bool = False): """ Load a workspace profile. Before switching, persist the current profile to the runtime copy. - Prefer loading the runtime copy; fall back to the baseline copy. + Prefer loading the runtime copy; fall back to the baseline copy. When + ``restore_baseline`` is True, first overwrite the runtime copy with the + baseline profile and then load it. Args: name (str | None): The name of the profile to load. If None, prompts the user. + restore_baseline (bool): If True, restore the runtime copy from the + baseline before loading. Defaults to False. """ if name == "": return @@ -851,6 +856,9 @@ class BECDockArea(DockAreaWidget): us_prev = open_runtime_settings(prev_name, namespace=namespace) self._write_snapshot_to_settings(us_prev, save_preview=True) + if restore_baseline: + restore_runtime_from_baseline(name, namespace=namespace) + settings = None if any(os.path.exists(path) for path in runtime_profile_candidates(name, namespace)): settings = open_runtime_settings(name, namespace=namespace) diff --git a/tests/unit_tests/test_dock_area.py b/tests/unit_tests/test_dock_area.py index 78abd601..f0ecd7de 100644 --- a/tests/unit_tests/test_dock_area.py +++ b/tests/unit_tests/test_dock_area.py @@ -1887,6 +1887,43 @@ class TestWorkspaceProfileOperations: widget_map = advanced_dock_area.widget_map() assert "test_widget" in widget_map + def test_load_profile_default_does_not_restore_baseline(self, advanced_dock_area): + """Regular profile loading should not restore the runtime copy.""" + profile_name = "load_without_baseline_restore" + helper = profile_helper(advanced_dock_area) + helper.open_runtime(profile_name).sync() + + with patch( + "bec_widgets.widgets.containers.dock_area.dock_area.restore_runtime_from_baseline" + ) as mock_restore: + advanced_dock_area.load_profile(profile_name) + + mock_restore.assert_not_called() + assert advanced_dock_area._current_profile_name == profile_name + + def test_load_profile_restores_baseline_without_dialog(self, advanced_dock_area): + """CLI loading can restore the runtime copy from baseline without confirmation.""" + profile_name = "alignment_scan" + helper = profile_helper(advanced_dock_area) + helper.open_baseline(profile_name).sync() + helper.open_runtime(profile_name).sync() + + with ( + patch( + "bec_widgets.widgets.containers.dock_area.dock_area.RestoreProfileDialog.confirm" + ) as mock_confirm, + patch( + "bec_widgets.widgets.containers.dock_area.dock_area.restore_runtime_from_baseline" + ) as mock_restore, + ): + advanced_dock_area.load_profile(profile_name, restore_baseline=True) + + mock_confirm.assert_not_called() + mock_restore.assert_called_once_with( + profile_name, namespace=advanced_dock_area.profile_namespace + ) + assert advanced_dock_area._current_profile_name == profile_name + def test_load_profile_materializes_runtime_namespace_fallback(self, advanced_dock_area): """Loading a runtime fallback copies it into the active namespace before opening.""" profile_name = "load_runtime_namespace_fallback"