BeforeAll { Import-Module "$PSScriptRoot/../src/State.psm1" -Force } Describe 'Run state recovery' { It 'blocks concurrent applies and permits a rerun after release' { $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'data') $first = Enter-ApplyLock -Paths $paths try { { Enter-ApplyLock -Paths $paths } | Should -Throw '*Another Config Central apply*' } finally { Exit-ApplyLock -Lock $first -Paths $paths } $second = Enter-ApplyLock -Paths $paths $second | Should -Not -BeNullOrEmpty Exit-ApplyLock -Lock $second -Paths $paths } It 'replaces state atomically without leaving temporary files' { $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'state-data') Write-JsonAtomic -Path $paths.State -Value @{ schemaVersion = 1; value = 'new' } (Read-JsonState -Path $paths.State -Default @{}).value | Should -Be 'new' @(Get-ChildItem $paths.Root -Filter '*.tmp').Count | Should -Be 0 } }