MicromateClient.get_setup() reads SUB 0x1A once and returns a MicromateSetup: record mode, sample rate, histogram interval, pre-trigger, stop mode, record time, auto-stop, active sensors, trigger sources, the three geophone trigger levels, mic type and (linear) mic trigger, job number, scaled distance, and the four notes. MEASURED FIELDS ONLY -- everything graded inferred or unknown in the reference stays in .raw. micromate/setup_block.py holds the decode as pure functions (no I/O), plus crc_ok(): one checksum rule for 0x1A, 0x08 and 0x2E. A failed checksum raises SetupChecksumError (a ProtocolError) unless verify=False; a short block raises either way. Two things the decoder gets right that a naive one would not: - the mic comes from channel block 4 (the ACTIVE mic), never blocks 5/6, which hold the stored settings of whichever mic is NOT fitted; - distance and charge weight convert back with the unit's own 3.2808 and 2.21, so 45 lb entered on the unit reads 45.0, not 44.89. TESTS: 69 new. 15 real fixtures (2,103 B each) -- 14 UM20147 reads from the 10-17/10-19 keypad passes, chosen so every field takes at least two values, plus UM12947's read from Thor's 9-24 capture (other firmware line). Expected values are what the operator read off the unit's screen, never the decoder's own output. Fixtures force-added, per the tests/fixtures/ convention. Full suite 541 passed; the 16 pre-existing failures (test_bw_ascii_report.py 15, test_event_file_io.py 1) are unchanged. VERIFIED ON HARDWARE: bridges/mm_client_check.py now prints the decoded setup; against UM20147 every line matched the unit's screen (Brian, 10-19). Also: the scratch harness uses the library checksum and checks all three blocks; mm_client_check's stale labels fixed (0x06 content[4:8] is the likely monitor-log count, not "unexplained"; 11.0BD is no longer "the first one this code has met"). No codec, store or DB change; no backfill. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
273 lines
11 KiB
Python
273 lines
11 KiB
Python
"""
|
|
Tests for micromate/setup_block.py — the Micromate setup block (SUB 0x1A) decoder.
|
|
|
|
Every fixture is a real read off a unit, taken with scratch/mm_config_diff.py
|
|
while an operator changed settings on the unit's own keypad, 2026-10-17/19.
|
|
**Expected values are what the operator read off the unit's screen**, recorded in
|
|
docs/micromate_protocol_reference.md — never values computed by this decoder.
|
|
A test whose expectation came from the code under test passes whatever the code
|
|
does.
|
|
|
|
Fixtures are 2,103-byte `0x1A` response data: an 11-byte prefix, then the
|
|
2,092-byte content the decoder reads. `um12947_2026-09-24.bin` is a second unit
|
|
on the other firmware line, cut from Thor's 9-24 "send TEST1" capture.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from micromate.protocol import ProtocolError # noqa: E402
|
|
from micromate.setup_block import SetupChecksumError, crc_ok, decode_setup # noqa: E402
|
|
|
|
FIXTURES = Path(__file__).parent / "fixtures" / "micromate-setup"
|
|
ALL_FIXTURES = sorted(FIXTURES.glob("*.bin"))
|
|
|
|
# Small real blocks for the generic checksum rule (content only, prefix stripped).
|
|
# 0x08 preferences, Imperial and Metric -- UM20147, 2026-10-19 17:07 / 17:14.
|
|
PREFS_IMPERIAL = bytes.fromhex(
|
|
"00580900000000000000000000000000000000000000000000000000000000000000000000"
|
|
"000000000100010001000100000000000000010001000000000000000000000000000000"
|
|
"000002000000000000000200000000b42f"
|
|
)
|
|
PREFS_METRIC = bytes.fromhex(
|
|
"00580900000000000000000000000000000000000000000000000000000000010000000000"
|
|
"000000000100010001000100000000000000010001000000000000000000000000000000"
|
|
"000002000000000000000200000000911a"
|
|
)
|
|
# 0x2E, same session.
|
|
BLOCK_2E = bytes.fromhex("001ad50000010000000000000000000000000000000000000000141a")
|
|
|
|
|
|
def content_of(name: str) -> bytes:
|
|
return (FIXTURES / f"{name}.bin").read_bytes()[11:]
|
|
|
|
|
|
# ── The checksum rule ─────────────────────────────────────────────────────────
|
|
|
|
def test_fixtures_are_present():
|
|
"""15 fixtures; a missing directory would otherwise make the parametrised
|
|
tests below collect nothing and pass vacuously."""
|
|
assert len(ALL_FIXTURES) == 15
|
|
|
|
|
|
@pytest.mark.parametrize("path", ALL_FIXTURES, ids=lambda p: p.stem)
|
|
def test_every_real_setup_read_passes_its_checksum(path):
|
|
assert crc_ok(path.read_bytes()[11:])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"block", [PREFS_IMPERIAL, PREFS_METRIC, BLOCK_2E], ids=["0x08-imperial", "0x08-metric", "0x2E"]
|
|
)
|
|
def test_the_rule_is_the_blocks_own_length_not_the_setup_size(block):
|
|
"""One rule covers three blocks: N = content[0:2], CRC over [0:N-2] stored at
|
|
[N:N+2]. An implementation hard-wired to the setup block's 2088 fails here."""
|
|
assert crc_ok(block)
|
|
|
|
|
|
@pytest.mark.parametrize("offset", [5, 144, 1738, 2087], ids=["mode", "note", "trigger", "last-covered"])
|
|
def test_one_changed_byte_fails_the_checksum(offset):
|
|
c = bytearray(content_of("um20147_baseline"))
|
|
c[offset] ^= 0x01
|
|
assert not crc_ok(bytes(c))
|
|
|
|
|
|
def test_a_changed_trailer_fails_the_checksum():
|
|
c = bytearray(content_of("um20147_baseline"))
|
|
c[2091] ^= 0x01
|
|
assert not crc_ok(bytes(c))
|
|
|
|
|
|
@pytest.mark.parametrize("n", [0, 1, 2, 100, 2091], ids=lambda n: f"{n}B")
|
|
def test_a_block_too_short_for_its_own_length_fails(n):
|
|
"""A truncated read must read as bad, never raise IndexError and never pass."""
|
|
assert not crc_ok(content_of("um20147_baseline")[:n])
|
|
|
|
|
|
# ── Fields: every value below is what the operator read off the screen ────────
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, mode_code, mode_name",
|
|
[
|
|
("um20147_waveform", 0x01, "Waveform"),
|
|
("um20147_waveform_manual", 0x02, "Waveform Manual"),
|
|
("um20147_histogram_300s", 0x03, "Histogram"),
|
|
("um20147_baseline", 0x04, "Histogram Combo"),
|
|
],
|
|
)
|
|
def test_record_mode(name, mode_code, mode_name):
|
|
"""Same codes as Series III, one byte later than the S3 analogy put them.
|
|
The keypad menu order is 03 04 01 02, so an off-by-one enum reads wrong here."""
|
|
s = decode_setup(content_of(name))
|
|
assert s.record_mode == mode_code
|
|
assert s.record_mode_name == mode_name
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, rate",
|
|
[("um20147_baseline", 1024), ("um20147_waveform_manual", 2048), ("um20147_4096sps", 4096)],
|
|
)
|
|
def test_sample_rate(name, rate):
|
|
"""4096 puts a literal 0x10 (DLE) at content[6] -- the case that shifted
|
|
every later field by one byte on Series III."""
|
|
assert decode_setup(content_of(name)).sample_rate == rate
|
|
|
|
|
|
@pytest.mark.parametrize("name, seconds", [("um20147_baseline", 2), ("um20147_histogram_300s", 300)])
|
|
def test_histogram_interval(name, seconds):
|
|
"""300 = 01 2c: the high byte is what proves the field is two bytes wide."""
|
|
assert decode_setup(content_of(name)).histogram_interval_s == seconds
|
|
|
|
|
|
@pytest.mark.parametrize("name, seconds", [("um20147_baseline", 1.0), ("um20147_pretrig025_auto", 0.25)])
|
|
def test_pretrigger_is_reported_positive(name, seconds):
|
|
"""Stored as NEGATIVE seconds (-1.0 / -0.25); the screen shows 1.0 / 0.25."""
|
|
assert decode_setup(content_of(name)).pretrigger_s == seconds
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, auto, record_time, auto_stop",
|
|
[("um20147_baseline", False, 10.0, 3), ("um20147_pretrig025_auto", True, 11.0, 1)],
|
|
)
|
|
def test_record_time_window(name, auto, record_time, auto_stop):
|
|
s = decode_setup(content_of(name))
|
|
assert s.auto_record_stop is auto
|
|
assert s.record_time_s == record_time
|
|
assert s.auto_stop_after_s == auto_stop
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, geo_on, mic_on, trig_geo, trig_mic",
|
|
[
|
|
("um20147_baseline", True, True, True, True),
|
|
("um20147_geo_only", True, False, True, False),
|
|
("um20147_mic_only", False, True, False, True),
|
|
("um20147_geo_trigger_off", True, True, False, True),
|
|
],
|
|
)
|
|
def test_active_sensors_and_trigger_sources_are_separate_masks(name, geo_on, mic_on, trig_geo, trig_mic):
|
|
"""content[25] is which sensors are on, content[29] which may trigger.
|
|
geo_trigger_off has both sensors on but only the mic triggering -- a decoder
|
|
reading both from one byte gets that fixture wrong."""
|
|
s = decode_setup(content_of(name))
|
|
assert (s.geophones_active, s.mic_active) == (geo_on, mic_on)
|
|
assert (s.trigger_on_geophones, s.trigger_on_mic) == (trig_geo, trig_mic)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, level",
|
|
[("um20147_baseline", 0.5), ("um20147_waveform_manual", 0.6), ("um12947_2026-09-24", 0.3)],
|
|
)
|
|
def test_geophone_trigger_level_per_channel(name, level):
|
|
s = decode_setup(content_of(name))
|
|
assert s.geo_trigger_ips == pytest.approx((level, level, level), abs=1e-6)
|
|
|
|
|
|
@pytest.mark.parametrize("name, db", [("um20147_baseline", 131.0), ("um20147_mic_140db", 140.0)])
|
|
def test_linear_mic_trigger_is_psi_and_reads_back_as_the_screens_db(name, db):
|
|
s = decode_setup(content_of(name))
|
|
assert s.mic_type == "linear"
|
|
assert s.mic_trigger_psi is not None
|
|
assert s.mic_trigger_db == pytest.approx(db, abs=0.01)
|
|
|
|
|
|
def test_the_mic_comes_from_block_4_not_from_the_stored_setups():
|
|
"""Block 4 is the ACTIVE mic; blocks 5/6 are stored linear/SLM settings.
|
|
With a sound-level mic selected, block 5 still holds a linear mic -- read it
|
|
and the decoder reports a mic that is not plugged in."""
|
|
s = decode_setup(content_of("um20147_sound_level_mic"))
|
|
assert s.mic_type == "sound_level"
|
|
assert s.mic_trigger_psi is None # its unit is not measured; not exposed
|
|
assert s.mic_trigger_db is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"name, enabled, number",
|
|
[
|
|
("um20147_baseline", False, 0),
|
|
("um20147_job_1234", True, 1234),
|
|
("um20147_scaled_distance", False, 1234), # switched off, number kept
|
|
],
|
|
)
|
|
def test_job_number(name, enabled, number):
|
|
s = decode_setup(content_of(name))
|
|
assert s.job_number_enabled is enabled
|
|
assert s.job_number == number
|
|
|
|
|
|
def test_scaled_distance_uses_the_units_own_conversion_constants():
|
|
"""Entered as 123 ft and 45 lb; the screen then showed scaled distance 18.34.
|
|
Stored as metres and kg via ft / 3.2808 and lb / 2.21 -- converting back with
|
|
the textbook 2.20462 shows 44.89 lb, not what the operator typed."""
|
|
s = decode_setup(content_of("um20147_scaled_distance"))
|
|
assert s.scaled_distance_enabled is True
|
|
assert s.distance_ft == pytest.approx(123.0, abs=1e-3)
|
|
assert s.charge_weight_lb == pytest.approx(45.0, abs=1e-3)
|
|
assert s.scaled_distance == pytest.approx(18.34, abs=0.005)
|
|
|
|
|
|
def test_scaled_distance_off_with_no_charge_weight():
|
|
s = decode_setup(content_of("um20147_baseline"))
|
|
assert s.scaled_distance_enabled is False
|
|
assert s.scaled_distance is None # no charge weight: nothing to divide by
|
|
|
|
|
|
def test_notes_are_label_value_pairs():
|
|
s = decode_setup(content_of("um20147_location_bench01"))
|
|
assert list(s.notes) == ["Location", "Client", "Company", "General Notes"]
|
|
assert s.notes["Location"] == "BENCH01"
|
|
|
|
|
|
def test_a_second_unit_on_the_other_firmware_line():
|
|
"""UM12947 (11.0CB), cut from Thor's 9-24 capture: a different unit, firmware
|
|
and setup. Strings are this job's notes as the read returned them."""
|
|
s = decode_setup(content_of("um12947_2026-09-24"))
|
|
assert s.name == "Univ of Pitt-1st Yr. Housing-Loc1 Ruskin.mmb"
|
|
assert s.notes == {
|
|
"Location": "Univ of Pitt-1st Yr Housing-Loc1 Ruskin",
|
|
"Client": "PJ Dick Inc.",
|
|
"Company": "Terra-Mechanics, Inc. - Harrison",
|
|
"General Notes": "Loc#1-Univ of Pitt. - Ruskin Hall",
|
|
}
|
|
|
|
|
|
def test_raw_is_the_content():
|
|
c = content_of("um20147_baseline")
|
|
assert decode_setup(c).raw == c
|
|
|
|
|
|
# ── Errors ────────────────────────────────────────────────────────────────────
|
|
|
|
def test_a_corrupt_block_raises_rather_than_decoding_garbage():
|
|
c = bytearray(content_of("um20147_baseline"))
|
|
c[5] = 0x05 # a mode byte no keypad produces
|
|
with pytest.raises(SetupChecksumError):
|
|
decode_setup(bytes(c))
|
|
|
|
|
|
def test_a_checksum_error_is_a_protocol_error():
|
|
"""Callers already catching ProtocolError around a read keep working."""
|
|
assert issubclass(SetupChecksumError, ProtocolError)
|
|
|
|
|
|
def test_verify_false_decodes_a_corrupt_block_anyway():
|
|
"""For diagnosis: see what a bad read contained instead of only that it was bad."""
|
|
c = bytearray(content_of("um20147_baseline"))
|
|
c[5] = 0x05
|
|
s = decode_setup(bytes(c), verify=False)
|
|
assert s.record_mode_name == "unknown (0x05)"
|
|
|
|
|
|
@pytest.mark.parametrize("verify", [True, False])
|
|
def test_a_short_block_raises_even_without_verify(verify):
|
|
"""verify=False skips the checksum, never the bounds -- a short read would
|
|
otherwise decode as zeros, or raise an IndexError from deep in a slice."""
|
|
with pytest.raises(ProtocolError, match="2092"):
|
|
decode_setup(content_of("um20147_baseline")[:1900], verify=verify)
|