Files
Assistant/tests/creator_commit/verify-remote.ps1
2026-08-16 16:25:55 +03:00

91 lines
5.6 KiB
PowerShell

param([string]$DockerContext = "docker-prod")
$ErrorActionPreference = "Stop"
$coreContainer = "assistant-core-runtime-1"
$commitContainer = "assistant-commit-runtime-1"
$continuityContainer = "assistant-continuity-runtime-1"
function Post-Json([string]$container, [string]$url, [object]$body) {
$json = $body | ConvertTo-Json -Depth 10 -Compress
$local = New-TemporaryFile
$remote = "/tmp/domovoy-$([guid]::NewGuid().ToString('N')).json"
try {
[IO.File]::WriteAllText($local, $json, [Text.UTF8Encoding]::new($false))
docker --context $DockerContext cp $local "${container}:$remote" | Out-Null
$result = docker --context $DockerContext exec $container wget -qO- --header "Content-Type: application/json" --post-file $remote $url
if ($LASTEXITCODE -ne 0) { throw "POST $url failed" }
return ($result -join "`n")
} finally {
docker --context $DockerContext exec --user root $container rm -f $remote 2>$null
Remove-Item -LiteralPath $local -Force
}
}
function Creator-Post([string]$url, [object]$body, [string]$token) {
$json = $body | ConvertTo-Json -Depth 10 -Compress
$local = New-TemporaryFile
$remote = "/tmp/domovoy-$([guid]::NewGuid().ToString('N')).json"
try {
[IO.File]::WriteAllText($local, $json, [Text.UTF8Encoding]::new($false))
docker --context $DockerContext cp $local "${commitContainer}:$remote" | Out-Null
$result = docker --context $DockerContext exec --env "CREATOR_TEST_SESSION=$token" $commitContainer sh -c 'wget -qO- --header="Content-Type: application/json" --header="Authorization: Bearer $CREATOR_TEST_SESSION" --post-file "$1" "$2"' sh $remote $url
if ($LASTEXITCODE -ne 0) { throw "Creator POST $url failed" }
return ($result -join "`n")
} finally {
docker --context $DockerContext exec --user root $commitContainer rm -f $remote 2>$null
Remove-Item -LiteralPath $local -Force
}
}
function Expect-CreatorStatus([string]$url, [object]$body, [string]$token, [int]$status) {
$json = $body | ConvertTo-Json -Depth 10 -Compress
$local = New-TemporaryFile
$remote = "/tmp/domovoy-$([guid]::NewGuid().ToString('N')).json"
try {
[IO.File]::WriteAllText($local, $json, [Text.UTF8Encoding]::new($false))
docker --context $DockerContext cp $local "${commitContainer}:$remote" | Out-Null
$previous = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$result = (docker --context $DockerContext exec --env "CREATOR_TEST_SESSION=$token" $commitContainer sh -c 'wget -S -O /dev/null --header="Content-Type: application/json" --header="Authorization: Bearer $CREATOR_TEST_SESSION" --post-file "$1" "$2"' sh $remote $url 2>&1) -join "`n"
$ErrorActionPreference = $previous
if ($result -notmatch "HTTP/1\.[01] $status") { throw "Creator POST $url did not return $status" }
} finally {
docker --context $DockerContext exec --user root $commitContainer rm -f $remote 2>$null
Remove-Item -LiteralPath $local -Force
}
}
$environment = docker --context $DockerContext inspect --format '{{range .Config.Env}}{{println .}}{{end}}' $coreContainer
$bootstrapEntry = $environment | Where-Object { $_ -like "CREATOR_BOOTSTRAP_TOKEN=*" } | Select-Object -First 1
if (-not $bootstrapEntry) { throw "Creator bootstrap metadata unavailable" }
$bootstrapToken = $bootstrapEntry.Substring("CREATOR_BOOTSTRAP_TOKEN=".Length)
$sessionResponse = docker --context $DockerContext exec --env "CREATOR_TEST_BOOTSTRAP=$bootstrapToken" $coreContainer sh -c 'wget -qO- --header="Authorization: Bearer $CREATOR_TEST_BOOTSTRAP" --post-data="" "$1/v1/creator/sessions"' sh http://localhost:8080
if ($LASTEXITCODE -ne 0) { throw "Creator session issuance failed" }
$creatorSession = ($sessionResponse | ConvertFrom-Json).Token
$suffix = [guid]::NewGuid().ToString("N")
$scope = "domovoy-global-scope"
$goal = "goal-creator-command-$suffix"
$task = "task-creator-command-$suffix"
$prospective = "prospective-creator-command-$suffix"
Post-Json $coreContainer http://localhost:8080/v1/tasks @{TaskID=$task;GoalID=$goal;ScopeID=$scope;Revision=1} | Out-Null
Post-Json $continuityContainer http://localhost:8081/v1/prospective-items @{
ID=$prospective;CommitmentID="commitment-$suffix";OwnerPrincipalID="domovoy-creator-principal"
ScopeID=$scope;GoalID=$goal;TriggerAtUnixMillis=1800000000000;State="DELIVERED";Revision=1
} | Out-Null
$command = @{
Target=@{TaskID=$task;GoalID=$goal;ScopeID=$scope;Revision=1}
ProspectiveItemID=$prospective;ExpectedRevision=1;EvidenceRef="artifact://creator-command/$suffix"
}
Expect-CreatorStatus http://localhost:8082/v1/creator/continuity/satisfy-delivered $command "invalid" 401
$action = Creator-Post http://localhost:8082/v1/creator/continuity/satisfy-delivered $command $creatorSession | ConvertFrom-Json
if ($action.State -ne "SUCCEEDED" -or -not $action.AuthorizedActionID -or -not $action.ActionIntentID -or -not $action.ActionAttemptID) {
throw "Creator command response is incomplete"
}
$updated = docker --context $DockerContext exec $continuityContainer wget -qO- "http://localhost:8081/v1/prospective-items/$prospective" | ConvertFrom-Json
if ($updated.State -ne "SATISFIED" -or $updated.Revision -ne 2) { throw "Creator command did not satisfy item exactly once" }
Expect-CreatorStatus http://localhost:8082/v1/creator/continuity/satisfy-delivered $command $creatorSession 409
Expect-CreatorStatus "http://localhost:8082/v1/creator/authorized-actions/$($action.AuthorizedActionID)/reconcile" @{} $creatorSession 409
Write-Output "Creator Commit command verification: PASS"