"""Storing a downloaded event: the same store + DB writes every ingest path makes.""" 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 device_access_fakes import FakeMiniMateClient, s3_event # noqa: E402 from sfm.database import SeismoDb # noqa: E402 from sfm.device_access.models import DownloadedEvent # noqa: E402 from sfm.event_ingest import backfill_series3_event, persist_download # noqa: E402 from sfm.waveform_store import WaveformStore # noqa: E402 THOR_IDF = Path(__file__).parent / "fixtures" / "thor-idf" @pytest.fixture def sdm(tmp_path): return WaveformStore(tmp_path / "waveforms"), SeismoDb(tmp_path / "seismo.db") def s4_download(name, verified=True): return DownloadedEvent(event_id=f"UM20147:055d4a81:{name[8:22]}", serial="UM20147", series=4, filename=name, verified=verified, idf_bytes=(THOR_IDF / name).read_bytes()) def test_a_series4_waveform_is_stored_and_findable(sdm): store, db = sdm out = persist_download(s4_download("UM20147_20250531135901.IDFW"), store=store, db=db) assert out.stored_filename == "UM20147_20250531135901.IDFW" and out.inserted assert (store.root / "UM20147" / out.stored_filename).exists() row = db.get_event(out.db_event_id) assert row["serial"] == "UM20147" and row["device_family"] == "series4" assert out.verified is True and out.sha256 def test_a_series4_histogram_is_stored(sdm): store, db = sdm out = persist_download(s4_download("UM20147_20250330070110.IDFH", verified=None), store=store, db=db) assert out.inserted and out.db_event_id and out.verified is None def test_storing_the_same_event_twice_refreshes_rather_than_duplicates(sdm): store, db = sdm first = persist_download(s4_download("UM20147_20250531135901.IDFW"), store=store, db=db) again = persist_download(s4_download("UM20147_20250531135901.IDFW"), store=store, db=db) assert (first.inserted, again.inserted) == (True, False) assert again.db_event_id == first.db_event_id assert len(db.query_events(serial="UM20147")) == 1 class RecordingStore: def __init__(self): self.saved = [] def save(self, ev, serial, a5_frames, *, geo_range="normal", source_kind="sfm-live"): self.saved.append((ev, serial, a5_frames, geo_range)) return {"filename": "T193LQ9K.OE0", "filesize": 8708, "sha256": "ab" * 32} def test_a_series3_event_is_backfilled_then_stored_as_series3(tmp_path): db = SeismoDb(tmp_path / "seismo.db") store = RecordingStore() fake = FakeMiniMateClient() ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1, 8, 0, 0)) dl = DownloadedEvent(event_id="BE18193:01110000:20261001080000", serial="BE18193", series=3, filename="T193LQ9K.OE0", verified=None, event=ev, a5_frames=["frame"], compliance=fake.compliance) out = persist_download(dl, store=store, db=db) saved_ev, serial, frames, geo_range = store.saved[0] assert (serial, frames, geo_range) == ("BE18193", ["frame"], 0) assert saved_ev.sample_rate == 1024 and saved_ev.rectime_seconds == 3.0 assert db.get_event(out.db_event_id)["device_family"] == "series3" assert out.stored_filename == "T193LQ9K.OE0" def test_backfill_derives_total_samples_only_when_the_decoded_one_is_implausible(): cc = FakeMiniMateClient().compliance # 1024 sps, 3.0 s ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1)) ev.total_samples = 40_000 backfill_series3_event(ev, cc) assert ev.total_samples == 3072 ev.total_samples = 3000 backfill_series3_event(ev, cc) assert ev.total_samples == 3000 def test_backfill_without_settings_changes_nothing(): ev = s3_event(0, "01110000", datetime.datetime(2026, 10, 1)) backfill_series3_event(ev, None) assert ev.sample_rate is None