|
| 1 | +"""Test for partial universe bug fix (issue #106) |
| 2 | +
|
| 3 | +Tests that partial universe optimization doesn't truncate DMX packets |
| 4 | +and lose channel values beyond the changed channels. |
| 5 | +""" |
| 6 | + |
| 7 | +from unittest.mock import MagicMock, Mock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from custom_components.dmx.io.dmx_io import DmxUniverse |
| 12 | +from custom_components.dmx.server import PortAddress |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_partial_universe_includes_all_configured_channels(): |
| 17 | + """Test that partial universe packets include all configured channels, not just changed ones. |
| 18 | +
|
| 19 | + This is a regression test for issue #106 where moving heads would return to home position |
| 20 | + after animations because their pan/tilt channels were being excluded from partial packets. |
| 21 | + """ |
| 22 | + # Setup mock controller |
| 23 | + mock_controller = Mock() |
| 24 | + mock_controller.send_dmx = MagicMock() |
| 25 | + |
| 26 | + # Create universe with partial universe enabled |
| 27 | + port_address = PortAddress(0, 0, 1) |
| 28 | + universe = DmxUniverse( |
| 29 | + port_address=port_address, |
| 30 | + controller=mock_controller, |
| 31 | + use_partial_universe=True, |
| 32 | + sacn_server=None, |
| 33 | + sacn_universe=None, |
| 34 | + hass=None, |
| 35 | + max_fps=30, |
| 36 | + ) |
| 37 | + |
| 38 | + # Simulate a moving head fixture with channels 1-10 |
| 39 | + # Channels 1-5: Pan (coarse), Pan (fine), Tilt (coarse), Tilt (fine), Speed |
| 40 | + # Channels 6-10: Dimmer, Red, Green, Blue, White |
| 41 | + initial_values = { |
| 42 | + 1: 128, # Pan coarse - middle position |
| 43 | + 2: 0, # Pan fine |
| 44 | + 3: 64, # Tilt coarse - middle position |
| 45 | + 4: 0, # Tilt fine |
| 46 | + 5: 100, # Speed |
| 47 | + 6: 255, # Dimmer - full |
| 48 | + 7: 255, # Red - full |
| 49 | + 8: 0, # Green - off |
| 50 | + 9: 0, # Blue - off |
| 51 | + 10: 0, # White - off |
| 52 | + } |
| 53 | + |
| 54 | + # Set initial values (simulating fixture setup) |
| 55 | + await universe.update_multiple_values(initial_values, send_update=True) |
| 56 | + |
| 57 | + # Verify first send (now uses partial universe too - no special "first send" behavior) |
| 58 | + assert mock_controller.send_dmx.call_count == 1 |
| 59 | + first_call_data = mock_controller.send_dmx.call_args[0][1] |
| 60 | + # Should be partial universe: up to channel 10, rounded to even = 10 bytes |
| 61 | + assert len(first_call_data) == 10, f"Expected 10 bytes, got {len(first_call_data)}" |
| 62 | + |
| 63 | + # Verify all initial values are in the packet |
| 64 | + for channel, value in initial_values.items(): |
| 65 | + assert first_call_data[channel - 1] == value, f"Channel {channel} should be {value}" |
| 66 | + |
| 67 | + mock_controller.send_dmx.reset_mock() |
| 68 | + |
| 69 | + # Now simulate an animation that only changes color channels (7-9) |
| 70 | + # This simulates what happens when a light entity animates colors |
| 71 | + # but leaves pan/tilt channels untouched |
| 72 | + color_update = { |
| 73 | + 7: 128, # Red - dimmed |
| 74 | + 8: 64, # Green - some green |
| 75 | + 9: 192, # Blue - mostly blue |
| 76 | + } |
| 77 | + |
| 78 | + await universe.update_multiple_values(color_update, send_update=True) |
| 79 | + |
| 80 | + # Verify second send |
| 81 | + assert mock_controller.send_dmx.call_count == 1 |
| 82 | + second_call_data = mock_controller.send_dmx.call_args[0][1] |
| 83 | + |
| 84 | + # THE BUG FIX: Packet should include ALL channels up to channel 10, |
| 85 | + # not just up to channel 9 (the highest changed channel) |
| 86 | + # Before fix: len would be 9 or 10 (rounded to even) |
| 87 | + # After fix: len should be 10 (includes all configured channels) |
| 88 | + assert ( |
| 89 | + len(second_call_data) >= 10 |
| 90 | + ), f"Partial universe packet should include all {max(initial_values.keys())} configured channels" |
| 91 | + |
| 92 | + # Verify pan/tilt channels (1-5) are still present with original values |
| 93 | + # This is the critical test - these channels were NOT in the update, |
| 94 | + # but they should still be included in the packet |
| 95 | + assert second_call_data[0] == 128, "Pan coarse should still be 128" |
| 96 | + assert second_call_data[1] == 0, "Pan fine should still be 0" |
| 97 | + assert second_call_data[2] == 64, "Tilt coarse should still be 64" |
| 98 | + assert second_call_data[3] == 0, "Tilt fine should still be 0" |
| 99 | + assert second_call_data[4] == 100, "Speed should still be 100" |
| 100 | + |
| 101 | + # Verify color channels have the new values |
| 102 | + assert second_call_data[6] == 128, "Red should be updated to 128" |
| 103 | + assert second_call_data[7] == 64, "Green should be updated to 64" |
| 104 | + assert second_call_data[8] == 192, "Blue should be updated to 192" |
| 105 | + |
| 106 | + # Verify dimmer and white are still at original values |
| 107 | + assert second_call_data[5] == 255, "Dimmer should still be 255" |
| 108 | + assert second_call_data[9] == 0, "White should still be 0" |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_partial_universe_with_only_low_channels_changed(): |
| 113 | + """Test that changing only low channels doesn't truncate high channel values.""" |
| 114 | + mock_controller = Mock() |
| 115 | + mock_controller.send_dmx = MagicMock() |
| 116 | + |
| 117 | + port_address = PortAddress(0, 0, 1) |
| 118 | + universe = DmxUniverse( |
| 119 | + port_address=port_address, |
| 120 | + controller=mock_controller, |
| 121 | + use_partial_universe=True, |
| 122 | + sacn_server=None, |
| 123 | + sacn_universe=None, |
| 124 | + hass=None, |
| 125 | + max_fps=30, |
| 126 | + ) |
| 127 | + |
| 128 | + # Set a high channel value (simulating a fixture at a high DMX address) |
| 129 | + initial_values = { |
| 130 | + 1: 100, # Low channel |
| 131 | + 50: 200, # High channel |
| 132 | + } |
| 133 | + |
| 134 | + await universe.update_multiple_values(initial_values, send_update=True) |
| 135 | + mock_controller.send_dmx.reset_mock() |
| 136 | + |
| 137 | + # Update only the low channel |
| 138 | + await universe.update_multiple_values({1: 150}, send_update=True) |
| 139 | + |
| 140 | + # Verify the packet still includes the high channel |
| 141 | + call_data = mock_controller.send_dmx.call_args[0][1] |
| 142 | + assert len(call_data) >= 50, "Packet should include channel 50" |
| 143 | + assert call_data[0] == 150, "Channel 1 should be updated" |
| 144 | + assert call_data[49] == 200, "Channel 50 should still have original value" |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.asyncio |
| 148 | +async def test_partial_universe_packet_size_is_even(): |
| 149 | + """Test that partial universe packets are properly rounded to even sizes.""" |
| 150 | + mock_controller = Mock() |
| 151 | + mock_controller.send_dmx = MagicMock() |
| 152 | + |
| 153 | + port_address = PortAddress(0, 0, 1) |
| 154 | + universe = DmxUniverse( |
| 155 | + port_address=port_address, |
| 156 | + controller=mock_controller, |
| 157 | + use_partial_universe=True, |
| 158 | + sacn_server=None, |
| 159 | + sacn_universe=None, |
| 160 | + hass=None, |
| 161 | + max_fps=30, |
| 162 | + ) |
| 163 | + |
| 164 | + # Set an odd number of channels |
| 165 | + await universe.update_multiple_values({1: 100, 2: 150, 3: 200}, send_update=True) |
| 166 | + mock_controller.send_dmx.reset_mock() |
| 167 | + |
| 168 | + # Update causing the max channel to be odd (3) |
| 169 | + await universe.update_multiple_values({3: 255}, send_update=True) |
| 170 | + |
| 171 | + call_data = mock_controller.send_dmx.call_args[0][1] |
| 172 | + # Should be rounded up to 4 (even number) |
| 173 | + assert len(call_data) % 2 == 0, "Partial universe packet should have even length" |
| 174 | + assert len(call_data) >= 4, "Should be rounded up from 3 to 4" |
0 commit comments