"""Series-4 events: the chain walk mapped into the shared shape; download at the cursor.""" from __future__ import annotations import datetime import os import sys import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from device_access_fakes import THOR_IDF, FakeMicromateClient # noqa: E402 from micromate.protocol import THOR_CHUNK_SIZE # noqa: E402 from sfm.device_access.errors import EventNotFound, StaleEvent # noqa: E402 from sfm.device_access.s4 import s4_download_event, s4_list_events # noqa: E402 W_KEY, H_KEY = bytes.fromhex("055d4a81"), bytes.fromhex("055d4a83") def test_listing_maps_each_ref_into_the_shared_shape(): out = s4_list_events(FakeMicromateClient(), "UM20147") assert (out.serial, out.series, len(out.events)) == ("UM20147", 4, 2) e = out.events[0] assert e.event_id == "UM20147:055d4a81:20250531135901" assert (e.index, e.timestamp, e.record_type) == (0, "2025-05-31T13:59:01", "waveform") assert e.peaks_ips == {"tran": 0.1, "vert": 0.15, "long": 0.05} assert e.filename == "UM20147_20250531135901.IDFW" assert e.detail["mic"] == 0.0002 and e.detail["size_bytes"] > 0 def test_download_walks_to_the_event_and_uses_thors_chunk_size(): fake = FakeMicromateClient() dl = s4_download_event(fake, "UM20147", W_KEY, "20250531135901") assert "iter_events" in fake.calls and "list_events" not in fake.calls assert fake.fetched == [("055d4a81", THOR_CHUNK_SIZE)] assert dl.idf_bytes == (THOR_IDF / "UM20147_20250531135901.IDFW").read_bytes() assert (dl.series, dl.filename, dl.verified) == (4, "UM20147_20250531135901.IDFW", True) def test_download_carries_the_units_timestamp_and_its_own_record(): # What the unit's 0x0C said: the store looks up an existing row by the # timestamp, and fills what the file alone cannot give from the record. ref = FakeMicromateClient().event_refs[0] ref.sensor_location, ref.setup = "Loc A", "test2" fake = FakeMicromateClient(event_refs=[ref]) dl = s4_download_event(fake, "UM20147", W_KEY, "20250531135901") assert dl.timestamp == datetime.datetime(2025, 5, 31, 13, 59, 1) assert dl.record == {"peak_vector_sum_ips": 0.2, "sensor_location": "Loc A", "setup": "test2"} def test_the_walk_stops_at_the_event_rather_than_reading_ahead(): fake = FakeMicromateClient() s4_download_event(fake, "UM20147", W_KEY, None) assert fake.cursor == 0 def test_a_histogram_has_no_self_check_so_verified_is_none(): dl = s4_download_event(FakeMicromateClient(), "UM20147", H_KEY, None) assert dl.verified is None and dl.filename.endswith(".IDFH") def test_a_stale_id_is_refused_and_nothing_is_downloaded(): fake = FakeMicromateClient() with pytest.raises(StaleEvent, match="20250531135901"): s4_download_event(fake, "UM20147", W_KEY, "20240101000000") assert fake.fetched == [] def test_a_key_that_is_not_stored_is_404(): with pytest.raises(EventNotFound): s4_download_event(FakeMicromateClient(), "UM20147", bytes.fromhex("deadbeef"), None) def test_a_record_whose_timestamp_did_not_decode_is_not_called_stale(): # Mirrors Series 3 (F3): an unreadable 0C timestamp is a comms/decode fault, # not proof the unit was erased -- and nothing is downloaded. from sfm.device_access.errors import MalformedReply fake = FakeMicromateClient() fake.event_refs[0].timestamp = None with pytest.raises(MalformedReply, match="0C"): s4_download_event(fake, "UM20147", W_KEY, "20250531135901") assert fake.fetched == []