Assert-Cleanup($visible.Count-eq1-and[string]$visible[0]-match'compact output'-and$hidden.Count-eq0)'Verbose diagnostics must not consume the normal RMM output budget.'
Assert-Cleanup($verbose.Count-eq1-and$verbose[0]-is[Management.Automation.VerboseRecord])'Full diagnostics must remain available on the verbose stream.'
}
&{
# Run the actual entry statement with an inert function. The host summary
# must survive, while success-stream objects must not become a second report.
Assert-Cleanup($output.Count-eq0-and$script:messages-contains'entry summary')'Core entry must suppress the duplicate object dump without suppressing the summary.'
Assert-Cleanup($options.DiskCleanupPreset-eq'Standard'-and-not$options.ContainsKey('DiskCleanup'))'Disk Cleanup must use a single selection, defaulting to Standard.'
# Lock down the intended policy: adding handlers changes what operators permit.
Assert-Cleanup($parsed.DiskCleanupPreset-eq'Custom'-and($parsed.DiskCleanupItems-join',')-ceq'Thumbnail Cache,Internet Cache Files')'Custom arrays must normalize and deduplicate handlers.'
Assert-Cleanup($selected.Count-eq1)'Shim did not return the inert workload result.'
$injectedOptions=$selected[0]
Assert-Cleanup(-not$injectedOptions.ReportOnly-and-not$injectedOptions.SystemTemp-and$injectedOptions.TempDays-eq14)'Run-Script lost Syncro-injected strings.'
Assert-Cleanup(($injectedOptions.DiskCleanupItems-join'|')-ceq($expectedItems-join'|'))"Run-Script lost the injected selection: $selectionName"
Assert-Cleanup($injectedOptions.DiagnosticDays-eq30-and$CleanupReportOnly-ceq'false'-and$CleanupTempDays-ceq'14'-and$CleanupDiskCleanupItems-ceq$selectionName)'Absent defaults or original caller variables changed.'
}
}
# All server/DC gates, including when enabled, and unknown OS failure.
Assert-Cleanup(-not$script:serverAssessmentRan-and-not$serverContext.Stats.ShadowCopies.Enabled-and$serverContext.Stats.ShadowCopies.Status-eq'Blocked on server'-and$null-eq$serverContext.Stats.ShadowCopies.CandidateBytes)'A default-disabled server operation must be listed without assessment.'
Assert-Cleanup($result.Count-eq22-and@($result|Where-ObjectStatus-eq'Blocked on server').Count-eq$blocked.Count)'Server report must list every blocked category.'
Assert-Cleanup(@($script:assessed|Where-Object{$_-in$blocked}).Count-eq0)'Server report must not assess blocked categories.'
}
# None is disabled in reports and apply; every nonempty preset remains blocked
# on both server types. No host inventories, registry operations or native calls.
Assert-Cleanup($report-match'(?s)DISABLED -.*DiskCleanup\s+-\s+-\s+.*None \(0 handlers\); will not run')'None must appear in the disabled report section with a clear explanation.'
Assert-Cleanup(($script:details-join"`n")-match'items not selected.*Previous Installations'-and$report-notmatch'Previous Installations|CleanupDiskCleanup=')'Unselected handler inventories belong in verbose diagnostics, not the compact report.'
Assert-Cleanup($c.Stats.DisableHibernation.UsageBytes-eq4GB-and$c.Stats.ShadowCopies.UsageBytes-eq2GB-and$c.Diagnostics.ComponentStore-match'not analyzed;.*superseded updates.*substantial.*little/none')'Disabled options must still receive usage measurements and an honest note about potential component-store savings.'
Assert-Cleanup($c.Stats.ShadowCopies.UsageNote.EndsWith("stored on $env:SystemDrive")-and($script:details-join"`n")-match'3,072.00 MiB allocated')'VSS must select the system volume, label its storage location and leave allocation details in verbose output.'
Assert-Cleanup($totals.EnabledBytes-eq0-and$totals.AdditionalDisabledBytes-eq0-and-not$c.Options.ShadowCopies-and$c.Errors-eq0)'Diagnostic usage is not reclaimable savings and must not enable cleanup.'
Assert-Cleanup($report-notmatch'\bHibernation:|ShadowStorage:'-and[regex]::Matches($report,'(?m)^ ComponentStore:').Count-eq1)'Hibernation and VSS must not produce separate diagnostic lines.'
Assert-Cleanup($report-match'DisableHibernation\s+-\s+4,096.00\s+file size; excluded from totals'-and$report-match'ShadowCopies\s+-\s+2,048.00\s+VSS used; excluded from totals; newest retained')'Measured usage must appear in the category MiB column with its meaning and exclusion from totals.'
Assert-Cleanup($report-notmatch'(DisableHibernation|ShadowCopies).*not estimated|GiB|MiB allocated')'Measured rows must not say not estimated or repeat allocation diagnostics.'
Assert-Cleanup($script:diagnosticProcesses-eq0-and$d.ComponentStore-eq$c.Diagnostics.ComponentStore)'Enabling component cleanup must not launch analysis or fabricate a measured recommendation in report mode.'
Assert-Cleanup($c.Stats.ShadowCopies.UsageBytes-eq2GB-and$c.Stats.ShadowCopies.UsageNote-match'stored on Z:')'Shadow storage on another drive must not be attributed to the system drive.'
Assert-Cleanup($c.Stats.DisableHibernation.UsageBytes-eq0-and$c.Stats.ShadowCopies.UsageBytes-eq0)'Absent hibernation or a missing shadow-storage association must measure zero bytes.'
$script:messages=@();Write-SystemCleanupReport$c
$report=$script:messages-join"`n"
Assert-Cleanup($report-match'DisableHibernation\s+-\s+0.00\s'-and$report-match'ShadowCopies\s+-\s+0.00\s'-and$report-notmatch'hiberfil.sys not present|no shadow-storage association|\bHibernation:|ShadowStorage:')'Absent usage must appear as zero MiB in the two rows without separate absence messages.'
Assert-Cleanup($null-eq$c.Stats.ShadowCopies.UsageBytes-and$c.Stats.ShadowCopies.UsageNote-match'unavailable')'Missing VSS measurements must not become zero.'
Assert-Cleanup($null-eq$c.Stats.DisableHibernation.UsageBytes-and$null-eq$c.Stats.ShadowCopies.UsageBytes-and$c.Stats.DisableHibernation.UsageNote-match'unavailable'-and$c.Stats.ShadowCopies.UsageNote-match'unavailable'-and$d.ComponentStore-match'not analyzed'-and$c.Errors-eq0)'Diagnostic failures must clear old measurements, preserve the component note and not fail reporting.'
$script:messages=@();Write-SystemCleanupReport$c
$report=$script:messages-join"`n"
Assert-Cleanup($report-match'DisableHibernation\s+-\s+-\s+unavailable'-and$report-match'ShadowCopies\s+-\s+-\s+unavailable')'Failed inspections must display unknown size rather than zero MiB.'
Assert-Cleanup($script:fileReads-eq0-and$script:storageReads-eq0-and$script:diagnosticProcesses-eq0-and$null-eq$c.Stats.DisableHibernation.UsageBytes-and$null-eq$c.Stats.ShadowCopies.UsageBytes-and$d.Count-eq1-and$d.ComponentStore-match'not analyzed')'Server reports must block hibernation/VSS assessment and only note potential component-store savings.'
$script:messages=@();Write-SystemCleanupReport$c
$report=$script:messages-join"`n"
Assert-Cleanup($report-match'DisableHibernation\s+-\s+-\s+not assessed'-and$report-match'ShadowCopies\s+-\s+-\s+not assessed')'Server-blocked rows must remain unassessed rather than showing zero usage.'
Assert-Cleanup($d.Count-eq0-and$script:fileReads-eq0-and$script:storageReads-eq0-and$script:diagnosticProcesses-eq0)'Apply mode must not run report diagnostics.'
Assert-Cleanup(-not$c.Stats.DownloadedExecutables.Enabled-and$c.Stats.DownloadedExecutables.CandidateFiles-eq2-and$c.Stats.DownloadedExecutables.CandidateBytes-eq6)'Disabled download estimate is missing or includes recent files.'
Assert-Cleanup($c.Stats.DuplicateDownloads.Enabled-and$c.Stats.DuplicateDownloads.CandidateFiles-eq1-and$c.Stats.DuplicateDownloads.CandidateBytes-eq3)'Disabled preview consumed an enabled category candidate.'
Assert-Cleanup(-not$c.Options.DownloadedExecutables-and(Test-Path-LiteralPath$potentialDuplicate))'Preview enabled a disabled option or deleted data.'
$totals=Get-SystemCleanupReportTotals$c
Assert-Cleanup($totals.EnabledBytes-eq3-and$totals.EnabledFiles-eq1-and$totals.AdditionalDisabledBytes-eq3-and$totals.AdditionalDisabledFiles-eq1)'Overlapping report totals double-counted a file.'
Assert-Cleanup($report.IndexOf('ENABLED -')-lt$report.IndexOf('DISABLED -')-and$report.IndexOf('DuplicateDownloads')-lt$report.IndexOf('DISABLED -')-and$report.IndexOf('DownloadedExecutables')-gt$report.IndexOf('DISABLED -'))'Enabled and disabled report groups are unclear or misplaced.'
Assert-Cleanup($report.IndexOf('Known savings:')-lt$report.IndexOf('ENABLED -')-and$report-match'Additional DISABLED'-and$report-match'<0.01')'Unique totals must appear first and small nonzero candidates must remain visible.'
Assert-Cleanup(-not$script:disabledRan-and(Test-Path-LiteralPath$potentialOriginal)-and(Test-Path-LiteralPath$potentialDuplicate)-and(Test-Path-LiteralPath$potentialRecent))'Apply mode ran a disabled category.'
Assert-Cleanup(-not(Test-Path-LiteralPath(Join-Path$recycle'S-1-5-21-fixture\$Rold'))-and-not(Test-Path-LiteralPath(Join-Path$recycle'S-1-5-21-fixture\$Iold')))'Old recycled pair not removed.'
foreach($exein@('powercfg.exe','schtasks.exe','esentutl.exe','vssadmin.exe','cleanmgr.exe')){$null=New-CleanupFixture('preview\Windows\System32\'+$exe)'inert, never executed'}
Assert-Cleanup($result.Count-eq22-and@($result|Where-ObjectStatus-ne'Preview').Count-eq0)"Full preview did not cover every category (count=$($result.Count))."
Assert-Cleanup(@($result|Where-Object{$_.Category-eq'DownloadedExecutables'-and-not$_.Enabled-and$_.CandidateFiles-eq1}).Count-eq1)'Default-disabled downloads were not assessed by the entry point.'
Assert-Cleanup(@($result|Where-Object{$_.Category-eq'ComponentStore'-and-not$_.Enabled-and$null-eq$_.CandidateBytes-and$_.EstimateStatus-eq'Not estimated'}).Count-eq1)'Unknown native savings must not be reported as zero.'
Assert-Cleanup(Test-Path-LiteralPath(Join-Path$env:SystemRoot'Temp\old.tmp'))'Full preview removed a fixture.'
Assert-Cleanup($script:compactReportBytes-lt4096)"22-category preview exceeded the 4 KiB regression budget ($script:compactReportBytes bytes)."
foreach($rowin$result){
Assert-Cleanup([regex]::Matches($compactOutput,('(?m)^\[SystemCleanup\] '+[regex]::Escape($row.Category)+'\s')).Count-eq1)"Compact report lost or repeated category $($row.Category)."
}
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'DisableHibernation\s+-\s+<0.01\s+file size'-and$compactOutput-match'ShadowCopies\s+-\s+0.00\s+VSS used'-and$compactOutput-notmatch'\bHibernation:|ShadowStorage:'-and$compactOutput-match'ComponentStore: not analyzed;.*superseded updates.*substantial.*little/none')'Full entry-point report must show usage in category rows and retain the component note even with cleanup disabled.'
Assert-Cleanup(@($result|Where-Object{$_.Category-eq'RevitJournals'-and$_.Status-eq'Skipped'}).Count-eq1)'An active application should skip cleanup, not fail the whole run.'
Assert-Cleanup(@($result|Where-Object{$_.Category-eq'DeliveryOptimization'-and-not$_.Enabled-and$_.Status-eq'Unavailable'-and$null-eq$_.CandidateBytes}).Count-eq1)'A missing optional feature must be reported as unavailable, not zero savings or a failed report.'
Assert-Cleanup($compactOutput-match'BrowserCache.*Partial; incomplete.*chrome.*msedge'-and$compactOutput-match'RevitJournals.*Skipped.*Revit is running'-and$compactOutput-match'DeliveryOptimization.*Unavailable.*not installed')'Compacting output must preserve partial, skipped and unavailable reasons.'
Assert-Cleanup($c.Errors-eq1-and$report-match'CrashDumps.*Failed; incomplete.*Synthetic failure.*see verbose'-and$report.Length-lt1600)'A long multiline error must retain its failure status without flooding the summary.'
Assert-Cleanup(($script:details-join"`n").Contains(('long diagnostic '*300)))'Compacted errors must remain complete in verbose diagnostics.'