tests/BuiltinTools.Tests.ps1

$moduleRoot = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $moduleRoot 'Octa.psd1') -Force

Describe 'Show-OctaDashboard' {
    It 'never changes registry or service state (SC-014)' {
        $before = Get-Service | Select-Object -ExpandProperty Status
        Show-OctaDashboard | Out-Null
        $after = Get-Service | Select-Object -ExpandProperty Status
        (Compare-Object $before $after) | Should Be $null
    }

    It 'reports non-null CPU, RAM, and OS info' {
        $report = Show-OctaDashboard
        $report.Cpu.Name | Should Not BeNullOrEmpty
        $report.Ram.TotalGB | Should Not Be 0
        $report.Os.Caption | Should Not BeNullOrEmpty
    }
}

Describe 'Invoke-OctaStartupManager' {
    It 'lists startup items without throwing and without modifying anything' {
        { Invoke-OctaStartupManager } | Should Not Throw
    }

    It 'reports NotFound for a startup item that does not exist, without modifying the real system' {
        $result = Invoke-OctaStartupManager -Disable 'ThisStartupItemDoesNotExist12345'
        $result.Status | Should Be 'NotFound'
    }

    It 'disabling a real Run-key item is genuinely undoable (regression test - Save-OctaRegistrySnapshot no longer self-checks existence, 006)' {
        $runKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
        $testName = 'OctaPesterTestStartup'
        # The Run key doesn't exist at all on some minimal CI images (found: GitHub Actions'
        # windows-latest runner) unlike any normal desktop Windows install - create it first.
        if (-not (Test-Path $runKey)) {
            New-Item -Path $runKey -Force | Out-Null
        }
        Remove-ItemProperty -Path $runKey -Name $testName -ErrorAction SilentlyContinue
        Set-ItemProperty -Path $runKey -Name $testName -Value 'C:\Windows\System32\cmd.exe /c exit' -Type String

        try {
            $result = Invoke-OctaStartupManager -Disable $testName
            $result.Status | Should Be 'Success'
            (Get-ItemProperty -Path $runKey -Name $testName -ErrorAction SilentlyContinue) | Should Be $null

            $undo = Undo-OctaRun -RunId $result.RunId
            $undo.Status | Should Be 'Success'
            (Get-ItemProperty -Path $runKey -Name $testName -ErrorAction SilentlyContinue).$testName | Should Not BeNullOrEmpty
        }
        finally {
            Remove-ItemProperty -Path $runKey -Name $testName -ErrorAction SilentlyContinue
        }
    }
}

Describe 'Invoke-OctaTaskBrowser' {
    It 'lists scheduled tasks without throwing' {
        { Invoke-OctaTaskBrowser } | Should Not Throw
    }
}

Describe 'Get-OctaCleanupTargets' {
    # Computed once for both It blocks below - scanning SoftwareDistribution\Download etc.
    # is real disk I/O, not free; read-only measurement only, never calls
    # Invoke-OctaDiskCleanup -Apply, which would delete real files on whoever runs this test.
    # Real deletion is validated on the disposable VM.
    $targets = Get-OctaCleanupTargets

    It 'measures real, non-negative bytes for every target and never throws' {
        foreach ($t in $targets) {
            ($t.MeasuredBytes -ge 0) | Should Be $true
            $t.Reversible | Should Be $false
            $t.IrreversibleReason | Should Not BeNullOrEmpty
        }
    }

    It 'classifies Windows.old as Risky if present on this machine' {
        $windowsOld = $targets | Where-Object { $_.Name -match 'Windows.old' }
        if ($windowsOld) {
            $windowsOld.RiskLevel | Should Be 'Risky'
        }
    }
}