2026-06-11 23:23:24 +00:00
|
|
|
"""v0.7 satpass_handler tests."""
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _envelope(norad_id=25544, sat_name="ISS", observer="Boise",
|
|
|
|
|
max_el=75.0, aos="2026-06-12T03:32:00Z",
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
los="2026-06-12T03:38:00Z", direction="NW-SE",
|
|
|
|
|
aos_compass="SW", los_compass="NE"):
|
2026-06-11 23:23:24 +00:00
|
|
|
"""Build a CloudEvents envelope for a satellite pass."""
|
|
|
|
|
return {
|
|
|
|
|
"specversion": "1.0",
|
|
|
|
|
"type": "central.sat.pass",
|
|
|
|
|
"source": "central",
|
|
|
|
|
"id": f"pass-{norad_id}-{aos}",
|
|
|
|
|
"data": {
|
2026-06-12 17:19:44 +00:00
|
|
|
"adapter": "n2yo_visualpasses",
|
2026-06-12 20:55:01 +00:00
|
|
|
"category": "pass.n2yo_visualpasses",
|
2026-06-11 23:23:24 +00:00
|
|
|
"severity": 0,
|
|
|
|
|
"data": {
|
|
|
|
|
"norad_id": norad_id,
|
2026-06-12 20:55:01 +00:00
|
|
|
"satellite_name": sat_name,
|
|
|
|
|
"observer_name": observer,
|
|
|
|
|
"max_elevation_deg": max_el,
|
|
|
|
|
"aos_time": aos,
|
|
|
|
|
"los_time": los,
|
|
|
|
|
"azimuth_at_peak_compass": direction,
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
"azimuth_at_aos_compass": aos_compass,
|
|
|
|
|
"azimuth_at_los_compass": los_compass,
|
2026-06-11 23:23:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_db():
|
|
|
|
|
"""Mock database connection."""
|
|
|
|
|
conn = MagicMock()
|
|
|
|
|
conn.execute.return_value.fetchone.return_value = None
|
|
|
|
|
conn.execute.return_value.lastrowid = 1
|
|
|
|
|
with patch("meshai.central.satpass_handler.get_db", return_value=conn):
|
|
|
|
|
yield conn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_adapter_config():
|
|
|
|
|
"""Mock adapter_config.satpass."""
|
|
|
|
|
cfg = MagicMock()
|
2026-06-12 17:19:44 +00:00
|
|
|
cfg.enabled = True
|
2026-06-11 23:23:24 +00:00
|
|
|
cfg.observers = [] # empty = all observers
|
|
|
|
|
cfg.min_elevation = 30
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
cfg.norad_ids = [25544] # must be non-empty for opt-in
|
|
|
|
|
cfg.dry_run = False
|
|
|
|
|
cfg.max_broadcasts_per_hour = 4
|
2026-06-11 23:23:24 +00:00
|
|
|
with patch("meshai.central.satpass_handler.adapter_config") as mock:
|
|
|
|
|
mock.satpass = cfg
|
2026-06-12 17:19:44 +00:00
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
if hasattr(handle_satpass, "_disabled_logged"):
|
|
|
|
|
del handle_satpass._disabled_logged
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
if hasattr(handle_satpass, "_no_norad_ids_logged"):
|
|
|
|
|
del handle_satpass._no_norad_ids_logged
|
2026-06-11 23:23:24 +00:00
|
|
|
yield cfg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSatpassHandler:
|
|
|
|
|
"""Tests for handle_satpass function."""
|
|
|
|
|
|
|
|
|
|
def test_high_elevation_pass_broadcasts(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""A pass with high elevation should broadcast."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
env = _envelope(max_el=75.0)
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
assert "ISS" in result
|
2026-06-11 23:23:24 +00:00
|
|
|
|
|
|
|
|
def test_low_elevation_pass_filtered(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""A pass below min_elevation should be filtered."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
mock_adapter_config.min_elevation = 30
|
|
|
|
|
env = _envelope(max_el=25.0)
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is None
|
|
|
|
|
|
|
|
|
|
def test_observer_filter_blocks_mismatch(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""A pass for non-configured observer should be filtered."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
mock_adapter_config.observers = ["Magic Valley"]
|
|
|
|
|
env = _envelope(observer="Boise")
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is None
|
|
|
|
|
|
|
|
|
|
def test_observer_filter_allows_match(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""A pass for configured observer should broadcast."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
mock_adapter_config.observers = ["Boise", "Magic Valley"]
|
|
|
|
|
env = _envelope(observer="Boise", max_el=45.0)
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
|
|
|
|
|
def test_norad_id_filter(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""NORAD ID filter should block non-matching satellites."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
mock_adapter_config.norad_ids = [25544] # ISS only
|
|
|
|
|
env = _envelope(norad_id=12345, max_el=60.0)
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.other", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is None
|
|
|
|
|
|
|
|
|
|
def test_dedup_blocks_second_broadcast(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""Second pass in same hour bucket should be deduplicated."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
# First call returns no existing broadcast
|
|
|
|
|
mock_db.execute.return_value.fetchone.return_value = None
|
|
|
|
|
|
|
|
|
|
env = _envelope(max_el=60.0)
|
|
|
|
|
result1 = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
assert result1 is not None
|
|
|
|
|
|
|
|
|
|
# Second call simulates existing broadcast
|
|
|
|
|
mock_db.execute.return_value.fetchone.return_value = {"last_broadcast_at": 1718163120}
|
|
|
|
|
|
|
|
|
|
result2 = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163180)
|
|
|
|
|
assert result2 is None
|
|
|
|
|
|
|
|
|
|
def test_wire_format(self, mock_db, mock_adapter_config):
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
"""Wire format should have 2 lines with correct info."""
|
2026-06-11 23:23:24 +00:00
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
env = _envelope(sat_name="ISS", max_el=75, observer="Boise",
|
|
|
|
|
direction="NW-SE", aos_compass="SW", los_compass="NE")
|
2026-06-11 23:23:24 +00:00
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
lines = result.split("\n")
|
fix(satpass): broadcast safety controls — opt-in filter, rate cap, dry-run, wire format
Incident response for 343 broadcasts in 126s (2026-06-12 22:10 UTC).
Five safety controls:
1. OPT-IN BIRD FILTER: norad_ids=[] now means "broadcast nothing"
(was "all birds"). Empty list logs once at INFO and suppresses all
broadcasts. The !satpass DM command remains ungated — it queries
any bird in the TLE cache using command_norad_ids as bare-command
default. Two paths, two rules.
2. RATE CAP: new satpass.max_broadcasts_per_hour (int, default 4).
Excess qualifying passes logged and suppressed. Broadcast path only.
3. DRY-RUN MODE: new satpass.dry_run (bool, default TRUE). Logs exact
wire text at INFO prefixed "DRY-RUN would air:" without dispatching.
Go-live: enabled=true + dry_run=true → observe → dry_run=false.
4. ELEVATION DEFAULT: min_elevation REGISTRY default already at 30
(confirmed, no change needed).
5. BROADCAST WIRE FORMAT: two-line LoRa-tight format with buckets:
🛰️ {name} {bucket}, {aos_compass}→{los_compass}
{duration} minute window, {rise}–{set} {AM/PM} MDT
Buckets: overhead (≥60°), high pass (30-59°), low pass (<30°).
DM format keeps exact degrees. One format_pass() function with
broadcast= mode switch — two callers, one function.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-12 23:00:03 +00:00
|
|
|
assert len(lines) == 2
|
|
|
|
|
assert "ISS" in lines[0]
|
|
|
|
|
assert "overhead" in lines[0]
|
|
|
|
|
assert "SW" in lines[0]
|
|
|
|
|
assert "NE" in lines[0]
|
|
|
|
|
assert "minute window" in lines[1]
|
2026-06-11 23:23:24 +00:00
|
|
|
|
|
|
|
|
def test_commit_callback_attached(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""Broadcast should attach commit callback."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
|
env = _envelope(max_el=60.0)
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data=data, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert "_on_broadcast_committed" in data
|
|
|
|
|
assert "_broadcast_audit" in data
|
|
|
|
|
assert data["_broadcast_audit"]["table"] == "satpass_events"
|
|
|
|
|
|
|
|
|
|
def test_wrong_adapter_ignored(self, mock_db, mock_adapter_config):
|
|
|
|
|
"""Envelope with wrong adapter should be ignored."""
|
|
|
|
|
from meshai.central.satpass_handler import handle_satpass
|
|
|
|
|
|
|
|
|
|
env = _envelope()
|
|
|
|
|
env["data"]["adapter"] = "some_other_adapter"
|
|
|
|
|
result = handle_satpass(env, "central.sat.pass.iss", data={}, now=1718163120)
|
|
|
|
|
|
|
|
|
|
assert result is None
|