Files
seismo-relay/tests/test_import_mlg_dir.py
serversdownandClaude Opus 5.5 ba826dfac6 fix(mlg): final-review fixes
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
2026-11-13 14:17:08 -05:00

59 lines
2.3 KiB
Python

"""
Tests for scripts/import_mlg_dir.py — bulk import of a directory of .MLGs (spec §7).
"""
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from scripts.import_mlg_dir import main
from sfm.database import SeismoDb
FIX = Path(__file__).parent / "fixtures" / "mlg-11-9-26"
def _counts(text: str) -> dict:
return {k: int(v) for k, v in (line.split() for line in text.strip().splitlines())}
def test_import_dir_counts(tmp_path, capsys):
src = tmp_path / "in" / "nested"
shutil.copytree(FIX, src)
db_path, store = tmp_path / "t.db", tmp_path / "store"
args = [str(tmp_path / "in"), "--db-path", str(db_path), "--store-root", str(store)]
assert main(args + ["--dry-run"]) == 0
dry = _counts(capsys.readouterr().out)
assert (dry["files"], dry["error"], dry["empty"]) == (16, 0, 3)
assert not db_path.exists() and not store.exists()
assert main(args) == 0
first = _counts(capsys.readouterr().out)
assert (first["files"], first["ok"], first["error"], first["empty"], first["unresolved"]) == (16, 16, 0, 3, 0)
assert first["skipped"] == 3 # BE115292.MLG repeats BE11529.MLG's records
assert [f["file_name"] for f in SeismoDb(db_path).query_mlg_files("BE6907")] == [
"BE6907750.MLG", "BE6907751.MLG", "BE6907899.MLG", "BE6907900.MLG",
"BE6907901.MLG", "BE6907902.MLG"]
assert main(args) == 0
again = _counts(capsys.readouterr().out)
assert (again["duplicate"], again["ok"], again["inserted"]) == (16, 0, 0)
def test_empty_files_are_resolved_whatever_the_folder_order(tmp_path, capsys):
"""The empty file sorts first by name and by time here; the two-pass order still
gives it its unit."""
d = tmp_path / "in"
d.mkdir()
shutil.copy2(FIX / "BE6907751.MLG", d / "BE6907751.MLG")
shutil.copy2(FIX / "BE6907750.MLG", d / "BE6907750.MLG")
os.utime(d / "BE6907751.MLG", (1_700_000_000, 1_700_000_000)) # older than its real file
os.utime(d / "BE6907750.MLG", (1_800_000_000, 1_800_000_000))
assert main([str(d), "--db-path", str(tmp_path / "t.db"), "--store-root", str(tmp_path / "s")]) == 0
assert _counts(capsys.readouterr().out)["unresolved"] == 0