Files
ersatztv/ErsatzTV.Scanner.Tests/Core/Metadata/MediaServerOtherVideoLibraryScannerTests.cs
T
timothyandClaude Opus 4.8 aa32fd78dd
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 5s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 9s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 7s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 7m23s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 7s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 15m20s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 18m18s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(477): guard media-server library sweeps against successful-but-empty fetches
A successful fetch returning zero items made existing.Except([]) flag the ENTIRE
library FileNotFound in one scan — feeding EmptyTrashHandler's permanent delete
and emptying every affected collection (dead channels). Add a shared
MediaServerReconciliationGuard that skips (and logs a Warning) the sweep when
incoming==0 while items exist, wired into the three library-level sweeps
(Television shows / Movie / OtherVideo).

An empty incoming set is indistinguishable at scan time from a mid-restore /
emptied-upstream error (both report a zero total), so this deliberately overrides
#476's degenerate "last item removed => empty incoming => flag" case. #476's
cascade still fires for partial deletions (survivors present); its characterization
test moves from an empty incoming to a survivor+removed partial-deletion case.

Tests: policy table (MediaServerReconciliationGuardTests) + per-scanner integration
proving the wiring (empty incoming + non-empty existing flags/reindexes nothing).
Proven non-vacuous by neutralizing the guard. Nested TV season/episode sweeps left
unguarded (bounded blast radius); ratio-threshold + projection-failure detection
deferred to a follow-up. docs/decisions.md updated.

Fixes #477

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-19 23:28:27 +02:00

109 lines
4.6 KiB
C#

using System.IO.Abstractions;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Metadata;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Metadata;
using ErsatzTV.Core.Plex;
using ErsatzTV.Scanner.Core.Interfaces;
using ErsatzTV.Scanner.Core.Interfaces.Metadata;
using ErsatzTV.Scanner.Core.Metadata;
using ErsatzTV.Scanner.Core.Plex;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
namespace ErsatzTV.Scanner.Tests.Core.Metadata;
// #477: a successful-but-empty media-server fetch must not flag a non-empty other-video library
// FileNotFound (which EmptyTrash could then permanently delete). Assert the sweep is skipped.
public class MediaServerOtherVideoLibraryScannerTests
{
[TestFixture]
public class CleanupFileNotFoundItems
{
[Test]
public async Task Empty_Incoming_With_Existing_OtherVideos_Does_Not_Flag()
{
var otherVideoRepository = Substitute.For<IPlexOtherVideoRepository>();
var scannerProxy = Substitute.For<IScannerProxy>();
var library = new PlexLibrary { Id = 9, Name = "Other Videos" };
otherVideoRepository.GetExistingOtherVideos(library)
.Returns(new List<PlexItemEtag>
{
new() { Key = "ov-1", State = MediaItemState.Normal },
new() { Key = "ov-2", State = MediaItemState.Normal }
});
var scanner = new TestOtherVideoLibraryScanner(scannerProxy);
Either<BaseError, Unit> result = await scanner.Scan(otherVideoRepository, library);
result.IsRight.ShouldBeTrue();
await otherVideoRepository.DidNotReceive().FlagFileNotFound(
Arg.Any<PlexLibrary>(), Arg.Any<List<string>>());
await scannerProxy.DidNotReceive().ReindexMediaItems(
Arg.Any<int[]>(), Arg.Any<CancellationToken>());
}
}
// Minimal concrete subclass exposing the cleanup path. Driven with an empty incoming list, so none of
// the per-item metadata members below are ever invoked — they exist only to satisfy the contract. The
// connection parameters are likewise never dereferenced on the empty path (passed null! below).
private sealed class TestOtherVideoLibraryScanner : MediaServerOtherVideoLibraryScanner<
PlexConnectionParameters, PlexLibrary, PlexOtherVideo, PlexItemEtag>
{
public TestOtherVideoLibraryScanner(IScannerProxy scannerProxy)
: base(
scannerProxy,
Substitute.For<IFileSystem>(),
Substitute.For<ILocalChaptersProvider>(),
Substitute.For<IMetadataRepository>(),
Substitute.For<ILogger>())
{
}
public Task<Either<BaseError, Unit>> Scan(
IPlexOtherVideoRepository otherVideoRepository,
PlexLibrary library) =>
ScanLibrary(
otherVideoRepository,
null!,
library,
_ => string.Empty,
false,
CancellationToken.None);
protected override IAsyncEnumerable<Tuple<PlexOtherVideo, int>> GetOtherVideoLibraryItems(
PlexConnectionParameters connectionParameters, PlexLibrary library) => EmptyOtherVideos();
protected override string MediaServerItemId(PlexOtherVideo otherVideo) => otherVideo.Key;
protected override string MediaServerEtag(PlexOtherVideo otherVideo) => otherVideo.Etag;
protected override Task<Option<OtherVideoMetadata>> GetFullMetadata(
PlexConnectionParameters connectionParameters, PlexLibrary library,
MediaItemScanResult<PlexOtherVideo> result, PlexOtherVideo incoming, bool deepScan) =>
throw new NotSupportedException();
protected override Task<Option<Tuple<OtherVideoMetadata, MediaVersion>>> GetFullMetadataAndStatistics(
PlexConnectionParameters connectionParameters, PlexLibrary library,
MediaItemScanResult<PlexOtherVideo> result, PlexOtherVideo incoming) =>
throw new NotSupportedException();
protected override Task<Either<BaseError, MediaItemScanResult<PlexOtherVideo>>> UpdateMetadata(
MediaItemScanResult<PlexOtherVideo> result, OtherVideoMetadata fullMetadata,
CancellationToken cancellationToken) =>
throw new NotSupportedException();
private static async IAsyncEnumerable<Tuple<PlexOtherVideo, int>> EmptyOtherVideos()
{
await Task.CompletedTask;
yield break;
}
}
}