"""Send/readback wiring for the light sliders (was a __main__ self-check in the panel; promoted here so CI counts it). Every deliberate input path must emit, and the status-poll readback must neither clobber a fresh user change nor echo a PUT back to the server.""" import time from types import SimpleNamespace import pytest from PySide6.QtCore import QPoint, QPointF, Qt from PySide6.QtGui import QWheelEvent from PySide6.QtWidgets import QApplication from aare.gui.panels.illumination_panel import READBACK_GRACE_S, IlluminationPanel @pytest.fixture def panel(qtbot): panel = IlluminationPanel() qtbot.addWidget(panel) return panel def _wheel_notch(slider): # One wheel notch; sliderReleased never fires for this path, which is # exactly the case the valueChanged wiring exists for. QApplication.sendEvent( slider, QWheelEvent( QPointF(5, 5), QPointF(5, 5), QPoint(0, 0), QPoint(0, 120), Qt.MouseButton.NoButton, Qt.KeyboardModifier.NoModifier, Qt.ScrollPhase.NoScrollPhase, False, ), ) def _status(front=90.0, back=10.0): return SimpleNamespace(bl=SimpleNamespace(front_light=front, back_light=back)) def test_wheel_notch_emits_both_lights(panel): front, back = [], [] panel.front_light.connect(front.append) panel.back_light.connect(back.append) _wheel_notch(panel.front_light_slider) _wheel_notch(panel.back_light_slider) assert front, "wheel adjustment must emit front_light" assert back, "wheel adjustment must emit back_light" def test_readback_grace_holds_user_value(panel): _wheel_notch(panel.front_light_slider) held = panel.front_light_slider.value() panel.update_daq_status(_status()) assert panel.front_light_slider.value() == held, "grace window must hold user value" def test_readback_applies_after_grace_without_echo(panel): sent = [] panel.front_light.connect(sent.append) panel._last_user_change = time.monotonic() - READBACK_GRACE_S - 1 panel.update_daq_status(_status()) assert panel.front_light_slider.value() == 90, "readback must apply after grace" assert panel.back_light_slider.value() == 10 assert not sent, "readback must not echo a PUT" def test_readback_skipped_while_sliding(panel): panel.on_slider_pressed() panel._last_user_change = 0.0 panel.update_daq_status(_status(front=55.0)) assert panel.front_light_slider.value() != 55 panel.on_slider_released() panel.update_daq_status(_status(front=55.0)) assert panel.front_light_slider.value() == 55