move_detect/tests/test_zones.py
유창욱 fe6a144754 fix(edge): stop hiding a failed camera; fix ghost dedup leak + silent zone drop
- Heartbeat reports the CONFIGURED camera count, not just the started ones: a
  camera that failed to construct at boot (wrong index / unseated ribbon) now
  shows as total/erroring instead of vanishing, so the server can see it is dark.
- GHOST emits without an engine cooldown key (ghost.update already fires once per
  never-reused track id) — removes a permanent _last_emit key that forget() could
  never reclaim (a slow 24/7 leak).
- Malformed zone rows are logged loudly instead of silently swallowed, matching
  the pixel-space branch's "don't fail dark" — a dead zone is no longer discovered
  only after a worker enters it.
2026-07-25 23:18:19 +09:00

58 lines
2.8 KiB
Python

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)