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

62 lines
5.4 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-weekly-smoke-' + [Guid]::NewGuid().ToString('N')
$requestID = 'pilot-weekly-smoke-' + [Guid]::NewGuid().ToString('N')
$weekdayOffset = (([int](Get-Date).DayOfWeek) + 6) % 7
$weekStart = (Get-Date).Date.AddDays(-$weekdayOffset).ToString('yyyy-MM-dd')
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
week_start = $weekStart
timezone = 'Asia/Shanghai'
reason = 'Verify pilot weekly review aggregation, redaction, and evidence gaps'
} | 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-weekly-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-weekly-minimal-v1') { throw 'Initial pilot weekly snapshot is invalid' }
if ($snapshot.candidate_version -ne '0.55.0-rc.local' -or $snapshot.build_digest -ne $buildDigest -or $snapshot.week_start -ne $weekStart -or $snapshot.timezone -ne 'Asia/Shanghai') { throw 'Pilot weekly candidate, date, or timezone is invalid' }
if (@($snapshot.privacy.by_type).Count -ne 4 -or @($snapshot.privacy.by_status).Count -ne 5) { throw 'Privacy distributions are incomplete' }
if ([long]$snapshot.config.aligned + [long]$snapshot.config.drifted + [long]$snapshot.config.unreported -ne [long]$snapshot.config.eligible_devices) { throw 'Configuration counts do not match eligible devices' }
$firmwareDevices = (@($snapshot.firmware_versions) | Measure-Object -Property device_count -Sum).Sum
if ([long]$firmwareDevices -ne [long]$snapshot.config.eligible_devices) { throw 'Firmware distribution does not match eligible devices' }
if ($snapshot.content_review.manual_revision_rate_available -ne ($snapshot.content_review.stories_review_touched -gt 0)) { throw 'Manual revision rate availability is invalid' }
if ($snapshot.content_review.story_confirmation_rate_available -ne (($snapshot.content_review.stories_confirmed + $snapshot.content_review.stories_rejected) -gt 0)) { throw 'Story confirmation rate availability is invalid' }
if ($snapshot.content_review.manual_revision_rate_basis_points -lt 0 -or $snapshot.content_review.manual_revision_rate_basis_points -gt 10000 -or $snapshot.content_review.story_confirmation_rate_basis_points -lt 0 -or $snapshot.content_review.story_confirmation_rate_basis_points -gt 10000) { throw 'Weekly rate is outside 0..10000 basis points' }
if (@($snapshot.external_evidence).Count -ne 1 -or $snapshot.external_evidence[0].code -ne 'complaints' -or $snapshot.external_evidence[0].status -ne 'REQUIRED') { throw 'Complaint evidence gap was not preserved' }
if (($createdResponse.Content -match 'final_text|provider_task_ref|access_url|credential_ref|story_title|subject_name')) { throw 'Pilot weekly snapshot contains a prohibited sensitive field' }
$replayResponse = Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$api/api/v1/ops/diagnostics/pilot-weekly-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 weekly idempotent replay is invalid' }
try {
$tuesday = (Get-Date $weekStart).AddDays(1).ToString('yyyy-MM-dd')
$badBody = @{candidate_version = '0.55.0-rc.local'; build_digest = $buildDigest; week_start = $tuesday; timezone = 'Asia/Shanghai'; reason = 'Verify non-Monday validation'} | ConvertTo-Json -Compress
Invoke-WebRequest -UseBasicParsing -Method Post -Uri "$api/api/v1/ops/diagnostics/pilot-weekly-snapshots" -Headers @{Authorization = "Bearer $owner"; 'X-Tenant-Id' = $tenant; 'Idempotency-Key' = ('bad-' + $key)} -ContentType 'application/json' -Body $badBody | Out-Null
throw 'A non-Monday week start must not be accepted'
} catch {
if ($null -eq $_.Exception.Response -or [int]$_.Exception.Response.StatusCode -ne 400) { throw }
}
Write-Host "Pilot weekly smoke passed: real OIDC, calendar-week aggregation, rate semantics, privacy distributions, device invariants, external evidence gap, redaction, replay, and Monday 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 weekly smoke audit and idempotency records removed; snapshots are transient.'
}