meshai/tests/test_central_region_routing.py
Matt Johnson 14d168822b fix(traffic): v0.5.7-traffic -- NATS pattern fix + itd_511 sub-adapter routing + categories audit
Second family of the v0.5.7 NATS-and-categories campaign. Weather went first because its NWS pattern was already legal; traffic was carrying invalid NATS syntax in production.

FIX 1 -- Invalid `>` mid-subject in traffic. Pre-v0.5.7-traffic the subject builder shipped `central.traffic.>.{state}` for both the traffic and roads511 adapters. NATS rules say `>` is only legal at the tail token; mid-subject `>` is rejected by the broker at subscribe time (or silently delivers nothing depending on server version). Replaced with Convention B (per Central v0.10.0 meshai_integration_guide.md): single-token `*` in the event_type slot, bare state suffix -- `central.traffic.*.id` for Idaho. Shared by the wzdx, tomtom_incidents and state_511_atis adapters.

FIX 2 -- roads511 dual subscribe. The new Idaho-only itd_511 adapter in Central v0.10.0 uses Convention A (`central.traffic.<event_type>.us.<state>`, the us.<state> form). Convention B (bare state) is shared with the rest of the traffic family. roads511 now owns BOTH:

    central.traffic.*.id        (Convention B, shared with traffic via _subject_owned)
    central.traffic.*.us.id     (Convention A, itd_511-only)

Sub-adapter routing in CentralConsumer._subject_owned (v0.5.1) already keeps shared subjects scoped to the right meshai source -- no change needed.

FIX 3 -- itd_511 -> roads511 in CENTRAL_ADAPTER_TO_SOURCE. Mirrors state_511_atis (added v0.5.3). Both Idaho 511 feeds collapse to a single meshai source for UX simplicity; future v0.6 may split them if Matt needs differential rules.

FIX 4 -- Roads-family categories audit + finer event_type mapping. Pre-v0.5.7-traffic the central path flattened every traffic-domain event to `traffic_congestion` because work_zone / incident / closure had no entries in _CATEGORY_MAP and fell through to the `traffic.` catchall (then the subject-domain fallback). Added three explicit map entries before the catchall:

    ("work_zone", "work_zone")        # catches "work_zone" and "work_zone.wzdx"
    ("incident",  "road_incident")    # catches incident.tomtom_incidents + bare
    ("closure",   "road_closure")     # catches closure + closure.itd_511

ALERT_CATEGORIES gains two new roads-family entries so the Advanced Rules editor can target them:

    work_zone       -- Active construction/maintenance work zone
    road_incident   -- Reported incident (crash, hazard, debris)

Existing entries `road_closure` and `traffic_congestion` kept. composer._CATEGORY_EMOJI gains matching glyphs (🚧 work_zone, 🚨 road_incident) so the live LoRa rendering lines up with the category example_message glyphs.

Audit cross-check (test_alert_categories_roads_complete enforces parity):
    Native emit: traffic.py -> traffic_congestion;  roads511.py -> road_closure
    Central path emit (via map_category): {road_closure, traffic_congestion, work_zone, road_incident}
    ALERT_CATEGORIES{toggle=roads}: {road_closure, traffic_congestion, work_zone, road_incident}
    Parity. No orphans, no missing.

DEFERRED to v0.5.8: itd_511_cameras / traffic_cameras stream lives at a different subject domain (central.traffic_cameras.>) and needs a new meshai source (roads_cameras or similar). Out of scope for v0.5.7.

Tests
-----
PYTHONPATH=. pytest -q: 366 passed (was 345; +21 net).
  - tests/test_traffic_v057.py (new): NATS-syntax checks (`>` only at tail, single-token `*`), traffic Convention B, roads511 dual-subscribe, shared bare-state subject, itd_511 + state_511_atis remap, map_category event_type preservation, ALERT_CATEGORIES roads parity (reflection-based scan of native emit + central path), required-fields check on the four roads entries.
  - tests/test_central_region_routing.py: updated `test_subjects_for_traffic_and_roads511_share_state_token` -> two new tests covering Convention B (traffic) and dual-subscribe (roads511).
  - tests/test_central_consumer.py: updated `test_subject_domain_fallback_for_unmapped_category` (work_zone.wzdx is now mapped, switched to a genuinely-unmapped category) + new `test_v057_traffic_work_zone_now_mapped` asserting wzdx envelopes land on ev.category=="work_zone".

