-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRun-AutonomousIntegrationLoop.Tests.ps1
More file actions
257 lines (245 loc) · 13.9 KB
/
Copy pathRun-AutonomousIntegrationLoop.Tests.ps1
File metadata and controls
257 lines (245 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Requires -Version 5.1
# Pester tests validating autonomous loop script behaviors (final status JSON & DiffExitCode) using
# environment-driven simulation (LOOP_SIMULATE) to avoid cross-process scriptblock passing.
Describe 'Run-AutonomousIntegrationLoop FinalStatusJsonPath emission' -Tag 'Unit' {
BeforeAll { . "$PSScriptRoot/TestHelpers.Schema.ps1" }
It 'emits final status JSON with expected shape in simulate mode' {
$here = Split-Path -Parent $PSCommandPath
$repoRoot = Resolve-Path (Join-Path $here '..')
$scriptPath = Join-Path $repoRoot 'scripts' 'Run-AutonomousIntegrationLoop.ps1'
$outDir = Join-Path $TestDrive 'loop'
New-Item -ItemType Directory -Path $outDir -Force | Out-Null
$finalStatusPath = Join-Path $outDir 'final-status.json'
$base = Join-Path $outDir 'VI1.vi'
$head = Join-Path $outDir 'VI2.vi'
New-Item -ItemType File -Path $base -Force | Out-Null
New-Item -ItemType File -Path $head -Force | Out-Null
$runner = Join-Path $outDir 'runner-finalstatus.ps1'
$runnerContent = @"
& '$scriptPath' -Base '$base' -Head '$head' -MaxIterations 3 -IntervalSeconds 0 -FinalStatusJsonPath '$finalStatusPath' -DiffSummaryFormat None -LogVerbosity Quiet -FailOnDiff:`$false -CustomExecutor { param(`$CliPath,`$Base,`$Head,`$ExecArgs) Start-Sleep -Milliseconds 3; return 0 }
exit `$LASTEXITCODE
"@
Set-Content -LiteralPath $runner -Encoding UTF8 -Value $runnerContent
pwsh -NoLogo -NoProfile -File $runner | Out-Null
$exit = $LASTEXITCODE
$exit | Should -Be 0
Test-Path -LiteralPath $finalStatusPath | Should -BeTrue
$json = (Get-Content -LiteralPath $finalStatusPath -Raw) | ConvertFrom-Json
$json.schema | Should -Be 'loop-final-status-v1'
Assert-JsonShape -Path $finalStatusPath -Spec 'FinalStatus' | Should -BeTrue
$json.iterations | Should -Be 3
$json.errors | Should -Be 0
$json.succeeded | Should -BeTrue
$json.basePath | Should -Match 'VI1\.vi'
$json.headPath | Should -Match 'VI2\.vi'
}
}
Describe 'Run-AutonomousIntegrationLoop DiffExitCode behavior' -Tag 'Unit' {
BeforeAll { . "$PSScriptRoot/TestHelpers.Schema.ps1" }
It 'returns custom diff exit code when diffs detected and no errors' {
$here = Split-Path -Parent $PSCommandPath
$repoRoot = Resolve-Path (Join-Path $here '..')
$scriptPath = Join-Path $repoRoot 'scripts' 'Run-AutonomousIntegrationLoop.ps1'
$outDir = Join-Path $TestDrive 'loop-diff'
New-Item -ItemType Directory -Path $outDir -Force | Out-Null
$finalStatusPath = Join-Path $outDir 'final-status.json'
$base = Join-Path $outDir 'A.vi'
$head = Join-Path $outDir 'B.vi'
New-Item -ItemType File -Path $base -Force | Out-Null
New-Item -ItemType File -Path $head -Force | Out-Null
$customDiffExit = 42
$runner = Join-Path $outDir 'runner-diffexit.ps1'
$runnerContent = @"
& '$scriptPath' -Base '$base' -Head '$head' -MaxIterations 2 -IntervalSeconds 0 -FinalStatusJsonPath '$finalStatusPath' -DiffSummaryFormat None -LogVerbosity Quiet -FailOnDiff:`$false -DiffExitCode $customDiffExit -CustomExecutor { param(`$CliPath,`$Base,`$Head,`$ExecArgs) Start-Sleep -Milliseconds 3; return 1 }
exit `$LASTEXITCODE
"@
Set-Content -LiteralPath $runner -Encoding UTF8 -Value $runnerContent
pwsh -NoLogo -NoProfile -File $runner | Out-Null
$exit = $LASTEXITCODE
$exit | Should -Be $customDiffExit
$json = (Get-Content -LiteralPath $finalStatusPath -Raw) | ConvertFrom-Json
$json.diffs | Should -BeGreaterThan 0
$json.errors | Should -Be 0
$json.succeeded | Should -BeTrue
}
}
Describe 'Run-AutonomousIntegrationLoop TestStand harness mode' -Tag 'Unit' {
It 'invokes the TestStand harness for each iteration with expected parameters' {
$here = Split-Path -Parent $PSCommandPath
$repoRoot = Resolve-Path (Join-Path $here '..')
$scriptPath = Join-Path $repoRoot 'scripts' 'Run-AutonomousIntegrationLoop.ps1'
$outDir = Join-Path $TestDrive 'loop-harness'
New-Item -ItemType Directory -Path $outDir -Force | Out-Null
$base = Join-Path $outDir 'BaseHarness.vi'
$head = Join-Path $outDir 'HeadHarness.vi'
$leasePath = Join-Path $outDir 'execution-cell.json'
$harnessLeasePath = Join-Path $outDir 'harness-instance.json'
New-Item -ItemType File -Path $base -Force | Out-Null
New-Item -ItemType File -Path $head -Force | Out-Null
@'
{
"schema": "priority/execution-cell-lease-report@v1",
"summary": {
"cellClass": "worker",
"suiteClass": "dual-plane-parity",
"operatorAuthorizationRef": "budget-auth://operator/session-2026-03-24",
"premiumSaganMode": false
}
}
'@ | Set-Content -LiteralPath $leasePath -Encoding UTF8
@'
{
"instanceId": "ts-loop-hooke-01",
"request": {
"role": "worker",
"runtimeSurface": "windows-native-teststand",
"processModelClass": "parallel-process-model"
},
"grant": {
"leaseId": "harness-lease-hooke-loop-01"
}
}
'@ | Set-Content -LiteralPath $harnessLeasePath -Encoding UTF8
$harnessStub = Join-Path $outDir 'TestStand-CompareHarness.ps1'
$logPath = Join-Path $outDir 'harness-log.ndjson'
$jsonLogPath = Join-Path $outDir 'loop.events.json'
$outputRoot = Join-Path $outDir 'outputs'
$stubContent = @"
param(
[string]`$BaseVi,
[string]`$HeadVi,
[Alias('LabVIEWPath')][string]`$LabVIEWExePath,
[string]`$LabVIEW64ExePath,
[string]`$LabVIEW32ExePath,
[Alias('LVCompareExePath')][string]`$LVComparePath,
[string]`$AgentId,
[string]`$AgentClass,
[string]`$ExecutionCellLeasePath,
[string]`$ExecutionCellId,
[string]`$ExecutionCellLeaseId,
[string]`$HarnessInstanceLeasePath,
[string]`$HarnessInstanceId,
[string]`$OutputRoot,
[ValidateSet('detect','spawn','skip')][string]`$Warmup,
[ValidateSet('single-compare','dual-plane-parity')][string]`$SuiteClass = 'single-compare',
[string[]]`$Flags,
[switch]`$RenderReport,
[switch]`$CloseLabVIEW,
[switch]`$CloseLVCompare,
[int]`$TimeoutSeconds,
[switch]`$DisableTimeout,
[switch]`$ReplaceFlags
)
`$log = `$env:HARNESS_LOG
if (-not `$log) { `$log = Join-Path (Split-Path `$OutputRoot -Parent) 'harness-log.ndjson' }
`$logDir = Split-Path -Parent `$log
if (`$logDir -and -not (Test-Path `$logDir)) { New-Item -ItemType Directory -Path `$logDir -Force | Out-Null }
`$payload = [ordered]@{
base = `$BaseVi
head = `$HeadVi
output = `$OutputRoot
warmup = `$Warmup
suiteClass = `$SuiteClass
labviewExe = `$LabVIEWExePath
labview64Exe = `$LabVIEW64ExePath
labview32Exe = `$LabVIEW32ExePath
agentId = `$AgentId
agentClass = `$AgentClass
executionCellLeasePath = `$ExecutionCellLeasePath
executionCellId = `$ExecutionCellId
executionCellLeaseId = `$ExecutionCellLeaseId
harnessInstanceLeasePath = `$HarnessInstanceLeasePath
harnessInstanceId = `$HarnessInstanceId
flags = @(`$Flags)
renderReport = `$RenderReport.IsPresent
closeLabVIEW = `$CloseLabVIEW.IsPresent
closeLVCompare = `$CloseLVCompare.IsPresent
timeout = `$TimeoutSeconds
disableTimeout = `$DisableTimeout.IsPresent
replaceFlags = `$ReplaceFlags.IsPresent
}
(`$payload | ConvertTo-Json -Compress) | Add-Content -Path `$log
`$sessionIndex = [ordered]@{
schema = 'teststand-compare-session-v1'
harnessInstance = [ordered]@{
instanceId = "session-`$HarnessInstanceId"
leaseId = 'harness-lease-session-hooke-loop-01'
leasePath = (Join-Path `$OutputRoot 'session-harness-lease.json')
}
processModel = [ordered]@{
runtimeSurface = 'windows-native-teststand'
processModelClass = if (`$SuiteClass -eq 'dual-plane-parity') { 'parallel-process-model' } else { 'sequential-process-model' }
}
}
(`$sessionIndex | ConvertTo-Json -Depth 8) | Set-Content -LiteralPath (Join-Path `$OutputRoot 'session-index.json') -Encoding UTF8
if (`$env:HARNESS_EXIT_CODE) { exit [int]`$env:HARNESS_EXIT_CODE }
exit 0
"@
Set-Content -LiteralPath $harnessStub -Encoding UTF8 -Value $stubContent
$env:HARNESS_LOG = $logPath
try {
$runner = Join-Path $outDir 'runner-harness.ps1'
$runnerContent = @"
& '$scriptPath' -Base '$base' -Head '$head' -MaxIterations 2 -IntervalSeconds 0 -LogVerbosity Quiet -JsonLogPath '$jsonLogPath' -LvCompareArgs '-foo 1 -bar' -UseTestStandHarness -TestStandHarnessPath '$harnessStub' -TestStandOutputRoot '$outputRoot' -TestStandWarmup detect -TestStandSuiteClass dual-plane-parity -TestStandLabVIEW64Path 'C:\Program Files\National Instruments\LabVIEW 2026\LabVIEW.exe' -TestStandLabVIEW32Path 'C:\Program Files (x86)\National Instruments\LabVIEW 2026\LabVIEW.exe' -TestStandAgentId 'hooke' -TestStandAgentClass 'subagent' -TestStandExecutionCellLeasePath '$leasePath' -TestStandExecutionCellId 'exec-cell-hooke-loop-01' -TestStandExecutionCellLeaseId 'lease-hooke-loop-01' -TestStandHarnessInstanceLeasePath '$harnessLeasePath' -TestStandHarnessInstanceId 'ts-loop-hooke-01' -TestStandRenderReport -TestStandCloseLabVIEW -TestStandCloseLVCompare -TestStandTimeoutSeconds 45 -TestStandReplaceFlags -FinalStatusJsonPath '$outDir/final.json'
exit `$LASTEXITCODE
"@
Set-Content -LiteralPath $runner -Encoding UTF8 -Value $runnerContent
pwsh -NoLogo -NoProfile -File $runner | Out-Null
$LASTEXITCODE | Should -Be 0
Test-Path -LiteralPath $logPath | Should -BeTrue
$entries = Get-Content -LiteralPath $logPath | ForEach-Object { $_ | ConvertFrom-Json }
$entries.Count | Should -Be 2
$entries[0].output | Should -Match 'iteration-0001$'
$entries[1].output | Should -Match 'iteration-0002$'
$entries | ForEach-Object { $_.warmup } | Sort-Object -Unique | Should -Be @('detect')
$entries | ForEach-Object { $_.suiteClass } | Sort-Object -Unique | Should -Be @('dual-plane-parity')
$entries | ForEach-Object { $_.labview64Exe } | Sort-Object -Unique | Should -Be @('C:\Program Files\National Instruments\LabVIEW 2026\LabVIEW.exe')
$entries | ForEach-Object { $_.labview32Exe } | Sort-Object -Unique | Should -Be @('C:\Program Files (x86)\National Instruments\LabVIEW 2026\LabVIEW.exe')
$entries | ForEach-Object { $_.agentId } | Sort-Object -Unique | Should -Be @('hooke')
$entries | ForEach-Object { $_.agentClass } | Sort-Object -Unique | Should -Be @('subagent')
$entries | ForEach-Object { $_.executionCellLeasePath } | Sort-Object -Unique | Should -Be @($leasePath)
$entries | ForEach-Object { $_.executionCellId } | Sort-Object -Unique | Should -Be @('exec-cell-hooke-loop-01')
$entries | ForEach-Object { $_.executionCellLeaseId } | Sort-Object -Unique | Should -Be @('lease-hooke-loop-01')
$entries | ForEach-Object { $_.harnessInstanceLeasePath } | Sort-Object -Unique | Should -Be @($harnessLeasePath)
$entries | ForEach-Object { $_.harnessInstanceId } | Sort-Object -Unique | Should -Be @('ts-loop-hooke-01')
$entries | ForEach-Object { $_.renderReport } | Sort-Object -Unique | Should -Be @($true)
$entries | ForEach-Object { $_.closeLabVIEW } | Sort-Object -Unique | Should -Be @($true)
$entries | ForEach-Object { $_.closeLVCompare } | Sort-Object -Unique | Should -Be @($true)
$entries | ForEach-Object { [int]$_.timeout } | Sort-Object -Unique | Should -Be @(45)
$entries | ForEach-Object { $_.replaceFlags } | Sort-Object -Unique | Should -Be @($true)
$entries | ForEach-Object { $_.flags } | ForEach-Object { $_ } | Sort-Object -Unique | Should -Be @('-bar','-foo','1')
$eventEntries = Get-Content -LiteralPath $jsonLogPath | ForEach-Object { $_ | ConvertFrom-Json }
$planEvent = $eventEntries | Where-Object { $_.type -eq 'plan' } | Select-Object -First 1
$planEvent.executionTopology.runtimeSurface | Should -Be 'windows-native-teststand'
$planEvent.executionTopology.processModelClass | Should -Be 'parallel-process-model'
$planEvent.executionTopology.executionCellLeaseId | Should -Be 'lease-hooke-loop-01'
$planEvent.executionTopology.harnessInstanceLeaseId | Should -Be 'harness-lease-hooke-loop-01'
$planEvent.executionTopology.harnessInstanceId | Should -Be 'ts-loop-hooke-01'
$lastHarnessResult = $eventEntries | Where-Object { $_.type -eq 'harnessResult' -and $_.PSObject.Properties.Name -contains 'exitCode' } | Select-Object -Last 1
$lastHarnessResult.executionTopology.executionCellLeaseId | Should -Be 'lease-hooke-loop-01'
$lastHarnessResult.executionTopology.harnessInstanceLeaseId | Should -Be 'harness-lease-session-hooke-loop-01'
$lastHarnessResult.executionTopology.harnessInstanceLeasePath | Should -Be (Join-Path (Join-Path $outputRoot 'iteration-0002') 'session-harness-lease.json')
$lastHarnessResult.executionTopology.harnessInstanceId | Should -Be 'session-ts-loop-hooke-01'
$finalStatus = Get-Content -LiteralPath (Join-Path $outDir 'final.json') -Raw | ConvertFrom-Json
$finalStatus.harness.path | Should -Be $harnessStub
$finalStatus.harness.output | Should -Be $outputRoot
$finalStatus.harness.suiteClass | Should -Be 'dual-plane-parity'
$finalStatus.harness.runtimeSurface | Should -Be 'windows-native-teststand'
$finalStatus.harness.processModelClass | Should -Be 'parallel-process-model'
$finalStatus.harness.windowsOnly | Should -BeTrue
$finalStatus.harness.requestedSimultaneous | Should -BeTrue
$finalStatus.harness.cellClass | Should -Be 'worker'
$finalStatus.harness.operatorAuthorizationRef | Should -Be 'budget-auth://operator/session-2026-03-24'
$finalStatus.harness.premiumSaganMode | Should -BeFalse
$finalStatus.harness.executionCellLeasePath | Should -Be $leasePath
$finalStatus.harness.executionCellId | Should -Be 'exec-cell-hooke-loop-01'
$finalStatus.harness.executionCellLeaseId | Should -Be 'lease-hooke-loop-01'
$finalStatus.harness.harnessInstanceLeasePath | Should -Be (Join-Path (Join-Path $outputRoot 'iteration-0002') 'session-harness-lease.json')
$finalStatus.harness.harnessInstanceLeaseId | Should -Be 'harness-lease-session-hooke-loop-01'
$finalStatus.harness.harnessInstanceId | Should -Be 'session-ts-loop-hooke-01'
}
finally {
Remove-Item Env:HARNESS_LOG -ErrorAction SilentlyContinue
}
}
}