move_detect/tests/test_alert.py
유창욱 9386dfc580 feat(delivery): install-path fixes + cafe24 package + delivery-audit hardening
Three intertwined bodies of work developed and tested together this session.
Full test suite green: python (edge) + phpunit (cloud).

## Edge/Pi install path
- setup_online.sh: apt-lock retry (apt-daily contention), Type=exec unit +
  StartLimitIntervalSec in [Unit], journalctl-on-failure, crash-loop detection.
- onnxruntime is now best-effort (no sdist / no 32-bit-ARM wheel); [6/8] probes the
  real backend and hard-fails if none works.
- Hailo HEF auto-fetch: edge/tools/fetch_hailo_hef.py (pinned sha256, refuses to guess
  8 vs 8L, HAILO_ARCH override for serial-less boards); hailo_pose.py run() timeout fix.
- install_offline.sh: mirrors the backend gate; onnxruntime optional.

## Cafe24 cloud package (cloud/deploy/)
- build_cafe24_package.sh emits move_detect_app/ (private, one folder) + www/move_detect/
  (docroot subdir); no-dev classmap autoloader; preflight.php; hardened .htaccess.
- Subdirectory mount support: Request::baseFrom/pathFor strip the mount prefix; index.php
  walks up to find the app root; views/JS prefix every URL with APP_BASE; session cookie
  scoped to the mount.

## Delivery audit — silent-failure & correctness (docs/delivery-audit.md)
All 4 blockers + all Phase-1 silent-failure + Phase-2 HIGH correctness:
- Edge cloud clients send a custom User-Agent (cafe24 WAF blocks python-requests) and
  allow_redirects=False.
- Daemon fails loud on blank CAFE24_URL/DEVICE_SECRET; run_loop logs swallowed exceptions;
  heartbeat carries real fps + per-camera health; UvcSource reopens a dead camera;
  offline queue is size-capped and drops poison rows; TARGET_FPS/QUEUE_DB validated.
- Zone coordinates normalized 0..1 on both sides (were canvas-px vs camera-px — every zone
  misplaced); pinch zones emit PINCH/HIGH (were downgraded to WARNING).
- Fall detection uses bbox cues when keypoints are low-confidence (occluded fallen person);
  ghost incident not cancelled by a bystander; displacement normalized per-second (FPS-invariant).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019Xmn8KZWqiQUguEMRtn6rW
2026-07-17 09:07:33 +09:00

49 lines
2.1 KiB
Python

import json
from edge.events import Event, EventType, Severity
from edge.alert import sign, PostClient
def _event():
return Event(EventType.FALL, Severity.HIGH, "cam-01", 7, None, 1000.0, 3.0,
{"torso_angle": 78.0}, None)
class _FakeResp:
def __init__(self, code): self.status_code = code
def test_sign_is_deterministic():
s1 = sign("secret", "n1", "1000", b"body")
s2 = sign("secret", "n1", "1000", b"body")
assert s1 == s2 and len(s1) == 64
def test_send_posts_signed_payload():
captured = {}
def fake_post(url, data=None, headers=None, timeout=None, **kw):
captured["url"] = url
captured["headers"] = headers
captured["body"] = data
captured["kw"] = kw
return _FakeResp(200)
client = PostClient("https://x/api/events", "pi-01", "secret",
nonce_fn=lambda: "n1", now_fn=lambda: 1000.0,
http_post=fake_post)
ok = client.send(_event(), [b"\xff\xd8jpeg"])
assert ok is True
assert captured["headers"]["X-Nonce"] == "n1"
assert captured["headers"]["X-Timestamp"] == "1000.0"
payload = json.loads(captured["body"])
assert payload["device_id"] == "pi-01"
assert payload["event_type"] == "fall"
assert len(payload["snapshots"]) == 1
expected = sign("secret", "n1", "1000.0", captured["body"].encode())
assert captured["headers"]["X-Signature"] == expected
# cafe24's shared-hosting WAF blocks the default python-requests/x.y User-Agent with an
# HTML 403 (curl passes) — a custom UA is required or every event silently 403s.
assert captured["headers"]["User-Agent"] == "move-detect-edge/1.0"
# The .htaccess http->https 302 must NOT be auto-followed as a GET (405/404); a redirect
# is a misconfiguration to surface, not to silently downgrade the POST.
assert captured["kw"].get("allow_redirects") is False
def test_send_returns_false_on_error_code():
client = PostClient("https://x", "pi-01", "s", lambda: "n", lambda: 1.0,
http_post=lambda *a, **k: _FakeResp(500))
assert client.send(_event(), []) is False