mirror of
https://github.com/zvx-echo6/meshai.git
synced 2026-06-10 17:04:45 +02:00
Three more per-adapter handlers landing in the same v0.5.9-incident-pipeline pattern: nws_handler.py with severity-floor gate (Warning+ broadcasts only, Moderate/Minor/Unknown skipped to event_log handled=0), event-type emoji map, CAP-id-based first-sight dedup via nws_alerts table; quake_handler.py with magnitude-floor gate (M3.0 globally + M2.5 within 250mi of Idaho centroid + tsunami at any M) using Haversine for the distance check, USGS data.place curated string preferred for the place anchor, leading emoji escalation (🌐 routine / ⚠️ M5+ / 🚨 tsunami), Magnitude spelled out per Matts call; swpc_handler.py with aggressive G3+/R3+/S1+ gate, plain-English wire headlines with (NOAA scale / underlying scalar) tail tag per Matts option C (e.g. "Strong geomagnetic storm (G3/Kp7) -- HF degraded, aurora possible"), routine Kp + protons persisted to swpc_events.payload_json for trending but never broadcast. All three share the v0.5.9 universal freshness gate and the no-Update first-sight-only pattern. Persistence uses the existing v0.5.8b nws_alerts, quake_events, swpc_events tables -- no migrations needed. Tests: was 634 (v0.5.9 baseline), now 686 (+52 net new; over-delivered because parametrized emoji map adds 14 rows). Synthetic probe over the 4 nws + 1 quake + 16,217 swpc captured envelopes from the batched investigation: Phase 1 = 0/0/0 broadcasts (all real captures correctly filtered by their respective gates); Phase 2 = 5/5 synthesized fresh test events broadcast correctly (Severe T-Storm warning, M4.1 Garden Valley quake, G3 geomagnetic storm, X1.2 flare, S1 proton). WFIGS handler unchanged. usgs_nwis deferred to v0.5.12 (threshold-curation work). Master OFF in prod. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
218 lines
6.8 KiB
Python
218 lines
6.8 KiB
Python
"""Tests for v0.5.10 SWPC space-weather handler."""
|
|
import pytest
|
|
|
|
from meshai.central.swpc_handler import handle_swpc
|
|
from meshai.persistence import close_thread_connection, init_db
|
|
from meshai.persistence import db as persistence_db
|
|
|
|
|
|
@pytest.fixture
|
|
def mem_db(monkeypatch, tmp_path):
|
|
db_path = str(tmp_path / "swpc-test.sqlite")
|
|
monkeypatch.setenv("MESHAI_DB_PATH", db_path)
|
|
persistence_db._initialised.clear()
|
|
close_thread_connection()
|
|
conn = init_db()
|
|
yield conn
|
|
close_thread_connection()
|
|
persistence_db._initialised.discard(db_path)
|
|
|
|
|
|
def _kindex_env(*, kp=3.0, event_id="kp_2026_06_05_15Z"):
|
|
return {
|
|
"id": event_id, "subject": "central.space.kindex",
|
|
"data": {
|
|
"id": event_id, "adapter": "swpc_kindex",
|
|
"category": "space.kindex", "severity": 0,
|
|
"geo": {},
|
|
"data": {"id": event_id, "kp_index": kp,
|
|
"time": "2026-06-05T15:00:00Z"},
|
|
},
|
|
}
|
|
|
|
|
|
def _protons_env(*, flux=1.0, event_id="p_2026_06_05_15Z"):
|
|
return {
|
|
"id": event_id, "subject": "central.space.proton_flux",
|
|
"data": {
|
|
"id": event_id, "adapter": "swpc_protons",
|
|
"category": "space.proton_flux", "severity": 0,
|
|
"geo": {},
|
|
"data": {"id": event_id, "p10mev": flux,
|
|
"time": "2026-06-05T15:00:00Z"},
|
|
},
|
|
}
|
|
|
|
|
|
def _alert_env(*, flare_class=None, kp=None, pfu=None,
|
|
event_id="alert_001", product_id="ALTPRO"):
|
|
d = {"id": event_id, "product_id": product_id,
|
|
"time": "2026-06-05T15:00:00Z"}
|
|
if flare_class: d["flare_class"] = flare_class
|
|
if kp: d["kp_index"] = kp
|
|
if pfu: d["p10mev"] = pfu
|
|
return {
|
|
"id": event_id, "subject": "central.space.alert.xrayflare",
|
|
"data": {
|
|
"id": event_id, "adapter": "swpc_alerts",
|
|
"category": "space.alert", "severity": 1,
|
|
"geo": {}, "data": d,
|
|
},
|
|
}
|
|
|
|
|
|
def _commit(data, t):
|
|
data["_on_broadcast_committed"](float(t))
|
|
|
|
|
|
# ---- geomagnetic storm ----
|
|
|
|
|
|
def test_kp_below_7_skipped(mem_db):
|
|
env = _kindex_env(kp=4.0, event_id="kp_low")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is None
|
|
# Row persisted for trending, not broadcast.
|
|
row = mem_db.execute(
|
|
"SELECT last_broadcast_at FROM swpc_events WHERE event_id='kp_low'"
|
|
).fetchone()
|
|
assert row is not None
|
|
assert row["last_broadcast_at"] is None
|
|
|
|
|
|
def test_kp7_g3_broadcasts(mem_db):
|
|
env = _kindex_env(kp=7.0, event_id="kp_g3")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert wire.startswith("🌌")
|
|
assert "G3" in wire
|
|
assert "Kp7" in wire
|
|
assert "geomagnetic storm" in wire.lower()
|
|
|
|
|
|
def test_kp9_g5_broadcasts_with_extreme_label(mem_db):
|
|
env = _kindex_env(kp=9.0, event_id="kp_g5")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert "G5" in wire
|
|
assert "extreme" in wire.lower()
|
|
|
|
|
|
# ---- solar flares ----
|
|
|
|
|
|
def test_m_class_flare_skipped(mem_db):
|
|
env = _alert_env(flare_class="M5.5", event_id="m55_flare")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is None
|
|
|
|
|
|
def test_x1_flare_r3_broadcasts(mem_db):
|
|
env = _alert_env(flare_class="X1.2", event_id="x1_flare")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert wire.startswith("🔆")
|
|
assert "R3" in wire
|
|
assert "X1.2" in wire
|
|
|
|
|
|
def test_x10_flare_r4_broadcasts(mem_db):
|
|
env = _alert_env(flare_class="X10", event_id="x10_flare")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert "R4" in wire or "R5" in wire
|
|
|
|
|
|
def test_flare_class_in_product_id(mem_db):
|
|
"""Some swpc_alerts encode the class in product_id rather than flare_class."""
|
|
env = _alert_env(event_id="prod_id_flare", product_id="X2.1 FLARE EVENT")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert "R3" in wire
|
|
|
|
|
|
# ---- proton events ----
|
|
|
|
|
|
def test_proton_below_threshold_skipped(mem_db):
|
|
env = _protons_env(flux=0.5, event_id="p_low")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is None
|
|
row = mem_db.execute(
|
|
"SELECT last_broadcast_at FROM swpc_events WHERE event_id='p_low'"
|
|
).fetchone()
|
|
assert row is not None
|
|
assert row["last_broadcast_at"] is None
|
|
|
|
|
|
def test_proton_s1_threshold_broadcasts(mem_db):
|
|
env = _protons_env(flux=15, event_id="p_s1")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert wire.startswith("☢️")
|
|
assert "S1" in wire
|
|
|
|
|
|
def test_proton_s2_broadcasts(mem_db):
|
|
env = _protons_env(flux=200, event_id="p_s2")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is not None
|
|
assert "S2" in wire
|
|
|
|
|
|
# ---- wire format ----
|
|
|
|
|
|
def test_wire_has_scale_code_and_scalar_tail(mem_db):
|
|
env = _kindex_env(kp=7.0, event_id="fmt1")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
# Matt's format: "🌌 Strong geomagnetic storm (G3/Kp7) -- HF degraded, ..."
|
|
assert "(G3/Kp7)" in wire
|
|
assert "--" in wire
|
|
|
|
|
|
# ---- per-event dedup ----
|
|
|
|
|
|
def test_per_event_dedup_no_reissue(mem_db):
|
|
env = _kindex_env(kp=7.0, event_id="dedup_kp")
|
|
data1 = {}
|
|
handle_swpc(env, env["subject"], data=data1, now=1_000_000)
|
|
_commit(data1, 1_000_001)
|
|
# Re-publish with same id and same Kp -- should not re-broadcast.
|
|
wire2 = handle_swpc(env, env["subject"], data={}, now=1_000_300)
|
|
assert wire2 is None
|
|
|
|
|
|
# ---- commit callback ----
|
|
|
|
|
|
def test_commit_callback_updates_last_broadcast(mem_db):
|
|
env = _kindex_env(kp=7.0, event_id="cb_swpc")
|
|
data = {}
|
|
handle_swpc(env, env["subject"], data=data, now=1_000_000)
|
|
pre = mem_db.execute(
|
|
"SELECT last_broadcast_at FROM swpc_events WHERE event_id='cb_swpc'"
|
|
).fetchone()
|
|
assert pre["last_broadcast_at"] is None
|
|
_commit(data, 1_000_001)
|
|
post = mem_db.execute(
|
|
"SELECT last_broadcast_at FROM swpc_events WHERE event_id='cb_swpc'"
|
|
).fetchone()
|
|
assert post["last_broadcast_at"] == 1_000_001
|
|
|
|
|
|
# ---- routine readings persist but never broadcast ----
|
|
|
|
|
|
def test_routine_kp_reading_persists_no_broadcast(mem_db):
|
|
"""Sub-G3 Kp must still be saved for trending queries."""
|
|
env = _kindex_env(kp=4.5, event_id="routine_kp")
|
|
wire = handle_swpc(env, env["subject"], data={}, now=1_000_000)
|
|
assert wire is None
|
|
row = mem_db.execute(
|
|
"SELECT event_type, payload_json FROM swpc_events "
|
|
"WHERE event_id='routine_kp'").fetchone()
|
|
assert row is not None
|
|
assert row["event_type"] == "swpc_kindex"
|
|
assert "kp_index" in row["payload_json"]
|