Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
67 lines
3.7 KiB
PowerShell
67 lines
3.7 KiB
PowerShell
$ErrorActionPreference = "Stop"
|
|
|
|
$repo = Split-Path -Parent $PSScriptRoot
|
|
Set-Location $repo
|
|
|
|
if (-not (Test-Path ".env")) {
|
|
throw "Copy .env.example to .env and configure local credentials first."
|
|
}
|
|
|
|
$base = "http://127.0.0.1:8080"
|
|
$client = [System.Net.Http.HttpClient]::new()
|
|
|
|
function Invoke-Api([string]$Method, [string]$Path, [string]$Token = "", $Body = $null, [string]$Authorization = "") {
|
|
$request = [System.Net.Http.HttpRequestMessage]::new([System.Net.Http.HttpMethod]::new($Method), "$base$Path")
|
|
if ($Token) { $request.Headers.Authorization = [System.Net.Http.Headers.AuthenticationHeaderValue]::new("Bearer", $Token) }
|
|
if ($Authorization) { $request.Headers.TryAddWithoutValidation("Authorization", $Authorization) | Out-Null }
|
|
if ($null -ne $Body) {
|
|
$json = ConvertTo-Json -InputObject $Body -Depth 10 -Compress
|
|
$request.Content = [System.Net.Http.StringContent]::new($json, [System.Text.Encoding]::UTF8, "application/json")
|
|
}
|
|
$response = $client.SendAsync($request).GetAwaiter().GetResult()
|
|
$content = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
|
|
$requestId = $response.Headers.GetValues("X-Request-Id") | Select-Object -First 1
|
|
if (-not $requestId) { throw "API response is missing X-Request-Id" }
|
|
$data = $null
|
|
if ($content) { $data = $content | ConvertFrom-Json }
|
|
return [pscustomobject]@{ Status = [int]$response.StatusCode; Data = $data }
|
|
}
|
|
|
|
try {
|
|
$password = "Persistence-" + [guid]::NewGuid().ToString("N")
|
|
$alicePayload = @{ username = "persist_" + [guid]::NewGuid().ToString("N").Substring(0, 12); display_name = "Persistence Alice"; password = $password }
|
|
$bobPayload = @{ username = "persist_" + [guid]::NewGuid().ToString("N").Substring(0, 12); display_name = "Persistence Bob"; password = $password }
|
|
$alice = (Invoke-Api "POST" "/api/v1/users" -Body $alicePayload).Data
|
|
$bob = (Invoke-Api "POST" "/api/v1/users" -Body $bobPayload).Data
|
|
$basic = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("$($alicePayload.username):$password"))
|
|
$session = (Invoke-Api "POST" "/api/v1/auth/sessions" -Authorization "Basic $basic").Data
|
|
$token = $session.token
|
|
$chat = (Invoke-Api "POST" "/api/v1/chats" -Token $token -Body @{ title = "Persistence check"; member_ids = @($alice.id, $bob.id) }).Data
|
|
$message = (Invoke-Api "POST" "/api/v1/chats/$($chat.id)/messages" -Token $token -Body @{ text = "persisted synthetic message" }).Data
|
|
|
|
& (Join-Path $PSScriptRoot "restart.ps1")
|
|
if ($LASTEXITCODE -ne 0) { throw "Restart action failed" }
|
|
|
|
$deadline = (Get-Date).AddSeconds(90)
|
|
do {
|
|
try {
|
|
$health = Invoke-Api "GET" "/health/ready"
|
|
$me = Invoke-Api "GET" "/api/v1/users/me" -Token $token
|
|
if ($health.Status -eq 200 -and $me.Status -eq 200) { break }
|
|
} catch { }
|
|
Start-Sleep -Milliseconds 500
|
|
} while ((Get-Date) -lt $deadline)
|
|
if ($me.Status -ne 200 -or $me.Data.id -ne $alice.id) { throw "Existing session did not survive service restart" }
|
|
|
|
$history = (Invoke-Api "GET" "/api/v1/chats/$($chat.id)/messages?limit=100" -Token $token).Data
|
|
if (-not ($history.items | Where-Object { $_.id -eq $message.id })) { throw "Confirmed message did not survive service restart" }
|
|
$logout = Invoke-Api "DELETE" "/api/v1/auth/sessions/current" -Token $token
|
|
if ($logout.Status -ne 204) { throw "Logout did not return 204" }
|
|
$revoked = Invoke-Api "GET" "/api/v1/users/me" -Token $token
|
|
if ($revoked.Status -ne 401) { throw "Revoked session remained usable" }
|
|
|
|
Write-Output "PASS: PostgreSQL message and existing Redis-backed session survived forced recreation of API, PostgreSQL, and Redis volumes; logout revoked the session."
|
|
} finally {
|
|
$client.Dispose()
|
|
}
|