tests/Undo.Tests.ps1

$moduleRoot = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $moduleRoot 'Octa.psd1') -Force
. (Join-Path $moduleRoot 'private\ActionHelpers.ps1')
. (Join-Path $moduleRoot 'private\StateSnapshot.ps1')

Describe 'Save-OctaActionSnapshot / Restore-OctaActionSnapshot round-trip (registry)' {
    $testKey = 'HKCU:\Software\OctaPesterTest'
    $tempFolder = Join-Path $env:TEMP ('octa-undo-test-{0}' -f [guid]::NewGuid())
    New-Item -ItemType Directory -Path $tempFolder -Force | Out-Null
    New-Item -Path $testKey -Force | Out-Null
    Set-ItemProperty -Path $testKey -Name 'Marker' -Value 1 -Type DWord

    It 'snapshots the prior value, then restores it after a change' {
        $action = New-OctaAction -TargetType Registry -TargetIdentifier "$testKey|Marker" `
            -CurrentValue 1 -PlannedValue 0 -Reversible $true

        $snapshot = Save-OctaActionSnapshot -Action $action -RunFolder $tempFolder
        $snapshot | Should Not Be $null

        Set-ItemProperty -Path $testKey -Name 'Marker' -Value 0 -Type DWord
        (Get-ItemProperty -Path $testKey -Name 'Marker').Marker | Should Be 0

        Restore-OctaActionSnapshot -Snapshot $snapshot -RunFolder $tempFolder
        (Get-ItemProperty -Path $testKey -Name 'Marker').Marker | Should Be 1
    }

    Remove-Item -Path $testKey -Recurse -Force -ErrorAction SilentlyContinue
    Remove-Item -Path $tempFolder -Recurse -Force -ErrorAction SilentlyContinue
}

Describe 'Undo-OctaRun' {
    It 'reports NotFound for a run id that does not exist' {
        $result = Undo-OctaRun -RunId 'this-run-id-does-not-exist'
        $result.Status | Should Be 'NotFound'
    }
}