Private
Public Access
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
"""tests/test_ir_codec.py"""
|
|
|
|
import pytest
|
|
|
|
from tuya_re.ir import base64_to_pulses, pulses_to_base64
|
|
|
|
# Golden vector, hand-computed:
|
|
# 9202 -> f2 23 | 4496 -> 90 11 | 538 -> 1a 02 (little-endian u16)
|
|
# -> base64 "8iOQERoC"
|
|
GOLDEN_PULSES = [9202, 4496, 538]
|
|
GOLDEN_B64 = "8iOQERoC"
|
|
|
|
# Real ir_study_code captured from a JMIR05 blaster in study mode (DP 202).
|
|
HARDWARE_B64 = (
|
|
"vALnA0gGwgdIBucDKAPnAygD5wMoAxESvALnAygDwgcoA+cDKAPnAygDGgUaB"
|
|
"RoF6wHRCBoF5wMoA+cDvAJaLOsB5wNIBtEIGgUaBW8BGgUoAxoF6wERErwCR"
|
|
"w68AhoF6wEhC7wCQgJvAecDKAPCB+sBbwFCAkgGbwEaBfMA0Ps="
|
|
)
|
|
|
|
|
|
def test_pulses_to_base64_golden():
|
|
assert pulses_to_base64(GOLDEN_PULSES) == GOLDEN_B64
|
|
|
|
|
|
def test_base64_to_pulses_golden():
|
|
assert base64_to_pulses(GOLDEN_B64) == GOLDEN_PULSES
|
|
|
|
|
|
def test_hardware_code_decodes_to_sane_pulses():
|
|
pulses = base64_to_pulses(HARDWARE_B64)
|
|
assert len(pulses) == 64
|
|
assert pulses[0] == 700
|
|
assert all(0 < p <= 0xFFFF for p in pulses)
|
|
# round-trips back to the same base64
|
|
assert pulses_to_base64(pulses) == HARDWARE_B64
|
|
|
|
|
|
def test_round_trip():
|
|
pulses = [9000, 4500, 560, 560, 560, 1690, 39000]
|
|
assert base64_to_pulses(pulses_to_base64(pulses)) == pulses
|
|
|
|
|
|
def test_value_too_large_raises():
|
|
with pytest.raises(ValueError, match="out of u16 range"):
|
|
pulses_to_base64([70000])
|
|
|
|
|
|
def test_odd_length_payload_raises():
|
|
import base64
|
|
|
|
bad = base64.b64encode(b"\x01\x02\x03").decode()
|
|
with pytest.raises(ValueError, match="not a whole number"):
|
|
base64_to_pulses(bad)
|