move_detect/server/tests/EventValidatorTest.php
유창욱 9e4225a338 feat(heatmap): near-miss events + spatial ingestion + camera risk-heatmap overlay
Heinrich's-law play: the signals we already compute but used to throw away
become a spatial risk map of the workplace, drawn over the camera's own frame.

Edge:
- near_fall (INFO): a track that reached FALLING (sustained prone) but recovered
  before the FALLEN confirm. Treated as a full event per product decision -
  same ingest, snapshots, dedup, ack flow as incidents. Fix en route: prone_s
  used a truthiness check that turned prone_since=0.0 into "no timer".
- Every per-track event (fall/no_motion/near_fall/immobility/zone hits) now
  carries the foot point in the zones' normalized 0..1 space (reusing the zone
  detector's own coordinate math); ghost's pixel-space loss point is normalized
  at emit. to_payload omits the key when absent, so an old server ignores it.

Server:
- move_events.spatial_x/spatial_y (nullable DOUBLE) + validator: optional
  [x, y] finite-number pair; range deliberately NOT enforced (an off-by-a-hair
  foot must never reject a FALL alert - renderers clamp).
- MIGRATION FIX: chk_events_type would have REJECTED the previous commit's
  lone_worker/overcapacity/overstay inserts on MySQL 8.0.16+ - the constraint
  is rebuilt with all 13 types (incl. near_fall).
- GET /api/heatmap/{camera}?days=N (1..90, default 30, 5000-point cap):
  spatial points with the same fail-closed site scoping as the event list.

UI (zone editor): "위험 히트맵" toggle + day window. Additive radial blots in
an offscreen layer under the zone polygons; severity weighs the alpha so a
cluster of near-misses reads lighter than one confirmed fall at the same spot.
Events console/detail label near_fall in Korean.

Verified: pytest 222 pass / PHPUnit 130 pass + Chrome real-render (blue
near-miss cluster + red incident hotspot inside the restricted zone, amber
cluster in the warning zone, zones drawn on top; no console errors).
2026-08-06 20:19:09 +09:00

103 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests;
use App\Ingest\EventValidator;
use PHPUnit\Framework\TestCase;
final class EventValidatorTest extends TestCase
{
private function valid(): array {
return ['device_id' => 'pi-1', 'camera_id' => 'cam-1', 'event_type' => 'fall',
'severity' => 'high', 'occurred_at' => 1000.0, 'snapshots' => ['aGk=']];
}
public function testAcceptsValid(): void {
$r = EventValidator::validate($this->valid());
$this->assertTrue($r['ok'], implode(',', $r['errors']));
$this->assertSame('fall', $r['clean']['event_type']);
}
public function testRejectsOverlongIds(): void {
// An overlong id must be a 400 (invalid), not a MySQL 'Data too long' 500 that the edge
// then retries forever. Columns: device_id/camera_id VARCHAR(64), zone_id CHAR(32).
$long = $this->valid(); $long['device_id'] = str_repeat('x', 65);
$this->assertFalse(EventValidator::validate($long)['ok']);
$cam = $this->valid(); $cam['camera_id'] = str_repeat('y', 65);
$this->assertFalse(EventValidator::validate($cam)['ok']);
$zone = $this->valid(); $zone['zone_id'] = str_repeat('z', 33);
$this->assertFalse(EventValidator::validate($zone)['ok']);
// a normal-length id still passes
$ok = $this->valid(); $ok['device_id'] = str_repeat('x', 64);
$this->assertTrue(EventValidator::validate($ok)['ok']);
}
public function testAcceptsAndCleansEventUuid(): void {
$p = $this->valid(); $p['event_uuid'] = str_repeat('a', 32);
$r = EventValidator::validate($p);
$this->assertTrue($r['ok'], implode(',', $r['errors']));
$this->assertSame(str_repeat('a', 32), $r['clean']['event_uuid']);
// Optional: omitting it is valid and clean carries null (legacy edges send no uuid).
$none = EventValidator::validate($this->valid());
$this->assertTrue($none['ok']);
$this->assertNull($none['clean']['event_uuid']);
}
public function testRejectsMalformedEventUuid(): void {
foreach (['XYZ', str_repeat('a', 31), str_repeat('A', 32), 'g' . str_repeat('a', 31)] as $bad) {
$p = $this->valid(); $p['event_uuid'] = $bad;
$this->assertFalse(EventValidator::validate($p)['ok'], "uuid '$bad' must be rejected");
}
}
public function testRejectsUnknownEventType(): void {
$p = $this->valid(); $p['event_type'] = 'explode';
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testAcceptsOccupancyRuleEventTypes(): void {
foreach (['lone_worker', 'overcapacity', 'overstay', 'near_fall'] as $type) {
$p = $this->valid(); $p['event_type'] = $type;
$this->assertTrue(EventValidator::validate($p)['ok'], "$type must be a known type");
}
}
public function testSpatialIsOptionalAndCanonicalized(): void {
$p = $this->valid();
$this->assertNull(EventValidator::validate($p)['clean']['spatial_x']);
$p['spatial'] = [0.42, '0.87'];
$clean = EventValidator::validate($p)['clean'];
$this->assertSame(0.42, $clean['spatial_x']);
$this->assertSame(0.87, $clean['spatial_y']);
}
public function testRejectsMalformedSpatial(): void {
foreach ([[0.5], [0.5, 'x'], 'mid', [0.5, NAN], [1, 2, 3]] as $i => $bad) {
$p = $this->valid(); $p['spatial'] = $bad;
$this->assertFalse(EventValidator::validate($p)['ok'], "spatial case #$i must be rejected");
}
}
public function testRejectsUnknownSeverity(): void {
$p = $this->valid(); $p['severity'] = 'ULTRA';
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsMissingRequired(): void {
$p = $this->valid(); unset($p['device_id']);
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsTooManySnapshots(): void {
$p = $this->valid(); $p['snapshots'] = ['a', 'b', 'c', 'd'];
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsNonNumericOccurredAt(): void {
$p = $this->valid(); $p['occurred_at'] = ['bad'];
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsNonStringDeviceId(): void {
$p = $this->valid(); $p['device_id'] = ['x'];
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsNonArraySnapshots(): void {
$p = $this->valid(); $p['snapshots'] = 'notarray';
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsNonStringSnapshotItem(): void {
$p = $this->valid(); $p['snapshots'] = [123];
$this->assertFalse(EventValidator::validate($p)['ok']);
}
public function testRejectsNonStringZoneId(): void {
$p = $this->valid(); $p['zone_id'] = ['z'];
$this->assertFalse(EventValidator::validate($p)['ok']);
}
}