A fresh whole-branch review found four Important issues; three of its Minors and one item it set aside were re-graded up. Each has a test that failed first. - A windowed timeline 500'd when a file came without its modified time (Python 3.10 rejects the 'Z' of received_at). received_at is now +00:00 and timeline times parse 'Z' too. - On the clock-step day an event-triggered call-in moved the event into the next session and lost the step. An event now joins the 'monitoring started' record of its own file. - A call-in whose records came first in another file (a cable download) read 'nothing_new'. Each file now keeps its own call summary (last_stop, no_file_stops) from when it arrived. - Sessions and call-ins now carry record_ids / file_ids / file_id, the shot report's proof (spec 5). - A non-'started' record with no stop made the unit's timeline 500 for good; it now opens a session whose end is unknown. - A serial from a file's bytes (or a hint) could write outside the store; only plain serials are used, and records with no serial are refused, not dropped. - Times with a UTC offset, an empty 'at', or a bad mtimes value gave 500s (or stored half a request); they are now 400s, checked first. - A missing call-in file made the gap after its Modem Exit read 'not monitoring' for days. Files are numbered <serial><counter>; a gap across a missing number is now 'records missing' (unknown). A long gap after a call-in with no number missing is 'no restart after a call-in': real outages on the DL2 data (BE6907 14 days from 2026-10-07; BE8081 over Labor Day 2019). Real fixtures BE6907 #899-#902 added. - Docs: BE8081 did write one empty file (BE80811.MLG). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
127 lines
5.0 KiB
Python
127 lines
5.0 KiB
Python
"""
|
|
Tests for sfm/mlg_ingest.py — one .MLG in: verified, its serial resolved, the
|
|
original kept, its records stored once (spec §3, §5, §6).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from sfm.database import SeismoDb
|
|
from sfm.mlg_ingest import ingest_mlg, resolve_serial
|
|
from sfm.waveform_store import WaveformStore
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
|
|
UTC = datetime.timezone.utc
|
|
|
|
|
|
def _env(tmp_path):
|
|
return SeismoDb(tmp_path / "t.db"), WaveformStore(tmp_path / "waveforms")
|
|
|
|
|
|
def _ingest(db, store, name, data=None, **kw):
|
|
return ingest_mlg(db, store, name, data if data is not None else (FIX / name).read_bytes(), **kw)
|
|
|
|
|
|
def test_ingest_ok(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
res = _ingest(db, store, "BE128443067.MLG")
|
|
assert (res["status"], res["serial"], res["records"], res["inserted"], res["skipped"]) == (
|
|
"ok", "BE12844", 3, 3, 0)
|
|
assert res["empty"] is False and res["serial_unresolved"] is False and res["file_id"]
|
|
assert (store.root / "BE12844" / "mlg" / "BE128443067.MLG").read_bytes() == (
|
|
FIX / "BE128443067.MLG").read_bytes()
|
|
|
|
|
|
def test_ingest_twice_is_duplicate(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
_ingest(db, store, "BE128443067.MLG")
|
|
res = _ingest(db, store, "BE128443067.MLG")
|
|
assert res["status"] == "duplicate" and res["inserted"] == 0
|
|
assert len(db.query_mlg_files("BE12844")) == 1
|
|
|
|
|
|
def test_corrupted_file_stores_nothing(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
data = bytearray((FIX / "BE128443065.MLG").read_bytes())
|
|
data[18 + 292 + 40] ^= 0xFF
|
|
res = _ingest(db, store, "BE128443065.MLG", bytes(data))
|
|
assert res["status"] == "error" and "record 1" in res["detail"]
|
|
assert db.query_mlg_files("BE12844") == [] and db.query_mlg_records("BE12844") == []
|
|
assert not list(store.root.rglob("mlg"))
|
|
|
|
|
|
def test_truncated_file_is_an_error(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
res = _ingest(db, store, "BE128443065.MLG", (FIX / "BE128443065.MLG").read_bytes()[:-5])
|
|
assert res["status"] == "error" and "not an MLG" in res["detail"]
|
|
assert not list(store.root.rglob("mlg"))
|
|
|
|
|
|
def test_empty_file_takes_known_serial(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
_ingest(db, store, "BE6907750.MLG")
|
|
res = _ingest(db, store, "BE6907751.MLG")
|
|
assert (res["status"], res["serial"], res["empty"], res["serial_unresolved"]) == (
|
|
"ok", "BE6907", True, False)
|
|
assert (store.root / "BE6907" / "mlg" / "BE6907751.MLG").exists()
|
|
|
|
|
|
def test_empty_file_without_known_serial_is_unresolved(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
res = _ingest(db, store, "BE6907751.MLG")
|
|
assert (res["status"], res["serial"], res["serial_unresolved"]) == ("ok", None, True)
|
|
assert (store.root / "_unresolved" / "mlg" / "BE6907751.MLG").exists()
|
|
|
|
|
|
def test_empty_file_takes_the_serial_hint(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
res = _ingest(db, store, "BE6907751.MLG", serial_hint="BE6907")
|
|
assert (res["serial"], res["serial_unresolved"]) == ("BE6907", False)
|
|
|
|
|
|
def test_empty_file_takes_the_longest_known_serial():
|
|
assert resolve_serial("BE11010304.MLG", None, None, {"BE1101", "BE11010"}) == "BE11010"
|
|
assert resolve_serial("XY123.MLG", None, None, {"BE1101"}) is None
|
|
assert resolve_serial("BE6907751.MLG", "BE12844", "BE6907", {"BE6907"}) == "BE12844"
|
|
assert resolve_serial("be6907751.mlg", None, None, {"BE6907"}) == "BE6907"
|
|
assert resolve_serial("BE6907X51.MLG", None, None, {"BE6907"}) is None # not digits after
|
|
|
|
|
|
def test_file_mtime_and_call_in_are_stored(tmp_path):
|
|
db, store = _env(tmp_path)
|
|
mtime = datetime.datetime(2026, 9, 6, 22, 46, tzinfo=UTC)
|
|
res = _ingest(db, store, "BE128443065.MLG", file_mtime=mtime, call_in=False)
|
|
f = db.get_mlg_file(res["file_id"])
|
|
assert (f["file_mtime"], f["call_in"]) == ("2026-09-06T22:46:00+00:00", 0)
|
|
|
|
|
|
def test_a_serial_that_is_not_a_plain_serial_is_refused(tmp_path):
|
|
from minimateplus.mlg_file import mlg_bytes, parse_mlg
|
|
db, store = _env(tmp_path)
|
|
good = parse_mlg((FIX / "BE128443065.MLG").read_bytes())
|
|
for r in good.records:
|
|
r.serial = "../hdr"
|
|
bad = mlg_bytes(type(good)(serial="../hdr", records=good.records))
|
|
res = _ingest(db, store, "BE128443065.MLG", bad)
|
|
assert res["status"] == "error" and "serial" in res["detail"]
|
|
res = _ingest(db, store, "BE6907751.MLG", serial_hint="../escaped")
|
|
assert res["status"] == "error" and "serial" in res["detail"]
|
|
assert not (tmp_path / "hdr").exists() and not (tmp_path / "escaped").exists()
|
|
assert not list(tmp_path.rglob("*.MLG"))
|
|
|
|
|
|
def test_records_with_no_serial_are_refused_not_dropped(tmp_path):
|
|
from minimateplus.mlg_file import mlg_bytes, parse_mlg
|
|
db, store = _env(tmp_path)
|
|
m = parse_mlg((FIX / "BE128443065.MLG").read_bytes())
|
|
for r in m.records:
|
|
r.serial = ""
|
|
res = _ingest(db, store, "XX0001.MLG", mlg_bytes(type(m)(serial="", records=m.records)))
|
|
assert res["status"] == "error" and "serial" in res["detail"]
|