diff --git a/src/Sleezer/Core/PostProcessing/CorruptionFailureHandler.cs b/src/Sleezer/Core/PostProcessing/CorruptionFailureHandler.cs index 829258ed..ba2d6a2b 100644 --- a/src/Sleezer/Core/PostProcessing/CorruptionFailureHandler.cs +++ b/src/Sleezer/Core/PostProcessing/CorruptionFailureHandler.cs @@ -8,12 +8,10 @@ namespace NzbDrone.Plugin.Sleezer.Core.PostProcessing; -public record CorruptionStrike(string File, string? Reason); - public interface ICorruptionFailureHandler { // Universal post-corruption workflow shared across every Sleezer download - // client: wipe the download folder, locate the grab history for this + // client: log each failed file, wipe the download folder, locate the grab history for this // download, and publish DownloadFailedEvent so Lidarr blocklists the // release and searches for a different one. Client-specific pre-steps // (e.g. slskd retry-budget zeroing, peer strike tracking) live in the @@ -57,6 +55,10 @@ public Task HandleAsync( if (strikes.Count == 0) return Task.CompletedTask; + // Every file, including those the message only counts: it points here. + foreach (CorruptionStrike strike in strikes) + _logger.Warn("[post-process] Corrupt file in download {DownloadId}: {Strike}", downloadId, CorruptionReport.Describe(strike)); + try { // 1. Wipe the whole download folder. One corrupt file poisons the @@ -131,7 +133,7 @@ public Task HandleAsync( SourceTitle = historyItem.SourceTitle, DownloadClient = downloadClientType, DownloadId = historyItem.DownloadId, - Message = BuildFailureMessage(strikes), + Message = CorruptionReport.FailureMessage(strikes), Data = historyItem.Data, TrackedDownload = tracked, SkipRedownload = false, @@ -149,12 +151,4 @@ public Task HandleAsync( return Task.CompletedTask; } - - private static string BuildFailureMessage(IReadOnlyList strikes) - { - string firstReason = strikes[0].Reason ?? "(unknown)"; - return strikes.Count == 1 - ? $"NzbDrone.Plugin.Sleezer: corrupt file detected — {firstReason}" - : $"NzbDrone.Plugin.Sleezer: {strikes.Count} corrupt files detected; first: {firstReason}"; - } } diff --git a/src/Sleezer/Core/PostProcessing/CorruptionReport.cs b/src/Sleezer/Core/PostProcessing/CorruptionReport.cs new file mode 100644 index 00000000..c3b428df --- /dev/null +++ b/src/Sleezer/Core/PostProcessing/CorruptionReport.cs @@ -0,0 +1,46 @@ +using System.Text; + +namespace NzbDrone.Plugin.Sleezer.Core.PostProcessing; + +public record CorruptionStrike(string File, string? Reason); + +/// Builds the failure message Lidarr shows in History and the Blocklist. +public static class CorruptionReport +{ + // Lidarr sends this text to notifications: Pushover's limit is 1,024 characters, Discord's 2,000. + public const int MaxMessageLength = 1000; + + /// Lists the failed files by name until the message is full, then counts the rest. + public static string FailureMessage(IReadOnlyList strikes) + { + List byName = [.. strikes.OrderBy(s => s.File, StringComparer.OrdinalIgnoreCase)]; + string count = byName.Count == 1 ? "1 file" : $"{byName.Count} files"; + StringBuilder message = new($"Sleezer: {count} failed the corruption check — "); + for (int i = 0; i < byName.Count; i++) + { + string entry = $"{(i > 0 ? "; " : "")}{Describe(byName[i])}"; + string rest = MoreSuffix(byName.Count - i - 1); + if (message.Length + entry.Length + rest.Length > MaxMessageLength) + { + if (i > 0) + return message.Append(MoreSuffix(byName.Count - i)).ToString(); + int cut = MaxMessageLength - message.Length - rest.Length - 1; + if (char.IsHighSurrogate(entry[cut - 1])) + cut--; + entry = entry[..cut] + "…"; + } + + message.Append(entry); + } + + return message.ToString(); + } + + /// One failed file and why, as the message and the log show it. + public static string Describe(CorruptionStrike strike) => $"{strike.File}: {strike.Reason ?? "no reason recorded"}"; + + /// The file's path inside the download folder, so a disc folder (CD1/, CD2/) stays in the name. + public static string FileLabel(string folder, string path) => Path.GetRelativePath(folder, path); + + private static string MoreSuffix(int left) => left > 0 ? $"; +{left} more (see the Lidarr log)" : string.Empty; +} diff --git a/src/Sleezer/Core/PostProcessing/CorruptionScanner.cs b/src/Sleezer/Core/PostProcessing/CorruptionScanner.cs index b4e86eb6..d24d737d 100644 --- a/src/Sleezer/Core/PostProcessing/CorruptionScanner.cs +++ b/src/Sleezer/Core/PostProcessing/CorruptionScanner.cs @@ -102,9 +102,8 @@ public async Task ScanAsync(string path, int timeoutSeconds, Cancellatio if (FfmpegErrorFormatter.IsAttachedPictureFailure(stderr)) return await ReverifyPastAttachedPictureAsync(path, timeoutSeconds, sw, ct); - string reason = FfmpegErrorFormatter.CleanFfmpegErrors(stderr); - _logger.Debug("Corruption scan {Path}: ffmpeg verdict corrupt (exit={ExitCode}) \u2014 {Reason}", path, exitCode, reason); - return new Result(true, reason); + _logger.Debug("Corruption scan {Path}: ffmpeg verdict corrupt (exit={ExitCode}) \u2014 {Stderr}", path, exitCode, FfmpegErrorFormatter.LogExcerpt(stderr)); + return DecodeFailure(stderr, exitCode); } // Defence in depth: ffmpeg sometimes exits 0 even with `-xerror -err_detect explode` @@ -120,9 +119,8 @@ public async Task ScanAsync(string path, int timeoutSeconds, Cancellatio string significant = FfmpegErrorFormatter.StripBenignMetadataNoise(stderr); if (!string.IsNullOrWhiteSpace(significant)) { - string reason = FfmpegErrorFormatter.CleanFfmpegErrors(significant); - _logger.Debug("Corruption scan {Path}: ffmpeg exited 0 but emitted error-level output \u2014 {Reason}", path, reason); - return new Result(true, reason); + _logger.Debug("Corruption scan {Path}: ffmpeg exited 0 but emitted error-level output \u2014 {Stderr}", path, FfmpegErrorFormatter.LogExcerpt(significant)); + return DecodeFailure(stderr, exitCode); } if (!string.IsNullOrWhiteSpace(stderr)) @@ -166,15 +164,17 @@ private async Task ReverifyPastAttachedPictureAsync(string path, int tim string significant = FfmpegErrorFormatter.StripBenignMetadataNoise(stderr); if (exitCode != 0 || !string.IsNullOrWhiteSpace(significant)) { - string reason = FfmpegErrorFormatter.CleanFfmpegErrors(string.IsNullOrWhiteSpace(significant) ? stderr : significant); - _logger.Debug("Corruption scan {Path}: corrupt beyond the attached picture (exit={ExitCode}) — {Reason}", path, exitCode, reason); - return new Result(true, reason); + _logger.Debug("Corruption scan {Path}: corrupt beyond the attached picture (exit={ExitCode}) — {Stderr}", path, exitCode, FfmpegErrorFormatter.LogExcerpt(stderr)); + return DecodeFailure(stderr, exitCode); } _logger.Info("Corruption scan {Path}: embedded cover art is malformed but the audio decoded clean in {ElapsedMs}ms — keeping the file", path, sw.ElapsedMilliseconds); return new Result(false, null); } + private static Result DecodeFailure(string stderr, int exitCode) => + new(true, FfmpegErrorFormatter.DecodeFailureReason(stderr, exitCode)); + /// /// Resolve the ffmpeg binary the scanner will invoke. Public so the post-import /// converter can reuse the exact same resolution + decode logic to verify its diff --git a/src/Sleezer/Core/PostProcessing/FfmpegErrorFormatter.cs b/src/Sleezer/Core/PostProcessing/FfmpegErrorFormatter.cs index 95cadf0b..9d593f4e 100644 --- a/src/Sleezer/Core/PostProcessing/FfmpegErrorFormatter.cs +++ b/src/Sleezer/Core/PostProcessing/FfmpegErrorFormatter.cs @@ -4,34 +4,102 @@ namespace NzbDrone.Plugin.Sleezer.Core.PostProcessing; /// /// Pure formatter for ffmpeg stderr output emitted during corruption scans. -/// Strips codec/address prefixes, deduplicates errors, and joins them with pipes +/// Strips codec/address prefixes and ffmpeg's follow-on lines, deduplicates errors, and joins them with pipes /// so the corrupt-file quarantine log stays readable instead of a 200-line dump. /// Kept separate from CorruptionScanner so it can be unit-tested without dragging /// in NLog, TagLib, and Xabe.FFmpeg. /// public static class FfmpegErrorFormatter { - private static readonly Regex CodecPrefixPattern = new(@"\[[\w:#/]+ @ 0x[0-9a-fA-F]+\]\s*", RegexOptions.Compiled); - private static readonly Regex PipeSeparatorPattern = new(@"\s*\|\s*", RegexOptions.Compiled); + private static readonly Regex CodecPrefixPattern = new(@"\[[^\]\s]+ @ 0x[0-9a-fA-F]+\]\s*", RegexOptions.Compiled); private static readonly Regex WhitespacePattern = new(@"\s+", RegexOptions.Compiled); + private const int LogExcerptLines = 20; + + // The muxer's complaint about packets a bad frame left behind; it follows the decoder's error. + private const string MuxerDtsMarker = "non monotonically increasing dts"; + + // Lines ffmpeg prints after the decoder's own error (n9.0.2; "Error while decoding stream" is 4.x-6.x). + private static readonly string[] FollowOnMarkers = + { + "decode_frame() failed", + "Decoding error:", + "Error submitting packet to decoder:", + "Error processing packet in decoder:", + "Task finished with error code:", + "Terminating thread with return code", + "Error while decoding stream", + MuxerDtsMarker, + }; + + // Image decoders: under -map 0:a they only ever read the embedded cover art. + private static readonly string[] CoverArtDecoderMarkers = + { + "mjpeg @ 0x", "png @ 0x", "bmp @ 0x", "gif @ 0x", "webp @ 0x", "tiff @ 0x", + }; + + /// The decoder's own errors from ffmpeg stderr, deduplicated and joined with " | "; one follow-on line if that is all there is. public static string CleanFfmpegErrors(string stderr) { - if (string.IsNullOrWhiteSpace(stderr)) + List errors = DistinctErrors(stderr); + if (errors.Count == 0) return "Non-zero exit code"; - string cleaned = CodecPrefixPattern.Replace(stderr, string.Empty); - cleaned = PipeSeparatorPattern.Replace(cleaned, " | "); - cleaned = WhitespacePattern.Replace(cleaned, " ").Trim(); + List specific = errors.FindAll(e => !IsFollowOn(e)); + if (specific.Count > 0) + return string.Join(" | ", specific); + + return errors.Find(e => !e.Contains(MuxerDtsMarker, System.StringComparison.Ordinal)) ?? errors[0]; + } + + /// The corruption reason for a file that failed ffmpeg's decode check. + public static string DecodeFailureReason(string stderr, int exitCode) + { + List lines = [.. (stderr ?? string.Empty).Split('\n').Where(l => !string.IsNullOrWhiteSpace(l))]; + List audio = lines.FindAll(l => !IsBenignMetadataNoise(l) && !ContainsAny(l, CoverArtDecoderMarkers)); + if (audio.Count > 0) + return $"{(exitCode == 0 ? "Audio has decode errors" : "Audio won't decode")} ({CleanFfmpegErrors(string.Join('\n', audio))})"; + + return lines.Count > 0 + ? $"Damaged tag or cover art ({CleanFfmpegErrors(string.Join('\n', lines))})" + : $"ffmpeg failed with exit code {exitCode}"; + } + + /// The first lines of ffmpeg stderr for the Debug log, with a count of the rest. + public static string LogExcerpt(string stderr) + { + string[] lines = stderr.Trim().Split('\n'); + if (lines.Length <= LogExcerptLines) + return string.Join('\n', lines); + + return string.Join('\n', lines.Take(LogExcerptLines)) + $"\n({lines.Length - LogExcerptLines} more lines)"; + } + + private static List DistinctErrors(string stderr) + { + List unique = new(); + if (string.IsNullOrWhiteSpace(stderr)) + return unique; - string[] parts = cleaned.Split(" | ", System.StringSplitOptions.RemoveEmptyEntries | System.StringSplitOptions.TrimEntries); HashSet seen = new(System.StringComparer.Ordinal); - List unique = new(parts.Length); - foreach (string p in parts) - if (seen.Add(p)) - unique.Add(p); + foreach (string line in CodecPrefixPattern.Replace(stderr, string.Empty).Split('\n')) + { + string error = WhitespacePattern.Replace(line, " ").Trim(); + if (error.Length > 0 && seen.Add(error)) + unique.Add(error); + } - return unique.Count > 0 ? string.Join(" | ", unique) : "Non-zero exit code"; + return unique; + } + + private static bool IsFollowOn(string error) => ContainsAny(error, FollowOnMarkers); + + private static bool ContainsAny(string line, string[] markers) + { + foreach (string marker in markers) + if (line.Contains(marker, System.StringComparison.Ordinal)) + return true; + return false; } /// @@ -69,13 +137,7 @@ public static bool IsAttachedPictureFailure(string stderr) => /// True if an ffmpeg stderr line is an ID3 tag-parse error the demuxer recovered /// from by skipping the tag - a metadata defect, not audio corruption. /// - public static bool IsBenignMetadataNoise(string line) - { - foreach (string marker in BenignMetadataMarkers) - if (line.Contains(marker, System.StringComparison.Ordinal)) - return true; - return false; - } + public static bool IsBenignMetadataNoise(string line) => ContainsAny(line, BenignMetadataMarkers); /// /// Return with benign ID3 tag-parse lines removed. If diff --git a/src/Sleezer/Core/PostProcessing/PostProcessRunner.cs b/src/Sleezer/Core/PostProcessing/PostProcessRunner.cs index f70bc35c..f1589214 100644 --- a/src/Sleezer/Core/PostProcessing/PostProcessRunner.cs +++ b/src/Sleezer/Core/PostProcessing/PostProcessRunner.cs @@ -93,7 +93,7 @@ public async Task RunAsync(PostProcessRequest request, CancellationToken c await EnsureFFmpegResolvedAsync(ct); var sw = Stopwatch.StartNew(); - List strikes = await ScanForCorruptAsync(request.Folder, request.Client.ToString(), ct); + List strikes = await ScanForCorruptAsync(request.Folder, ct); logger.Info("[post-process] {Client} item {ID}: scan completed in {ElapsedMs}ms — {StrikeCount} strike(s)", request.Client, request.DownloadId, sw.ElapsedMilliseconds, strikes.Count); @@ -153,7 +153,7 @@ await preImportTagger.TagCompletedDownloadAsync( logger.Info("[post-process] {Client} item {ID}: tagging completed in {ElapsedMs}ms", request.Client, request.DownloadId, tagSw.ElapsedMilliseconds); } - private async Task> ScanForCorruptAsync(string folder, string clientName, CancellationToken ct) + private async Task> ScanForCorruptAsync(string folder, CancellationToken ct) { List strikes = []; @@ -168,8 +168,7 @@ private async Task> ScanForCorruptAsync(string folder, st if (!result.IsCorrupt) continue; - logger.Warn("[post-process] {Client} corrupt file: {File} — {Reason}", clientName, Path.GetFileName(path), result.Reason); - strikes.Add(new CorruptionStrike(Path.GetFileName(path), result.Reason)); + strikes.Add(new CorruptionStrike(CorruptionReport.FileLabel(folder, path), result.Reason)); } return strikes; diff --git a/src/Sleezer/Download/Clients/Soulseek/SlskdCorruptionHandler.cs b/src/Sleezer/Download/Clients/Soulseek/SlskdCorruptionHandler.cs index 0ba2fd3f..5635f764 100644 --- a/src/Sleezer/Download/Clients/Soulseek/SlskdCorruptionHandler.cs +++ b/src/Sleezer/Download/Clients/Soulseek/SlskdCorruptionHandler.cs @@ -1,4 +1,3 @@ -using NLog; using NzbDrone.Core.Indexers; using NzbDrone.Plugin.Sleezer.Core.PostProcessing; using NzbDrone.Plugin.Sleezer.Download.Clients.Soulseek.Models; @@ -22,22 +21,19 @@ Task HandleCorruptDownloadAsync( // Slskd-internal strike shape: carries the SlskdFileState so the handler can // zero its retry budget. Projected to the shared CorruptionStrike before // delegating folder deletion / event publishing to ICorruptionFailureHandler. -public record SlskdCorruptionStrike(SlskdFileState? FileState, string? Reason); +public record SlskdCorruptionStrike(SlskdFileState? FileState, string File, string? Reason); public class SlskdCorruptionHandler : ISlskdCorruptionHandler { private readonly ICorruptionFailureHandler _failureHandler; private readonly ISlskdCorruptUserTracker _userTracker; - private readonly Logger _logger; public SlskdCorruptionHandler( ICorruptionFailureHandler failureHandler, - ISlskdCorruptUserTracker userTracker, - Logger logger) + ISlskdCorruptUserTracker userTracker) { _failureHandler = failureHandler; _userTracker = userTracker; - _logger = logger; } public void UseSettings(int definitionId, SlskdProviderSettings settings) => _userTracker.UseThreshold(definitionId, settings.BanUserAfterCorruptCount); @@ -57,11 +53,7 @@ public async Task HandleCorruptDownloadAsync( // so zeroing the budget prevents the re-enqueue path from firing even if a // later state transition comes through. foreach (SlskdCorruptionStrike strike in strikes) - { strike.FileState?.UpdateMaxRetryCount(0); - string fn = strike.FileState != null ? SlskdTextProcessor.GetFileNameFromFilename(strike.FileState.File.Filename) : "(unmapped)"; - _logger.Info("Corruption strike: {File} | reason: {Reason}", fn, strike.Reason ?? "(unknown)"); - } // 2. Record per-user strikes so repeat offenders can be auto-banned. if (!string.IsNullOrEmpty(item.Username) && settings.BanUserAfterCorruptCount > 0) @@ -71,11 +63,7 @@ public async Task HandleCorruptDownloadAsync( // 3. Delegate the universal steps (wipe folder, publish DownloadFailedEvent // so Lidarr blocklists and re-searches) to the shared handler. - CorruptionStrike[] shared = strikes - .Select(s => new CorruptionStrike( - File: s.FileState != null ? SlskdTextProcessor.GetFileNameFromFilename(s.FileState.File.Filename) : "(unmapped)", - Reason: s.Reason)) - .ToArray(); + CorruptionStrike[] shared = strikes.Select(s => new CorruptionStrike(s.File, s.Reason)).ToArray(); await _failureHandler.HandleAsync( downloadId: item.ID, diff --git a/src/Sleezer/Download/Clients/Soulseek/SlskdDownloadManager.cs b/src/Sleezer/Download/Clients/Soulseek/SlskdDownloadManager.cs index 63ff604c..653fca56 100644 --- a/src/Sleezer/Download/Clients/Soulseek/SlskdDownloadManager.cs +++ b/src/Sleezer/Download/Clients/Soulseek/SlskdDownloadManager.cs @@ -1735,7 +1735,7 @@ private async Task> ScanFolderForCorruptionAsync( if (fileState == null) _logger.Warn("[post-process] Corrupt file {Filename} not mapped to any tracked slskd transfer; counting as corruption but skipping retry short-circuit.", filename); - strikes.Add(new SlskdCorruptionStrike(fileState, result.Reason)); + strikes.Add(new SlskdCorruptionStrike(fileState, CorruptionReport.FileLabel(folderPath, path), result.Reason)); } watch.Stop(); diff --git a/tests/Sleezer.Tests/AlbumWideFailureTests.cs b/tests/Sleezer.Tests/AlbumWideFailureTests.cs index e9dcdea2..a2c32584 100644 --- a/tests/Sleezer.Tests/AlbumWideFailureTests.cs +++ b/tests/Sleezer.Tests/AlbumWideFailureTests.cs @@ -1,13 +1,12 @@ -using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; using NzbDrone.Core.Download; using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.History; using NzbDrone.Core.MediaFiles.Events; -using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Parser.Model; using NzbDrone.Plugin.Sleezer.Blocklisting; using NzbDrone.Plugin.Sleezer.Core.Qobuz; +using Sleezer.Tests.Fakes; using Xunit; namespace Sleezer.Tests; @@ -67,17 +66,6 @@ public void A_failure_with_no_reason_blocks_only_the_grabbed_tier() public void Unstreamable_tracks_rule_out_the_album_only_when_it_must_be_complete(int unstreamable, bool requireCompleteAlbum, bool albumWide) => Assert.Equal(albumWide, AlbumWideFailure.Covers(QobuzAlbumFailure.Reason(unstreamable, requireCompleteAlbum))); - private sealed class CapturingEvents : IEventAggregator - { - public List Failed { get; } = []; - - public void PublishEvent(TEvent @event) where TEvent : class, IEvent - { - if (@event is DownloadFailedEvent failed) - Failed.Add(failed); - } - } - private sealed class GrabHistory(EntityHistory grab) : IHistoryService { public List Find(string downloadId, EntityHistoryEventType eventType) => diff --git a/tests/Sleezer.Tests/CorruptionFailureHandlerTests.cs b/tests/Sleezer.Tests/CorruptionFailureHandlerTests.cs new file mode 100644 index 00000000..b71b1846 --- /dev/null +++ b/tests/Sleezer.Tests/CorruptionFailureHandlerTests.cs @@ -0,0 +1,34 @@ +using NLog; +using NLog.Targets; +using NzbDrone.Common.Disk; +using NzbDrone.Core.History; +using NzbDrone.Plugin.Sleezer.Core.PostProcessing; +using Sleezer.Tests.Fakes; +using Xunit; +using Fake = Sleezer.Tests.PreImportTaggerWholeDownloadTests.Fake; + +namespace Sleezer.Tests; + +public class CorruptionFailureHandlerTests +{ + [Fact] + public async Task Every_failed_file_is_logged_including_those_the_message_only_counts() + { + EntityHistory grab = new() { ArtistId = 1, AlbumId = 10, DownloadId = "dl-1", SourceTitle = "Artist - Album", Data = [] }; + IHistoryService history = Fake.Of((m, _) => + m.Name == nameof(IHistoryService.Find) ? new List { grab } : throw new NotSupportedException(m.Name)); + IDiskProvider disk = Fake.Of((m, _) => + m.Name == nameof(IDiskProvider.FolderExists) ? false : throw new NotSupportedException(m.Name)); + CapturingEvents events = new(); + (Logger logger, MemoryTarget logs) = TestLogs.Capturing("corruption"); + CorruptionStrike[] strikes = [.. Enumerable.Range(1, 30).Select(i => new CorruptionStrike($"{i:00} - Track Number {i:00}.flac", "Audio won't decode (invalid residual)"))]; + + await new CorruptionFailureHandler(history, events, disk, logger) + .HandleAsync("dl-1", "Artist - Album", "/downloads/Artist - Album", strikes, "SoulseekDownloadProtocol", CancellationToken.None); + + Assert.EndsWith("; +17 more (see the Lidarr log)", Assert.Single(events.Failed).Message); + List warned = logs.Logs.Where(l => l.StartsWith("Warn|[post-process] Corrupt file in download \"dl-1\": ", StringComparison.Ordinal)).ToList(); + Assert.Equal(30, warned.Count); + Assert.Contains("Warn|[post-process] Corrupt file in download \"dl-1\": \"30 - Track Number 30.flac: Audio won't decode (invalid residual)\"", warned); + } +} diff --git a/tests/Sleezer.Tests/CorruptionReportTests.cs b/tests/Sleezer.Tests/CorruptionReportTests.cs new file mode 100644 index 00000000..4757439e --- /dev/null +++ b/tests/Sleezer.Tests/CorruptionReportTests.cs @@ -0,0 +1,98 @@ +using NzbDrone.Plugin.Sleezer.Core.PostProcessing; +using Xunit; + +namespace Sleezer.Tests; + +public class CorruptionReportTests +{ + [Fact] + public void FailureMessage_names_the_one_failed_file_and_its_reason() + { + string message = CorruptionReport.FailureMessage([new("03 - Track Three.flac", "Audio won't decode (invalid residual)")]); + + Assert.Equal("Sleezer: 1 file failed the corruption check — 03 - Track Three.flac: Audio won't decode (invalid residual)", message); + } + + [Fact] + public void FailureMessage_lists_every_failed_file_sorted_by_name() + { + string message = CorruptionReport.FailureMessage( + [ + new("11 - Track Eleven.flac", "Decode timed out (>120s)"), + new("03 - Track Three.flac", "Audio won't decode (invalid residual)"), + new("07 - Track Seven.flac", "Audio won't decode (invalid sync code | invalid frame header)"), + ]); + + Assert.Equal( + "Sleezer: 3 files failed the corruption check — " + + "03 - Track Three.flac: Audio won't decode (invalid residual); " + + "07 - Track Seven.flac: Audio won't decode (invalid sync code | invalid frame header); " + + "11 - Track Eleven.flac: Decode timed out (>120s)", + message); + } + + [Fact] + public void FailureMessage_says_so_when_a_strike_has_no_reason() + { + string message = CorruptionReport.FailureMessage([new("01 - Track One.mp3", null)]); + + Assert.Equal("Sleezer: 1 file failed the corruption check — 01 - Track One.mp3: no reason recorded", message); + } + + [Fact] + public void FailureMessage_carries_the_decode_reason_for_real_ffmpeg_output() + { + string message = CorruptionReport.FailureMessage([new("03 - Track Three.flac", FfmpegErrorFormatter.DecodeFailureReason(FfmpegErrorFormatterTests.DamagedFlacStderr, 1))]); + + Assert.Equal("Sleezer: 1 file failed the corruption check — 03 - Track Three.flac: Audio won't decode (invalid subframe padding)", message); + } + + [Fact] + public void FailureMessage_stops_listing_at_1000_characters_and_counts_the_rest() + { + CorruptionStrike[] strikes = [.. Enumerable.Range(1, 30).Select(i => new CorruptionStrike($"{i:00} - Track Number {i:00}.flac", "Audio won't decode (invalid residual)"))]; + + string message = CorruptionReport.FailureMessage(strikes); + + Assert.StartsWith("Sleezer: 30 files failed the corruption check — 01 - Track Number 01.flac: Audio won't decode (invalid residual); 02 - ", message); + Assert.EndsWith("; 13 - Track Number 13.flac: Audio won't decode (invalid residual); +17 more (see the Lidarr log)", message); + Assert.True(message.Length <= 1000, $"{message.Length} characters"); + } + + [Fact] + public void FailureMessage_cuts_a_first_entry_too_long_to_fit() + { + string message = CorruptionReport.FailureMessage( + [ + new("01 - Track One.flac", "Audio won't decode (" + new string('x', 1200) + ")"), + new("02 - Track Two.flac", "Audio won't decode (invalid residual)"), + ]); + + Assert.StartsWith("Sleezer: 2 files failed the corruption check — 01 - Track One.flac: Audio won't decode (xxx", message); + Assert.EndsWith("xxx…; +1 more (see the Lidarr log)", message); + Assert.Equal(1000, message.Length); + } + + [Fact] + public void FailureMessage_never_cuts_between_the_halves_of_a_surrogate_pair() + { + // The first entry is cut to 922 characters ("01.flac: " is 9), so 912 x's put the emoji's high surrogate last. + string reason = new string('x', 912) + "\U0001F3B5" + new string('x', 1200); + + string message = CorruptionReport.FailureMessage([new("01.flac", reason), new("02.flac", "Audio won't decode (invalid residual)")]); + + // Text-free checks first: a failure text holding a lone surrogate drops the result from the run. + Assert.Equal(-1, Array.FindIndex(message.ToCharArray(), char.IsSurrogate)); + Assert.Equal(999, message.Length); + Assert.EndsWith("xxx…; +1 more (see the Lidarr log)", message); + } + + [Fact] + public void FileLabel_keeps_the_disc_folder_of_a_multi_disc_download() + { + string folder = Path.Combine(Path.GetTempPath(), "Artist - Album"); + + Assert.Equal(Path.Combine("CD2", "01 - Intro.flac"), CorruptionReport.FileLabel(folder, Path.Combine(folder, "CD2", "01 - Intro.flac"))); + Assert.Equal("03 - Track Three.flac", CorruptionReport.FileLabel(folder, Path.Combine(folder, "03 - Track Three.flac"))); + } +} diff --git a/tests/Sleezer.Tests/Fakes/LidarrServiceFakes.cs b/tests/Sleezer.Tests/Fakes/LidarrServiceFakes.cs index c3ff4795..14c809d1 100644 --- a/tests/Sleezer.Tests/Fakes/LidarrServiceFakes.cs +++ b/tests/Sleezer.Tests/Fakes/LidarrServiceFakes.cs @@ -1,7 +1,11 @@ +using NLog; +using NLog.Config; +using NLog.Targets; using NzbDrone.Common.Messaging; using NzbDrone.Core.Blocklisting; using NzbDrone.Core.CustomFormats; using NzbDrone.Core.Datastore; +using NzbDrone.Core.Download; using NzbDrone.Core.Download.History; using NzbDrone.Core.History; using NzbDrone.Core.MediaFiles; @@ -123,3 +127,26 @@ internal sealed class FakeFormatCalculator : ICustomFormatCalculationService public List ParseCustomFormat(EntityHistory history, Artist artist) => throw new NotSupportedException(); public List ParseCustomFormat(LocalTrack localTrack) => throw new NotSupportedException(); } + +internal sealed class CapturingEvents : IEventAggregator +{ + public List Failed { get; } = []; + + public void PublishEvent(TEvent @event) where TEvent : class, IEvent + { + if (@event is DownloadFailedEvent failed) + Failed.Add(failed); + } +} + +internal static class TestLogs +{ + /// A real NLog logger whose lines land in the returned target as "Level|message". + public static (Logger Logger, MemoryTarget Logs) Capturing(string name) + { + MemoryTarget logs = new() { Layout = "${level}|${message}" }; + LoggingConfiguration config = new(); + config.AddRuleForAllLevels(logs); + return (new LogFactory { Configuration = config }.GetLogger(name), logs); + } +} diff --git a/tests/Sleezer.Tests/FfmpegErrorFormatterTests.cs b/tests/Sleezer.Tests/FfmpegErrorFormatterTests.cs index d70af391..affff49c 100644 --- a/tests/Sleezer.Tests/FfmpegErrorFormatterTests.cs +++ b/tests/Sleezer.Tests/FfmpegErrorFormatterTests.cs @@ -24,25 +24,201 @@ public void CleanFfmpegErrors_strips_codec_address_prefixes() } [Fact] - public void CleanFfmpegErrors_deduplicates_pipe_separated_repeats() + public void CleanFfmpegErrors_deduplicates_repeated_lines() { - // The formatter splits on " | " and dedups — mirrors ffmpeg's own repeat-error format - // "X | X | X" that shows up when a single frame fails and -xerror hasn't fired yet. - string stderr = "[mp3 @ 0x1] Same error | [mp3 @ 0x2] Same error | [mp3 @ 0x3] Same error"; - string result = FfmpegErrorFormatter.CleanFfmpegErrors(stderr); - int occurrences = result.Split("Same error").Length - 1; - Assert.Equal(1, occurrences); + string stderr = "[mp3 @ 0x1] Same error\n[mp3 @ 0x2] Same error\n[mp3 @ 0x3] Same error\n"; + Assert.Equal("Same error", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Theory] + [InlineData("[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5581f2a3c840] stream 0, offset 0x2c4f1: partial file\n", "stream 0, offset 0x2c4f1: partial file")] + [InlineData("[in#0/mov,mp4,m4a,3gp,3g2,mj2 @ 0x5581f2a3c840] Error during demuxing: Invalid data found when processing input\n", "Error during demuxing: Invalid data found when processing input")] + public void CleanFfmpegErrors_strips_the_m4a_demuxers_comma_separated_prefix(string stderr, string expected) + { + Assert.Equal(expected, FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); } [Fact] - public void CleanFfmpegErrors_preserves_newline_separated_distinct_errors() + public void CleanFfmpegErrors_keeps_a_pipe_inside_an_error_line() + { + string stderr = "[in#0 @ 0x1] Error opening input file /music/Artist|Name - Track.flac.\n"; + Assert.Equal("Error opening input file /music/Artist|Name - Track.flac.", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void CleanFfmpegErrors_joins_newline_separated_distinct_errors_with_pipes() { - // Newline-separated stderr collapses to one whitespace-joined string — documents - // the current behavior so future refactors don't silently change it. string stderr = "[a @ 0x1] First\n[b @ 0x2] Second"; - string result = FfmpegErrorFormatter.CleanFfmpegErrors(stderr); - Assert.Contains("First", result); - Assert.Contains("Second", result); + Assert.Equal("First | Second", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + // Captured from ffmpeg n9.0.2 (Lidarr's /config/ffmpeg) on files with 3 KB of random bytes mid-stream. + internal const string DamagedFlacStderr = + "[flac @ 0x1460e3582c40] invalid subframe padding\n" + + "[flac @ 0x1460e3582c40] decode_frame() failed\n" + + "[aist#0:0/flac @ 0x1460e357fe40] [dec:flac @ 0x1460e3589500] Decoding error: Invalid data found when processing input\n" + + "[aist#0:0/flac @ 0x1460e357fe40] [dec:flac @ 0x1460e3589500] Error processing packet in decoder: Invalid data found when processing input\n" + + "[aist#0:0/flac @ 0x1460e357fe40] [dec:flac @ 0x1460e3589500] Task finished with error code: -1094995529 (Invalid data found when processing input)\n" + + "[aist#0:0/flac @ 0x1460e357fe40] [dec:flac @ 0x1460e3589500] Terminating thread with return code -1094995529 (Invalid data found when processing input)\n"; + + private const string DamagedMp3Stderr = + "[mp3float @ 0x152c41c64440] Header missing\n" + + "[aist#0:0/mp3 @ 0x152c41c41840] [dec:mp3float @ 0x152c41c66500] Error submitting packet to decoder: Invalid data found when processing input\n" + + "[aist#0:0/mp3 @ 0x152c41c41840] [dec:mp3float @ 0x152c41c66500] Error processing packet in decoder: Invalid data found when processing input\n" + + "[aist#0:0/mp3 @ 0x152c41c41840] [dec:mp3float @ 0x152c41c66500] Task finished with error code: -1094995529 (Invalid data found when processing input)\n" + + "[aist#0:0/mp3 @ 0x152c41c41840] [dec:mp3float @ 0x152c41c66500] Terminating thread with return code -1094995529 (Invalid data found when processing input)\n"; + + [Fact] + public void CleanFfmpegErrors_keeps_only_the_decoder_error_from_a_damaged_flac() + { + Assert.Equal("invalid subframe padding", FfmpegErrorFormatter.CleanFfmpegErrors(DamagedFlacStderr)); + } + + [Fact] + public void CleanFfmpegErrors_keeps_only_the_decoder_error_from_a_damaged_mp3() + { + Assert.Equal("Header missing", FfmpegErrorFormatter.CleanFfmpegErrors(DamagedMp3Stderr)); + } + + [Fact] + public void CleanFfmpegErrors_keeps_each_distinct_decoder_error_once_in_order() + { + // Several bad frames before -xerror stopped, as logged for real FLACs, plus the muxer's dts complaint. + string stderr = + "[flac @ 0x1] invalid residual\n" + + "[flac @ 0x1] decode_frame() failed\n" + + "[flac @ 0x1] invalid sync code\n" + + "[flac @ 0x1] invalid frame header\n" + + "[flac @ 0x1] decode_frame() failed\n" + + "[flac @ 0x1] invalid sync code\n" + + "[flac @ 0x1] invalid frame header\n" + + "[aist#0:0/flac @ 0x2] [dec:flac @ 0x3] Decoding error: Invalid data found when processing input\n" + + "[out#0/null @ 0x4] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 5967360 >= 3720816\n" + + "[aist#0:0/flac @ 0x2] [dec:flac @ 0x3] Task finished with error code: -1094995529 (Invalid data found when processing input)\n"; + + Assert.Equal("invalid residual | invalid sync code | invalid frame header", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void CleanFfmpegErrors_falls_back_to_the_first_line_when_ffmpeg_printed_only_follow_on_lines() + { + string stderr = + "[aist#0:0/mp3 @ 0x1] [dec:mp3float @ 0x2] Error submitting packet to decoder: Invalid data found when processing input\n" + + "[aist#0:0/mp3 @ 0x1] [dec:mp3float @ 0x2] Error processing packet in decoder: Invalid data found when processing input\n" + + "[aist#0:0/mp3 @ 0x1] [dec:mp3float @ 0x2] Terminating thread with return code -1094995529 (Invalid data found when processing input)\n"; + + Assert.Equal("Error submitting packet to decoder: Invalid data found when processing input", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void CleanFfmpegErrors_drops_old_ffmpegs_error_while_decoding_line() + { + // ffmpeg 4.x-6.x prints this unprefixed line after the decoder's own error. + string stderr = + "[mp3float @ 0x1] Header missing\n" + + "Error while decoding stream #0:0: Invalid data found when processing input\n"; + + Assert.Equal("Header missing", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void CleanFfmpegErrors_falls_back_past_the_muxers_dts_line() + { + string stderr = + "[out#0/null @ 0x4] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 5967360 >= 3720816\n" + + "[aist#0:0/flac @ 0x2] [dec:flac @ 0x3] Decoding error: Invalid data found when processing input\n"; + + Assert.Equal("Decoding error: Invalid data found when processing input", FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void CleanFfmpegErrors_keeps_a_decoder_error_that_quotes_ffmpegs_generic_text() + { + string stderr = + "[flac @ 0x1] invalid sync code\n" + + "[mjpeg @ 0x2] unable to decode APP fields: Invalid data found when processing input\n"; + + Assert.Equal( + "invalid sync code | unable to decode APP fields: Invalid data found when processing input", + FfmpegErrorFormatter.CleanFfmpegErrors(stderr)); + } + + [Fact] + public void DecodeFailureReason_puts_plain_words_before_the_decoder_error() + { + Assert.Equal("Audio won't decode (invalid subframe padding)", FfmpegErrorFormatter.DecodeFailureReason(DamagedFlacStderr, 1)); + } + + [Fact] + public void DecodeFailureReason_leaves_tag_noise_out_of_a_real_decode_error() + { + string stderr = "Incorrect BOM value\n[mp3float @ 0x1] Header missing\nError reading comment frame, skipped\n"; + + Assert.Equal("Audio won't decode (Header missing)", FfmpegErrorFormatter.DecodeFailureReason(stderr, 1)); + } + + [Fact] + public void DecodeFailureReason_says_the_audio_has_errors_when_ffmpeg_decoded_to_the_end() + { + Assert.Equal( + "Audio has decode errors (overread, skip -5 enddists for 3)", + FfmpegErrorFormatter.DecodeFailureReason("[mp3float @ 0x1] overread, skip -5 enddists for 3\n", 0)); + } + + [Fact] + public void DecodeFailureReason_names_a_damaged_tag_or_cover_art_when_that_is_all_ffmpeg_printed() + { + string stderr = "[flac @ 0x1] Could not read mimetype from an attached picture\n"; + + Assert.Equal( + "Damaged tag or cover art (Could not read mimetype from an attached picture)", + FfmpegErrorFormatter.DecodeFailureReason(stderr, 1)); + } + + [Fact] + public void DecodeFailureReason_names_a_cover_art_jpeg_error_as_cover_art() + { + string stderr = "[mjpeg @ 0x2] unable to decode APP fields: Invalid data found when processing input\n"; + + Assert.Equal( + "Damaged tag or cover art (unable to decode APP fields: Invalid data found when processing input)", + FfmpegErrorFormatter.DecodeFailureReason(stderr, 1)); + } + + [Fact] + public void DecodeFailureReason_keeps_the_audio_decoders_line_beside_a_cover_art_error() + { + string stderr = + "[mjpeg @ 0x2] unable to decode APP fields: Invalid data found when processing input\n" + + "[aist#0:0/mp3 @ 0x1] [dec:mp3float @ 0x3] Error submitting packet to decoder: Invalid data found when processing input\n"; + + Assert.Equal( + "Audio won't decode (Error submitting packet to decoder: Invalid data found when processing input)", + FfmpegErrorFormatter.DecodeFailureReason(stderr, 1)); + } + + [Fact] + public void DecodeFailureReason_gives_the_exit_code_when_ffmpeg_printed_nothing() + { + Assert.Equal("ffmpeg failed with exit code 69", FfmpegErrorFormatter.DecodeFailureReason("", 69)); + } + + [Fact] + public void LogExcerpt_keeps_the_first_20_lines_and_counts_the_rest() + { + string stderr = string.Concat(Enumerable.Range(1, 25).Select(i => $"[flac @ 0x1] line {i}\n")); + + string excerpt = FfmpegErrorFormatter.LogExcerpt(stderr); + + Assert.StartsWith("[flac @ 0x1] line 1\n[flac @ 0x1] line 2\n", excerpt); + Assert.EndsWith("[flac @ 0x1] line 20\n(5 more lines)", excerpt); + Assert.DoesNotContain("line 21", excerpt); + } + + [Fact] + public void LogExcerpt_keeps_short_stderr_whole() + { + Assert.Equal("[flac @ 0x1] a\n[flac @ 0x1] b", FfmpegErrorFormatter.LogExcerpt("[flac @ 0x1] a\n[flac @ 0x1] b\n")); } [Fact] diff --git a/tests/Sleezer.Tests/Sleezer.Tests.csproj b/tests/Sleezer.Tests/Sleezer.Tests.csproj index 74041a4b..0ac4babf 100644 --- a/tests/Sleezer.Tests/Sleezer.Tests.csproj +++ b/tests/Sleezer.Tests/Sleezer.Tests.csproj @@ -46,6 +46,12 @@ LinkBase="SourceUnderTest" /> + + + ((m, _) => + m.Name == nameof(IHistoryService.Find) ? new List { grab } : throw new NotSupportedException(m.Name)); + IDiskProvider disk = Fake.Of((m, _) => + m.Name == nameof(IDiskProvider.FolderExists) ? false : throw new NotSupportedException(m.Name)); + CapturingEvents events = new(); + Logger logger = LogManager.CreateNullLogger(); + SlskdCorruptionHandler handler = new(new CorruptionFailureHandler(history, events, disk, logger), new SlskdCorruptUserTracker(logger)); + + SlskdFileState mapped = new(Transfer(@"@@peer\Music\Album\07 remote name.flac")); + string source = """[{"Filename":"@@peer\\Music\\Album\\07 remote name.flac","Size":1000}]"""; + SlskdDownloadItem item = new(new ReleaseInfo { Source = source, Title = "Artist - Album", DownloadUrl = "u" }) { ID = "dl-1" }; + + await handler.HandleCorruptDownloadAsync( + item, + [ + new SlskdCorruptionStrike(null, "03 - Track Three.flac", "Audio won't decode (invalid residual)"), + new SlskdCorruptionStrike(mapped, "07 - Track Seven.flac", "Audio won't decode (Header missing)"), + ], + "/downloads/Artist - Album", + new SlskdProviderSettings(), + CancellationToken.None); + + Assert.Equal( + "Sleezer: 2 files failed the corruption check — " + + "03 - Track Three.flac: Audio won't decode (invalid residual); " + + "07 - Track Seven.flac: Audio won't decode (Header missing)", + Assert.Single(events.Failed).Message); + Assert.Equal(0, mapped.MaxRetryCount); + } + + private static SlskdDownloadFile Transfer(string filename) => new( + Id: "id", + Username: "peer", + Direction: "Download", + Filename: filename, + Size: 1000, + StartOffset: 0, + State: "Completed, Succeeded", + RequestedAt: DateTime.UtcNow, + EnqueuedAt: DateTime.UtcNow, + StartedAt: DateTime.UtcNow, + BytesTransferred: 1000, + AverageSpeed: 0, + BytesRemaining: 0, + ElapsedTime: TimeSpan.Zero, + PercentComplete: 100, + RemainingTime: TimeSpan.Zero, + EndedAt: null, + BatchId: null); +} diff --git a/tests/Sleezer.Tests/SlskdRemovedGrabTests.cs b/tests/Sleezer.Tests/SlskdRemovedGrabTests.cs index 7758ec65..c0fa2172 100644 --- a/tests/Sleezer.Tests/SlskdRemovedGrabTests.cs +++ b/tests/Sleezer.Tests/SlskdRemovedGrabTests.cs @@ -2,7 +2,6 @@ using System.Net; using FluentValidation.Results; using NLog; -using NLog.Config; using NLog.Targets; using NzbDrone.Common.Disk; using NzbDrone.Common.Http; @@ -10,6 +9,7 @@ using NzbDrone.Plugin.Sleezer.Download.Clients.Soulseek; using NzbDrone.Plugin.Sleezer.Download.Clients.Soulseek.Models; using NzbDrone.Plugin.Sleezer.Indexers.Soulseek; +using Sleezer.Tests.Fakes; using Xunit; namespace Sleezer.Tests; @@ -220,14 +220,6 @@ public void a_transfer_handed_to_a_regrab_is_no_leftover_of_the_removed_attempt( Assert.Equal(["own"], leftovers.Select(f => f.Id)); } - private static (Logger Logger, MemoryTarget Logs) CapturingLogger() - { - MemoryTarget logs = new() { Layout = "${level}|${message}" }; - LoggingConfiguration config = new(); - config.AddRuleForAllLevels(logs); - return (new LogFactory { Configuration = config }.GetLogger("canceller"), logs); - } - [Fact] public async Task a_transfer_slskd_already_dropped_is_skipped_quietly() { @@ -235,7 +227,7 @@ public async Task a_transfer_slskd_already_dropped_is_skipped_quietly() api.Transfers.Add(Transfer("gone", "Completed, Succeeded", "grab-batch")); api.Transfers.Add(Transfer("next", "Completed, Succeeded", "grab-batch")); api.GoneDeletes.Add("gone"); - (Logger logger, MemoryTarget logs) = CapturingLogger(); + (Logger logger, MemoryTarget logs) = TestLogs.Capturing("canceller"); int count = await SlskdBatchCanceller.CancelAsync(api, new SlskdProviderSettings(), "peer", new HashSet { "grab-batch" }, null, logger); @@ -251,7 +243,7 @@ public async Task a_delete_slskd_refuses_is_warned_and_does_not_stop_the_rest() api.Transfers.Add(Transfer("refused", "Completed, Succeeded", "grab-batch")); api.Transfers.Add(Transfer("next", "Completed, Succeeded", "grab-batch")); api.FailingDeletes.Add("refused"); - (Logger logger, MemoryTarget logs) = CapturingLogger(); + (Logger logger, MemoryTarget logs) = TestLogs.Capturing("canceller"); int count = await SlskdBatchCanceller.CancelAsync(api, new SlskdProviderSettings(), "peer", new HashSet { "grab-batch" }, null, logger);