minimateplus/mlg_file.py parses the measured format: an 18-byte prefix, then length-led 292-byte records, each with a CRC-16/AUG-CCITT over [0:288] stored little-endian. A record that fails its checksum is returned flagged, never silently dropped. One shared CRC function (minimateplus/crc.py) replaces micromate/setup_block's copy. Fixtures: 12 real .MLG files (DL2's ACH folder + 3 bench files) in tests/fixtures/mlg-11-9-26/, force-added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
111 lines
4.4 KiB
Python
111 lines
4.4 KiB
Python
"""
|
|
Tests for minimateplus/mlg_file.py — Blastware's monitor log (.MLG) reader and writer.
|
|
|
|
Fixtures (tests/fixtures/mlg-11-9-26/) are real files: DL2's ACH folder
|
|
(BE12844, BE6907 from 2026; BE8081 from 2018-19) and three bench files.
|
|
Expected values come from an independent scratch parser that was checked
|
|
against DL2's event-file footers (785 / 785 BE8081 records matched by stop,
|
|
flag word and trigger source), never from the code under test. Format:
|
|
docs/instantel_protocol_reference.md D.4, spec
|
|
docs/superpowers/specs/2026-11-09-mlg-unit-timeline-design.md.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
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 minimateplus.crc import crc16_aug_ccitt
|
|
from minimateplus.mlg_file import MlgFormatError, parse_mlg, read_mlg
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
|
|
dt = datetime.datetime
|
|
|
|
|
|
def test_crc_matches_the_micromate_rule():
|
|
from micromate.setup_block import _crc16_aug_ccitt
|
|
assert crc16_aug_ccitt(b"123456789") == _crc16_aug_ccitt(b"123456789") == 0xE5CC
|
|
|
|
|
|
def test_every_fixture_record_passes_its_checksum():
|
|
files = sorted(FIX.glob("*.MLG"))
|
|
assert len(files) == 12
|
|
for p in files:
|
|
assert parse_mlg(p.read_bytes()).crc_failures == [], p.name
|
|
|
|
|
|
def test_closed_records():
|
|
m = read_mlg(FIX / "BE128443065.MLG")
|
|
assert m.serial == "BE12844" and len(m.records) == 2
|
|
r1, r2 = m.records
|
|
assert (r1.index, r1.start, r1.stop, r1.flags, r1.trigger_source) == (
|
|
1, dt(2026, 9, 5, 18, 47, 26), dt(2026, 9, 5, 20, 0, 0), 0x0010, 0)
|
|
assert r1.description == "Timer Stop"
|
|
assert r1.trigger_level == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
assert r1.serial == "BE12844"
|
|
assert (r2.start, r2.stop, r2.description) == (
|
|
dt(2026, 9, 6, 6, 0, 13), dt(2026, 9, 6, 18, 45, 0), "Modem Exit")
|
|
|
|
|
|
def test_monitoring_started_and_an_event():
|
|
m = read_mlg(FIX / "BE128443067.MLG")
|
|
start, ev = m.records[1], m.records[2]
|
|
assert start.is_start and start.stop is None
|
|
assert start.description == "Monitoring started"
|
|
assert start.trigger_level == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
assert (ev.flags, ev.trigger_source, ev.description) == (0x0001, 0x0001, "Event Recorded")
|
|
assert ev.event_time == dt(2026, 9, 8, 13, 38, 22)
|
|
assert ev.text == "Geo: 0.0500 in/s Mic: 125.0 dB(L)\rTran: 0.0500 in/s"
|
|
assert ev.trigger_level == "Geo: 0.0500 in/s Mic: 125.0 dB(L)"
|
|
|
|
|
|
def test_histogram_with_a_waveform_event():
|
|
r = read_mlg(FIX / "BE8081.MLG").records
|
|
assert (r[1].flags, r[1].trigger_source, r[1].description) == (
|
|
0x0021, 0x0400, "Event Recorded Modem Exit")
|
|
assert r[1].trigger_level == "Histogram"
|
|
assert (r[2].trigger_source, r[2].trigger_level) == (0x0002, "Geo: 0.300 in/s")
|
|
|
|
|
|
def test_trigger_level_with_or_without_a_space_before_the_colon():
|
|
a = read_mlg(FIX / "BE11529.MLG").records[0]
|
|
b = read_mlg(FIX / "BE115292.MLG").records[0]
|
|
assert a.text == "Trigger Level: Histogram" and b.text == "Trigger Level : Histogram"
|
|
assert a.trigger_level == b.trigger_level == "Histogram"
|
|
|
|
|
|
def test_empty_file():
|
|
m = read_mlg(FIX / "BE6907751.MLG")
|
|
assert m.serial is None and m.records == [] and m.empty
|
|
|
|
|
|
def test_a_flipped_byte_fails_only_its_record():
|
|
data = bytearray((FIX / "BE128443065.MLG").read_bytes())
|
|
data[18 + 292 + 40] ^= 0xFF # inside record 1's text
|
|
m = parse_mlg(bytes(data))
|
|
assert m.crc_failures == [1]
|
|
assert m.records[0].crc_ok is False and m.records[1].crc_ok is True
|
|
|
|
|
|
def test_a_flipped_byte_in_the_header_record_is_reported():
|
|
data = bytearray((FIX / "BE128443065.MLG").read_bytes())
|
|
data[18 + 100] ^= 0xFF # inside the header record's zero area
|
|
assert parse_mlg(bytes(data)).crc_failures == [0]
|
|
|
|
|
|
@pytest.mark.parametrize("mangle", [
|
|
lambda d: d[:-1], # not 18 + 292·n
|
|
lambda d: b"\x00" + d[1:], # wrong prefix
|
|
lambda d: d[:18] + b"\x23" + d[19:], # header length field not 290
|
|
lambda d: d[:6], # shorter than the prefix
|
|
], ids=["not-whole", "prefix", "length-field", "short"])
|
|
def test_structure_errors_raise(mangle):
|
|
with pytest.raises(MlgFormatError):
|
|
parse_mlg(mangle((FIX / "BE128443065.MLG").read_bytes()))
|