"""Packet layout and colour parsing for scripts/smc_pad.py.""" from __future__ import annotations import importlib.util import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] spec = importlib.util.spec_from_file_location("smc_pad", ROOT / "scripts" / "smc_pad.py") pad = importlib.util.module_from_spec(spec) assert spec.loader is not None sys.modules["smc_pad"] = pad spec.loader.exec_module(pad) def test_write_packet_matches_a_live_encoder_write() -> None: # Encoder 0 of preset slot 4, rewritten with its own six bytes. packet = pad.vendor_packet(pad.WRITE, 5, 4 * 3539 + 115, bytes.fromhex("0002001e007f")) assert packet.hex() == "0059220e000005bf3700000600000002001e007f5f" def test_state_block_from_this_pad() -> None: block = bytes.fromhex("77003204000002000101040000000000") slot, bank = pad.parse_state(block) assert (slot, bank) == (4, 3) def test_colour_names_and_hex() -> None: assert pad.colour_of("Blue") == (0x00, 0x00, 0xF0) assert pad.colour_of("#f000f0") == (0xF0, 0x00, 0xF0) assert pad.colour_of("ff8800") == (0xFF, 0x88, 0x00) def test_ranges_and_later_pairs_win() -> None: chosen = pad.colour_map(["1-4", "blue", "3", "red", "1,2", "white"], "off") assert chosen[1] == pad.COLOURS["white"] assert chosen[3] == pad.COLOURS["red"] assert chosen[4] == pad.COLOURS["blue"] assert chosen[16] == pad.COLOURS["off"] def test_paint_changes_only_the_named_pad() -> None: bank = bytes(pad.PAD_COUNT * pad.PAD_RECORD_SIZE) painted = pad.paint(bank, {1: (1, 2, 3), 16: (4, 5, 6)}) assert pad.rgb_at(painted, 1) == (1, 2, 3) assert pad.rgb_at(painted, 16) == (4, 5, 6) assert pad.rgb_at(painted, 2) == (0, 0, 0) assert painted[3:5] == bank[3:5] def test_bank_address_of_the_visible_bank() -> None: assert pad.bank_address(4, 3) == 4 * 3539 + 211 + 2 * 16 * 26