Files
story/scripts/smoke-pilot-daily-snapshot.ps1
T

55 lines
4.2 KiB
PowerShell
Raw Normal View History

2026-08-26 06:41:07 +08:00
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true
$root = Split-Path -Parent $PSScriptRoot
Set-Location -LiteralPath $root
$api = 'http://localhost:58080'
$tokenUrl = 'http://localhost:58180/realms/voice-local/protocol/openid-connect/token'
$tenant = '10000000-0000-0000-0000-000000000010'
$key = 'pilot-daily-smoke-' + [Guid]::NewGuid().ToString('N')
$requestID = 'pilot-daily-smoke-' + [Guid]::NewGuid().ToString('N')
function Get-Token {
(Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType 'application/x-www-form-urlencoded' -Body @{
grant_type = 'password'; client_id = 'voice-platform-web'; username = 'owner'; password = 'LocalOnly123!'; scope = 'openid'
}).access_token
}
try {
$owner = Get-Token
$buildDigest = (docker image inspect data-terminal-cloud-api --format '{{.Id}}').Trim()
$body = @{
candidate_version = '0.55.0-rc.local'
build_digest = $buildDigest
local_date = Get-Date -Format 'yyyy-MM-dd'
timezone = 'Asia/Shanghai'
reason = 'Verify pilot daily snapshot aggregation, redaction, and download output'
} | ConvertTo-Json -Compress
$headers = @{Authorization = "Bearer $owner"; 'X-Tenant-Id' = $tenant; 'Idempotency-Key' = $key; 'X-Request-Id' = $requestID}
$createdResponse = Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$api/api/v1/ops/diagnostics/pilot-daily-snapshots" -Headers $headers -ContentType 'application/json' -Body $body
$snapshot = ($createdResponse.Content | ConvertFrom-Json).data
if ($createdResponse.StatusCode -ne 201 -or $snapshot.redaction_policy -ne 'ops-pilot-daily-minimal-v1') { throw 'Initial pilot daily snapshot is invalid' }
if ($snapshot.candidate_version -ne '0.55.0-rc.local' -or $snapshot.build_digest -ne $buildDigest -or $snapshot.timezone -ne 'Asia/Shanghai' -or $snapshot.local_date -ne (Get-Date -Format 'yyyy-MM-dd')) { throw 'Pilot daily candidate, date, or timezone is invalid' }
if (@($snapshot.conversations.statuses).Count -ne 8) { throw 'Conversation status distribution is incomplete' }
$conversationTotal = ($snapshot.conversations.statuses | Measure-Object -Property count -Sum).Sum
if ([long]$conversationTotal -ne [long]$snapshot.conversations.total) { throw 'Conversation total does not match the status distribution' }
if ([long]$snapshot.devices.online + [long]$snapshot.devices.offline -ne [long]$snapshot.devices.eligible) { throw 'Device online and offline counts do not match eligible total' }
if (($createdResponse.Content -match 'provider_task_ref|final_text|access_url|recording|subject_name')) { throw 'Pilot daily snapshot contains a prohibited sensitive field' }
$replayResponse = Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$api/api/v1/ops/diagnostics/pilot-daily-snapshots" -Headers $headers -ContentType 'application/json' -Body $body
if ($replayResponse.StatusCode -ne 200 -or $replayResponse.Headers.'Idempotency-Replayed' -ne 'true' -or ($replayResponse.Content | ConvertFrom-Json).data.id -ne $snapshot.id) { throw 'Pilot daily idempotent replay is invalid' }
try {
$badBody = @{candidate_version = '0.55.0-rc.local'; build_digest = $buildDigest; local_date = Get-Date -Format 'yyyy-MM-dd'; timezone = 'Mars/Base'; reason = 'Verify invalid timezone validation'} | ConvertTo-Json -Compress
Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$api/api/v1/ops/diagnostics/pilot-daily-snapshots" -Headers @{Authorization = "Bearer $owner"; 'X-Tenant-Id' = $tenant; 'Idempotency-Key' = ('bad-' + $key)} -ContentType 'application/json' -Body $badBody | Out-Null
throw 'An invalid IANA timezone must not be accepted'
} catch {
if ($null -eq $_.Exception.Response -or [int]$_.Exception.Response.StatusCode -ne 400) { throw }
}
Write-Host "Pilot daily smoke passed: real OIDC, natural-day aggregation, fixed status distribution, device invariants, redaction, replay, and timezone validation; snapshot=$($snapshot.id)."
} finally {
docker compose exec -T postgres psql -U voice -d voice_platform -v ON_ERROR_STOP=1 -c "BEGIN; DELETE FROM audit_event WHERE request_id='$requestID'; DELETE FROM idempotency_record WHERE idempotency_key IN ('$key','bad-$key'); COMMIT;" | Out-Null
Write-Host 'Pilot daily smoke audit and idempotency records removed; snapshots are transient.'
}