"""Tests for the idle ADS-B watch sweep. Regression: the refresh loop only persisted aircraft for *connected browser viewports*, so ``aircraft_positions`` went silent for a day whenever nobody had the map open (observed 2026-11-20 04:58 -> 2026-11-21). When no viewport is registered the loop must sweep the chokepoint presets instead. """ from __future__ import annotations import asyncio from chokepoints import chokepoints from live_layers import bbox_center_radius_nm, parse_bbox from main import _adsb_watch_boxes, _adsb_refresh_loop from ws_manager import manager def test_watch_boxes_cover_every_chokepoint() -> None: boxes = _adsb_watch_boxes() assert len(boxes) == len(chokepoints()) for box in boxes: minlon, minlat, maxlon, maxlat = box # Preset catalog order is minlat,minlon,maxlat,maxlon: a lon/lat swap # would put e.g. Hormuz at lat 56 lon 26. assert -90 <= minlat < maxlat <= 90 assert -180 <= minlon < maxlon <= 180 # ADSB.lol 403s any dist > 250 nm; live_layer clamps to 150 nm. assert bbox_center_radius_nm(minlon, minlat, maxlon, maxlat)[2] <= 150 def test_watch_box_order_matches_catalog() -> None: hormuz = next(p for p in chokepoints() if p["id"] == "hormuz") minlat, minlon, maxlat, maxlon = (float(v) for v in hormuz["bbox"].split(",")) assert (minlon, minlat, maxlon, maxlat) in _adsb_watch_boxes() # parse_bbox (the app's viewport order) agrees with the converted box. assert parse_bbox(f"{minlon},{minlat},{maxlon},{maxlat}") == (minlon, minlat, maxlon, maxlat) def test_idle_loop_polls_watch_boxes(monkeypatch) -> None: """With no client viewport the loop must call fetch_aircraft(persist=True).""" calls: list[tuple[str, bool]] = [] async def fake_fetch(bbox: str, limit: int = 2000, persist: bool = False): calls.append((bbox, persist)) return [] monkeypatch.setattr("live_layers.fetch_aircraft", fake_fetch) assert manager.viewports() == [] async def run() -> None: try: await asyncio.wait_for(_adsb_refresh_loop(), 0.5) except asyncio.TimeoutError: pass asyncio.run(run()) assert calls, "idle loop persisted nothing" assert all(persist for _, persist in calls) assert {bbox for bbox, _ in calls} == { f"{b[0]},{b[1]},{b[2]},{b[3]}" for b in _adsb_watch_boxes() }