Branchwell

Lightwell / prototype / test_lightwell.py

1import socket
2import unittest
3from datetime import datetime, time, timezone
4from unittest.mock import patch
5from lightwell import Controller, LightState, Schedule, SimulatedLights
6
7class ControllerTests(unittest.TestCase):
8 def setUp(self):
9 self.lights = SimulatedLights("holiday", "accent")
10 self.controller = Controller(self.lights, {"holiday", "accent"})
11 self.level = LightState(True, 45, (255, 181, 100))
12 self.controller.add_scene("night", {"holiday":self.level, "accent":self.level})
13 self.controller.add_schedule(Schedule("evening", time(18), "night"))
14 self.now = datetime(2026, 11, 29, 18, 0, tzinfo=timezone.utc)
15
16 def test_local_scene_does_not_open_network_connections(self):
17 with patch.object(socket, 'socket', side_effect=AssertionError("unexpected network access")):
18 self.assertEqual(self.controller.run_due(self.now), {"evening":{"holiday":"applied","accent":"applied"}})
19 self.assertEqual(self.lights.states["holiday"], self.level)
20 self.assertEqual(self.lights.states["accent"], self.level)
21
22 def test_unreachable_device_keeps_its_last_state(self):
23 before=self.lights.states["accent"]
24 self.lights.offline_devices.add("accent")
25 self.assertEqual(self.controller.apply("night"), {"holiday":"applied","accent":"unreachable"})
26 self.assertEqual(self.lights.states["accent"], before)
27
28 def test_schedule_runs_once_per_day_not_in_earlier_minute(self):
29 self.assertFalse(self.controller.run_due(self.now.replace(hour=17, minute=59)))
30 self.assertTrue(self.controller.run_due(self.now))
31 self.assertFalse(self.controller.run_due(self.now.replace(second=45)))
32 self.assertTrue(self.controller.run_due(self.now.replace(day=30)))
33
34 def test_invalid_values_and_unknown_targets(self):
35 with self.assertRaises(ValueError): LightState(True, 101, (1,2,3))
36 with self.assertRaises(ValueError): LightState(True, 10, (1,2,256))
37 with self.assertRaises(ValueError): self.controller.add_scene("bad", {"missing":self.level})
38 with self.assertRaises(ValueError): self.controller.add_schedule(Schedule("bad", time(1), "missing"))
39 with self.assertRaises(ValueError): self.controller.run_due(self.now.replace(tzinfo=None))
40
41 def test_scene_is_snapshot_not_alias(self):
42 desired={"accent":self.level}
43 self.controller.add_scene("accent-only", desired)
44 desired["holiday"]=self.level
45 self.assertEqual(self.controller.apply("accent-only"), {"accent":"applied"})
46
47if __name__ == "__main__":
48 unittest.main()