move_detect/server/tests/EventValidatorTest.php
유창욱 b358822247 feat(ergo): posture-load exposure scoring (REBA bands) with anonymous reports
Same pose stream, new safety axis: musculoskeletal-load prevention. This is an
honest 2D reduction of REBA - band exposure TIME, not a precise REBA score.

Edge:
- geometry: arm_elevation_deg (shoulder->elbow vs vertical-down, best of both
  arms) and knee_flexion_deg (hip-knee-ankle interior angle, worst of both
  legs). Both return None when no confident side exists - "unmeasurable" must
  never read as "0 deg / straight".
- detectors/ergonomics.py: per-frame dt accumulation into bands (trunk 20-60
  and 60+ deg via the existing torso angle, arm 90+ deg, knee <110 deg), gated
  on confident core keypoints; detection gaps are dt-capped (2s) so a dropout
  never counts as exposure. Hourly per-camera report, skipped when nothing was
  observed (an empty-workshop report is noise).
- Engine: tracks in an incident state (FALLING/FALLEN/NO_MOTION) are excluded
  - a fallen worker's 90-deg trunk is an incident, not an ergonomic exposure.
  The report event (ergo_report/INFO) carries no track_id and no spatial:
  a camera-level anonymous aggregate, per the PIPA-first product stance.
- run_once: NO snapshots on ergo_report (a frame would turn a statistic into
  surveillance imagery); capture is skipped entirely when only ergo fired.

Server/UI: ergo_report accepted (validator + chk_events_type, edited in the
still-undeployed heatmap migration); detail view now renders event metrics
with Korean labels and seconds humanized - the ergo report's whole value IS
its metrics. Korean type label in list + detail.

Verified: pytest 232 pass (10 new) / PHPUnit 135 pass. NOTE: the detail-view
visual check is pending - the Chrome extension disconnected mid-verification;
harness is staged under the session scratchpad (events/1/view).
2026-08-06 21:46:39 +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', 'ergo_report'] 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']);
}
}