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
132 lines
5.4 KiB
Python
132 lines
5.4 KiB
Python
"""
|
|
Tests for the .MLG routes in sfm/server.py (spec §5).
|
|
|
|
httpx is not installed in this venv, so starlette's TestClient tests skip; these
|
|
call the route functions directly, passing every parameter (a FastAPI Query()
|
|
default is not a value).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import io
|
|
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 fastapi import HTTPException, UploadFile
|
|
|
|
from sfm.database import SeismoDb
|
|
from sfm.waveform_store import WaveformStore
|
|
|
|
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
|
|
|
|
|
|
def _env(tmp_path, monkeypatch):
|
|
import sfm.server as srv
|
|
db, store = SeismoDb(tmp_path / "t.db"), WaveformStore(tmp_path / "waveforms")
|
|
monkeypatch.setattr(srv, "_db", db, raising=False)
|
|
monkeypatch.setattr(srv, "_store", store, raising=False)
|
|
return srv, db, store
|
|
|
|
|
|
def _import(srv, uploads, *, serial=None, call_in=True, mtimes=None):
|
|
files = [UploadFile(file=io.BytesIO(data), filename=name) for name, data in uploads]
|
|
return asyncio.run(srv.db_import_mlg_file(files=files, serial=serial, call_in=call_in, mtimes=mtimes))
|
|
|
|
|
|
def _fixture(name):
|
|
return name, (FIX / name).read_bytes()
|
|
|
|
|
|
def test_import_route_stores_good_file_and_refuses_bad_one(tmp_path, monkeypatch):
|
|
srv, db, store = _env(tmp_path, monkeypatch)
|
|
name, good = _fixture("BE128443065.MLG")
|
|
bad = bytearray(good)
|
|
bad[18 + 292 + 40] ^= 0xFF
|
|
out = _import(srv, [(name, good), ("BE128443099.MLG", bytes(bad))])
|
|
assert out["count"] == 2
|
|
assert [r["status"] for r in out["results"]] == ["ok", "error"]
|
|
assert "record 1" in out["results"][1]["detail"]
|
|
assert [f["file_name"] for f in db.query_mlg_files("BE12844")] == ["BE128443065.MLG"]
|
|
assert sorted(p.name for p in store.root.rglob("*.MLG")) == ["BE128443065.MLG"]
|
|
|
|
|
|
def test_import_route_reads_mtimes(tmp_path, monkeypatch):
|
|
srv, db, _ = _env(tmp_path, monkeypatch)
|
|
out = _import(srv, [_fixture("BE128443065.MLG")], mtimes='{"BE128443065.MLG": 1788734760}')
|
|
f = db.get_mlg_file(out["results"][0]["file_id"])
|
|
assert f["file_mtime"] == "2026-09-06T22:46:00+00:00"
|
|
|
|
|
|
def test_import_route_rejects_bad_mtimes(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
with pytest.raises(HTTPException) as exc:
|
|
_import(srv, [_fixture("BE128443065.MLG")], mtimes="not json")
|
|
assert exc.value.status_code == 400
|
|
|
|
|
|
def test_timeline_and_monitoring_at_routes(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
_import(srv, [_fixture("BE8081321.MLG")])
|
|
tl = srv.db_unit_timeline(serial="BE8081", from_dt=None, to_dt=None)
|
|
assert tl["serial"] == "BE8081" and len(tl["sessions"]) == 4
|
|
windowed = srv.db_unit_timeline(serial="BE8081", from_dt="2019-04-13T00:00:00", to_dt=None)
|
|
assert [s["start"] for s in windowed["sessions"]] == [
|
|
"2019-04-12T18:38:14", "2019-04-13T07:05:15", "2019-04-15T12:47:05"]
|
|
assert [g["meaning"] for g in windowed["gaps"]] == ["call-in", "lost power"]
|
|
m = srv.db_unit_monitoring_at(serial="BE8081", at="2019-04-14T12:00:00")
|
|
assert m["serial"] == "BE8081" and m["status"] == "not_monitoring"
|
|
with pytest.raises(HTTPException) as exc:
|
|
srv.db_unit_monitoring_at(serial="BE8081", at="yesterday")
|
|
assert exc.value.status_code == 400
|
|
|
|
|
|
def test_timeline_routes_for_unknown_serial(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
tl = srv.db_unit_timeline(serial="BE0000", from_dt=None, to_dt=None)
|
|
assert (tl["sessions"], tl["gaps"], tl["call_ins"], tl["clock_steps"]) == ([], [], [], [])
|
|
assert srv.db_unit_monitoring_at(serial="BE0000", at="2026-09-08T10:00:00")["status"] == "unknown"
|
|
|
|
|
|
def test_mlg_records_route(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
_import(srv, [_fixture("BE8081321.MLG")])
|
|
out = srv.db_mlg_records(serial="BE8081", from_dt=None, to_dt=None)
|
|
assert out["count"] == 6 and out["records"][0]["start_time"] == "2019-04-12T17:17:59"
|
|
|
|
|
|
def test_mlg_file_route_returns_original_bytes(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
name, data = _fixture("BE8081321.MLG")
|
|
file_id = _import(srv, [(name, data)])["results"][0]["file_id"]
|
|
resp = srv.db_mlg_file(file_id=file_id)
|
|
assert Path(resp.path).read_bytes() == data and resp.filename == name
|
|
with pytest.raises(HTTPException) as exc:
|
|
srv.db_mlg_file(file_id="no-such-id")
|
|
assert exc.value.status_code == 404
|
|
|
|
|
|
def test_times_with_an_offset_or_empty_are_400(tmp_path, monkeypatch):
|
|
srv, _, _ = _env(tmp_path, monkeypatch)
|
|
for at in ("2019-04-14T12:00:00Z", "2019-04-14T12:00:00+00:00", ""):
|
|
with pytest.raises(HTTPException) as exc:
|
|
srv.db_unit_monitoring_at(serial="BE8081", at=at)
|
|
assert exc.value.status_code == 400, at
|
|
with pytest.raises(HTTPException) as exc:
|
|
srv.db_mlg_records(serial="BE8081", from_dt="2019-04-14T12:00:00Z", to_dt=None)
|
|
assert exc.value.status_code == 400
|
|
|
|
|
|
def test_bad_mtime_values_are_refused_before_anything_is_stored(tmp_path, monkeypatch):
|
|
srv, db, _ = _env(tmp_path, monkeypatch)
|
|
with pytest.raises(HTTPException) as exc:
|
|
_import(srv, [_fixture("BE128443065.MLG"), _fixture("BE128443067.MLG")],
|
|
mtimes='{"BE128443067.MLG": "noon"}')
|
|
assert exc.value.status_code == 400
|
|
assert db.query_mlg_files("BE12844") == []
|