1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import math
import os
import pytest
from firmament.effects import EffectError, LiveEffect, effect_leds, load, render, scale
from firmament.positions import Led
LEDS = effect_leds([Led(0, 0.0, 0.0, 0.0, 1), Led(1, 0.0, 1.0, 0.0, 0)])
def _effect(tmp_path, source, name="effect.py"):
path = tmp_path / name
path.write_text(source)
return path
def _save(path, source):
"""Rewrite the file with a newer mtime, as an editor's save would."""
stat = path.stat()
path.write_text(source)
os.utime(path, ns=(stat.st_atime_ns, stat.st_mtime_ns + 1_000_000_000))
def test_renders_each_led_from_its_position_and_time(tmp_path):
color = load(_effect(tmp_path, "def color(led, t):\n return (led.index, led.y * 100, t)\n"))
assert render(color, LEDS, 7.0, tmp_path) == bytes([0, 0, 7, 1, 100, 7])
def test_clamps_and_rounds_channels(tmp_path):
color = load(_effect(tmp_path, "def color(led, t):\n return [-5, 300, 12.6]\n"))
assert render(color, LEDS[:1], 0, tmp_path) == bytes([0, 255, 13])
@pytest.mark.parametrize(
("returned", "match"),
[
("None", "expected \\(r, g, b\\)"),
("(1, 2)", "expected \\(r, g, b\\)"),
("'abc'", "expected \\(r, g, b\\)"),
("(1, 2, 'x')", "expected numbers"),
("(1, 2, float('nan'))", "expected numbers"),
("(1, 2, True)", "expected numbers"),
],
)
def test_rejects_values_that_arent_colors(tmp_path, returned, match):
color = load(_effect(tmp_path, f"def color(led, t):\n return {returned}\n"))
with pytest.raises(EffectError, match=f"for LED 0; {match}"):
render(color, LEDS, 0, tmp_path)
def test_reports_the_effect_files_line_for_an_error(tmp_path):
path = _effect(tmp_path, "def color(led, t):\n return 1 / 0\n")
with pytest.raises(EffectError, match="effect.py:2: ZeroDivisionError"):
render(load(path), LEDS, 0, path)
@pytest.mark.parametrize(
("source", "match"),
[
("def color(led, t)\n", "effect.py:1: SyntaxError"),
("x = 1\n\nraise RuntimeError('boom')\n", "effect.py:3: RuntimeError: boom"),
("def colour(led, t):\n return (0, 0, 0)\n", "doesn't define color\\(led, t\\)"),
],
)
def test_load_rejects_broken_files(tmp_path, source, match):
with pytest.raises(EffectError, match=match):
load(_effect(tmp_path, source))
def test_effect_leds_add_normalized_and_cylindrical_coordinates():
leds = [Led(0, 1, 0, 0, 1), Led(1, -1, 2, 0, 1), Led(2, 0, 1, -1, 0), Led(3, 0, 1, 1, 0)]
expected = [
(0.0, 1.0, math.pi / 2, 1.0, -1.0, 0.0),
(1.0, 1.0, -math.pi / 2, -1.0, 1.0, 0.0),
(0.5, 1.0, math.pi, 0.0, 0.0, -1.0),
(0.5, 1.0, 0.0, 0.0, 0.0, 1.0),
]
for e, want in zip(effect_leds(leds), expected, strict=True):
assert (e.h, e.r, e.theta, e.nx, e.ny, e.nz) == pytest.approx(want)
def test_effect_leds_keep_each_leds_position():
led = effect_leds([Led(0, 1, 2, 3, 4), Led(1, 5, 6, 7, 0)])[0]
assert (led.index, led.x, led.y, led.z, led.views) == (0, 1, 2, 3, 4)
def test_normalized_positions_keep_proportions():
near, far = effect_leds([Led(0, 0, 0, 0, 1), Led(1, 4, 1, 0, 1)])
assert (near.nx, near.ny, near.nz) == pytest.approx((-1.0, -0.25, 0.0))
assert (far.nx, far.ny, far.nz) == pytest.approx((1.0, 0.25, 0.0))
def test_a_map_with_no_extent_gets_zeros():
led = effect_leds([Led(0, 1, 1, 1, 1), Led(1, 1, 1, 1, 1)])[0]
assert (led.h, led.r, led.theta, led.nx, led.ny, led.nz) == (0, 0, 0, 0, 0, 0)
def test_scale_applies_brightness():
assert scale(bytes([255, 128, 0]), 64) == bytes([64, 32, 0])
class TestLiveEffect:
def test_reloads_when_the_file_changes(self, tmp_path):
path = _effect(tmp_path, "def color(led, t):\n return (1, 1, 1)\n")
reports: list[str] = []
live = LiveEffect(path, reports.append)
assert live.frame(LEDS, 0) == bytes([1] * 6)
_save(path, "def color(led, t):\n return (2, 2, 2)\n")
assert live.frame(LEDS, 0) == bytes([2] * 6)
assert reports == ["reloaded effect.py"]
def test_keeps_playing_when_a_save_doesnt_load(self, tmp_path):
path = _effect(tmp_path, "def color(led, t):\n return (1, 1, 1)\n")
reports: list[str] = []
live = LiveEffect(path, reports.append)
live.frame(LEDS, 0)
_save(path, "def color(led, t)\n")
assert live.frame(LEDS, 0) == bytes([1] * 6)
assert live.frame(LEDS, 0) == bytes([1] * 6)
assert reports == ["effect.py:1: SyntaxError: expected ':'; keeping the current version"]
def test_goes_back_to_the_last_working_version_when_a_save_fails_to_render(self, tmp_path):
path = _effect(tmp_path, "def color(led, t):\n return (1, 1, 1)\n")
reports: list[str] = []
live = LiveEffect(path, reports.append)
live.frame(LEDS, 0)
_save(path, "def color(led, t):\n return led.w\n")
assert live.frame(LEDS, 0) == bytes([1] * 6)
assert live.frame(LEDS, 0) == bytes([1] * 6)
assert len(reports) == 2
assert "effect.py:2: AttributeError" in reports[1]
assert reports[1].endswith("going back to the last version that worked")
def test_shows_black_until_the_file_changes_when_nothing_works(self, tmp_path):
path = _effect(tmp_path, "def color(led, t):\n return (1, 1, 1) if t < 1 else None\n")
reports: list[str] = []
live = LiveEffect(path, reports.append)
assert live.frame(LEDS, 0) == bytes([1] * 6)
assert live.frame(LEDS, 1) == bytes(6)
assert live.frame(LEDS, 2) == bytes(6)
assert len(reports) == 1
assert reports[0].endswith("showing black until the file changes")
_save(path, "def color(led, t):\n return (3, 3, 3)\n")
assert live.frame(LEDS, 3) == bytes([3] * 6)
def test_starts_black_when_the_first_load_fails(self, tmp_path):
reports: list[str] = []
live = LiveEffect(_effect(tmp_path, "def color(\n"), reports.append)
assert live.frame(LEDS, 0) == bytes(6)
assert len(reports) == 1
assert "keeping" not in reports[0]