feat(patterns): merge pattern styles and add mode support

Consolidate legacy pattern ids into meteor, particles, sparkle, chase,
and colour_cycle with n6/mode style selection; add pattern_modes helper,
self-contained tests/all.py, and preset mode alias on wire.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-16 21:14:54 +12:00
parent 794f1a2841
commit 55a97ac51c
44 changed files with 998 additions and 1539 deletions

View File

@@ -1,14 +1,50 @@
#!/usr/bin/env python3
"""Self-contained led-driver test runner for MicroPython/mpremote."""
"""Self-contained led-driver test runner for MicroPython/mpremote.
Run on device (from led-driver repo root)::
mpremote connect <port> run tests/all.py
Or via dev helper::
python dev.py <port> test
"""
import json
import os
import sys
import utime
from machine import WDT
from settings import Settings
from presets import Presets, run_tick
from utils import convert_and_reorder_colors
def _bootstrap_import_path():
"""Find ``settings`` / ``presets`` whether this file lives in ``tests/`` or ``:/``."""
try:
import uos as os
except ImportError:
import os
candidates = []
try:
here = __file__.rsplit("/", 1)[0]
if here:
candidates.append(here)
parent = here.rsplit("/", 1)[0]
if parent:
candidates.append(parent)
except NameError:
pass
candidates.extend([".", "..", "/"])
for p in candidates:
if p and p not in sys.path:
sys.path.insert(0, p)
_bootstrap_import_path()
from settings import Settings # noqa: E402
from presets import Presets, run_tick # noqa: E402
from preset import Preset # noqa: E402
from utils import convert_and_reorder_colors # noqa: E402
class _TestContext:
@@ -27,6 +63,20 @@ class _TestContext:
utime.sleep_ms(sleep_ms)
def _pattern_loaded(ctx, pattern_id):
return pattern_id in ctx.presets.patterns
def _smoke_preset(ctx, name, data, ms=80):
pattern_id = data.get("p") or data.get("pattern")
if not _pattern_loaded(ctx, pattern_id):
raise AssertionError("pattern not loaded: %s" % pattern_id)
ctx.presets.edit(name, data)
if not ctx.presets.select(name):
raise AssertionError("select failed: %s" % name)
ctx.tick_for_ms(ms)
def _process_message(ctx, payload):
"""Small test helper that mirrors the main message handling logic."""
try:
@@ -93,8 +143,7 @@ def _process_message(ctx, payload):
should_apply_default = this_device_name_norm in normalized_targets
if (
should_apply_default
and
isinstance(default_name, str)
and isinstance(default_name, str)
and default_name
and default_name in ctx.presets.presets
):
@@ -145,6 +194,40 @@ def test_preset_edit_sanitization():
assert not hasattr(p, "unknown_field")
def test_preset_mode_alias_maps_to_n6():
ctx = _TestContext()
ctx.presets.edit(
"rainbow_mode",
{"pattern": "colour_cycle", "mode": 1, "d": 50, "n1": 2, "a": True},
)
p = ctx.presets.presets["rainbow_mode"]
assert p.p == "colour_cycle"
assert p.n6 == 1
def test_style_mode_and_legacy_aliases():
from patterns.pattern_modes import style_mode
p = Preset({"p": "colour_cycle", "mode": 0, "d": 50, "c": [(255, 0, 0)]})
assert style_mode(p, 0, {"rainbow": 1}) == 0
legacy = Preset({"p": "rainbow", "d": 50, "c": [(255, 0, 0)]})
assert style_mode(legacy, 0, {"rainbow": 1}) == 1
ctx = _TestContext()
legacy_ids = (
"rainbow",
"meteor_rain",
"snowfall",
"sparkle_trail",
"marquee",
"northern_wave",
)
for lid in legacy_ids:
if not _pattern_loaded(ctx, lid):
raise AssertionError("legacy alias not registered: %s" % lid)
def test_colour_conversion_and_transition():
ctx = _TestContext()
msg = {
@@ -162,7 +245,6 @@ def test_colour_conversion_and_transition():
result = _process_message(ctx, msg)
assert result == "ok"
assert ctx.presets.selected == "fade"
# Smoke-run the generator to ensure math runs without type errors.
ctx.tick_for_ms(250)
@@ -172,19 +254,54 @@ def test_pattern_smoke():
"t_on": {"p": "on", "c": [(16, 8, 4)]},
"t_off": {"p": "off"},
"t_blink": {"p": "blink", "c": [(255, 0, 0)], "d": 20},
"t_rainbow": {"p": "rainbow", "d": 5, "n1": 2},
"t_pulse": {"p": "pulse", "c": [(255, 0, 0)], "n1": 20, "n2": 10, "n3": 20, "d": 10},
"t_transition": {"p": "transition", "c": [(255, 0, 0), (0, 0, 255)], "d": 30},
"t_colour_cycle": {"p": "colour_cycle", "n6": 0, "d": 5, "n1": 2, "c": [(255, 0, 0), (0, 255, 0)]},
"t_chase": {"p": "chase", "c": [(255, 0, 0), (0, 0, 255)], "n1": 3, "n2": 2, "n3": 1, "n4": 1, "d": 20},
"t_circle": {"p": "circle", "c": [(255, 255, 0), (0, 0, 8)], "n1": 5, "n2": 10, "n3": 5, "n4": 2},
}
for name, data in cases.items():
ctx.presets.edit(name, data)
assert ctx.presets.select(name), "select failed: %s" % name
ctx.tick_for_ms(120)
_smoke_preset(ctx, name, data, ms=100)
def test_merged_pattern_modes():
"""Smoke each style (``n6`` / ``mode``) for merged multi-mode patterns."""
ctx = _TestContext()
colors = [(200, 220, 255), (255, 180, 80)]
cases = (
("mc_grad", "colour_cycle", {"p": "colour_cycle", "n6": 0, "n1": 2, "d": 8, "c": colors}),
("mc_wheel", "colour_cycle", {"p": "colour_cycle", "mode": 1, "n1": 2, "d": 8}),
("chase_std", "chase", {"p": "chase", "n6": 0, "n1": 2, "n2": 2, "n3": 1, "n4": 1, "d": 15, "c": colors}),
("chase_marq", "chase", {"p": "chase", "n6": 1, "n1": 3, "n2": 2, "n3": 1, "d": 15, "c": colors}),
("meteor_0", "meteor", {"p": "meteor", "n6": 0, "n1": 4, "n2": 2, "n3": 8, "d": 10, "c": colors}),
("meteor_1", "meteor", {"p": "meteor", "n6": 1, "n1": 3, "n2": 2, "n3": 4, "d": 10, "c": colors}),
("part_0", "particles", {"p": "particles", "n6": 0, "n1": 4, "n2": 1, "d": 10, "c": colors}),
("part_1", "particles", {"p": "particles", "mode": 1, "n1": 3, "n2": 1, "n3": 4, "d": 10, "c": colors}),
("spark_0", "sparkle", {"p": "sparkle", "n6": 0, "n1": 4, "n2": 6, "d": 10, "c": colors}),
("spark_1", "sparkle", {"p": "sparkle", "n6": 1, "n1": 3, "n2": 4, "n3": 2, "d": 10, "c": colors}),
("aurora_0", "aurora", {"p": "aurora", "n6": 0, "n1": 3, "n2": 2, "n3": 0, "d": 12, "c": colors}),
("aurora_1", "aurora", {"p": "aurora", "mode": 1, "n1": 8, "n2": 2, "n3": 1, "d": 12, "c": colors}),
)
for name, pattern_id, data in cases:
if not _pattern_loaded(ctx, pattern_id):
continue
_smoke_preset(ctx, name, data, ms=60)
legacy_smoke = (
("leg_rainbow", "rainbow", {"p": "rainbow", "d": 8, "n1": 2}),
("leg_ice", "ice_sparkle", {"p": "ice_sparkle", "n1": 3, "n2": 2, "n3": 2, "d": 10, "c": colors}),
("leg_wave", "northern_wave", {"p": "northern_wave", "n1": 6, "n2": 2, "n3": 1, "d": 12, "c": colors}),
("leg_star", "starfall", {"p": "starfall", "n1": 3, "n2": 1, "n3": 3, "d": 10, "c": colors}),
)
for name, pattern_id, data in legacy_smoke:
if not _pattern_loaded(ctx, pattern_id):
continue
_smoke_preset(ctx, name, data, ms=60)
def test_patterns_do_not_use_blocking_sleep():
try:
import uos as os
except ImportError:
import os
pattern_dir = "patterns"
offenders = []
try:
@@ -192,8 +309,9 @@ def test_patterns_do_not_use_blocking_sleep():
except OSError:
raise AssertionError("patterns directory is missing")
skip = frozenset(("__init__.py", "main.py", "pattern_modes.py"))
for filename in files:
if not filename.endswith(".py") or filename in ("__init__.py", "main.py"):
if not filename.endswith(".py") or filename in skip:
continue
path = pattern_dir + "/" + filename
try:
@@ -223,6 +341,7 @@ def test_default_requires_existing_preset():
_process_message(ctx, {"v": "1", "default": "exists"})
assert ctx.settings.get("default") == "exists"
def test_default_targets_gate_by_device_name():
ctx = _TestContext()
ctx.settings["name"] = "a"
@@ -243,6 +362,11 @@ def test_default_targets_gate_by_device_name():
def test_save_and_load_roundtrip():
try:
import uos as os
except ImportError:
import os
ctx = _TestContext()
ctx.presets.edit(
"persist",
@@ -270,8 +394,11 @@ def run_all():
tests = [
test_invalid_messages_do_not_crash,
test_preset_edit_sanitization,
test_preset_mode_alias_maps_to_n6,
test_style_mode_and_legacy_aliases,
test_colour_conversion_and_transition,
test_pattern_smoke,
test_merged_pattern_modes,
test_patterns_do_not_use_blocking_sleep,
test_default_requires_existing_preset,
test_default_targets_gate_by_device_name,