import unittest from unittest.mock import patch from kurgan.theme import Theme from kurgan.lighting.lighting_node import LightingNodeBusy, LightingNodeCore, direct_packets from kurgan.lighting.case_fans import RGBLighting, _SIDE_CHASE_ROUTES, side_chase_frame class FakeChannel: def __init__(self): self.sent: list[bytes] = [] self.closed = False def drain(self): pass def send(self, payload): self.sent.append(payload) def read(self, timeout_ms): return bytes((0, 0, 0, 9, 16)) + bytes(12) def close(self): self.closed = True def open_node() -> tuple[LightingNodeCore, FakeChannel]: node, ch = LightingNodeCore(), FakeChannel() node.ch = ch return node, ch class LightingNodeProtocolTest(unittest.TestCase): def test_packets_split_colour_channels_in_fifties_and_skip_unchanged(self): colors = [(i % 256, 2, 3) for i in range(204)] packets = direct_packets(0, colors) self.assertEqual([p[:5] for p in packets[:3]], [bytes((0x32, 0, 0, 50, c)) for c in range(3)]) self.assertEqual(packets[0][5:], bytes(range(50))) self.assertEqual(packets[-1][:5], bytes((0x32, 0, 200, 4, 2))) self.assertEqual(len(packets), 15) changed = list(colors) changed[120] = (0, 2, 3) self.assertEqual(direct_packets(0, changed, colors), [direct_packets(0, changed)[6]]) def test_show_takes_software_control_once_then_sends_only_changes(self): node, ch = open_node() frame = side_chase_frame(0) with patch("kurgan.lighting.lighting_node.time.monotonic", return_value=10.0): node.show(0, frame) self.assertEqual(ch.sent[0], bytes((0x38, 0, 0x02))) self.assertEqual(len(ch.sent), 1 + 15 + 1) self.assertEqual(ch.sent[-1], bytes((0x33, 0xFF))) ch.sent.clear() node.show(0, frame) self.assertEqual(ch.sent, []) node.show(0, side_chase_frame(1)) self.assertTrue(0 < len(ch.sent) - 1 <= 10) # only changed red/green chunks + commit, no port state self.assertTrue(all(p[0] == 0x32 and p[4] in (0, 1) for p in ch.sent[:-1])) def test_keepalive_recommits_and_close_returns_to_hardware_effect(self): node, ch = open_node() with patch("kurgan.lighting.lighting_node.time.monotonic", return_value=10.0): node.show(0, [(1, 2, 3)] * 204) ch.sent.clear() with patch("kurgan.lighting.lighting_node.time.monotonic", return_value=14.0): node.keepalive() self.assertEqual(ch.sent, []) with patch("kurgan.lighting.lighting_node.time.monotonic", return_value=15.5): node.keepalive() self.assertEqual(ch.sent, [bytes((0x33, 0xFF))]) node.close() self.assertEqual(ch.sent[-1], bytes((0x38, 0, 0x01))) self.assertTrue(ch.closed) def test_rejected_command_raises(self): node, ch = open_node() ch.read = lambda timeout_ms: bytes((0, 1)) + bytes(15) with self.assertRaisesRegex(OSError, "0x38"): node.show(0, [(0, 0, 0)]) def test_steps_aside_for_competing_corsair_software_before_opening(self): with patch("kurgan.lighting.lighting_node.corsair_software_running", return_value=True) as running, \ patch("kurgan.lighting.lighting_node.hid_device_paths") as paths: with self.assertRaises(LightingNodeBusy): LightingNodeCore().open() running.assert_called_once_with("Corsair Lighting Node Core", fan_tools=False) paths.assert_not_called() class FakeNode: def __init__(self): self.opened = 0 self.closed = 0 self.frames: list[list[tuple[int, int, int]]] = [] self.firmware = "0.9.16" def open(self): self.opened += 1 def close(self): self.closed += 1 def show(self, channel, colors): assert channel == 0 self.frames.append(list(colors)) def keepalive(self): pass class RGBLightingTest(unittest.TestCase): def setUp(self): self.node = FakeNode() self.lighting = RGBLighting(Theme(), node_factory=lambda: self.node) def test_temperature_colours_each_side_by_its_own_part_and_releases_when_off(self): theme = Theme() color = lambda t: theme.level_color(t, theme.temp_warn, theme.temp_hot) self.lighting.update(40, True, gpu_temp=85) self.assertEqual(self.node.frames[-1], [color(85)] * 102 + [color(40)] * 102) for led in _SIDE_CHASE_ROUTES[0]: # the CPU-side chase route lies in the CPU colour half self.assertEqual(self.node.frames[-1][led], color(40)) self.assertEqual(self.lighting.status, "Temperature lighting active (GPU 85 \u00b0C, CPU 40 \u00b0C)") self.lighting.update(85, True) # no GPU reading: GPU side follows the CPU self.assertEqual(self.node.frames[-1], [color(85)] * 204) self.lighting.update(85, False) self.assertEqual(self.node.closed, 1) self.assertIsNone(self.lighting.node) self.assertEqual(self.lighting.status, "off") def test_busy_or_failed_node_retries_later_without_spinning(self): self.node.open = lambda: (_ for _ in ()).throw(LightingNodeBusy("Lighting Node CORE busy: exit iCUE")) with patch("kurgan.lighting.case_fans.time.monotonic", return_value=100.0): self.lighting.update(40, True) self.assertEqual(self.lighting.status, "Lighting Node CORE busy: exit iCUE") self.node.open = lambda: None with patch("kurgan.lighting.case_fans.time.monotonic", return_value=105.0): self.lighting.update(40, True) self.assertEqual(self.node.frames, []) with patch("kurgan.lighting.case_fans.time.monotonic", return_value=111.0): self.lighting.update(40, True) self.assertEqual(len(self.node.frames), 1) self.node.show = lambda channel, colors: (_ for _ in ()).throw(OSError("unplugged")) with self.assertLogs("kurgan.lighting.case_fans", level="WARNING"): self.lighting.update(90, True) self.assertEqual(self.lighting.status, "Lighting Node CORE connection lost") self.assertIsNone(self.lighting.node) def test_chase_uses_elapsed_time(self): for now, expected in ((100.0, 1), (100.05, 1), (100.31, 2)): with patch("kurgan.lighting.case_fans.time.monotonic", return_value=now): self.lighting.update(None, False, side_chase=True) self.assertEqual(len(self.node.frames), expected) self.assertEqual(self.node.frames, [side_chase_frame(0), side_chase_frame(2)]) self.lighting.update(None, False) self.assertEqual(self.node.closed, 1) def test_chase_cpu_route_and_mirrored_gpu_route(self): cpu, gpu = _SIDE_CHASE_ROUTES expected_cpu = ( list(range(147, 158)) + [146] + [183, 182, 181, 180, 191, 190] + list(range(116, 124)) + list(range(112, 116)) + list(range(189, 183, -1)) ) self.assertEqual(list(cpu), expected_cpu) for route in (cpu, gpu): self.assertEqual(len(route), 36) self.assertEqual(len(set(route)), 36) self.assertTrue(all(10 <= index % 34 < 22 for index in route)) # Compare physical clock positions, not raw indices, because mounts differ. cpu_right = {4: 11, 5: 20, 3: 20} gpu_fan = {4: 0, 5: 1, 3: 2} for cpu_led, gpu_led in zip(cpu, gpu): fan, local = divmod(cpu_led, 34) self.assertEqual(gpu_led // 34, gpu_fan[fan]) cpu_clock = (local - cpu_right[fan]) % 12 gpu_clock = (gpu_led % 34 - 17) % 12 self.assertEqual(gpu_clock, 11 - cpu_clock) def test_chase_has_one_head_per_side_and_four_fading_trail_leds(self): for step in (0, 1, 12, 18, 30, 35, 36, 1000): with self.subTest(step=step): frame = side_chase_frame(step) self.assertEqual(len(frame), 204) for route in _SIDE_CHASE_ROUTES: lit = {led: frame[led] for led in route if frame[led] != (24, 0, 0)} expected = { route[(step - age) % 36]: color for age, color in enumerate(((255, 48, 0), (209, 38, 0), (163, 29, 0), (116, 19, 0), (70, 10, 0))) } self.assertEqual(lit, expected) inner_lit = { index: frame[index] for fan in {led // 34 for led in route} for index in range(fan * 34 + 22, fan * 34 + 34) if frame[index] != (24, 0, 0) } self.assertEqual(len(inner_lit), 5) for outer, color in expected.items(): fan, local = divmod(outer, 34) right = (17, 17, 17, 20, 11, 20)[fan] outer_clock = (local - right) % 12 inner = next(index for index, value in inner_lit.items() if value == color) self.assertEqual(inner // 34, fan) inner_clock = (inner % 34 - 22 - (right - 10)) % 12 self.assertEqual(inner_clock, 11 - outer_clock) for index, color in enumerate(frame): if index % 34 < 10: self.assertEqual(color, (48, 0, 0)) self.assertEqual(side_chase_frame(0), side_chase_frame(36)) def test_chase_speeds_up_with_power_without_jumping(self): self.assertAlmostEqual(self.lighting.chase_rate(None), 1 / 0.15) self.assertAlmostEqual(self.lighting.chase_rate(100), 1 / 0.15) self.assertAlmostEqual(self.lighting.chase_rate(350), (1 / 0.15 + 1 / 0.025) / 2) self.assertAlmostEqual(self.lighting.chase_rate(900), 1 / 0.025) steps = [] def tick(now, watts=None): if watts is not None: self.lighting.set_power(watts) with patch("kurgan.lighting.case_fans.time.monotonic", return_value=now): self.lighting.update(None, False, side_chase=True) steps.append(self.lighting._last_chase_step) self.lighting.set_power(120) tick(100.0) for i in range(1, 31): # 1.5 s at idle: 10 LEDs tick(100.0 + i * 0.05) self.assertEqual(steps[-1], 10) for i in range(1, 121): # power jumps to 600 W for 6 s tick(101.5 + i * 0.05, 600) self.assertEqual(max(b - a for a, b in zip(steps, steps[1:])), 2) self.assertGreater(steps[-1] - steps[30], 180) # mostly at 40 LEDs/s once eased in self.assertEqual(self.lighting.status, "Red side chase active (25 ms/LED at 600 W)") def test_fast_chase_caps_core_frames_but_keeps_its_speed(self): self.lighting.set_power(900) self.lighting._chase_timing.rate = 40.0 with patch("kurgan.lighting.case_fans.time.monotonic", return_value=100.0): self.lighting.update(None, False, side_chase=True) self.lighting._chase_timing.rate = 40.0 for i in range(1, 101): # polled every 10 ms for 1 s with patch("kurgan.lighting.case_fans.time.monotonic", return_value=100.0 + i * 0.01): self.lighting.update(None, False, side_chase=True) self.assertLessEqual(len(self.node.frames), 23) self.assertIn(self.lighting._last_chase_step, (39, 40)) if __name__ == "__main__": unittest.main()