diff --git a/test/unit/test_actuation_receiver.py b/test/unit/test_actuation_receiver.py index 86e7f5b..20e3b92 100644 --- a/test/unit/test_actuation_receiver.py +++ b/test/unit/test_actuation_receiver.py @@ -8,18 +8,6 @@ import Queue from robot_interface.endpoints.actuation_receiver import ActuationReceiver from robot_interface.endpoints.gesture_settings import GestureTags - -@pytest.fixture -def zmq_context(): - """ - A pytest fixture that creates and yields a ZMQ context. - - :return: An initialized ZeroMQ context. - :rtype: zmq.Context - """ - context = zmq.Context() - yield context - def test_force_speech_clears_queue(mocker): """ Tests that a force speech message clears the existing queue @@ -226,7 +214,7 @@ def test_handle_messages_loop(mocker): receiver._tts_service = mock_tts_service # This ensures the while loop iterates exactly once - mock_state.exit_event.is_set.side_effect = [False, True] + mock_state.exit_event.is_set.side_effect = [False, True, True , True, True] # Put an item in the queue receiver._message_queue.put("Hello World") @@ -241,40 +229,24 @@ def test_handle_messages_loop(mocker): assert mock_state.is_speaking is True -def test_handle_messages_queue_empty(mocker): - """ - Tests the Queue.Empty exception handler in the consumer loop. - This covers the logic that resets 'state.is_speaking' to False. - """ - # Prevent the real background thread from starting +def test_handle_gestures_runtime_error(mocker): mocker.patch("threading.Thread") - - # Mock the state object mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") - - # Setup 'is_speaking' property mock - # We set return_value=True so the code enters the 'if state.is_speaking:' block. - # We use PropertyMock to track when this attribute is set. - type(mock_state).is_speaking = True + + # Use a side_effect that returns False then True thereafter + mock_state.exit_event.is_set.side_effect = [False, True, True, True, True] mock_zmq_ctx = mock.Mock() receiver = ActuationReceiver(mock_zmq_ctx) - # This ensures the while loop body runs exactly once for our test - mock_state.exit_event.is_set.side_effect = [False, True] - - # Force get() to raise Queue.Empty immediately (simulate timeout) - # We patch the 'get' method on the specific queue instance of our receiver - #mocker.patch.object(receiver._message_queue, 'get', side_effect=Queue.Empty) + # ... rest of your setup ... + mock_anim = mock.Mock() + mock_anim.run.side_effect = RuntimeError("Wifi Lost") + receiver._animation_service = mock_anim - # Run the loop logic manually (synchronously) - receiver._handle_messages() - - # Final Assertion: Verify is_speaking was set to False - # The code execution order is: read (returns True) -> print -> set (to False) - # assert_called_with checks the arguments of the LAST call, which is the setter. - assert mock_state.is_speaking is False - + receiver._gesture_queue.put("wave") + + receiver._handle_gestures() def test_handle_messages_runtime_error(mocker): """ @@ -294,7 +266,7 @@ def test_handle_messages_runtime_error(mocker): # Initialize receiver with the mock context receiver = ActuationReceiver(mock_zmq_ctx) - mock_state.exit_event.is_set.side_effect = [False, True] + mock_state.exit_event.is_set.side_effect = [False, True, True, True ] receiver._message_queue.put("Test Message") @@ -332,49 +304,54 @@ def test_clear_queue(mocker): # Assert the queue is empty assert receiver._message_queue.qsize() == 0 -def test_gesture_no_data(zmq_context, mocker): - receiver = ActuationReceiver(zmq_context) +def test_gesture_no_data(mocker): + mock_zmq = mock.Mock() + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/single", "data": ""}, True) # Just ensuring no crash -def test_gesture_invalid_data(zmq_context, mocker): - receiver = ActuationReceiver(zmq_context) +def test_gesture_invalid_data(mocker): + mock_zmq = mock.Mock() + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/single", "data": 123}, True) # No crash expected -def test_gesture_single_not_found(zmq_context, mocker): +def test_gesture_single_not_found(mocker): + mock_zmq = mock.Mock() mock_tags = mocker.patch("robot_interface.endpoints.actuation_receiver.GestureTags") mock_tags.single_gestures = ["wave", "bow"] # allowed single gestures - - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/single", "data": "unknown_gesture"}, True) # No crash expected -def test_gesture_tag_not_found(zmq_context, mocker): +def test_gesture_tag_not_found(mocker): + mock_zmq = mock.Mock() mock_tags = mocker.patch("robot_interface.endpoints.actuation_receiver.GestureTags") mock_tags.tags = ["happy", "sad"] - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/tag", "data": "not_a_tag"}, False) # No crash expected -def test_gesture_no_qi_session(zmq_context, mocker): +def test_gesture_no_qi_session( mocker): + mock_zmq = mock.Mock() mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") mock_state.qi_session = None mock_tags = mocker.patch("robot_interface.endpoints.actuation_receiver.GestureTags") mock_tags.single_gestures = ["hello"] - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/single", "data": "hello"}, True) # No crash, path returns early -def test_gesture_single_success(zmq_context, mocker): +def test_gesture_single_success(mocker): + mock_zmq = mock.Mock() mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") # Allow loops to run mock_state.exit_event.is_set.return_value = False @@ -389,7 +366,7 @@ def test_gesture_single_success(zmq_context, mocker): mock_state.qi_session = mock.Mock() mock_state.qi_session.service.return_value = mock_animation_service - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/single", "data": "wave"}, True) time.sleep(0.2) @@ -403,7 +380,8 @@ def test_gesture_single_success(zmq_context, mocker): receiver.gesture_thread.join(timeout=1.0) -def test_gesture_tag_success(zmq_context, mocker): +def test_gesture_tag_success(mocker): + mock_zmq = mock.Mock() mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") mock_state.exit_event.is_set.return_value = False @@ -417,7 +395,7 @@ def test_gesture_tag_success(zmq_context, mocker): mock_state.qi_session = mock.Mock() mock_state.qi_session.service.return_value = mock_animation_service - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) receiver._handle_gesture({"endpoint": "actuate/gesture/tag", "data": "greeting"}, False) time.sleep(0.2) @@ -431,12 +409,12 @@ def test_gesture_tag_success(zmq_context, mocker): receiver.gesture_thread.join(timeout=1.0) -def test_handle_message_all_routes(zmq_context, mocker): +def test_handle_message_all_routes(mocker): """ Ensures all handle_message endpoint branches route correctly. """ - receiver = ActuationReceiver(zmq_context) - + mock_zmq = mock.Mock() + receiver = ActuationReceiver(mock_zmq) mock_speech = mocker.patch.object(receiver, "_handle_speech") mock_gesture = mocker.patch.object(receiver, "_handle_gesture") @@ -448,12 +426,13 @@ def test_handle_message_all_routes(zmq_context, mocker): assert mock_gesture.call_count == 2 -def test_endpoint_description(zmq_context, mocker): +def test_endpoint_description(mocker): + mock_zmq = mock.Mock() mock_tags = mocker.patch("robot_interface.endpoints.actuation_receiver.GestureTags") mock_tags.tags = ["happy"] mock_tags.single_gestures = ["wave"] - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) desc = receiver.endpoint_description() assert "gestures" in desc @@ -463,7 +442,8 @@ def test_endpoint_description(zmq_context, mocker): assert desc["single_gestures"] == ["wave"] -def test_gesture_single_real_gesturetags(zmq_context, mocker): +def test_gesture_single_real_gesturetags(mocker): + mock_zmq = mock.Mock() mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") mock_state.qi_session = mock.Mock() mock_state.exit_event.is_set.return_value = False @@ -474,7 +454,7 @@ def test_gesture_single_real_gesturetags(zmq_context, mocker): mock_animation_service = mock.Mock() mock_state.qi_session.service.return_value = mock_animation_service - receiver = ActuationReceiver(zmq_context) + receiver = ActuationReceiver(mock_zmq) assert len(GestureTags.single_gestures) > 0, "GestureTags.single_gestures must not be empty" gesture = GestureTags.single_gestures[0] @@ -557,7 +537,7 @@ def test_handle_gestures_loop_empty(mocker): receiver = ActuationReceiver(mock_zmq_ctx) # Run loop exactly once - mock_state.exit_event.is_set.side_effect = [False, True] + mock_state.exit_event.is_set.side_effect = [False, True, True, True] # We don't put anything in the queue, so .get(timeout=0.1) will raise Queue.Empty. # The code should catch it and pass. @@ -565,29 +545,4 @@ def test_handle_gestures_loop_empty(mocker): # If we reached here without raising an exception, the test passes. # We can assert that the queue is still valid/empty. - assert receiver._gesture_queue.empty() - - -def test_handle_gestures_runtime_error(mocker): - mocker.patch("threading.Thread") - mock_state = mocker.patch("robot_interface.endpoints.actuation_receiver.state") - - mock_zmq_ctx = mock.Mock() - receiver = ActuationReceiver(mock_zmq_ctx) - - # Run loop exactly once - mock_state.exit_event.is_set.side_effect = [False, True] - - # Setup the service to fail - mock_anim = mock.Mock() - mock_anim.run.side_effect = RuntimeError("Wifi Lost") - receiver._animation_service = mock_anim - - # Add item to trigger the service call - receiver._gesture_queue.put("wave") - - receiver._handle_gestures() - - # Assert that the exit_event was triggered - assert mock_state.exit_event.set.called - + assert receiver._gesture_queue.empty() \ No newline at end of file