import logging from edge.zones import zones_from_config, thresholds_from_config # Zones ship NORMALIZED (0..1 of the image); fixtures use that range. def test_parses_zone_rows_json_string_polygon(): cfg = {"zones": [ {"zone_id": "z1", "camera_id": "cam-1", "level": "restricted", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"}, ], "thresholds": {"fall_prone_s": 3.0}} zones = zones_from_config(cfg) assert len(zones) == 1 assert zones[0].zone_id == "z1" assert zones[0].level == "restricted" assert zones[0].polygon == [(0.0, 0.0), (0.9, 0.0), (0.9, 0.9)] assert thresholds_from_config(cfg)["fall_prone_s"] == 3.0 def test_skips_malformed_row_and_warns(caplog): # A malformed row (missing polygon_json/level here) must be DROPPED and LOGGED — never silently # swallowed, matching the pixel-space branch's "don't fail dark" principle: a dead zone with no # signal is only discovered after a worker enters it. cfg = {"zones": [{"zone_id": "bad"}], "thresholds": {}} with caplog.at_level(logging.WARNING): assert zones_from_config(cfg) == [] assert any("bad" in r.message and "malformed" in r.message for r in caplog.records) def test_empty_config(): assert zones_from_config({}) == [] assert thresholds_from_config({}) == {} def test_skips_short_point_without_crashing(): cfg = {"zones": [ {"zone_id": "bad", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[9],[0.9,0.9]]"}, {"zone_id": "ok", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"}, ]} zones = zones_from_config(cfg) # bad row skipped, good row survives -- function did not crash assert [z.zone_id for z in zones] == ["ok"] def test_non_iterable_zones_returns_empty(): assert zones_from_config({"zones": 5}) == [] def test_rejects_legacy_pixel_space_polygon_and_warns(caplog): # A polygon saved by the pre-normalization dashboard is in canvas PIXELS (values in the # hundreds). After the edge normalizes the foot to 0..1 it can never match, so such a zone # would silently detect nothing. zones_from_config must DROP it and warn loudly (so the # operator redraws it) rather than fail dark. cfg = {"zones": [ {"zone_id": "legacy", "camera_id": "cam-1", "level": "restricted", "polygon_json": "[[320,180],[600,180],[600,400]]"}, {"zone_id": "modern", "camera_id": "cam-1", "level": "restricted", "polygon_json": "[[0.25,0.25],[0.75,0.25],[0.75,0.75]]"}, ]} with caplog.at_level(logging.WARNING): zones = zones_from_config(cfg) assert [z.zone_id for z in zones] == ["modern"] # legacy dropped, modern kept assert any("legacy" in r.message and "pixel" in r.message for r in caplog.records) def test_parses_occupancy_rules(): cfg = {"zones": [ {"zone_id": "z1", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]", "rules_json": '{"min_workers": 2, "max_workers": 4, "max_dwell_s": 600}'}, {"zone_id": "z2", "camera_id": "c", "level": "warning", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]"}, ]} zones = zones_from_config(cfg) assert zones[0].rules == {"min_workers": 2, "max_workers": 4, "max_dwell_s": 600} assert zones[1].rules is None def test_malformed_rules_keep_zone_but_warn(caplog): # A broken rules blob must not kill the zone's INTRUSION detection (the polygon is # valid) -- keep the zone, drop the rules, and say so loudly. cfg = {"zones": [ {"zone_id": "z1", "camera_id": "c", "level": "restricted", "polygon_json": "[[0,0],[0.9,0],[0.9,0.9]]", "rules_json": '{"min_workers": -1}'}, ]} with caplog.at_level(logging.WARNING): zones = zones_from_config(cfg) assert len(zones) == 1 and zones[0].rules is None assert any("rules_json" in r.message for r in caplog.records)