Safe-mode preserved (master off, all family toggles off, all adapters native, central disabled). No live toggle flipped. Not tagging yet -- v0.5.7 tag waits until all families ship.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 06:10:12 +00:00

100 lines
4.1 KiB
Python

"""v0.5.4: Central v0.9.20 region-aware subject building.
Exercises `_subjects_for(adapter, region)` and the wiring through
`CentralConsumer._subject_owned()`. The spec is hard-coded in the test
strings on purpose so a future drift in the v0.9.20 subject scheme
fails noisily here instead of silently shipping wrong filters.
"""
from meshai.central.consumer import (
CentralConsumer,
_subjects_for,
_SUBJECTS_BARE,
)
from meshai.config import EnvironmentalConfig
# --------------------------------------------------------------------- per-adapter
def test_subjects_for_nws_us_id():
"""NWS: region BEFORE wildcard (matches alert.<region>.<...>)."""
assert _subjects_for("nws", "us.id") == ["central.wx.alert.us.id.>"]
def test_subjects_for_usgs_quake_us_id():
"""USGS quake: region AFTER wildcard."""
assert _subjects_for("usgs_quake", "us.id") == ["central.quake.event.>.us.id"]
def test_subjects_for_firms_us_id():
"""FIRMS hotspots: region AFTER wildcard, hotspot domain explicit."""
assert _subjects_for("firms", "us.id") == ["central.fire.hotspot.>.us.id"]
def test_subjects_for_fires_us_id_uses_state_token():
"""NIFC fires: state-only token at depth-4 for both incident + perimeter."""
assert _subjects_for("fires", "us.id") == [
"central.fire.incident.id.>",
"central.fire.perimeter.id.>",
]
def test_subjects_for_traffic_uses_convention_b():
"""v0.5.7-traffic: traffic adapter -> bare-state Convention B with `*`
in the event_type slot. Pre-v0.5.7-traffic this was `>.{state}` which
is invalid NATS (`>` must be at the tail). The bare-state subject is
shared with roads511 (sub-adapter routing picks the right meshai source)."""
assert _subjects_for("traffic", "us.id") == ["central.traffic.*.id"]
def test_subjects_for_roads511_dual_subscribes_convention_a_and_b():
"""v0.5.7-traffic: roads511 owns BOTH the shared bare-state subject
(Convention B, shared with traffic) AND the us.<state> subject
(Convention A) where the new Idaho-only itd_511 adapter publishes."""
assert _subjects_for("roads511", "us.id") == [
"central.traffic.*.id",
"central.traffic.*.us.id",
]
def test_subjects_for_usgs_includes_unknown_workaround():
"""USGS hydro: subscribes to BOTH the region-tagged filter and the
".unknown" filter to cover gauges whose state Central v0.9.20 can't
infer yet (workaround until v0.9.20.1 backfills the tag)."""
assert _subjects_for("usgs", "us.id") == [
"central.hydro.>.us.id",
"central.hydro.>.unknown",
]
def test_subjects_for_swpc_stays_global():
"""SWPC: space weather is planetary; region argument is ignored."""
assert _subjects_for("swpc", "us.id") == ["central.space.>"]
assert _subjects_for("swpc", "us.mt") == ["central.space.>"] # same regardless
assert _subjects_for("swpc", "") == ["central.space.>"]
# --------------------------------------------------------------------- backward compat
def test_subjects_for_empty_region_falls_back_to_bare_wildcards():
"""Empty/None region = pre-v0.9.20 behaviour for every adapter, byte-identical
to the legacy _SUBJECTS_BARE map. Adapters absent from the map return []."""
for adapter, expected in _SUBJECTS_BARE.items():
assert _subjects_for(adapter, "") == expected, f"empty region mismatch for {adapter}"
assert _subjects_for(adapter, None) == expected, f"None region mismatch for {adapter}"
# Unknown adapters return empty regardless of region.
assert _subjects_for("ducting", "us.id") == []
assert _subjects_for("avalanche", "") == []
# --------------------------------------------------------------------- integration
def test_central_region_default_propagates_to_consumer_subjects():
"""Default region = 'us.id': flipping nws to central → consumer subscribes
to the region-aware subject, not the bare wildcard."""
env = EnvironmentalConfig()
assert env.central.region == "us.id" # spec default
env.nws.feed_source = "central"
so = CentralConsumer(env, None)._subject_owned()
assert list(so.keys()) == ["central.wx.alert.us.id.>"]
assert so["central.wx.alert.us.id.>"] == {"nws"}