diff --git a/tests/test_command_palette_sim.py b/tests/test_command_palette_sim.py index 3f0efc80..4df98a22 100644 --- a/tests/test_command_palette_sim.py +++ b/tests/test_command_palette_sim.py @@ -24,15 +24,16 @@ def test_palette_starts_hidden(live_gui: Any) -> None: # palette IS closable via the callback, not that it happens to be # closed at this moment. Resetting here makes the assertion meaningful # without depending on test ordering. - client.push_event("custom_callback", { - "callback": "_toggle_command_palette", - "args": [], - }) - deadline = time.time() + 2.0 - while time.time() < deadline: - if client.get_value("show_command_palette") is False: - break - time.sleep(0.05) + # Two toggles from default-closed state = closed. Two toggles from + # open state = open. So we MUST check state and toggle only if open. + if client.get_value("show_command_palette") is True: + client.push_event("custom_callback", { + "callback": "_toggle_command_palette", + "args": [], + }) + deadline = time.time() + 2.0 + while client.get_value("show_command_palette") is not False and time.time() < deadline: + time.sleep(0.05) state = client.get_value("show_command_palette") assert state is not None, "show_command_palette should be a gettable field" assert state is False, f"Palette should be closable, got {state}" @@ -41,6 +42,14 @@ def test_palette_starts_hidden(live_gui: Any) -> None: def test_palette_toggles_via_callback(live_gui: Any) -> None: """The _toggle_command_palette callback should open and close the palette.""" client = ApiHookClient() + # Force-close palette first (live_gui is session-scoped; prior tests may leave it open). + # Two toggles from default-closed state = closed. Two toggles from open state = open. + # So we MUST check state and toggle only if open. Then poll to verify closure. + if client.get_value("show_command_palette") is True: + client.push_event("custom_callback", {"callback": "_toggle_command_palette", "args": []}) + deadline = time.time() + 2.0 + while client.get_value("show_command_palette") is not False and time.time() < deadline: + time.sleep(0.05) assert client.get_value("show_command_palette") is False # Open via custom callback @@ -86,6 +95,14 @@ def test_palette_query_state_resets_on_open(live_gui: Any) -> None: This ensures a fresh state every time the user opens the palette. """ client = ApiHookClient() + # Force-close palette first (live_gui is session-scoped; prior tests may leave it open). + # Two toggles from default-closed state = closed. Two toggles from open state = open. + # So we MUST check state and toggle only if open. Then poll to verify closure. + if client.get_value("show_command_palette") is True: + client.push_event("custom_callback", {"callback": "_toggle_command_palette", "args": []}) + deadline = time.time() + 2.0 + while client.get_value("show_command_palette") is not False and time.time() < deadline: + time.sleep(0.05) # Open once, set some state (simulate user typing) client.push_event("custom_callback", {