Debian Codegraph @

tests/unit_tests/test_chat_presenter.py

raw
1 # Unit tests for chat_presenter.py methods.
2 #
3 # Copyright 2025 Igalia, S.L.
4 # Author: Joanmarie Diggs <[email protected]>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
19 # Boston MA 02110-1301 USA.
20
21 # pylint: disable=wrong-import-position
22 # pylint: disable=import-outside-toplevel
23 # pylint: disable=protected-access
24
25 """Unit tests for chat_presenter.py methods."""
26
27 from __future__ import annotations
28
29 from typing import TYPE_CHECKING
30
31 import gi
32 import pytest
33
34 gi.require_version("Atspi", "2.0")
35 from gi.repository import Atspi
36
37 if TYPE_CHECKING:
38 from unittest.mock import Mock
39
40 from .orca_test_context import OrcaTestContext
41
42
43 @pytest.mark.unit
44 class TestConversation:
45 """Test Conversation class methods."""
46
47 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
48 """Returns dependencies for chat module testing."""
49
50 essential_modules = test_context.setup_shared_dependencies(
51 [
52 "orca.ax_utilities",
53 "orca.ax_text",
54 "orca.input_event_manager",
55 "orca.braille_presenter",
56 "orca.presentation_manager",
57 ],
58 )
59
60 debug_mock = essential_modules["orca.debug"]
61 debug_mock.print_message = test_context.Mock()
62 debug_mock.print_tokens = test_context.Mock()
63
64 return essential_modules
65
66 def test_init(self, test_context: OrcaTestContext) -> None:
67 """Test Conversation.__init__."""
68
69 self._setup_dependencies(test_context)
70 from orca.chat_presenter import Conversation
71
72 mock_log = test_context.Mock(spec=Atspi.Accessible)
73 conversation = Conversation("TestRoom", mock_log)
74
75 assert conversation.get_name() == "TestRoom"
76 assert conversation._log == mock_log
77 assert not conversation.has_messages()
78 assert conversation.get_message_count() == 0
79
80 def test_get_name(self, test_context: OrcaTestContext) -> None:
81 """Test Conversation.get_name returns the conversation name."""
82
83 self._setup_dependencies(test_context)
84 from orca.chat_presenter import Conversation
85
86 mock_log = test_context.Mock(spec=Atspi.Accessible)
87 conversation = Conversation("MyChannel", mock_log)
88
89 assert conversation.get_name() == "MyChannel"
90
91 def test_is_log(self, test_context: OrcaTestContext) -> None:
92 """Test Conversation.is_log identifies the log object."""
93
94 self._setup_dependencies(test_context)
95 from orca.chat_presenter import Conversation
96
97 mock_log = test_context.Mock(spec=Atspi.Accessible)
98 other_obj = test_context.Mock(spec=Atspi.Accessible)
99 conversation = Conversation("TestRoom", mock_log)
100
101 assert conversation.is_log(mock_log) is True
102 assert conversation.is_log(other_obj) is False
103
104 def test_add_and_get_message(self, test_context: OrcaTestContext) -> None:
105 """Test Conversation.add_message and get_message."""
106
107 self._setup_dependencies(test_context)
108 from orca.chat_presenter import Conversation
109
110 mock_log = test_context.Mock(spec=Atspi.Accessible)
111 conversation = Conversation("TestRoom", mock_log)
112
113 conversation.add_message("Hello")
114 conversation.add_message("World")
115
116 assert conversation.get_message(-1) == "World"
117 assert conversation.get_message(-2) == "Hello"
118
119 def test_has_messages_and_count(self, test_context: OrcaTestContext) -> None:
120 """Test Conversation.has_messages and get_message_count."""
121
122 self._setup_dependencies(test_context)
123 from orca.chat_presenter import Conversation
124
125 mock_log = test_context.Mock(spec=Atspi.Accessible)
126 conversation = Conversation("TestRoom", mock_log)
127
128 assert not conversation.has_messages()
129 assert conversation.get_message_count() == 0
130
131 conversation.add_message("Message 1")
132 assert conversation.has_messages()
133 assert conversation.get_message_count() == 1
134
135 conversation.add_message("Message 2")
136 assert conversation.get_message_count() == 2
137
138 def test_message_history_limit(self, test_context: OrcaTestContext) -> None:
139 """Test that Conversation respects HISTORY_SIZE limit."""
140
141 self._setup_dependencies(test_context)
142 from orca.chat_presenter import Conversation
143
144 mock_log = test_context.Mock(spec=Atspi.Accessible)
145 conversation = Conversation("TestRoom", mock_log)
146
147 # Add more messages than HISTORY_SIZE
148 for i in range(Conversation.HISTORY_SIZE + 3):
149 conversation.add_message(f"Message {i}")
150
151 assert conversation.get_message_count() == Conversation.HISTORY_SIZE
152 # Oldest messages should have been dropped
153 assert conversation.get_message(-1) == f"Message {Conversation.HISTORY_SIZE + 2}"
154
155 def test_typing_status(self, test_context: OrcaTestContext) -> None:
156 """Test Conversation typing status methods."""
157
158 self._setup_dependencies(test_context)
159 from orca.chat_presenter import Conversation
160
161 mock_log = test_context.Mock(spec=Atspi.Accessible)
162 conversation = Conversation("TestRoom", mock_log)
163
164 assert conversation.get_typing_status() == ""
165
166 conversation.set_typing_status("User is typing...")
167 assert conversation.get_typing_status() == "User is typing..."
168
169 def test_get_message_invalid_index(self, test_context: OrcaTestContext) -> None:
170 """Test Conversation.get_message raises IndexError for invalid index."""
171
172 self._setup_dependencies(test_context)
173 from orca.chat_presenter import Conversation
174
175 mock_log = test_context.Mock(spec=Atspi.Accessible)
176 conversation = Conversation("TestRoom", mock_log)
177
178 # Empty conversation should raise IndexError
179 with pytest.raises(IndexError):
180 conversation.get_message(-1)
181
182 # Add one message
183 conversation.add_message("Hello")
184
185 # Valid indices work
186 assert conversation.get_message(-1) == "Hello"
187
188 # Out of bounds still raises
189 with pytest.raises(IndexError):
190 conversation.get_message(-2)
191
192
193 @pytest.mark.unit
194 class TestConversationList:
195 """Test ConversationList class methods."""
196
197 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
198 """Returns dependencies for chat module testing."""
199
200 essential_modules = test_context.setup_shared_dependencies(
201 [
202 "orca.ax_utilities",
203 "orca.ax_text",
204 "orca.input_event_manager",
205 "orca.braille_presenter",
206 "orca.presentation_manager",
207 ],
208 )
209
210 return essential_modules
211
212 def test_init(self, test_context: OrcaTestContext) -> None:
213 """Test ConversationList.__init__."""
214
215 self._setup_dependencies(test_context)
216 from orca.chat_presenter import ConversationList
217
218 conv_list = ConversationList()
219
220 assert not conv_list.has_messages()
221 assert conv_list.get_message_count() == 0
222
223 def test_add_message_without_conversation(self, test_context: OrcaTestContext) -> None:
224 """Test ConversationList.add_message with no conversation."""
225
226 self._setup_dependencies(test_context)
227 from orca.chat_presenter import ConversationList
228
229 conv_list = ConversationList()
230 conv_list.add_message("Hello", None)
231
232 assert conv_list.has_messages()
233 assert conv_list.get_message_count() == 1
234
235 message, name, log = conv_list.get_message_and_name(-1)
236 assert message == "Hello"
237 assert name == ""
238 assert log is None
239
240 def test_add_message_with_conversation(self, test_context: OrcaTestContext) -> None:
241 """Test ConversationList.add_message with a conversation."""
242
243 self._setup_dependencies(test_context)
244 from orca.chat_presenter import Conversation, ConversationList
245
246 mock_log = test_context.Mock(spec=Atspi.Accessible)
247 conversation = Conversation("TestRoom", mock_log)
248
249 conv_list = ConversationList()
250 conv_list.add_message("Hello from room", conversation)
251
252 message, name, log = conv_list.get_message_and_name(-1)
253 assert message == "Hello from room"
254 assert name == "TestRoom"
255 assert log is mock_log
256
257 def test_iteration(self, test_context: OrcaTestContext) -> None:
258 """Test ConversationList iteration."""
259
260 self._setup_dependencies(test_context)
261 from orca.chat_presenter import Conversation, ConversationList
262
263 mock_log1 = test_context.Mock(spec=Atspi.Accessible)
264 mock_log2 = test_context.Mock(spec=Atspi.Accessible)
265 conv1 = Conversation("Room1", mock_log1)
266 conv2 = Conversation("Room2", mock_log2)
267
268 conv_list = ConversationList()
269 conv_list.add_message("Msg1", conv1)
270 conv_list.add_message("Msg2", conv2)
271
272 conversations = list(conv_list)
273 assert len(conversations) == 2
274 assert conv1 in conversations
275 assert conv2 in conversations
276
277 def test_iteration_empty(self, test_context: OrcaTestContext) -> None:
278 """Test ConversationList iteration when empty."""
279
280 self._setup_dependencies(test_context)
281 from orca.chat_presenter import ConversationList
282
283 conv_list = ConversationList()
284 conversations = list(conv_list)
285 assert len(conversations) == 0
286
287 def test_message_history_limit(self, test_context: OrcaTestContext) -> None:
288 """Test that ConversationList respects HISTORY_SIZE limit."""
289
290 self._setup_dependencies(test_context)
291 from orca.chat_presenter import Conversation, ConversationList
292
293 conv_list = ConversationList()
294
295 # Add more messages than HISTORY_SIZE
296 for i in range(Conversation.HISTORY_SIZE + 3):
297 conv_list.add_message(f"Message {i}", None)
298
299 assert conv_list.get_message_count() == Conversation.HISTORY_SIZE
300
301 def test_get_message_and_name_invalid_index(self, test_context: OrcaTestContext) -> None:
302 """Test ConversationList.get_message_and_name raises IndexError for invalid index."""
303
304 self._setup_dependencies(test_context)
305 from orca.chat_presenter import ConversationList
306
307 conv_list = ConversationList()
308
309 # Empty list should raise IndexError
310 with pytest.raises(IndexError):
311 conv_list.get_message_and_name(-1)
312
313 # Add one message
314 conv_list.add_message("Hello", None)
315
316 # Valid index works
317 message, _, _ = conv_list.get_message_and_name(-1)
318 assert message == "Hello"
319
320 # Out of bounds still raises
321 with pytest.raises(IndexError):
322 conv_list.get_message_and_name(-2)
323
324
325 @pytest.mark.unit
326 class TestChat:
327 """Test Chat class methods."""
328
329 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
330 """Returns dependencies for Chat class testing."""
331
332 essential_modules = test_context.setup_shared_dependencies(
333 [
334 "orca.ax_utilities",
335 "orca.ax_text",
336 "orca.input_event_manager",
337 "orca.braille_presenter",
338 "orca.presentation_manager",
339 ],
340 )
341
342 debug_mock = essential_modules["orca.debug"]
343 debug_mock.print_message = test_context.Mock()
344 debug_mock.print_tokens = test_context.Mock()
345
346 cmdnames_mock = essential_modules["orca.cmdnames"]
347 cmdnames_mock.CHAT_TOGGLE_ROOM_NAME_PREFIX = "Toggle room name prefix"
348 cmdnames_mock.CHAT_TOGGLE_BUDDY_TYPING = "Toggle buddy typing"
349 cmdnames_mock.CHAT_TOGGLE_MESSAGE_HISTORIES = "Toggle message histories"
350 cmdnames_mock.CHAT_PREVIOUS_MESSAGE = "Previous message"
351 cmdnames_mock.CHAT_NEXT_MESSAGE = "Next message"
352
353 messages_mock = essential_modules["orca.messages"]
354 messages_mock.CHAT_NO_MESSAGES = "No chat messages"
355 messages_mock.CHAT_LIST_TOP = "Top"
356 messages_mock.CHAT_LIST_BOTTOM = "Bottom"
357 messages_mock.CHAT_ROOM_NAME_PREFIX_ON = "Room name prefix on"
358 messages_mock.CHAT_ROOM_NAME_PREFIX_OFF = "Room name prefix off"
359 messages_mock.CHAT_BUDDY_TYPING_ON = "Buddy typing on"
360 messages_mock.CHAT_BUDDY_TYPING_OFF = "Buddy typing off"
361 messages_mock.CHAT_SEPARATE_HISTORIES_ON = "Separate histories on"
362 messages_mock.CHAT_SEPARATE_HISTORIES_OFF = "Separate histories off"
363
364 input_event_mock = essential_modules["orca.input_event"]
365 input_event_handler_mock = test_context.Mock()
366 input_event_mock.InputEventHandler = test_context.Mock(
367 return_value=input_event_handler_mock,
368 )
369
370 keybindings_mock = essential_modules["orca.keybindings"]
371 keybindings_mock.NO_MODIFIER_MASK = 0
372 keybindings_mock.ORCA_MODIFIER_MASK = 4
373
374 from orca import gsettings_registry
375
376 registry = gsettings_registry.get_registry()
377 registry.clear_runtime_values()
378
379 return essential_modules
380
381 def test_init(self, test_context: OrcaTestContext) -> None:
382 """Test Chat.__init__."""
383
384 self._setup_dependencies(test_context)
385 from orca.chat_presenter import Chat
386
387 mock_script = test_context.Mock()
388 chat = Chat(mock_script)
389
390 assert chat._script == mock_script
391
392 def test_get_conversation_for_object_name_mismatch(self, test_context: OrcaTestContext) -> None:
393 """Test get_conversation_for_object when name doesn't match log's room."""
394
395 self._setup_dependencies(test_context)
396 from orca.chat_presenter import Chat, Conversation
397
398 mock_script = test_context.Mock()
399 chat = Chat(mock_script)
400
401 mock_log1 = test_context.Mock(spec=Atspi.Accessible)
402 conversation1 = Conversation("Room1", mock_log1)
403 chat._conversation_list.add_message("Hello", conversation1)
404
405 test_context.patch_object(chat, "get_chat_room_name", return_value="Room2")
406
407 result = chat.get_conversation_for_object(mock_log1)
408
409 assert result is None
410
411
412 @pytest.mark.unit
413 class TestChatPresenter:
414 """Test ChatPresenter class methods."""
415
416 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
417 """Returns dependencies for ChatPresenter class testing."""
418
419 essential_modules = test_context.setup_shared_dependencies(
420 [
421 "orca.ax_utilities",
422 "orca.ax_text",
423 "orca.input_event_manager",
424 "orca.braille_presenter",
425 "orca.presentation_manager",
426 ],
427 )
428
429 debug_mock = essential_modules["orca.debug"]
430 debug_mock.print_message = test_context.Mock()
431 debug_mock.print_tokens = test_context.Mock()
432
433 cmdnames_mock = essential_modules["orca.cmdnames"]
434 cmdnames_mock.CHAT_TOGGLE_ROOM_NAME_PREFIX = "Toggle room name prefix"
435 cmdnames_mock.CHAT_TOGGLE_BUDDY_TYPING = "Toggle buddy typing"
436 cmdnames_mock.CHAT_TOGGLE_MESSAGE_HISTORIES = "Toggle message histories"
437 cmdnames_mock.CHAT_PREVIOUS_MESSAGE = "Previous message"
438 cmdnames_mock.CHAT_NEXT_MESSAGE = "Next message"
439
440 messages_mock = essential_modules["orca.messages"]
441 messages_mock.CHAT_ROOM_NAME_PREFIX_ON = "Room name prefix on"
442 messages_mock.CHAT_ROOM_NAME_PREFIX_OFF = "Room name prefix off"
443 messages_mock.CHAT_BUDDY_TYPING_ON = "Buddy typing on"
444 messages_mock.CHAT_BUDDY_TYPING_OFF = "Buddy typing off"
445 messages_mock.CHAT_SEPARATE_HISTORIES_ON = "Separate histories on"
446 messages_mock.CHAT_SEPARATE_HISTORIES_OFF = "Separate histories off"
447 messages_mock.CHAT_NO_MESSAGES = "No messages"
448 messages_mock.CHAT_LIST_TOP = "Top of chat"
449 messages_mock.CHAT_LIST_BOTTOM = "Bottom of chat"
450 messages_mock.CHAT_MESSAGE_FROM_ROOM = "Message from %s"
451
452 input_event_mock = essential_modules["orca.input_event"]
453 input_event_handler_mock = test_context.Mock()
454 input_event_mock.InputEventHandler = test_context.Mock(
455 return_value=input_event_handler_mock,
456 )
457
458 keybindings_mock = essential_modules["orca.keybindings"]
459 keybindings_mock.NO_MODIFIER_MASK = 0
460 keybindings_mock.ORCA_MODIFIER_MASK = 4
461 keybindings_mock.DEFAULT_MODIFIER_MASK = 8
462
463 from orca import gsettings_registry
464
465 registry = gsettings_registry.get_registry()
466 registry.clear_runtime_values()
467
468 return essential_modules
469
470 def test_get_handlers(self, test_context: OrcaTestContext) -> None:
471 """Test ChatPresenter registers commands with CommandManager."""
472
473 self._setup_dependencies(test_context)
474 from orca import command_manager
475 from orca.chat_presenter import get_presenter
476
477 presenter = get_presenter()
478 presenter.set_up_commands()
479 cmd_manager = command_manager.get_manager()
480
481 assert cmd_manager.get_keyboard_command("chat_previous_message") is not None
482 assert cmd_manager.get_keyboard_command("chat_next_message") is not None
483 assert cmd_manager.get_keyboard_command("chat_toggle_room_name_prefix") is not None
484 assert cmd_manager.get_keyboard_command("chat_toggle_buddy_typing") is not None
485 assert cmd_manager.get_keyboard_command("chat_toggle_message_histories") is not None
486
487 def test_toggle_prefix_on_to_off(self, test_context: OrcaTestContext) -> None:
488 """Test toggle_prefix from on to off."""
489
490 essential_modules = self._setup_dependencies(test_context)
491 from orca.chat_presenter import get_presenter
492
493 mock_script = test_context.Mock()
494
495 presenter = get_presenter()
496 presenter.set_speak_room_name(True)
497 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
498 pres_manager.present_message.reset_mock()
499 result = presenter.toggle_prefix(mock_script, None)
500
501 assert result is True
502 assert presenter.get_speak_room_name() is False
503 pres_manager.present_message.assert_called_with(
504 essential_modules["orca.messages"].CHAT_ROOM_NAME_PREFIX_OFF,
505 )
506
507 def test_toggle_prefix_off_to_on(self, test_context: OrcaTestContext) -> None:
508 """Test toggle_prefix from off to on."""
509
510 essential_modules = self._setup_dependencies(test_context)
511 from orca.chat_presenter import get_presenter
512
513 mock_script = test_context.Mock()
514
515 presenter = get_presenter()
516 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
517 pres_manager.present_message.reset_mock()
518 result = presenter.toggle_prefix(mock_script, None)
519
520 assert result is True
521 assert presenter.get_speak_room_name() is True
522 pres_manager.present_message.assert_called_with(
523 essential_modules["orca.messages"].CHAT_ROOM_NAME_PREFIX_ON,
524 )
525
526 def test_toggle_buddy_typing_on_to_off(self, test_context: OrcaTestContext) -> None:
527 """Test toggle_buddy_typing from on to off."""
528
529 essential_modules = self._setup_dependencies(test_context)
530 from orca.chat_presenter import get_presenter
531
532 mock_script = test_context.Mock()
533
534 presenter = get_presenter()
535 presenter.set_announce_buddy_typing(True)
536 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
537 pres_manager.present_message.reset_mock()
538 result = presenter.toggle_buddy_typing(mock_script, None)
539
540 assert result is True
541 assert presenter.get_announce_buddy_typing() is False
542 pres_manager.present_message.assert_called_with(
543 essential_modules["orca.messages"].CHAT_BUDDY_TYPING_OFF,
544 )
545
546 def test_toggle_buddy_typing_off_to_on(self, test_context: OrcaTestContext) -> None:
547 """Test toggle_buddy_typing from off to on."""
548
549 essential_modules = self._setup_dependencies(test_context)
550 from orca.chat_presenter import get_presenter
551
552 mock_script = test_context.Mock()
553
554 presenter = get_presenter()
555 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
556 pres_manager.present_message.reset_mock()
557 result = presenter.toggle_buddy_typing(mock_script, None)
558
559 assert result is True
560 assert presenter.get_announce_buddy_typing() is True
561 pres_manager.present_message.assert_called_with(
562 essential_modules["orca.messages"].CHAT_BUDDY_TYPING_ON,
563 )
564
565 def test_toggle_message_histories_on_to_off(self, test_context: OrcaTestContext) -> None:
566 """Test toggle_message_histories from on to off."""
567
568 essential_modules = self._setup_dependencies(test_context)
569 from orca.chat_presenter import get_presenter
570
571 mock_script = test_context.Mock()
572
573 presenter = get_presenter()
574 presenter.set_room_histories(True)
575 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
576 pres_manager.present_message.reset_mock()
577 result = presenter.toggle_message_histories(mock_script, None)
578
579 assert result is True
580 assert presenter.get_room_histories() is False
581 pres_manager.present_message.assert_called_with(
582 essential_modules["orca.messages"].CHAT_SEPARATE_HISTORIES_OFF,
583 )
584
585 def test_toggle_message_histories_off_to_on(self, test_context: OrcaTestContext) -> None:
586 """Test toggle_message_histories from off to on."""
587
588 essential_modules = self._setup_dependencies(test_context)
589 from orca.chat_presenter import get_presenter
590
591 mock_script = test_context.Mock()
592
593 presenter = get_presenter()
594 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
595 pres_manager.present_message.reset_mock()
596 result = presenter.toggle_message_histories(mock_script, None)
597
598 assert result is True
599 assert presenter.get_room_histories() is True
600 pres_manager.present_message.assert_called_with(
601 essential_modules["orca.messages"].CHAT_SEPARATE_HISTORIES_ON,
602 )
603
604 def _setup_navigation_mocks(
605 self,
606 test_context: OrcaTestContext,
607 essential_modules: dict[str, Mock],
608 ) -> Mock:
609 """Setup additional mocks needed for navigation tests. Returns mock_script."""
610
611 script_manager_mock = essential_modules["orca.script_manager"]
612 script_manager_instance = script_manager_mock.get_manager.return_value
613 script_manager_instance.get_active_script = test_context.Mock(return_value=None)
614
615 mock_script = test_context.Mock()
616 speech_gen = test_context.Mock()
617 speech_gen.voice = test_context.Mock(return_value=None)
618 mock_script.get_speech_generator = test_context.Mock(return_value=speech_gen)
619 mock_script.speak_message = test_context.Mock()
620 mock_script.display_message = test_context.Mock()
621 mock_script.app = test_context.Mock()
622
623 return mock_script
624
625 def test_present_previous_no_messages(self, test_context: OrcaTestContext) -> None:
626 """Test present_previous_message with no messages."""
627
628 essential_modules = self._setup_dependencies(test_context)
629 from orca.chat_presenter import Chat, get_presenter
630
631 mock_script = test_context.Mock()
632 chat = Chat(mock_script)
633 mock_script.chat = chat
634
635 presenter = get_presenter()
636 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
637 pres_manager.present_message.reset_mock()
638 result = presenter.present_previous_message(mock_script, None)
639
640 assert result is True
641 pres_manager.present_message.assert_called_with(
642 essential_modules["orca.messages"].CHAT_NO_MESSAGES,
643 )
644
645 def test_present_next_no_messages(self, test_context: OrcaTestContext) -> None:
646 """Test present_next_message with no messages."""
647
648 essential_modules = self._setup_dependencies(test_context)
649 from orca.chat_presenter import Chat, get_presenter
650
651 mock_script = test_context.Mock()
652 chat = Chat(mock_script)
653 mock_script.chat = chat
654
655 presenter = get_presenter()
656 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
657 pres_manager.present_message.reset_mock()
658 result = presenter.present_next_message(mock_script, None)
659
660 assert result is True
661 pres_manager.present_message.assert_called_with(
662 essential_modules["orca.messages"].CHAT_NO_MESSAGES,
663 )
664
665 def test_navigation_with_messages(self, test_context: OrcaTestContext) -> None:
666 """Test navigation through message history."""
667
668 essential_modules = self._setup_dependencies(test_context)
669 from orca.chat_presenter import Chat, Conversation, get_presenter
670
671 mock_script = self._setup_navigation_mocks(test_context, essential_modules)
672 chat = Chat(mock_script)
673 mock_script.chat = chat
674
675 mock_log = test_context.Mock(spec=Atspi.Accessible)
676 conversation = Conversation("TestRoom", mock_log)
677 chat._conversation_list.add_message("Message 1", conversation)
678 chat._conversation_list.add_message("Message 2", conversation)
679 chat._conversation_list.add_message("Message 3", conversation)
680
681 presenter = get_presenter()
682 presenter.present_previous_message(mock_script, None)
683 assert chat._current_index == -1
684
685 presenter.present_previous_message(mock_script, None)
686 assert chat._current_index == -2
687
688 presenter.present_next_message(mock_script, None)
689 assert chat._current_index == -1
690
691 def test_navigation_top_boundary(self, test_context: OrcaTestContext) -> None:
692 """Test navigation stops at top boundary."""
693
694 essential_modules = self._setup_dependencies(test_context)
695 from orca.chat_presenter import Chat, Conversation, get_presenter
696
697 mock_script = self._setup_navigation_mocks(test_context, essential_modules)
698 chat = Chat(mock_script)
699 mock_script.chat = chat
700
701 mock_log = test_context.Mock(spec=Atspi.Accessible)
702 conversation = Conversation("TestRoom", mock_log)
703 chat._conversation_list.add_message("Message 1", conversation)
704 chat._conversation_list.add_message("Message 2", conversation)
705
706 presenter = get_presenter()
707 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
708 presenter.present_previous_message(mock_script, None)
709 presenter.present_previous_message(mock_script, None)
710
711 pres_manager.present_message.reset_mock()
712 presenter.present_previous_message(mock_script, None)
713
714 pres_manager.present_message.assert_called_with(
715 essential_modules["orca.messages"].CHAT_LIST_TOP,
716 )
717
718 def test_navigation_bottom_boundary(self, test_context: OrcaTestContext) -> None:
719 """Test navigation stops at bottom boundary."""
720
721 essential_modules = self._setup_dependencies(test_context)
722 from orca.chat_presenter import Chat, Conversation, get_presenter
723
724 mock_script = self._setup_navigation_mocks(test_context, essential_modules)
725 chat = Chat(mock_script)
726 mock_script.chat = chat
727
728 mock_log = test_context.Mock(spec=Atspi.Accessible)
729 conversation = Conversation("TestRoom", mock_log)
730 chat._conversation_list.add_message("Message 1", conversation)
731 chat._conversation_list.add_message("Message 2", conversation)
732
733 presenter = get_presenter()
734 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
735 presenter.present_previous_message(mock_script, None)
736
737 pres_manager.present_message.reset_mock()
738 presenter.present_next_message(mock_script, None)
739
740 pres_manager.present_message.assert_called_with(
741 essential_modules["orca.messages"].CHAT_LIST_BOTTOM,
742 )
743
744 def _setup_verbosity_test(
745 self,
746 test_context: OrcaTestContext,
747 verbosity: int,
748 ) -> tuple[dict[str, Mock], Mock]:
749 """Sets up mocks for utter_message verbosity tests. Returns (modules, script)."""
750
751 essential_modules = self._setup_dependencies(test_context)
752 mock_script = self._setup_navigation_mocks(test_context, essential_modules)
753
754 nick_map = {0: "all", 1: "all-if-focused", 2: "focused-channel", 3: "active-channel"}
755 from orca import gsettings_registry
756
757 gsettings_registry.get_registry().set_runtime_value(
758 "chat",
759 "message-verbosity",
760 nick_map[verbosity],
761 )
762 return essential_modules, mock_script
763
764 def test_utter_message_all_speaks_regardless(self, test_context: OrcaTestContext) -> None:
765 """Test that ALL verbosity speaks regardless of focus or active channel."""
766
767 essential_modules, mock_script = self._setup_verbosity_test(test_context, 0)
768 from orca.chat_presenter import get_presenter
769
770 presenter = get_presenter()
771 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
772 pres_manager.speak_accessible_text.reset_mock()
773 mock_obj = test_context.Mock()
774 presenter.utter_message(
775 mock_script,
776 mock_obj,
777 "Room",
778 "Hello",
779 focused=False,
780 active_channel=False,
781 )
782
783 pres_manager.speak_accessible_text.assert_called_once()
784
785 def test_utter_message_active_channel_speaks_when_active(
786 self,
787 test_context: OrcaTestContext,
788 ) -> None:
789 """Test that ACTIVE_CHANNEL verbosity speaks when active_channel=True."""
790
791 essential_modules, mock_script = self._setup_verbosity_test(test_context, 3)
792 from orca.chat_presenter import get_presenter
793
794 presenter = get_presenter()
795 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
796 pres_manager.speak_accessible_text.reset_mock()
797 mock_obj = test_context.Mock()
798 presenter.utter_message(
799 mock_script,
800 mock_obj,
801 "Room",
802 "Hello",
803 focused=False,
804 active_channel=True,
805 )
806
807 pres_manager.speak_accessible_text.assert_called_once()
808
809 def test_utter_message_active_channel_silent_when_inactive(
810 self,
811 test_context: OrcaTestContext,
812 ) -> None:
813 """Test that ACTIVE_CHANNEL verbosity is silent when active_channel=False."""
814
815 essential_modules, mock_script = self._setup_verbosity_test(test_context, 3)
816 from orca.chat_presenter import get_presenter
817
818 presenter = get_presenter()
819 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
820 pres_manager.speak_accessible_text.reset_mock()
821 mock_obj = test_context.Mock()
822 presenter.utter_message(
823 mock_script,
824 mock_obj,
825 "Room",
826 "Hello",
827 focused=False,
828 active_channel=False,
829 )
830
831 pres_manager.speak_accessible_text.assert_not_called()
832
833 def test_utter_message_focused_channel_silent_when_unfocused(
834 self,
835 test_context: OrcaTestContext,
836 ) -> None:
837 """Test that FOCUSED_CHANNEL is silent when unfocused even if active_channel=True."""
838
839 essential_modules, mock_script = self._setup_verbosity_test(test_context, 2)
840 from orca.chat_presenter import get_presenter
841
842 presenter = get_presenter()
843 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
844 pres_manager.speak_accessible_text.reset_mock()
845 mock_obj = test_context.Mock()
846 presenter.utter_message(
847 mock_script,
848 mock_obj,
849 "Room",
850 "Hello",
851 focused=False,
852 active_channel=True,
853 )
854
855 pres_manager.speak_accessible_text.assert_not_called()
856
857 def test_utter_message_focused_channel_speaks_when_focused(
858 self,
859 test_context: OrcaTestContext,
860 ) -> None:
861 """Test that FOCUSED_CHANNEL speaks when focused=True."""
862
863 essential_modules, mock_script = self._setup_verbosity_test(test_context, 2)
864 from orca.chat_presenter import get_presenter
865
866 presenter = get_presenter()
867 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
868 pres_manager.speak_accessible_text.reset_mock()
869 mock_obj = test_context.Mock()
870 presenter.utter_message(
871 mock_script,
872 mock_obj,
873 "",
874 "Hello",
875 focused=True,
876 active_channel=True,
877 )
878
879 pres_manager.speak_accessible_text.assert_called_once()