improve detection of space savings

This commit is contained in:
2026-10-23 11:22:55 -04:00
parent ec9141d062
commit c5ef81dfca
4 changed files with 181 additions and 7 deletions
+66
View File
@@ -30,6 +30,7 @@ function New-CleanupFixture {
try {
$core = Read-CleanupAst 'Start-SystemCleanup.ps1'
$shim = Read-CleanupAst 'rmm/Start-SystemCleanup.ps1'
Assert-Cleanup ($core.Extent.Text -notmatch '(?i)AnalyzeComponentStore|dism\.exe') 'Reports must not ship a DISM analysis invocation.'
foreach ($definition in $core.FindAll({param($n) $n -is [Management.Automation.Language.FunctionDefinitionAst]}, $false)) {
. ([scriptblock]::Create($definition.Extent.Text))
}
@@ -250,6 +251,70 @@ try {
Assert-Cleanup ($c.Errors -eq 0) 'None must not attempt native cleanup, even through the helper directly.'
}
# Report diagnostics remain independent of enablement and never inflate savings.
& {
$script:fileReads=0; $script:storageReads=0; $script:diagnosticProcesses=0
function Start-Process { $script:diagnosticProcesses++; throw 'Report diagnostics must not launch native programs.' }
$script:hiberState='present'; $script:storageState='present'
$sourceId='\\?\Volume{11111111-1111-1111-1111-111111111111}\'
$otherId='\\?\Volume{22222222-2222-2222-2222-222222222222}\'
$script:storageTarget=$sourceId
function Get-Item {
param($LiteralPath)
$script:fileReads++
if ($script:hiberState -eq 'missing') { throw [Management.Automation.ItemNotFoundException]::new('fixture absent') }
if ($script:hiberState -eq 'denied') { throw [UnauthorizedAccessException]::new('fixture denied') }
[pscustomobject]@{Length=4GB; PSIsContainer=$false; Attributes=[IO.FileAttributes]::Hidden}
}
function Get-CimInstance {
param($ClassName)
$script:storageReads++
if ($script:storageState -eq 'denied') { throw 'Synthetic CIM access failure' }
if ($ClassName -eq 'Win32_Volume') {
[pscustomobject]@{DeviceID=$sourceId; DriveLetter=$env:SystemDrive}
[pscustomobject]@{DeviceID=$otherId; DriveLetter='Z:'}
} elseif ($ClassName -eq 'Win32_ShadowStorage' -and $script:storageState -ne 'empty') {
# Source as escaped WMI path; destination as embedded CIM-style object.
[pscustomobject]@{Volume='Win32_Volume.DeviceID="\\\\?\\Volume{11111111-1111-1111-1111-111111111111}\\"'; DiffVolume=[pscustomobject]@{DeviceID=$script:storageTarget}; UsedSpace=$(if ($script:storageState -eq 'invalid') { $null } else { 2GB }); AllocatedSpace=3GB}
[pscustomobject]@{Volume=[pscustomobject]@{DeviceID=$otherId}; DiffVolume=[pscustomobject]@{DeviceID=$otherId}; UsedSpace=99GB; AllocatedSpace=100GB}
}
}
$c=New-CleanupContext
foreach ($name in @('DisableHibernation','ShadowCopies','ComponentStore')) {
$c.Options[$name]=$false
Invoke-SystemCleanupCategory $c $name {}
}
$c.Diagnostics=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($c.Diagnostics.Hibernation -match '4.00 GiB file size' -and $c.Diagnostics.ShadowStorage -match '2.00 GiB used / 3.00 GiB allocated' -and $c.Diagnostics.ComponentStore -match 'not analyzed;.*superseded updates.*substantial.*little/none') 'Disabled options must still receive usage diagnostics and an honest note about potential component-store savings.'
Assert-Cleanup ($c.Diagnostics.ShadowStorage -notmatch '99.00|100.00' -and $c.Diagnostics.ShadowStorage.EndsWith("stored on $env:SystemDrive")) 'VSS must select the system volume and report its actual storage location.'
$totals=Get-SystemCleanupReportTotals $c
Assert-Cleanup ($totals.EnabledBytes -eq 0 -and $totals.AdditionalDisabledBytes -eq 0 -and -not $c.Options.ShadowCopies -and $c.Errors -eq 0) 'Diagnostic usage is not reclaimable savings and must not enable cleanup.'
$script:messages=@()
Write-SystemCleanupReport $c
$report=$script:messages -join "`n"
Assert-Cleanup ($report -match 'DIAGNOSTICS - current usage/potential, excluded from savings totals' -and [regex]::Matches($report,'(?m)^ (Hibernation|ShadowStorage|ComponentStore):').Count -eq 3) 'Diagnostics must occupy three compact lines outside category totals.'
$c.Options.ComponentStore=$true
$d=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($script:diagnosticProcesses -eq 0 -and $d.ComponentStore -eq $c.Diagnostics.ComponentStore) 'Enabling component cleanup must not launch analysis or fabricate a measured recommendation in report mode.'
$script:storageTarget=$otherId
Assert-Cleanup ((Get-SystemCleanupShadowStorageDiagnostic) -match 'stored on Z:') 'Shadow storage on another drive must not be attributed to the system drive.'
$script:storageState='empty'; $script:hiberState='missing'
$d=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($d.Hibernation -eq 'hiberfil.sys not present' -and $d.ShadowStorage -match 'no shadow-storage association') 'Absent hibernation or VSS data must be reported explicitly.'
$script:storageState='invalid'
Assert-Cleanup ((Get-SystemCleanupDiagnostics $c).ShadowStorage -match 'unavailable') 'Missing VSS measurements must not become zero.'
$script:storageState='denied'; $script:hiberState='denied'
$d=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($d.Hibernation -match 'unavailable' -and $d.ShadowStorage -match 'unavailable' -and $d.ComponentStore -match 'not analyzed' -and $c.Errors -eq 0) 'A diagnostic failure must not prevent the other diagnostics or fail cleanup reporting.'
$script:fileReads=0; $script:storageReads=0
$c.OS.IsServer=$true
$d=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($script:fileReads -eq 0 -and $script:storageReads -eq 0 -and $script:diagnosticProcesses -eq 0 -and $d.Hibernation -match 'server policy' -and $d.ShadowStorage -match 'server policy' -and $d.ComponentStore -match 'not analyzed') 'Server reports must block hibernation/VSS assessment and only note potential component-store savings.'
$c.Options.ReportOnly=$false
$d=Get-SystemCleanupDiagnostics $c
Assert-Cleanup ($d.Count -eq 0 -and $script:fileReads -eq 0 -and $script:storageReads -eq 0 -and $script:diagnosticProcesses -eq 0) 'Apply mode must not run report diagnostics.'
}
# File ages, literal brackets, nested fresh files, no changes in preview.
$root=Join-Path $testRoot 'temps'
$old=New-CleanupFixture 'temps\old[1].tmp'
@@ -502,6 +567,7 @@ try {
}
Assert-Cleanup ($compactOutput -notmatch 'Assessing |selected items:|items not selected|CandidateFiles\s*:' -and $compactOutput -match 'Standard \(7 handlers\)') 'Default output must show the preset without progress chatter, handler inventories, or object dumps.'
Assert-Cleanup (($script:details -join "`n") -match 'items not selected.*Previous Installations') 'Full handler details must remain available in verbose output.'
Assert-Cleanup ($compactOutput -match 'Hibernation: .*file size' -and $compactOutput -match 'ShadowStorage:' -and $compactOutput -match 'ComponentStore: not analyzed;.*superseded updates.*substantial.*little/none') 'Full entry-point report must include usage and potential savings notes even with cleanup disabled.'
function Test-SystemCleanupAppRunning { return $true }
function Get-Command { return $null }
$o.DiskCleanupItems=@(); $o.DiskCleanupPreset='None'