67 lines
2.5 KiB
PHP
67 lines
2.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
http_response_code(404);
|
|
exit;
|
|
}
|
|
|
|
require_once dirname(__DIR__) . '/NiroTelemetrySnapshot.php';
|
|
|
|
function check(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
fwrite(STDERR, "FAIL: {$message}\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$now = time();
|
|
$summary = NiroTelemetrySnapshot::summarize([
|
|
'received_at' => $now,
|
|
'availability' => 'online',
|
|
'state' => [
|
|
'_sample_epoch' => $now - 3,
|
|
'ignition' => 'true',
|
|
'speed' => '27',
|
|
'gear_code' => '5',
|
|
'battery_soc_12v' => '93',
|
|
'total_distance_to_empty' => '761',
|
|
'odometer' => '259653.7',
|
|
'driver_door' => 'false',
|
|
'outside_temperature' => '25.0',
|
|
'audio_mode' => '54',
|
|
'hev_mode_code' => '2',
|
|
'head_unit_power' => '0',
|
|
'n_framework_available' => 'true',
|
|
'n_drive_mode_valid' => 'true',
|
|
'n_current_drive_mode_code' => '2',
|
|
'n_terrain_supported' => 'false',
|
|
],
|
|
], ['stale_after' => 20]);
|
|
|
|
check($summary['available'] === true, 'snapshot is available');
|
|
check($summary['online'] === true, 'availability topic is respected');
|
|
check($summary['stale'] === false, 'fresh sample is not stale');
|
|
check($summary['metric_count'] === 17, 'only mapped supported metrics are counted');
|
|
check($summary['services'] === ['available' => 7, 'total' => 7], 'source families are counted');
|
|
check($summary['metrics']['speed']['value'] === '27', 'speed mapping');
|
|
check($summary['metrics']['gear']['value'] === '5', 'live root gear mapping');
|
|
check($summary['metrics']['outside_temperature']['value'] === '25.0', 'climate mapping');
|
|
check($summary['metrics']['driver_door']['supported'] === true, 'zero is supported');
|
|
check($summary['metrics']['distance_to_empty']['value'] === '761', 'live HEV range mapping');
|
|
check($summary['metrics']['odometer']['value'] === '259653.7', 'odometer is already km');
|
|
check($summary['metrics']['n_current_drive_mode']['value'] === '2', 'N readback mapping');
|
|
check($summary['metrics']['route_guide']['supported'] === false, 'removed raw route value stays unsupported');
|
|
check(!array_key_exists('state', $summary), 'raw snapshot is not exposed');
|
|
|
|
$stale = NiroTelemetrySnapshot::summarize([
|
|
'received_at' => $now - 90,
|
|
'availability' => 'offline',
|
|
'state' => ['_sample_epoch' => $now - 90],
|
|
], ['stale_after' => 20]);
|
|
check($stale['online'] === false, 'offline state is exposed');
|
|
check($stale['stale'] === true, 'old sample is stale');
|
|
|
|
echo "PASS\n";
|