|
| 1 | +"""Tests for motion detection event endpoints.""" |
| 2 | + |
| 3 | +from datetime import datetime, timedelta, timezone |
| 4 | +from app.models.models import MotionEvent |
| 5 | + |
| 6 | + |
| 7 | +def test_list_motion_events_empty(viewer_client): |
| 8 | + """No motion events returns empty list.""" |
| 9 | + resp = viewer_client.get("/api/motion/events") |
| 10 | + assert resp.status_code == 200 |
| 11 | + data = resp.json() |
| 12 | + assert data["total"] == 0 |
| 13 | + assert data["events"] == [] |
| 14 | + |
| 15 | + |
| 16 | +def test_list_motion_events_with_data(viewer_client, db): |
| 17 | + """Motion events are returned in reverse chronological order.""" |
| 18 | + now = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 19 | + for i in range(3): |
| 20 | + db.add(MotionEvent( |
| 21 | + org_id="org_test123", |
| 22 | + camera_id="cam_front", |
| 23 | + node_id="node_1", |
| 24 | + score=50 + i * 10, |
| 25 | + segment_seq=100 + i, |
| 26 | + timestamp=now - timedelta(minutes=10 - i), |
| 27 | + )) |
| 28 | + db.commit() |
| 29 | + |
| 30 | + resp = viewer_client.get("/api/motion/events") |
| 31 | + assert resp.status_code == 200 |
| 32 | + data = resp.json() |
| 33 | + assert data["total"] == 3 |
| 34 | + assert len(data["events"]) == 3 |
| 35 | + # Most recent first |
| 36 | + assert data["events"][0]["score"] == 70 |
| 37 | + assert data["events"][2]["score"] == 50 |
| 38 | + |
| 39 | + |
| 40 | +def test_list_motion_events_camera_filter(viewer_client, db): |
| 41 | + """Filtering by camera_id returns only matching events.""" |
| 42 | + now = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 43 | + db.add(MotionEvent(org_id="org_test123", camera_id="cam_front", node_id="n1", score=80, timestamp=now)) |
| 44 | + db.add(MotionEvent(org_id="org_test123", camera_id="cam_back", node_id="n1", score=60, timestamp=now)) |
| 45 | + db.commit() |
| 46 | + |
| 47 | + resp = viewer_client.get("/api/motion/events?camera_id=cam_front") |
| 48 | + assert resp.status_code == 200 |
| 49 | + data = resp.json() |
| 50 | + assert data["total"] == 1 |
| 51 | + assert data["events"][0]["camera_id"] == "cam_front" |
| 52 | + |
| 53 | + |
| 54 | +def test_motion_events_org_isolation(viewer_client, db): |
| 55 | + """Events from other orgs are not visible.""" |
| 56 | + now = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 57 | + db.add(MotionEvent(org_id="org_test123", camera_id="cam_1", node_id="n1", score=90, timestamp=now)) |
| 58 | + db.add(MotionEvent(org_id="org_other", camera_id="cam_2", node_id="n2", score=85, timestamp=now)) |
| 59 | + db.commit() |
| 60 | + |
| 61 | + resp = viewer_client.get("/api/motion/events") |
| 62 | + data = resp.json() |
| 63 | + assert data["total"] == 1 |
| 64 | + assert data["events"][0]["camera_id"] == "cam_1" |
| 65 | + |
| 66 | + |
| 67 | +def test_motion_stats(viewer_client, db): |
| 68 | + """Stats endpoint returns per-camera aggregates.""" |
| 69 | + now = datetime.now(tz=timezone.utc).replace(tzinfo=None) |
| 70 | + for score in [40, 60, 80]: |
| 71 | + db.add(MotionEvent( |
| 72 | + org_id="org_test123", camera_id="cam_front", node_id="n1", |
| 73 | + score=score, timestamp=now - timedelta(minutes=score), |
| 74 | + )) |
| 75 | + db.add(MotionEvent( |
| 76 | + org_id="org_test123", camera_id="cam_back", node_id="n1", |
| 77 | + score=95, timestamp=now, |
| 78 | + )) |
| 79 | + db.commit() |
| 80 | + |
| 81 | + resp = viewer_client.get("/api/motion/events/stats") |
| 82 | + assert resp.status_code == 200 |
| 83 | + data = resp.json() |
| 84 | + cameras = {c["camera_id"]: c for c in data["cameras"]} |
| 85 | + assert len(cameras) == 2 |
| 86 | + assert cameras["cam_front"]["event_count"] == 3 |
| 87 | + assert cameras["cam_front"]["peak_score"] == 80 |
| 88 | + assert cameras["cam_back"]["event_count"] == 1 |
| 89 | + assert cameras["cam_back"]["peak_score"] == 95 |
0 commit comments