From 08eccc499e57851441e843c63437d8348a9ff32f Mon Sep 17 00:00:00 2001 From: Niloth P <20315308+Niloth-p@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:41:44 +0530 Subject: [PATCH] core/boxes: Flash footer message on search misses, without narrowing. --- tests/core/test_core.py | 58 +++++++++++++++++++++++++++++++-- zulipterminal/core.py | 5 ++- zulipterminal/ui_tools/boxes.py | 8 +++-- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/tests/core/test_core.py b/tests/core/test_core.py index ff6701a41d..5bc1373692 100644 --- a/tests/core/test_core.py +++ b/tests/core/test_core.py @@ -518,8 +518,8 @@ def test_stream_muting_confirmation_popup( "search_within_topic_narrow", ], ) - @pytest.mark.parametrize("msg_ids", [({200, 300, 400}), (set()), ({100})]) - def test_search_message( + @pytest.mark.parametrize("msg_ids", [({200, 300, 400}), ({100})]) + def test_search_message__hits( self, initial_narrow: List[Any], final_narrow: List[Any], @@ -550,6 +550,60 @@ def set_msg_ids(*args: Any, **kwargs: Any) -> None: create_msg.assert_called_once_with(controller.model, msg_ids) assert controller.model.index == dict(index_search_messages, search=msg_ids) + @pytest.mark.parametrize( + "initial_narrow, final_narrow", + [ + ([], [["search", "FOO"]]), + ([["search", "BOO"]], [["search", "FOO"]]), + ([["stream", "PTEST"]], [["stream", "PTEST"], ["search", "FOO"]]), + ( + [["pm-with", "foo@zulip.com"], ["search", "BOO"]], + [["pm-with", "foo@zulip.com"], ["search", "FOO"]], + ), + ( + [["stream", "PTEST"], ["topic", "RDS"]], + [["stream", "PTEST"], ["topic", "RDS"], ["search", "FOO"]], + ), + ], + ids=[ + "Default_all_msg_search", + "redo_default_search", + "search_within_stream", + "pm_search_again", + "search_within_topic_narrow", + ], + ) + def test_search_message__no_hits( + self, + initial_narrow: List[Any], + final_narrow: List[Any], + controller: Controller, + mocker: MockerFixture, + index_search_messages: Index, + msg_ids: Set[int] = set(), + ) -> None: + get_message = mocker.patch(MODEL + ".get_messages") + create_msg = mocker.patch(MODULE + ".create_msg_box_list") + mocker.patch(MODEL + ".get_message_ids_in_current_narrow", return_value=msg_ids) + controller.model.index = index_search_messages # Any initial search index + controller.view.message_view = mocker.patch("urwid.ListBox") + controller.model.narrow = initial_narrow + + def set_msg_ids(*args: Any, **kwargs: Any) -> None: + controller.model.index["search"].update(msg_ids) + + get_message.side_effect = set_msg_ids + assert controller.model.index["search"] == {500} + + controller.search_messages("FOO") + + assert controller.model.narrow == final_narrow + get_message.assert_called_once_with( + num_after=0, num_before=30, anchor=10000000000 + ) + create_msg.assert_not_called() + assert controller.model.index == dict(index_search_messages, search=msg_ids) + @pytest.mark.parametrize( "screen_size, expected_popup_size", [ diff --git a/zulipterminal/core.py b/zulipterminal/core.py index 712feddcde..6972a80be6 100644 --- a/zulipterminal/core.py +++ b/zulipterminal/core.py @@ -504,13 +504,15 @@ def show_media_confirmation_popup( self, question, callback, location="center" ) - def search_messages(self, text: str) -> None: + def search_messages(self, text: str) -> bool: # Search for a text in messages self.model.index["search"].clear() self.model.set_search_narrow(text) self.model.get_messages(num_after=0, num_before=30, anchor=10000000000) msg_id_list = self.model.get_message_ids_in_current_narrow() + if len(msg_id_list) == 0: + return False w_list = create_msg_box_list(self.model, msg_id_list) self.view.message_view.log.clear() @@ -518,6 +520,7 @@ def search_messages(self, text: str) -> None: focus_position = 0 if 0 <= focus_position < len(w_list): self.view.message_view.set_focus(focus_position) + return True def save_draft_confirmation_popup(self, draft: Composition) -> None: question = urwid.Text( diff --git a/zulipterminal/ui_tools/boxes.py b/zulipterminal/ui_tools/boxes.py index 64a1cae9a1..c1803ef67e 100644 --- a/zulipterminal/ui_tools/boxes.py +++ b/zulipterminal/ui_tools/boxes.py @@ -1032,9 +1032,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]: return key elif is_command_key("EXECUTE_SEARCH", key): - self.controller.exit_editor_mode() - self.controller.search_messages(self.text_box.edit_text) - self.controller.view.middle_column.set_focus("body") + if self.controller.search_messages(self.text_box.edit_text): + self.controller.exit_editor_mode() + self.controller.view.middle_column.set_focus("body") + else: + self.controller.report_error(["No results found."]) return key key = super().keypress(size, key)