move_detect/tests/test_zones.py
유창욱 2ea0420de7 fix(delivery): resolve codex + workflow review findings across edge/cloud/deploy
Two independent adversarial reviews (codex CLI + a 6-dimension verification
workflow) of the delivery-hardening branch surfaced ~40 findings; this closes
the confirmed correctness/security/deploy blockers plus a codex re-review pass.

edge:
- geometry: track_displacement per-second threshold recalibrated (0.02->0.25,
  env-tunable via MOTION_THR, validated finite/positive); confidence-filter
  keypoints with a bbox-center fallback so hallucinated points don't fake motion
- fall: low-confidence frames no longer cancel an active FALLING/FALLEN incident
  (freeze + reset recovery timer so recovery needs a fresh confident streak);
  baseline height only seeded from trusted frames
- queue: PostClient.send() returns a tri-state SendResult; only definite 4xx
  count toward the poison drop cap, so a network outage (incl. 408/425/429/5xx)
  never deletes queued safety events; legacy rows persist their minted uuid
- config: reject placeholder / http / short DEVICE_SECRET at startup
- zones: warn+skip legacy pixel-space polygons that would silently match nothing

cloud:
- zone endpoints wired into P3-2b site-scope (cross-site read/write/delete
  closed) + role gate on list; catch narrowed so DB faults surface as 500
- snapshot purge keeps the DB row when unlink() fails (no untrackable PIPA orphan)
- ingest dedup path re-stores missing snapshots (+ short-write guard); UNIQUE
  (event_id, content_hash) makes recovery idempotent under retry/race
- event list cursor carries full-precision occurred_at (no float rounding at the
  page boundary); string keyset end-to-end
- SiteScope '' fails closed (only NULL is global); dashboard fetches full page cap
- runnable migration (site/event_uuid + backfill ordering + indexes); rate-limit
  reap fails open

deploy:
- offline bundle hashes EVERY file (fatal on missing/failed/injected; models in
  SBOM; arch in tarball name; REQUIRE_SIGNATURE option)
- installer verifies before any mutation, Hailo arch preflight before install,
  atomic src swap, backend reconcile preserves a working operator choice
- systemd DeviceAllow=char-dma_heap (the /dev/dma_heap/ dir form was inert)

Verified: edge pytest 165 passed / 2 skipped; cloud phpunit 109 passed / 4
skipped; deploy scripts bash -n clean. Reviewed by codex twice (findings +
fixes) with the second-pass findings folded in.
2026-07-17 19:24:42 +09:00

53 lines
2.4 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():
cfg = {"zones": [{"zone_id": "bad"}], "thresholds": {}}
assert zones_from_config(cfg) == []
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)