$ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $true $projectRoot = Split-Path -Parent $PSScriptRoot Set-Location -LiteralPath $projectRoot $tenantId = '10000000-0000-0000-0000-000000000001' $deviceId = '10000000-0000-0000-0000-000000000501' try { $setupSql = @" DELETE FROM web_event WHERE resource_type='device' AND resource_public_id=(SELECT public_id FROM device WHERE id='$deviceId'::uuid); DELETE FROM device_presence_state WHERE tenant_id='$tenantId'::uuid AND device_id='$deviceId'::uuid; SELECT record_device_presence('$tenantId'::uuid,'$deviceId'::uuid,now()); SELECT record_device_presence('$tenantId'::uuid,'$deviceId'::uuid,now()+interval '1 second'); UPDATE device_presence_state SET last_seen_at=now()-interval '10 minutes' WHERE tenant_id='$tenantId'::uuid AND device_id='$deviceId'::uuid; SELECT expire_device_presence(now()-interval '2 minutes',1000); "@ $setupSql | docker compose exec -T postgres psql -U voice -d voice_platform -v ON_ERROR_STOP=1 -q | Out-Null $rows = docker compose exec -T postgres psql -U voice -d voice_platform -t -A -F '|' -c "SELECT event_type,status FROM web_event WHERE resource_type='device' AND resource_public_id=(SELECT public_id FROM device WHERE id='$deviceId'::uuid) ORDER BY id" | Out-String $events = @($rows.Trim() -split "`r?`n") if ($events.Count -ne 2) { throw "Expected one online and one offline transition, received $($events.Count): $rows" } if ($events[0] -ne 'device.presence.changed|online' -or $events[1] -ne 'device.presence.changed|offline') { throw "Presence transition sequence was unexpected: $rows" } $state = (docker compose exec -T postgres psql -U voice -d voice_platform -t -A -c "SELECT online FROM device_presence_state WHERE tenant_id='$tenantId'::uuid AND device_id='$deviceId'::uuid" | Out-String).Trim() if ($state -ne 'f') { throw "Device presence state was not expired: $state" } Write-Host 'Device presence online deduplication and offline expiry event smoke passed' } finally { $cleanupSql = @" DELETE FROM web_event WHERE resource_type='device' AND resource_public_id=(SELECT public_id FROM device WHERE id='$deviceId'::uuid); DELETE FROM device_presence_state WHERE tenant_id='$tenantId'::uuid AND device_id='$deviceId'::uuid; "@ $cleanupSql | docker compose exec -T postgres psql -U voice -d voice_platform -v ON_ERROR_STOP=1 -q | Out-Null }