from unittest.mock import MagicMock, patch import pytest from PySide6.QtWidgets import QMessageBox from aare.gui.widgets.message_box import ( experiment_hutch_shutter_check, reply_box, ring_current_auto_check, ring_current_low_check, timer_box, ) def test_reply_box(qtbot): parent = MagicMock() with patch("PySide6.QtWidgets.QMessageBox.question") as mock_question: mock_question.return_value = QMessageBox.StandardButton.Yes res = reply_box(parent, "Title", "Message") assert res == QMessageBox.StandardButton.Yes mock_question.assert_called_once() def test_timer_box_auto_accept(qtbot): parent = None condition_func = MagicMock(return_value=True) # We need to process events for timer to fire box = timer_box(parent, condition_func=condition_func) # Wait until box is closed by check() qtbot.waitUntil(lambda: not box.isVisible(), timeout=2000) assert box.result() == QMessageBox.StandardButton.Yes @pytest.mark.timeout(10) def test_ring_current_low_check_ok(qtbot): # Should return True immediately if current is high enough assert ring_current_low_check(None, 361.0) is True def test_ring_current_low_check_low_yes(qtbot): with patch("aare.gui.widgets.message_box.reply_box") as mock_reply: mock_reply.return_value = QMessageBox.StandardButton.Yes assert ring_current_low_check(None, 50.0) is True mock_reply.assert_called_once() def test_ring_current_low_check_low_no(qtbot): with patch("aare.gui.widgets.message_box.reply_box") as mock_reply: mock_reply.return_value = QMessageBox.StandardButton.No assert ring_current_low_check(None, 50.0) is False def test_experiment_hutch_shutter_check_open(qtbot): assert experiment_hutch_shutter_check(None, True) is True def test_experiment_hutch_shutter_check_closed_yes(qtbot): with patch("aare.gui.widgets.message_box.reply_box") as mock_reply: mock_reply.return_value = QMessageBox.StandardButton.Yes assert experiment_hutch_shutter_check(None, False) is True def test_ring_current_auto_check_yes(qtbot): # This one uses a nested event loop, which can be tricky to test. # We'll mock timer_box to return a box that we can close manually. with patch("aare.gui.widgets.message_box.timer_box") as mock_timer_box: box = QMessageBox() box.setStandardButtons(QMessageBox.StandardButton.Yes) mock_timer_box.return_value = box # We need to close the box after some time to break the loop from PySide6.QtCore import QTimer QTimer.singleShot(100, lambda: box.done(QMessageBox.StandardButton.Yes)) res = ring_current_auto_check(None, 50.0, lambda: False) assert res is True