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
107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
"""Series-3 events: listed from headers, one downloaded, mapped into the shared shape."""
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
import os
|
|
import struct
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from device_access_fakes import FakeMiniMateClient, s3_event # noqa: E402
|
|
from sfm.device_access.errors import ( # noqa: E402
|
|
EventNotFound, MalformedReply, StaleEvent, WrongUnit,
|
|
)
|
|
from sfm.device_access.models import UnitAddress # noqa: E402
|
|
from sfm.device_access.s3 import Series3Adapter # noqa: E402
|
|
|
|
TCP = UnitAddress(host="10.0.0.5", tcp_port=12345, port=None, baud=None)
|
|
FIRST = bytes.fromhex("01110000")
|
|
|
|
|
|
def adapter(*clients):
|
|
queue = list(clients)
|
|
seen = []
|
|
|
|
def factory(address, **kw):
|
|
seen.append(kw)
|
|
return queue.pop(0)
|
|
a = Series3Adapter(client_factory=factory)
|
|
a.seen = seen
|
|
return a
|
|
|
|
|
|
def test_listing_maps_each_header_into_the_shared_shape():
|
|
out = adapter(FakeMiniMateClient()).list_events(TCP, "BE18193")
|
|
assert (out.serial, out.series, len(out.events)) == ("BE18193", 3, 2)
|
|
e = out.events[1]
|
|
assert e.event_id == "BE18193:0111245a:20261002093015"
|
|
assert (e.index, e.timestamp, e.record_type) == (1, "2026-10-02T09:30:15", "histogram")
|
|
assert e.peaks_ips == {"tran": 0.05, "vert": 0.08, "long": 0.03}
|
|
assert e.peak_vector_sum_ips == 0.12 and e.detail["mic_psi"] == 0.0001
|
|
assert e.filename and e.filename.startswith("T193")
|
|
|
|
|
|
def test_listing_uses_the_header_walk_and_the_long_timeout():
|
|
fake = FakeMiniMateClient()
|
|
a = adapter(fake)
|
|
a.list_events(TCP, "BE18193")
|
|
assert "list_event_headers" in fake.calls and "download_event_by_key" not in fake.calls
|
|
assert a.seen == [{"timeout": 120.0}]
|
|
assert fake.closed == 1
|
|
|
|
|
|
def test_listing_refuses_a_different_unit():
|
|
with pytest.raises(WrongUnit):
|
|
adapter(FakeMiniMateClient(serial="BE11529")).list_events(TCP, "BE18193")
|
|
|
|
|
|
def test_download_returns_the_event_its_frames_and_the_settings():
|
|
fake = FakeMiniMateClient()
|
|
dl = adapter(fake).download_event(TCP, "BE18193", FIRST, "20261001080000")
|
|
assert (dl.series, dl.event_id, dl.verified) == (3, "BE18193:01110000:20261001080000", None)
|
|
assert dl.event is fake.events[0] and dl.a5_frames == ["frame"]
|
|
assert dl.compliance is fake.compliance and dl.filename.startswith("T193")
|
|
assert dl.timestamp == datetime.datetime(2026, 10, 1, 8, 0, 0)
|
|
|
|
|
|
def test_download_refuses_a_different_unit_before_downloading():
|
|
fake = FakeMiniMateClient(serial="BE11529")
|
|
with pytest.raises(WrongUnit):
|
|
adapter(fake).download_event(TCP, "BE18193", FIRST, None)
|
|
assert "download_event_by_key" not in fake.calls
|
|
|
|
|
|
def test_a_key_that_is_not_stored_is_404():
|
|
with pytest.raises(EventNotFound):
|
|
adapter(FakeMiniMateClient()).download_event(TCP, "BE18193", bytes.fromhex("0111ffff"), None)
|
|
|
|
|
|
def test_a_stale_id_is_refused_rather_than_downloading_another_event():
|
|
with pytest.raises(StaleEvent, match="20261001080000"):
|
|
adapter(FakeMiniMateClient()).download_event(TCP, "BE18193", FIRST, "20250101000000")
|
|
|
|
|
|
def test_an_id_without_a_timestamp_skips_the_check():
|
|
dl = adapter(FakeMiniMateClient()).download_event(TCP, "BE18193", FIRST, None)
|
|
assert dl.event_id == "BE18193:01110000:20261001080000"
|
|
|
|
|
|
def test_a_failed_5a_is_a_clear_error():
|
|
fake = FakeMiniMateClient(events=[s3_event(0, "01110000",
|
|
datetime.datetime(2026, 10, 1, 8, 0, 0), frames=False)])
|
|
with pytest.raises(MalformedReply, match="5A"):
|
|
adapter(fake).download_event(TCP, "BE18193", FIRST, None)
|
|
|
|
|
|
def test_the_64k_page_bug_is_named_not_a_bare_500():
|
|
fake = FakeMiniMateClient()
|
|
|
|
def boom(key4):
|
|
raise struct.error("argument out of range")
|
|
fake.download_event_by_key = boom
|
|
with pytest.raises(MalformedReply, match="64 KB"):
|
|
adapter(fake).download_event(TCP, "BE18193", FIRST, None)
|