Files
seismo-relay/tests/test_device_access_s4_events.py
T
serversdownandClaude Opus 5.5 ef096e3082 fix(sfm): a download never downgrades a stored event -- an existing row is returned, nothing written
THOR's watcher (and Blastware's ACH) store every event through
/db/import/idf_file and /db/import/blastware_file, with the paired report.
Storing a download of the same event afterwards overwrote that richer copy:
on Series 4 the file, .h5 and sidecar were rewritten (extensions dropped);
on Series 3 the row lost PVS, project strings and ZC frequencies and was
repointed at a second, review-less file set.

persist_download now looks up (serial, timestamp) first.  A row there is
returned as it stands -- inserted false, detail.already_stored true -- with
no store call, no insert_events and no sidecar write.  Never merge.

- DownloadedEvent gains `timestamp` (the unit's own event time) and
  `record` (Series 4's 0x0C fields); both adapters set them.
- The Series-3 `event is None` guard now runs before backfill/save, with a
  message that is true there ("nothing was stored").
- Tests build the watcher's state exactly as the import endpoints do and
  prove the row dict, the sidecar bytes and the file mtimes are unchanged.

No schema change, no migration, no codec change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-10-21 17:35:23 -04:00

72 lines
3.0 KiB
Python

"""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)