The Kurgan 1.0.0: one process for the LCD, fans, lighting, local LLM servers and the web UI
This commit is contained in:
commit
f631386618
71 files changed
+14616
No files matched your search
@@ -0,0 +1,383 @@
|
||||
import unittest
|
||||
import threading
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from kurgan.theme import Theme
|
||||
from kurgan.lighting.memory import (
|
||||
MemoryFrame, MemoryLighting, PawnIoSmbus, _crc8, _led_packet, _rgb_bytes,
|
||||
_solid_frame, memory_chase_frame, memory_chase_step,
|
||||
)
|
||||
from kurgan.lighting.case_fans import ChaseTiming, RGBLighting, side_chase_frame
|
||||
|
||||
|
||||
class FakeDriver:
|
||||
def __init__(self):
|
||||
self.open_count = 0
|
||||
self.closed = 0
|
||||
self.frames: list[MemoryFrame] = []
|
||||
self.forced: list[bool] = []
|
||||
|
||||
def open(self):
|
||||
self.open_count += 1
|
||||
|
||||
def show(self, color):
|
||||
self.frames.append(_solid_frame(color))
|
||||
|
||||
def show_frame(self, colors, *, force=False):
|
||||
self.frames.append(colors)
|
||||
self.forced.append(force)
|
||||
|
||||
def close(self):
|
||||
self.closed += 1
|
||||
|
||||
|
||||
class MemoryLightingTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.theme = Theme()
|
||||
self.driver = FakeDriver()
|
||||
self.lighting = MemoryLighting(self.theme, driver_factory=lambda: self.driver)
|
||||
|
||||
def test_disabled_start_keeps_ram_dark(self):
|
||||
self.lighting.update(40, False)
|
||||
self.assertEqual(self.lighting.status, "RAM lighting off")
|
||||
self.assertEqual(self.driver.open_count, 1)
|
||||
self.assertEqual(self.driver.frames, [_solid_frame((0, 0, 0))])
|
||||
|
||||
def test_temperature_control_updates_all_modules_and_stays_dark_when_disabled(self):
|
||||
color = (24, 0, 0)
|
||||
hot = (255, 48, 0)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.0):
|
||||
self.lighting.update(40, True)
|
||||
self.assertEqual(self.driver.open_count, 1)
|
||||
self.assertEqual(self.driver.frames, [_solid_frame(color)])
|
||||
self.assertEqual(self.lighting.status, "RAM temperature lighting active (CPU 40 °C)")
|
||||
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=101.0):
|
||||
self.lighting.update(85, True)
|
||||
self.assertEqual(self.driver.frames, [_solid_frame(color), _solid_frame(hot)])
|
||||
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=102.0):
|
||||
self.lighting.update(85, False)
|
||||
self.assertEqual(self.driver.frames[-1], _solid_frame((0, 0, 0)))
|
||||
self.assertEqual(self.lighting.status, "RAM lighting off")
|
||||
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=107.0):
|
||||
self.lighting.update(None, False)
|
||||
self.assertEqual(self.driver.frames, [_solid_frame(c) for c in (color, hot, (0, 0, 0), (0, 0, 0))])
|
||||
|
||||
self.lighting.close()
|
||||
self.assertEqual(self.driver.frames[-1], _solid_frame((0, 0, 0)))
|
||||
self.assertEqual(self.driver.closed, 1)
|
||||
|
||||
def test_missing_temperature_neither_claims_the_bus_nor_replaces_a_live_color(self):
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.0):
|
||||
self.lighting.update(None, True)
|
||||
self.assertEqual(self.lighting.status, "RAM temperature lighting waiting for CPU/GPU temperature")
|
||||
self.assertEqual(self.driver.open_count, 0)
|
||||
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=101.0):
|
||||
self.lighting.update(60, True)
|
||||
color = self.driver.frames[-1]
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=102.0):
|
||||
self.lighting.update(None, True)
|
||||
self.assertEqual(self.driver.frames, [color])
|
||||
self.assertEqual(self.lighting.status, "RAM temperature lighting waiting for CPU/GPU temperature")
|
||||
|
||||
def test_red_orange_ramp_uses_hottest_valid_sensor_and_clamps_at_thresholds(self):
|
||||
cases = (
|
||||
(40, 35, (24, 0, 0), "CPU 40"),
|
||||
(40, 65, (140, 24, 0), "GPU 65"),
|
||||
(80, 65, (255, 48, 0), "CPU 80"),
|
||||
(60, 95, (255, 48, 0), "GPU 95"),
|
||||
(None, 50, (24, 0, 0), "GPU 50"),
|
||||
(float("nan"), 65, (140, 24, 0), "GPU 65"),
|
||||
(65, float("inf"), (140, 24, 0), "CPU 65"),
|
||||
)
|
||||
for cpu, gpu, expected, source in cases:
|
||||
with self.subTest(cpu=cpu, gpu=gpu):
|
||||
self.lighting.update(cpu, True, gpu_temp=gpu)
|
||||
self.assertEqual(self.driver.frames[-1], _solid_frame(expected))
|
||||
self.assertIn(source, self.lighting.status)
|
||||
self.lighting.update(float("nan"), True, gpu_temp=float("inf"))
|
||||
self.assertEqual(self.driver.frames[-1], _solid_frame((140, 24, 0)))
|
||||
self.assertEqual(self.lighting.status, "RAM temperature lighting waiting for CPU/GPU temperature")
|
||||
|
||||
def test_worker_uses_both_temperatures_from_sensor_handoff(self):
|
||||
stop, enabled = Mock(), Mock()
|
||||
stop.is_set.side_effect = [False, True]
|
||||
enabled.is_set.return_value = True
|
||||
self.lighting.set_temperature(40, 80)
|
||||
self.lighting.run(stop, enabled)
|
||||
self.assertEqual(self.driver.frames, [_solid_frame((255, 48, 0)), _solid_frame((0, 0, 0))])
|
||||
|
||||
def test_invalid_temperature_range_is_rejected(self):
|
||||
for warn, hot in ((80, 80), (90, 80), (float("nan"), 80), (50, float("inf"))):
|
||||
with self.subTest(warn=warn, hot=hot), self.assertRaises(ValueError):
|
||||
MemoryLighting(Theme(temp_warn=warn, temp_hot=hot))
|
||||
|
||||
def test_failed_connection_retries_after_backoff(self):
|
||||
factory = Mock(side_effect=[OSError("PawnIO denied"), self.driver])
|
||||
self.lighting = MemoryLighting(self.theme, driver_factory=factory)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.0):
|
||||
with self.assertLogs("kurgan.lighting.memory", level="WARNING"):
|
||||
self.lighting.update(40, True)
|
||||
self.assertEqual(self.lighting.status, "PawnIO denied")
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=105.0):
|
||||
self.lighting.update(40, True)
|
||||
self.assertEqual(factory.call_count, 1)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=110.0):
|
||||
self.lighting.update(40, True)
|
||||
self.assertEqual(factory.call_count, 2)
|
||||
self.assertEqual(len(self.driver.frames), 1)
|
||||
|
||||
def test_chase_uses_elapsed_time_with_capped_writes_and_power_acceleration(self):
|
||||
self.lighting.set_power(550)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
for index in range(1, 50):
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100 + index * 0.01):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.5):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.assertGreaterEqual(len(self.driver.frames), 10)
|
||||
self.assertLessEqual(len(self.driver.frames), 12)
|
||||
self.assertAlmostEqual(self.lighting._chase_timing.phase, 26)
|
||||
self.assertIn(self.driver.frames[-1], tuple(memory_chase_frame(step) for step in range(23, 26)))
|
||||
self.assertEqual(self.lighting.status, "RAM red side chase active (0.9 s/cycle at 550 W)")
|
||||
|
||||
self.lighting = MemoryLighting(self.theme, lambda: self.driver)
|
||||
self.lighting.set_power(150)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=200):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=200.5):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.lighting.set_power(550)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=200.6):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.assertAlmostEqual(self.lighting._chase_timing.rate, 10)
|
||||
self.assertAlmostEqual(self.lighting._chase_timing.phase, 6 + 13 / 3)
|
||||
|
||||
def test_pattern_switches_and_off_bypass_chase_frame_cap(self):
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.01):
|
||||
self.lighting.update(65, True)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.02):
|
||||
self.lighting.update(None, False)
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100.03):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.driver.frames, [
|
||||
memory_chase_frame(5), _solid_frame((140, 24, 0)),
|
||||
_solid_frame((0, 0, 0)), memory_chase_frame(5),
|
||||
])
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=105.03):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.assertTrue(self.driver.forced[-1])
|
||||
self.lighting.close()
|
||||
self.assertEqual(self.driver.frames[-1], _solid_frame((0, 0, 0)))
|
||||
|
||||
def test_fast_chase_worker_avoids_large_position_jumps_with_fan_refresh_budget(self):
|
||||
now = [100.0]
|
||||
stop = Mock()
|
||||
stop.is_set.side_effect = lambda: now[0] > 101
|
||||
stop.wait.side_effect = lambda delay: now.__setitem__(0, now[0] + delay)
|
||||
chase = threading.Event()
|
||||
chase.set()
|
||||
self.lighting.set_power(550)
|
||||
writes = []
|
||||
show_frame = self.driver.show_frame
|
||||
|
||||
def record(colors, *, force=False):
|
||||
writes.append((now[0], int(self.lighting._chase_timing.phase)))
|
||||
show_frame(colors, force=force)
|
||||
|
||||
with patch("kurgan.lighting.memory.time.monotonic", side_effect=lambda: now[0]), \
|
||||
patch.object(self.driver, "show_frame", side_effect=record):
|
||||
self.lighting.run(stop, threading.Event(), chase)
|
||||
self.assertGreaterEqual(len(writes), 16)
|
||||
self.assertLessEqual(len(writes), 23)
|
||||
self.assertTrue(all(b[0] - a[0] >= 0.045 for a, b in zip(writes, writes[1:])))
|
||||
self.assertLessEqual(max(b[1] - a[1] for a, b in zip(writes, writes[1:])), 3)
|
||||
self.assertGreaterEqual(writes[-1][1], 42)
|
||||
|
||||
def test_worker_accepts_chase_without_temperature_readings(self):
|
||||
stop = Mock()
|
||||
stop.is_set.side_effect = [False, True]
|
||||
chase = threading.Event()
|
||||
chase.set()
|
||||
self.lighting.run(stop, threading.Event(), chase)
|
||||
self.assertEqual(self.driver.frames, [memory_chase_frame(5), _solid_frame((0, 0, 0))])
|
||||
|
||||
def test_moving_chase_still_refreshes_unchanged_sticks_every_five_seconds(self):
|
||||
for index in range(11):
|
||||
with patch("kurgan.lighting.memory.time.monotonic", return_value=100 + index * 0.5):
|
||||
self.lighting.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.driver.forced, [True] + [False] * 9 + [True])
|
||||
|
||||
class MemoryChaseTest(unittest.TestCase):
|
||||
def test_mirrored_heads_follow_six_visits_without_repeating_end_sticks(self):
|
||||
positions = (
|
||||
(0, 3, 0), (5, 3, 5), (6, 2, 5), (11, 2, 0),
|
||||
(12, 1, 0), (17, 1, 5), (18, 0, 5), (23, 0, 0),
|
||||
(24, 1, 0), (29, 1, 5), (30, 2, 5), (35, 2, 0),
|
||||
(36, 3, 0), (41, 3, 5),
|
||||
)
|
||||
for step, stick, left in positions:
|
||||
with self.subTest(step=step):
|
||||
frame = memory_chase_frame(step)
|
||||
heads = [(row, led) for row, colors in enumerate(frame)
|
||||
for led, color in enumerate(colors) if color == (255, 48, 0)]
|
||||
self.assertEqual(heads, [(stick, left), (stick, 11 - left)])
|
||||
for step in range(36):
|
||||
frame = memory_chase_frame(step)
|
||||
self.assertEqual(len(frame), 4)
|
||||
for colors in frame:
|
||||
self.assertEqual(len(colors), 12)
|
||||
self.assertEqual(colors[:6], colors[6:][::-1])
|
||||
self.assertEqual(frame, memory_chase_frame(step + 36))
|
||||
|
||||
def test_four_led_fading_trail_matches_fan_palette(self):
|
||||
frame = memory_chase_frame(10)
|
||||
self.assertEqual(frame[2][1:6], ((255, 48, 0), (209, 38, 0), (163, 29, 0),
|
||||
(116, 19, 0), (70, 10, 0)))
|
||||
self.assertEqual(frame[3], ((24, 0, 0),) * 12)
|
||||
|
||||
|
||||
class SharedChaseTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.clock = ChaseTiming(0.125)
|
||||
self.clock.reset(6)
|
||||
self.node = Mock()
|
||||
self.node.firmware = "0.9.16"
|
||||
self.driver = FakeDriver()
|
||||
self.fans = RGBLighting(Theme(), node_factory=lambda: self.node, chase_timing=self.clock)
|
||||
self.memory = MemoryLighting(Theme(), lambda: self.driver, self.clock)
|
||||
self.fans.set_power(150)
|
||||
|
||||
def test_whole_cycles_share_physical_bottom_and_top_anchors(self):
|
||||
for index, (now, fan_step, ram_step) in enumerate(((100, 6, 5), (102.25, 24, 23), (104.5, 42, 41))):
|
||||
with self.subTest(now=now), patch("time.monotonic", return_value=now):
|
||||
if index % 2:
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
else:
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.node.show.call_args.args[1], side_chase_frame(fan_step))
|
||||
self.assertEqual(self.driver.frames[-1], memory_chase_frame(ram_step))
|
||||
self.assertEqual(self.memory.status, "RAM red side chase active (4.5 s/cycle)")
|
||||
|
||||
def test_joining_and_switching_patterns_never_resets_shared_phase(self):
|
||||
with patch("time.monotonic", return_value=100):
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
with patch("time.monotonic", return_value=102.25):
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.driver.frames[-1], memory_chase_frame(23))
|
||||
with patch("time.monotonic", return_value=102.26):
|
||||
self.memory.update(65, True)
|
||||
with patch("time.monotonic", return_value=102.75):
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.clock.phase, 28)
|
||||
self.assertEqual(self.driver.frames[-1], memory_chase_frame(memory_chase_step(28)))
|
||||
with patch("time.monotonic", return_value=103):
|
||||
self.fans.update(None, False)
|
||||
with patch("time.monotonic", return_value=103.5):
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.clock.phase, 34)
|
||||
|
||||
def test_out_of_order_worker_timestamps_do_not_double_count_elapsed_time(self):
|
||||
self.clock.sample(100)
|
||||
self.clock.sample(100.5)
|
||||
phase, _ = self.clock.sample(100.25)
|
||||
self.assertEqual(phase, 10)
|
||||
phase, _ = self.clock.sample(101)
|
||||
self.assertEqual(phase, 14)
|
||||
|
||||
def test_load_change_has_one_acceleration_and_phase_for_both_patterns(self):
|
||||
with patch("time.monotonic", return_value=100):
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.memory.set_power(550)
|
||||
with patch("time.monotonic", return_value=100.5):
|
||||
self.memory.update(None, False, side_chase=True)
|
||||
self.fans.update(None, False, side_chase=True)
|
||||
self.assertEqual(self.clock.rate, 24)
|
||||
self.assertEqual(self.clock.phase, 18)
|
||||
self.assertEqual(self.node.show.call_args.args[1], side_chase_frame(18))
|
||||
self.assertEqual(self.driver.frames[-1], memory_chase_frame(17))
|
||||
|
||||
|
||||
class CorsairDramPacketTest(unittest.TestCase):
|
||||
def test_direct_packet_contains_twelve_rgb_leds_and_crc8(self):
|
||||
color = (12, 34, 56)
|
||||
packet = _led_packet((color,) * 12)
|
||||
payload = bytes((12,)) + bytes(color) * 12
|
||||
self.assertEqual(len(packet), 38)
|
||||
self.assertEqual(packet[:-1], payload)
|
||||
self.assertEqual(packet[-1], _crc8(payload))
|
||||
self.assertEqual(_crc8(b"123456789"), 0xF4)
|
||||
|
||||
def test_direct_frame_only_writes_all_four_controllers_without_saving_effects(self):
|
||||
bus = PawnIoSmbus()
|
||||
calls = []
|
||||
bus._write_block = lambda address, register, data: calls.append((address, register, data))
|
||||
with patch("kurgan.lighting.memory.PawnIoSmbus._bus"):
|
||||
bus.show((255, 0, 0))
|
||||
self.assertEqual([call[:2] for call in calls],
|
||||
[(address, register) for address in (0x18, 0x19, 0x1A, 0x1B)
|
||||
for register in (0x31, 0x32)])
|
||||
self.assertTrue(all(len(data) == size for (_, _, data), size
|
||||
in zip(calls, (32, 6) * 4)))
|
||||
for index in range(0, len(calls), 2):
|
||||
self.assertEqual(calls[index][2] + calls[index + 1][2], _led_packet(((255, 0, 0),) * 12))
|
||||
|
||||
def test_smbus_block_write_encodes_payload_length_and_zero_padding(self):
|
||||
bus = PawnIoSmbus()
|
||||
bus._execute = Mock()
|
||||
data = bytes(range(32))
|
||||
bus._write_block(0x1B, 0x31, data)
|
||||
operation, args = bus._execute.call_args.args
|
||||
packed = b"".join(cell.to_bytes(8, "little") for cell in args[4:])
|
||||
self.assertEqual(operation, b"ioctl_smbus_xfer")
|
||||
self.assertEqual(args[:4], (0x1B, 0, 0x31, 5))
|
||||
self.assertEqual(packed, bytes((32,)) + data + bytes(7))
|
||||
|
||||
def test_invalid_rgb_values_are_rejected(self):
|
||||
for color in ((-1, 0, 0), (0, 256, 0), (1.5, 0, 0), (1, 2)):
|
||||
with self.subTest(color=color), self.assertRaises(ValueError):
|
||||
_rgb_bytes(color)
|
||||
|
||||
def test_per_led_packets_and_only_changed_sticks_are_written(self):
|
||||
frame = memory_chase_frame(0)
|
||||
packet = _led_packet(frame[3])
|
||||
self.assertEqual(packet[:-1], bytes((12,)) + bytes(channel for color in frame[3] for channel in color))
|
||||
self.assertEqual(packet[-1], _crc8(packet[:-1]))
|
||||
bus = PawnIoSmbus()
|
||||
bus._write_block = Mock()
|
||||
with patch("kurgan.lighting.memory.PawnIoSmbus._bus"):
|
||||
bus.show_frame(frame)
|
||||
self.assertEqual(bus._write_block.call_count, 8)
|
||||
bus._write_block.reset_mock()
|
||||
bus.show_frame(frame)
|
||||
bus._write_block.assert_not_called()
|
||||
changed = frame[:3] + (((255, 0, 0),) * 12,)
|
||||
bus.show_frame(changed)
|
||||
self.assertEqual([c.args[:2] for c in bus._write_block.call_args_list], [(0x1B, 0x31), (0x1B, 0x32)])
|
||||
bus._write_block.reset_mock()
|
||||
bus.show_frame(changed, force=True)
|
||||
self.assertEqual(bus._write_block.call_count, 8)
|
||||
|
||||
def test_invalid_frame_is_rejected_before_any_hardware_write(self):
|
||||
bus = PawnIoSmbus()
|
||||
bus._write_block = Mock()
|
||||
frame = _solid_frame((0, 0, 0))
|
||||
for invalid in (frame[:3], frame[:3] + (frame[3][:11],)):
|
||||
with self.subTest(frame=invalid), self.assertRaises(ValueError):
|
||||
bus.show_frame(invalid)
|
||||
bus._write_block.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user