Files
mtusi2026lab3-fork/scripts/manual.ps1
T

43 lines
2.3 KiB
PowerShell

param([string]$BaseUrl = 'http://127.0.0.1:8080')
# Dot-source this script to keep the synthetic session in the current terminal.
$Lab = @{ BaseUrl = $BaseUrl.TrimEnd('/'); Headers = @{} }
function Invoke-LabApi {
param(
[string]$Method,
[string]$Path,
[object]$Body = $null,
[hashtable]$Headers = $Lab.Headers
)
$request = @{ Method=$Method; Uri=($Lab.BaseUrl+$Path); Headers=$Headers; UseBasicParsing=$true; ErrorAction='Stop' }
if ($null -ne $Body) {
$request.ContentType = 'application/json; charset=utf-8'
$request.Body = [Text.Encoding]::UTF8.GetBytes(($Body | ConvertTo-Json -Depth 8 -Compress))
}
$response = Invoke-WebRequest @request
$Lab.LastInstance = $response.Headers['X-Instance-Id'] -join ','
Write-Host ('HTTP {0}; X-Instance-Id={1}; X-Request-Id={2}' -f [int]$response.StatusCode, $Lab.LastInstance, ($response.Headers['X-Request-Id'] -join ','))
if ($response.Content) { return ($response.Content | ConvertFrom-Json) }
}
$suffix = [guid]::NewGuid().ToString('N').Substring(0,10)
$password = 'Synthetic-' + [guid]::NewGuid().ToString('N')
$username = 'manual_a_' + $suffix
$Lab.Alice = Invoke-LabApi POST '/api/v1/users' @{username=$username; display_name='Alice'; password=$password}
$Lab.Bob = Invoke-LabApi POST '/api/v1/users' @{username=('manual_b_'+$suffix); display_name='Bob'; password=$password}
$basic = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(($username+':'+$password)))
$session = Invoke-LabApi POST '/api/v1/auth/sessions' -Headers @{Authorization=('Basic '+$basic)}
$Lab.Headers = @{ Authorization=('Bearer '+$session.token) }
$Lab.Chat = Invoke-LabApi POST '/api/v1/chats' @{title='Manual lab3'; member_ids=@($Lab.Alice.id,$Lab.Bob.id)}
$Lab.MessagePath = '/api/v1/chats/'+$Lab.Chat.id+'/messages'
$Lab.Key = [guid]::NewGuid().ToString('N')
$Lab.Headers['Idempotency-Key'] = $Lab.Key
$Lab.Body = @{ text='Manual idempotent message' }
$Lab.Message = Invoke-LabApi POST $Lab.MessagePath $Lab.Body
for ($i=0; $i -lt 4; $i++) {
$replay = Invoke-LabApi POST $Lab.MessagePath $Lab.Body
if ($replay.id -ne $Lab.Message.id) { throw 'Replay created a duplicate' }
}
Remove-Variable password,basic,session
Write-Host ('Ready: chat={0}; message={1}. Session is kept in $Lab; do not print or save its Authorization header.' -f $Lab.Chat.id,$Lab.Message.id)