diff --git a/Modules/Controller.ps1 b/Modules/Controller.ps1 index 4194e4f..70a75c9 100644 --- a/Modules/Controller.ps1 +++ b/Modules/Controller.ps1 @@ -1495,7 +1495,7 @@ function Invoke-MainLoop { & $CompleteLogRunHandler } else { $elapsedString = Format-ElapsedTime -Elapsed $runStopwatch.Elapsed - $summaryLines = Get-SummaryLines -Stats $stats -ElapsedString $elapsedString -ModeFlags $resolved + $summaryLines = Get-SummaryLines -Stats $stats -ElapsedString $elapsedString -ModeFlags $resolved -FullLogging $script:FullLogging Write-SummaryReport -Lines $summaryLines -FullLogging $script:FullLogging & $CompleteLogRunHandler } diff --git a/Modules/Summary.ps1 b/Modules/Summary.ps1 index f933084..1198d23 100644 --- a/Modules/Summary.ps1 +++ b/Modules/Summary.ps1 @@ -25,6 +25,29 @@ function Format-ElapsedTime { return ("{0:D2}h {1:D2}m {2:D2}s" -f [int][Math]::Floor($Elapsed.TotalHours), $Elapsed.Minutes, $Elapsed.Seconds) } +function Format-SummaryCount { + param( + [int]$Count, + [Parameter(Mandatory)] + [string]$Singular + ) + + $noun = $Singular + if ($Count -ne 1) { $noun += "s" } + return ($Count.ToString() + " " + $noun) +} + +function Format-SummaryRow { + param( + [Parameter(Mandatory)] + [string]$Label, + [Parameter(Mandatory)] + [string[]]$Parts + ) + + return ($Label.PadRight(10) + ": " + ($Parts -join ", ")) +} + function Get-SummaryLines { param( [Parameter(Mandatory)] @@ -32,44 +55,102 @@ function Get-SummaryLines { [Parameter(Mandatory)] [string]$ElapsedString, [Parameter(Mandatory)] - [hashtable]$ModeFlags + [hashtable]$ModeFlags, + [bool]$FullLogging = $false ) - $lines = New-Object System.Collections.Generic.List[string] - $lines.Add("=== SUMMARY ===") + $modeNames = @{ + FullPipeline = "Full Pipeline" + ImageConversion = "Image Conversion" + AnimatedConversion = "Animated Conversion" + ZipFolders = "Zip Folders" + } + + $rows = New-Object System.Collections.Generic.List[string] + $attention = New-Object System.Collections.Generic.List[string] if ($ModeFlags.RunImageConversion) { - $lines.Add("Loose Img Conv. : Success = " + $Stats.LooseImgConverted.Success + " | Failed = " + $Stats.LooseImgConverted.Fail + " | Skipped (animated) = " + $Stats.LooseImgConverted.Skipped) - $lines.Add("Loose Img Deleted : Success = " + $Stats.LooseImgDeleted.Success + " | Failed = " + $Stats.LooseImgDeleted.Fail) - $lines.Add("Loose Stale Cleaned : Success = " + $Stats.LooseStaleCleaned.Success + " | Failed = " + $Stats.LooseStaleCleaned.Fail) - $lines.Add("Covers Normalised : Success = " + $Stats.CoversNormalized.Success + " | Failed = " + $Stats.CoversNormalized.Fail) + $conv = $Stats.LooseImgConverted; $del = $Stats.LooseImgDeleted; $stale = $Stats.LooseStaleCleaned + $parts = New-Object System.Collections.Generic.List[string] + if ($conv.Success -gt 0) { $parts.Add([string]$conv.Success + " converted") } + if ($conv.Fail -gt 0) { $parts.Add([string]$conv.Fail + " FAILED") } + if ($conv.Skipped -gt 0) { $parts.Add([string]$conv.Skipped + " skipped") } + if ($del.Success -gt 0) { $parts.Add((Format-SummaryCount -Count $del.Success -Singular "original") + " deleted") } + if ($del.Fail -gt 0) { $parts.Add((Format-SummaryCount -Count $del.Fail -Singular "original") + " NOT deleted") } + if ($stale.Success -gt 0) { $parts.Add((Format-SummaryCount -Count $stale.Success -Singular "already-converted original") + " cleaned up") } + if ($stale.Fail -gt 0) { $parts.Add((Format-SummaryCount -Count $stale.Fail -Singular "already-converted original") + " NOT cleaned up") } + if ($parts.Count -gt 0) { $rows.Add((Format-SummaryRow -Label "Images" -Parts $parts.ToArray())) } + if ($conv.Fail -gt 0) { $attention.Add((Format-SummaryCount -Count $conv.Fail -Singular "image") + " failed") } + $notDeleted = [int]$del.Fail + [int]$stale.Fail + if ($notDeleted -gt 0) { $attention.Add((Format-SummaryCount -Count $notDeleted -Singular "original image") + " not deleted") } } if ($ModeFlags.RunAnimatedConversion) { - $lines.Add("Loose Anim. Conv. : Success = " + $Stats.LooseAnimatedConverted.Success + " | Failed = " + $Stats.LooseAnimatedConverted.Fail + " | Skipped (already converted) = " + $Stats.LooseAnimatedConverted.Skipped) - $lines.Add("Loose Anim. Deleted : Success = " + $Stats.LooseAnimatedDeleted.Success + " | Failed = " + $Stats.LooseAnimatedDeleted.Fail) + $conv = $Stats.LooseAnimatedConverted; $del = $Stats.LooseAnimatedDeleted + $parts = New-Object System.Collections.Generic.List[string] + if ($conv.Success -gt 0) { $parts.Add([string]$conv.Success + " converted") } + if ($conv.Fail -gt 0) { $parts.Add([string]$conv.Fail + " FAILED") } + if ($conv.Skipped -gt 0) { $parts.Add([string]$conv.Skipped + " skipped (already converted)") } + if ($del.Success -gt 0) { $parts.Add((Format-SummaryCount -Count $del.Success -Singular "original") + " deleted") } + if ($del.Fail -gt 0) { $parts.Add((Format-SummaryCount -Count $del.Fail -Singular "original") + " NOT deleted") } + if ($parts.Count -gt 0) { $rows.Add((Format-SummaryRow -Label "Animated" -Parts $parts.ToArray())) } + if ($conv.Fail -gt 0) { $attention.Add((Format-SummaryCount -Count $conv.Fail -Singular "animated file") + " failed") } + if ($del.Fail -gt 0) { $attention.Add((Format-SummaryCount -Count $del.Fail -Singular "animated original") + " not deleted") } } - if ($ModeFlags.RunImageConversion -or $ModeFlags.RunAnimatedConversion) { - $lines.Add("Inspect Failed : " + $Stats.InspectFailed.Count + " file(s) - could not determine if animated") + $zip = $null; $folders = $null; $skipLabel = "skipped" + if ($ModeFlags.RunZip) { $zip = $Stats.LooseFolderZipped; $folders = $Stats.LooseFolderDeleted } + elseif ($ModeFlags.RunZipOnly) { $zip = $Stats.ZipOnlyZipped; $folders = $Stats.ZipOnlyFolderDeleted; $skipLabel = "skipped (animated originals kept)" } + if ($null -ne $zip) { + $parts = New-Object System.Collections.Generic.List[string] + if ($zip.Success -gt 0) { $parts.Add([string]$zip.Success + " zipped") } + if ($zip.Fail -gt 0) { $parts.Add([string]$zip.Fail + " FAILED") } + if ($zip.Skipped -gt 0) { $parts.Add([string]$zip.Skipped + " " + $skipLabel) } + if ($folders.Success -gt 0) { $parts.Add((Format-SummaryCount -Count $folders.Success -Singular "folder") + " deleted") } + if ($folders.Fail -gt 0) { $parts.Add((Format-SummaryCount -Count $folders.Fail -Singular "folder") + " NOT deleted") } + if ($parts.Count -gt 0) { $rows.Add((Format-SummaryRow -Label "Chapters" -Parts $parts.ToArray())) } + $notZipped = [int]$zip.Fail + [int]$zip.Skipped + if ($notZipped -gt 0) { $attention.Add((Format-SummaryCount -Count $notZipped -Singular "chapter") + " not zipped") } + if ($folders.Fail -gt 0) { $attention.Add((Format-SummaryCount -Count $folders.Fail -Singular "chapter folder") + " not deleted") } } - if ($ModeFlags.RunZip) { - $lines.Add("Loose Folders Zipped: Success = " + $Stats.LooseFolderZipped.Success + " | Failed = " + $Stats.LooseFolderZipped.Fail + " | Skipped (unconverted images) = " + $Stats.LooseFolderZipped.Skipped) - $lines.Add("Loose Fldr Deleted : Success = " + $Stats.LooseFolderDeleted.Success + " | Failed = " + $Stats.LooseFolderDeleted.Fail) + if ($ModeFlags.RunImageConversion) { + $covers = $Stats.CoversNormalized + $parts = New-Object System.Collections.Generic.List[string] + if ($covers.Success -gt 0) { $parts.Add([string]$covers.Success + " normalized") } + if ($covers.Fail -gt 0) { $parts.Add([string]$covers.Fail + " FAILED") } + if ($parts.Count -gt 0) { $rows.Add((Format-SummaryRow -Label "Covers" -Parts $parts.ToArray())) } + if ($covers.Fail -gt 0) { $attention.Add((Format-SummaryCount -Count $covers.Fail -Singular "cover") + " failed") } } - if ($ModeFlags.RunZipOnly) { - $lines.Add("Zip-only Zipped : Success = " + $Stats.ZipOnlyZipped.Success + " | Failed = " + $Stats.ZipOnlyZipped.Fail + " | Skipped (animated originals kept) = " + $Stats.ZipOnlyZipped.Skipped) - $lines.Add("Zip-only Fldr Del. : Success = " + $Stats.ZipOnlyFolderDeleted.Success + " | Failed = " + $Stats.ZipOnlyFolderDeleted.Fail) + # Kept apart from the Fail counts: these files were never tried. + if (($ModeFlags.RunImageConversion -or $ModeFlags.RunAnimatedConversion) -and $Stats.InspectFailed.Count -gt 0) { + $left = (Format-SummaryCount -Count $Stats.InspectFailed.Count -Singular "file") + $rows.Add((Format-SummaryRow -Label "Left alone" -Parts @($left + " (name clash, or couldn't tell if animated)"))) + $attention.Add($left + " left alone") } - $lines.Add("Time taken : " + $ElapsedString) + $modeName = [string]$modeNames[[string]$ModeFlags['Mode']] + $title = "SUMMARY" + if ($modeName) { $title += ": " + $modeName } + if ($Stats.Aborted) { $title += " - ABORTED" } + + $lines = New-Object System.Collections.Generic.List[string] + $lines.Add("=== " + $title + " (" + $ElapsedString + ") ===") + foreach ($row in $rows) { $lines.Add($row) } if ($Stats.Aborted) { - $lines.Add("Run was ABORTED : " + $Stats.ForceKilledCount + " job(s) force-stopped after the drain timeout") - $lines.Add("=== Processing ABORTED ===") - } else { - $lines.Add("=== Processing complete ===") + $abortText = "stopped before finishing" + if ($Stats.ForceKilledCount -gt 0) { $abortText += ", " + (Format-SummaryCount -Count $Stats.ForceKilledCount -Singular "job") + " force-stopped after the drain timeout" } + $lines.Add((Format-SummaryRow -Label "Aborted" -Parts @($abortText))) + } elseif ($rows.Count -eq 0) { + $lines.Add("Nothing was converted or zipped.") + } + if ($attention.Count -gt 0) { + $logText = " - see the Errors/Warnings logs" + if ($FullLogging) { $logText = " - see the log" } + # A chapter skipped only because the run was aborted isn't in the logs. + if ($Stats.Aborted -and $attention.Count -eq 1 -and $null -ne $zip -and $zip.Skipped -gt 0 -and [int]$zip.Fail -eq 0) { $logText = "" } + $lines.Add((Format-SummaryRow -Label "Attention" -Parts $attention.ToArray()) + $logText) } return ,$lines.ToArray() @@ -84,16 +165,12 @@ function Write-SummaryReport { ) Write-Host "" - # -ForceConsole: without it, Write-Log - # (Logging.ps1) suppresses console output whenever Clean Console - # Output is enabled, and nothing turns CCR off before this runs - # (Controller.ps1 sets it once per loop iteration, the summary is - # written inside that same iteration) - so with CCO on, the user would see - # a single blank line and no summary at all: no counts, no elapsed - # time, no "=== Processing ABORTED ===" footer. Every other - # end-of-operation Summary-level Write-Log call in this codebase - # (UnwantedImage.ps1's four) passes -ForceConsole too. - foreach ($line in $Lines) { Write-Log $line -Level Summary -ForceConsole } + # Not Write-Log: the console gets the lines without a timestamp and level, + # even with Clean Console Output on. The full log still gets both. + foreach ($line in $Lines) { + Write-LogLineToRunFiles -Line (Format-LogLine -Message $line -Level "Summary") -Level "Summary" + Write-LogLineToHost -Line $line -Level "Summary" + } if (-not $FullLogging) { Write-SummaryLog -Lines $Lines diff --git a/_Harness/Summary.Harness.ps1 b/_Harness/Summary.Harness.ps1 index 5abb2ce..d46feb7 100644 --- a/_Harness/Summary.Harness.ps1 +++ b/_Harness/Summary.Harness.ps1 @@ -3,9 +3,9 @@ Standalone test harness for Modules\Summary.ps1. .DESCRIPTION - Dot-sources the real module and stubs Write-Log/Write-SummaryLog with - call tracking (same pattern as AbortPipeline.Harness.ps1) - this - module doesn't need the real Logging.ps1 built/working to be tested. + Dot-sources the real module and stubs the Logging.ps1 functions it calls + with call tracking (same pattern as AbortPipeline.Harness.ps1). Only the + last scenario loads the real Logging.ps1. .EXAMPLE .\_Harness\Summary.Harness.ps1 @@ -27,6 +27,24 @@ function Write-Log { $script:writeLogCalls.Add(@{ Message = $Message; Level = $Level; ForceConsole = [bool]$ForceConsole }) } +$script:hostLines = New-Object System.Collections.Generic.List[string] +$script:runFileLines = New-Object System.Collections.Generic.List[string] +$script:lineLevels = New-Object System.Collections.Generic.List[string] +function Format-LogLine { + param([string]$Message, [string]$Level) + return "STAMP [" + $Level + "] " + $Message +} +function Write-LogLineToHost { + param([string]$Line, [string]$Level) + $script:hostLines.Add($Line) + $script:lineLevels.Add($Level) +} +function Write-LogLineToRunFiles { + param([string]$Line, [string]$Level) + $script:runFileLines.Add($Line) + $script:lineLevels.Add($Level) +} + $script:summaryLogCalls = New-Object System.Collections.Generic.List[string[]] function Write-SummaryLog { param([string[]]$Lines) @@ -80,127 +98,189 @@ Test-Assert ((Format-ElapsedTime -Elapsed ([timespan]::new(0, 59, 59) + [timespa Write-Host "" # ============================================================================== -# Scenario D: Get-SummaryLines - Full Pipeline's flags (image + animated -# + zip) show exactly those three groups, nothing else +# Scenario C: Format-SummaryCount +# ============================================================================== +Write-Host "=== Scenario C: Format-SummaryCount ===" -ForegroundColor Cyan +Test-Assert ((Format-SummaryCount -Count 1 -Singular "folder") -eq "1 folder") "one gets the singular" +Test-Assert ((Format-SummaryCount -Count 0 -Singular "folder") -eq "0 folders") "zero gets the plural" +Test-Assert ((Format-SummaryCount -Count 24 -Singular "folder") -eq "24 folders") "more than one gets the plural" +Write-Host "" + +function Get-FullPipelineFlags { + return @{ Mode = 'FullPipeline'; RunImageConversion = $true; RunAnimatedConversion = $true; RunZip = $true; RunZipOnly = $false } +} + +# ============================================================================== +# Scenario D: a clean Full Pipeline run - one row per thing that happened, +# nothing at zero, no Attention row # ============================================================================== -Write-Host "=== Scenario D: Get-SummaryLines, Full Pipeline flags ===" -ForegroundColor Cyan +Write-Host "=== Scenario D: clean Full Pipeline run ===" -ForegroundColor Cyan $statsD = New-StatsTracker -$statsD.InspectFailed.Count = 3 -$flagsD = @{ RunImageConversion = $true; RunAnimatedConversion = $true; RunZip = $true; RunZipOnly = $false } -$blobD = (Get-SummaryLines -Stats $statsD -ElapsedString "00h 00m 05s" -ModeFlags $flagsD) -join "`n" -Test-Assert ($blobD -match "Loose Img Conv\.") "shows loose image conversion rows" -Test-Assert ($blobD -match "Loose Anim\. Conv\.") "shows loose animated conversion rows" -Test-Assert ($blobD -match "Loose Folders Zipped") "shows the zip-after-conversion rows" -Test-Assert ($blobD -notmatch "ZIPs Extracted") "does not show the (unrelated) ZIP-pipeline rows" -Test-Assert ($blobD -notmatch "Zip-only") "does not show the (unrelated) zip-only rows" -Test-Assert ($blobD -match "Inspect Failed\s*: 3 file\(s\)") "shows Inspect Failed with the real count" -$statsD.CoversNormalized.Success = 4; $statsD.CoversNormalized.Fail = 1 -$blobDc = (Get-SummaryLines -Stats $statsD -ElapsedString "00h 00m 05s" -ModeFlags $flagsD) -join "`n" -Test-Assert ($blobDc -match "Covers Normalised\s*: Success = 4 \| Failed = 1") "shows the Covers Normalised row with its counts" -$flagsDi = Get-AllModeFlagsFalse -$flagsDi.RunImageConversion = $true -Test-Assert (((Get-SummaryLines -Stats (New-StatsTracker) -ElapsedString "00h 00m 01s" -ModeFlags $flagsDi) -join "`n") -match "Covers Normalised\s*: Success = 0 \| Failed = 0") "Image Conversion alone shows the row too, even at zero, like the other rows" -$flagsDa = Get-AllModeFlagsFalse -$flagsDa.RunAnimatedConversion = $true -Test-Assert (((Get-SummaryLines -Stats (New-StatsTracker) -ElapsedString "00h 00m 01s" -ModeFlags $flagsDa) -join "`n") -notmatch "Covers Normalised") "Animated Conversion alone does not show it - covers are only normalised by the image modes" +$statsD.LooseImgConverted.Success = 463; $statsD.LooseImgDeleted.Success = 463 +$statsD.LooseAnimatedConverted.Success = 12; $statsD.LooseAnimatedDeleted.Success = 12 +$statsD.LooseFolderZipped.Success = 24; $statsD.LooseFolderDeleted.Success = 24 +$statsD.CoversNormalized.Success = 3 +$linesD = Get-SummaryLines -Stats $statsD -ElapsedString "00h 12m 41s" -ModeFlags (Get-FullPipelineFlags) +$expectedD = @( + "=== SUMMARY: Full Pipeline (00h 12m 41s) ===", + "Images : 463 converted, 463 originals deleted", + "Animated : 12 converted, 12 originals deleted", + "Chapters : 24 zipped, 24 folders deleted", + "Covers : 3 normalized" +) +Test-Assert (($linesD -join "`n") -eq ($expectedD -join "`n")) ("exactly the expected lines (got: " + ($linesD -join " / ") + ")") Write-Host "" # ============================================================================== -# Scenario D2: Inspect Failed shows when ONLY RunAnimatedConversion is set -# (not RunImageConversion) - proves the shared row is on an -or, not -and, -# since either engine can hit this failure mode independently. +# Scenario D2: everything that can go wrong shows up, both on its own row +# and in Attention # ============================================================================== -Write-Host "=== Scenario D2: Inspect Failed shows for RunAnimatedConversion alone ===" -ForegroundColor Cyan +Write-Host "=== Scenario D2: failures always show ===" -ForegroundColor Cyan $statsD2 = New-StatsTracker -$statsD2.InspectFailed.Count = 1 -$flagsD2 = Get-AllModeFlagsFalse -$flagsD2.RunAnimatedConversion = $true -$blobD2 = (Get-SummaryLines -Stats $statsD2 -ElapsedString "00h 00m 01s" -ModeFlags $flagsD2) -join "`n" -Test-Assert ($blobD2 -match "Inspect Failed\s*: 1 file\(s\)") "Inspect Failed shows with RunAnimatedConversion alone, RunImageConversion false" +$statsD2.LooseImgConverted.Fail = 2; $statsD2.LooseImgConverted.Skipped = 5 +$statsD2.LooseImgDeleted.Fail = 1; $statsD2.LooseStaleCleaned.Success = 4; $statsD2.LooseStaleCleaned.Fail = 1 +$statsD2.LooseAnimatedConverted.Fail = 1; $statsD2.LooseAnimatedConverted.Skipped = 6; $statsD2.LooseAnimatedDeleted.Fail = 2 +$statsD2.LooseFolderZipped.Fail = 1; $statsD2.LooseFolderZipped.Skipped = 3; $statsD2.LooseFolderDeleted.Fail = 1 +$statsD2.CoversNormalized.Fail = 1 +$statsD2.InspectFailed.Count = 7 +$linesD2 = Get-SummaryLines -Stats $statsD2 -ElapsedString "00h 00m 05s" -ModeFlags (Get-FullPipelineFlags) +$expectedD2 = @( + "=== SUMMARY: Full Pipeline (00h 00m 05s) ===", + "Images : 2 FAILED, 5 skipped, 1 original NOT deleted, 4 already-converted originals cleaned up, 1 already-converted original NOT cleaned up", + "Animated : 1 FAILED, 6 skipped (already converted), 2 originals NOT deleted", + "Chapters : 1 FAILED, 3 skipped, 1 folder NOT deleted", + "Covers : 1 FAILED", + "Left alone: 7 files (name clash, or couldn't tell if animated)", + "Attention : 2 images failed, 2 original images not deleted, 1 animated file failed, 2 animated originals not deleted, 4 chapters not zipped, 1 chapter folder not deleted, 1 cover failed, 7 files left alone - see the Errors/Warnings logs" +) +Test-Assert (($linesD2 -join "`n") -eq ($expectedD2 -join "`n")) ("exactly the expected lines (got: " + ($linesD2 -join " / ") + ")") +$linesD2Full = Get-SummaryLines -Stats $statsD2 -ElapsedString "00h 00m 05s" -ModeFlags (Get-FullPipelineFlags) -FullLogging $true +Test-Assert ($linesD2Full[-1] -like "Attention : *7 files left alone - see the log") "with full logging on, Attention points to the one log" + +$statsD2s = New-StatsTracker +$statsD2s.LooseImgConverted.Skipped = 5; $statsD2s.LooseAnimatedConverted.Skipped = 6 +Test-Assert (((Get-SummaryLines -Stats $statsD2s -ElapsedString "x" -ModeFlags (Get-FullPipelineFlags)) -join "`n") -notmatch "Attention") "files skipped on purpose aren't an Attention item" +Write-Host "" + +# ============================================================================== +# Scenario D3: a mode only reports on what it runs - a counter belonging to +# another mode never leaks into its summary +# ============================================================================== +Write-Host "=== Scenario D3: each mode shows only its own rows ===" -ForegroundColor Cyan +$statsD3 = New-StatsTracker +foreach ($key in @('LooseImgConverted', 'LooseImgDeleted', 'LooseStaleCleaned', 'CoversNormalized', 'LooseAnimatedConverted', 'LooseAnimatedDeleted', 'LooseFolderZipped', 'LooseFolderDeleted', 'ZipOnlyZipped', 'ZipOnlyFolderDeleted')) { + $statsD3[$key].Success = 1; $statsD3[$key].Fail = 1 +} +$statsD3.InspectFailed.Count = 1 +$rowLabelsD3 = { + param($Mode, $Flag) + $flags = Get-AllModeFlagsFalse + $flags['Mode'] = $Mode + $flags[$Flag] = $true + $lines = Get-SummaryLines -Stats $statsD3 -ElapsedString "x" -ModeFlags $flags + return (@($lines | Select-Object -Skip 1 | ForEach-Object { ($_ -split ':')[0].Trim() }) -join ",") +} +Test-Assert ((& $rowLabelsD3 'ImageConversion' 'RunImageConversion') -eq "Images,Covers,Left alone,Attention") "Image Conversion: images, covers, left-alone files" +Test-Assert ((& $rowLabelsD3 'AnimatedConversion' 'RunAnimatedConversion') -eq "Animated,Left alone,Attention") "Animated Conversion: animated, left-alone files" +Test-Assert ((& $rowLabelsD3 'ZipFolders' 'RunZipOnly') -eq "Chapters,Attention") "Zip Folders: chapters only" Write-Host "" # ============================================================================== -# Scenario E: Get-SummaryLines - RunZipOnly shows only zip-only rows +# Scenario E: Zip Folders reads its own counters, and says why a chapter +# was skipped # ============================================================================== -Write-Host "=== Scenario E: Get-SummaryLines, RunZipOnly only ===" -ForegroundColor Cyan +Write-Host "=== Scenario E: Zip Folders ===" -ForegroundColor Cyan $statsE = New-StatsTracker +$statsE.ZipOnlyZipped.Success = 9; $statsE.ZipOnlyZipped.Skipped = 2; $statsE.ZipOnlyFolderDeleted.Success = 9 $flagsE = Get-AllModeFlagsFalse +$flagsE['Mode'] = 'ZipFolders' $flagsE.RunZipOnly = $true -$blobE = (Get-SummaryLines -Stats $statsE -ElapsedString "00h 00m 01s" -ModeFlags $flagsE) -join "`n" -Test-Assert ($blobE -match "Zip-only Zipped") "shows zip-only rows" -$statsE.ZipOnlyZipped.Skipped = 2 -$blobE2 = (Get-SummaryLines -Stats $statsE -ElapsedString "00h 00m 01s" -ModeFlags $flagsE) -join "`n" -Test-Assert ($blobE2 -match "Skipped \(animated originals kept\) = 2") "the zip-only row shows folders skipped for an .avif/.webp pair" -Test-Assert ($blobE -notmatch "Loose") "shows nothing from any loose-pipeline group" -Test-Assert ($blobE -notmatch "ZIPs Extracted") "shows nothing from the ZIP-pipeline group" -Test-Assert ($blobE -notmatch "Inspect Failed") "does not show Inspect Failed - RunZipOnly alone doesn't touch either conversion engine" +$linesE = Get-SummaryLines -Stats $statsE -ElapsedString "00h 00m 01s" -ModeFlags $flagsE +Test-Assert ($linesE[0] -eq "=== SUMMARY: Zip Folders (00h 00m 01s) ===") "the header names the mode" +Test-Assert ($linesE[1] -eq "Chapters : 9 zipped, 2 skipped (animated originals kept), 9 folders deleted") ("the Chapters row (got: " + $linesE[1] + ")") +Test-Assert ($linesE[2] -eq "Attention : 2 chapters not zipped - see the Errors/Warnings logs") ("the Attention row (got: " + $linesE[2] + ")") Write-Host "" # ============================================================================== -# Scenario F: Get-SummaryLines - every flag false still returns a valid -# (if minimal) report: header, elapsed time, footer, no group rows +# Scenario F: a run that did nothing says so - without claiming there was +# nothing to do, since a scan that failed outright also leaves every counter +# at zero # ============================================================================== -Write-Host "=== Scenario F: Get-SummaryLines, all flags false ===" -ForegroundColor Cyan -$statsF = New-StatsTracker -$linesF = Get-SummaryLines -Stats $statsF -ElapsedString "00h 00m 00s" -ModeFlags (Get-AllModeFlagsFalse) -Test-Assert ($linesF.Count -eq 3) "exactly 3 lines with no group rows: header, time taken, footer" -Test-Assert ($linesF -contains "=== SUMMARY ===") "still has the header" -Test-Assert (($linesF -join "`n") -match "Time taken\s+: 00h 00m 00s") "still reports elapsed time" -Test-Assert ($linesF -contains "=== Processing complete ===") "still has the footer" -Test-Assert (($linesF -join "`n") -notmatch "ABORTED") "a normal (non-aborted) run's footer says nothing about being aborted" +Write-Host "=== Scenario F: nothing done ===" -ForegroundColor Cyan +$linesF = Get-SummaryLines -Stats (New-StatsTracker) -ElapsedString "00h 00m 02s" -ModeFlags (Get-FullPipelineFlags) +Test-Assert (($linesF -join "`n") -eq "=== SUMMARY: Full Pipeline (00h 00m 02s) ===`nNothing was converted or zipped.") ("just the header and one line (got: " + ($linesF -join " / ") + ")") +$linesFn = Get-SummaryLines -Stats (New-StatsTracker) -ElapsedString "00h 00m 02s" -ModeFlags (Get-AllModeFlagsFalse) +Test-Assert ($linesFn[0] -eq "=== SUMMARY (00h 00m 02s) ===") "flags with no mode name still get a header" Write-Host "" # ============================================================================== -# Scenario F2: an aborted run's summary must not end with the same -# "Processing complete" footer as a normal finish - -# Stats.Aborted/Stats.ForceKilledCount (tracked by every pipeline with -# an abort loop, per Controller.ps1's own $totals) reach -# Get-SummaryLines' output. +# Scenario F2: an aborted run says so in the header and on its own row, with +# the force-stopped count when there is one # ============================================================================== -Write-Host "=== Scenario F2: Get-SummaryLines reflects an aborted run, not 'Processing complete' ===" -ForegroundColor Cyan +Write-Host "=== Scenario F2: aborted runs ===" -ForegroundColor Cyan $statsF2 = New-StatsTracker $statsF2.Aborted = $true +$linesF2 = Get-SummaryLines -Stats $statsF2 -ElapsedString "00h 00m 05s" -ModeFlags (Get-FullPipelineFlags) +Test-Assert (($linesF2 -join "`n") -eq "=== SUMMARY: Full Pipeline - ABORTED (00h 00m 05s) ===`nAborted : stopped before finishing") ("an abort with nothing done gets only the Aborted row (got: " + ($linesF2 -join " / ") + ")") $statsF2.ForceKilledCount = 3 -$linesF2 = Get-SummaryLines -Stats $statsF2 -ElapsedString "00h 00m 05s" -ModeFlags (Get-AllModeFlagsFalse) -$blobF2 = $linesF2 -join "`n" -Test-Assert ($linesF2 -notcontains "=== Processing complete ===") "the normal 'Processing complete' footer is NOT used for an aborted run" -Test-Assert ($linesF2 -contains "=== Processing ABORTED ===") "the footer clearly marks the run as aborted" -Test-Assert ($blobF2 -match "ABORTED") "the report visibly mentions the abort somewhere" -Test-Assert ($blobF2 -match "3") "the force-killed count (3) is reported" - -$statsF2b = New-StatsTracker -$linesF2b = Get-SummaryLines -Stats $statsF2b -ElapsedString "00h 00m 05s" -ModeFlags (Get-AllModeFlagsFalse) -Test-Assert ($linesF2b -contains "=== Processing complete ===") "sanity check: Stats.Aborted = `$false (the default) still gets the normal footer" +$statsF2.LooseImgConverted.Success = 8 +$linesF2b = Get-SummaryLines -Stats $statsF2 -ElapsedString "00h 00m 05s" -ModeFlags (Get-FullPipelineFlags) +Test-Assert ($linesF2b[1] -eq "Images : 8 converted") "what got done before the abort still shows" +$statsF2c = New-StatsTracker +$statsF2c.Aborted = $true +$statsF2c.LooseFolderZipped.Skipped = 2 +Test-Assert ((Get-SummaryLines -Stats $statsF2c -ElapsedString "x" -ModeFlags (Get-FullPipelineFlags))[-1] -eq "Attention : 2 chapters not zipped") "chapters skipped by the abort don't point to the logs, which don't mention them" +$statsF2c.LooseImgConverted.Fail = 1 +Test-Assert ((Get-SummaryLines -Stats $statsF2c -ElapsedString "x" -ModeFlags (Get-FullPipelineFlags))[-1] -like "* - see the Errors/Warnings logs") "a real failure on an aborted run still points to the logs" +Test-Assert ($linesF2b[2] -eq "Aborted : stopped before finishing, 3 jobs force-stopped after the drain timeout") ("the force-stopped count shows (got: " + $linesF2b[2] + ")") Write-Host "" # ============================================================================== -# Scenario G: Write-SummaryReport - full-logging mode does NOT call -# Write-SummaryLog (Write-Log already wrote every line to the one -# combined file in that mode); non-full-logging mode DOES +# Scenario G: Write-SummaryReport - the console gets the bare lines, the log +# files get a timestamp and level, and Write-SummaryLog is only called when +# full logging is off # ============================================================================== -Write-Host "=== Scenario G: Write-SummaryReport dispatch by -FullLogging ===" -ForegroundColor Cyan -$reportLinesG = @("=== SUMMARY ===", "Time taken : 00h 00m 01s", "=== Processing complete ===") +Write-Host "=== Scenario G: Write-SummaryReport ===" -ForegroundColor Cyan +$reportLinesG = @("=== SUMMARY: Zip Folders (00h 00m 01s) ===", "Nothing was converted or zipped.") -$script:writeLogCalls.Clear() -$script:summaryLogCalls.Clear() Write-SummaryReport -Lines $reportLinesG -FullLogging $true -Test-Assert ($script:writeLogCalls.Count -eq $reportLinesG.Count) "Write-Log was called once per line" -Test-Assert (@($script:writeLogCalls | Where-Object { $_.Level -ne 'Summary' }).Count -eq 0) "every Write-Log call used -Level Summary" +Test-Assert (($script:hostLines -join "`n") -eq ($reportLinesG -join "`n")) "the console gets each line exactly as given, with no timestamp or level" +Test-Assert (($script:runFileLines -join "`n") -eq (($reportLinesG | ForEach-Object { "STAMP [Summary] " + $_ }) -join "`n")) "each line goes to the run's log file with its timestamp and level" +Test-Assert (@($script:lineLevels | Where-Object { $_ -ne 'Summary' }).Count -eq 0) "every line is written at Summary level" +Test-Assert ($script:writeLogCalls.Count -eq 0) "Write-Log isn't used - it would hide the lines under Clean Console Output and add a prefix" Test-Assert ($script:summaryLogCalls.Count -eq 0) "Write-SummaryLog was NOT called in full-logging mode" -# Without -ForceConsole, Write-Log suppresses -# console output whenever Clean Console Output is enabled, and nothing -# turns CCO off before the summary is written - so the whole summary -# (every line) silently vanished from the console with CCO on. -Test-Assert (@($script:writeLogCalls | Where-Object { -not $_.ForceConsole }).Count -eq 0) "every Write-Log call passed -ForceConsole (so the summary still shows even with Clean Console Output enabled)" - -$script:writeLogCalls.Clear() -$script:summaryLogCalls.Clear() + +$script:hostLines.Clear() Write-SummaryReport -Lines $reportLinesG -FullLogging $false -Test-Assert ($script:writeLogCalls.Count -eq $reportLinesG.Count) "Write-Log was still called once per line in non-full-logging mode" +Test-Assert (($script:hostLines -join "`n") -eq ($reportLinesG -join "`n")) "the console gets the same lines in non-full-logging mode" Test-Assert ($script:summaryLogCalls.Count -eq 1) "Write-SummaryLog WAS called exactly once in non-full-logging mode" Test-Assert (($script:summaryLogCalls[0] -join "`n") -eq ($reportLinesG -join "`n")) "the exact same lines were passed through to Write-SummaryLog" Write-Host "" +# ============================================================================== +# Scenario H: against the real Logging.ps1 - Clean Console Output doesn't hide +# the summary, and the full log keeps the timestamp and level +# ============================================================================== +Write-Host "=== Scenario H: with the real Logging.ps1 ===" -ForegroundColor Cyan +$scratchH = Join-Path ([System.IO.Path]::GetTempPath()) ("summary-harness-" + [guid]::NewGuid().ToString("N")) +$null = New-Item -ItemType Directory -Path $scratchH +try { + . (Join-Path $PSScriptRoot "..\Modules\Logging.ps1") + Initialize-LogPaths -TargetPath "C:\SummaryH\Path" -FullLogging $true -ScriptDir $scratchH + Initialize-CleanConsoleOutput -Enabled $true + $consoleH = @(& { Write-SummaryReport -Lines $reportLinesG -FullLogging $true } 6>&1 | ForEach-Object { "$_" }) + Test-Assert (($consoleH -join "|") -eq ("|" + ($reportLinesG -join "|"))) ("a blank line, then the bare lines, with Clean Console Output on (got: " + ($consoleH -join "|") + ")") + $fileH = @(Get-Content -LiteralPath $script:logFile) + Test-Assert ($fileH.Count -eq 2 -and $fileH[1] -match '^\d{4}-\d\d-\d\d \d{6} \[Summary\] Nothing was converted or zipped.$') ("the full log has the timestamp and level (got: " + ($fileH -join " / ") + ")") + + Initialize-LogPaths -TargetPath "C:\SummaryH\Path" -FullLogging $false -ScriptDir $scratchH + $null = & { Write-SummaryReport -Lines $reportLinesG -FullLogging $false } 6>&1 + Test-Assert (((Get-Content -LiteralPath $script:logSummaryFile) -join "`n") -match "(?m)^Nothing was converted or zipped.$") "the Summary file gets the bare lines" +} finally { + Remove-Item -LiteralPath $scratchH -Recurse -Force -ErrorAction SilentlyContinue +} +Write-Host "" + # ============================================================================== # Summary # ============================================================================== diff --git a/module-descriptions.md b/module-descriptions.md index f576221..e038057 100644 --- a/module-descriptions.md +++ b/module-descriptions.md @@ -686,24 +686,28 @@ Builds the end-of-run summary. It covers three things: the counter hashtable tha |---|---| | `New-StatsTracker` | Returns a new hashtable with every counter at zero. Each call returns an independent copy. | | `Format-ElapsedTime -Elapsed ` | Returns the time as `HHh MMm SSs`, zero-padded. | -| `Get-SummaryLines -Stats -ElapsedString -ModeFlags ` | Returns the report as an array of strings. Only the sections for modes that ran are included. | -| `Write-SummaryReport -Lines -FullLogging ` | Prints each line at Summary level. When full logging is off, it also writes the same lines to the summary log file. | +| `Get-SummaryLines -Stats -ElapsedString -ModeFlags [-FullLogging ]` | Returns the report as an array of strings: a header, one row per kind of work that actually happened, and an Attention row if something needs a look. | +| `Write-SummaryReport -Lines -FullLogging ` | Prints the lines and writes them to the log. When full logging is off, it writes them to the summary log file instead. | +| Helpers | `Format-SummaryCount` ("1 folder" / "2 folders"), `Format-SummaryRow` (lines up the labels). | **How it works** - Counter keys: `LooseImgConverted`, `LooseImgDeleted`, `LooseStaleCleaned`, `CoversNormalized`, `LooseAnimatedConverted`, `LooseAnimatedDeleted`, `LooseFolderZipped`, `LooseFolderDeleted`, `ZipOnlyZipped` and `ZipOnlyFolderDeleted` each hold `Success` and `Fail` counts. The converted and zipped keys also hold `Skipped`. `InspectFailed` holds only `Count`. There are also top-level `Aborted` (boolean) and `ForceKilledCount` (integer) values. -- `Get-SummaryLines` adds sections based on the mode flags: `RunImageConversion` adds four rows (the fourth is "Covers Normalised", shown even at zero like the others), `RunAnimatedConversion` adds two, and `RunZip` and `RunZipOnly` add two each. The "Inspect Failed" row appears when either conversion flag is set. It is kept apart from the Fail counts because it means the file could not be checked for animation, not that a conversion failed. A missing flag counts as off. -- Every report then has a `Time taken` line. If `Stats.Aborted` is true, it adds a "Run was ABORTED" line with the `ForceKilledCount` value and ends with `=== Processing ABORTED ===`. Otherwise it ends with `=== Processing complete ===`. +- The header names the mode (from the flags' `Mode`) and the elapsed time, plus `ABORTED` when `Stats.Aborted` is true. +- Rows depend on the mode flags: `RunImageConversion` gives Images and Covers, `RunAnimatedConversion` gives Animated, `RunZip` or `RunZipOnly` gives Chapters, and either conversion flag gives "Left alone" (the `InspectFailed` count: a name clash, or a file that couldn't be checked for animation, so it was never tried). A missing flag counts as off. +- Counts at zero are left out, and a row with nothing in it isn't shown. Conversions and the deletes that follow them share a row. If no row is left, the report says "Nothing was converted or zipped." +- An aborted run gets an Aborted row, with the `ForceKilledCount` value when there is one. +- The Attention row repeats what failed, wasn't deleted, wasn't zipped or was left alone, and points to the Errors/Warnings logs (or the one log, with `-FullLogging`), unless the only item is chapters an abort left unzipped, which the logs don't mention. Files skipped on purpose aren't included. - `Get-SummaryLines` returns with `return ,` so the array reaches the caller intact. - `Format-ElapsedTime` uses the total hours, so a run longer than a day shows as `25h 05m 09s` rather than wrapping. -- `Write-SummaryReport` writes a blank line first. Each report line goes through `Write-Log -Level Summary -ForceConsole`, so the summary still appears when Clean Console Output is on. When `FullLogging` is false, the same lines are also written as one block by `Write-SummaryLog`. When it is true, that file write is skipped and the lines reach the log only through `Write-Log`. +- `Write-SummaryReport` writes a blank line first. Each report line goes to the console bare (no timestamp or level), even with Clean Console Output on, and to the full log, when that's on, with both. When `FullLogging` is false, the same lines are also written as one block by `Write-SummaryLog`. **Things to watch out for** -- `-ForceConsole` is required. Without it, `Write-Log` drops console output whenever Clean Console Output is on, and the summary would not show on screen at all. -- The module calls `Write-Log` and `Write-SummaryLog` from `Logging.ps1`. Dot-source that file before calling anything here. +- `Write-SummaryReport` doesn't use `Write-Log`, which would add a prefix and drop the lines from the console under Clean Console Output. +- The module calls `Format-LogLine`, `Write-LogLineToRunFiles`, `Write-LogLineToHost` and `Write-SummaryLog` from `Logging.ps1`. Dot-source that file before calling anything here. - `Get-SummaryLines` reads counters by these exact key names. A pipeline that writes a counter under a different name will have its results silently left out of the report. -- A missing counter field prints as a blank value rather than raising an error. +- A missing counter field counts as zero, so it's left out rather than raising an error. - `Get-SummaryLines` expects `ElapsedString` already formatted. It does not call `Format-ElapsedTime` itself. ---