Files
ersatztv/ErsatzTV.Scanner.Tests/Core/Metadata/MediaServerMovieLibraryScannerTests.cs
T
timothy 6f4497e1ce fix(484): guard the nested TV season and episode sweeps against projection failures
Review finding 1 (blocking). ScanSeasons' FlagFileNotFoundSeasons and ScanEpisodes'
FlagFileNotFoundEpisodes had no guard at all — neither #477's nor #484's — so ProjectToSeason /
ProjectToEpisode returning Failed() was computed and discarded.

#477 scoped those out because "the blast radius is one show's seasons / one season's episodes",
which holds for a per-parent EMPTY fetch but not for a projection failure: that is systematic by
construction. One bad code path fires on every parent, so every season enumerates zero episodes,
existing.Except([]) is the whole episode library, and EmptyTrashHandler deletes it permanently.

Threads the counter into GetSeasonLibraryItems / GetEpisodeLibraryItems(WithoutPeople) for
Jellyfin and Emby using the same optional-trailing-param shape, and guards both sweeps with
MediaServerReconciliationGuard.ShouldFlagMissingDescendants — the same class and the same private
failure predicate as ShouldFlagMissing, deliberately WITHOUT #477's empty-fetch branch so
per-parent empty behaviour (and #476's cascade, which depends on it) is unchanged.

Also from the review:
- finding 3: tests now pin the same-instance JOIN at every level (movie, show, season, episode,
  music video) by driving the real ScanLibrary entry point and recording the failure from inside
  the enumeration, so a refactor handing the api client a fresh counter goes red.
- finding 4: the missing-library Failed() branch is documented as defensive and unreachable.
- finding 2: the mass-Skip residual (Emby's response-shape-dependent MediaSources guard, Plex's
  pre-projection filter) is stated as a known limitation in the decision record.
- finding 5: the log-contract change (only the #484 message when both refusals apply) is noted.

fixes #484
2026-07-25 16:36:35 +02:00

232 lines
10 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.Jellyfin;
using ErsatzTV.Core.Metadata;
using ErsatzTV.Scanner.Core.Interfaces;
using ErsatzTV.Scanner.Core.Interfaces.Metadata;
using ErsatzTV.Scanner.Core.Metadata;
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 movie library FileNotFound
// (which EmptyTrash could then permanently delete). Assert the sweep is skipped when nothing came in.
public class MediaServerMovieLibraryScannerTests
{
[TestFixture]
public class CleanupFileNotFoundItems
{
[Test]
public async Task Empty_Incoming_With_Existing_Movies_Does_Not_Flag()
{
var movieRepository = Substitute.For<IJellyfinMovieRepository>();
var scannerProxy = Substitute.For<IScannerProxy>();
var library = new JellyfinLibrary { Id = 3, Name = "Movies" };
movieRepository.GetExistingMovies(library)
.Returns(new List<JellyfinItemEtag>
{
new() { ItemId = "movie-1", State = MediaItemState.Normal },
new() { ItemId = "movie-2", State = MediaItemState.Normal }
});
var scanner = new TestMovieLibraryScanner(scannerProxy);
Either<BaseError, Unit> result = await scanner.Scan(
movieRepository,
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
library);
result.IsRight.ShouldBeTrue();
await movieRepository.DidNotReceive().FlagFileNotFound(
Arg.Any<JellyfinLibrary>(), Arg.Any<List<string>>());
await scannerProxy.DidNotReceive().ReindexMediaItems(
Arg.Any<int[]>(), Arg.Any<CancellationToken>());
}
// #484 finding 3: ScanLibrary creates ONE counter and must hand that same instance to both
// GetMovieLibraryItems and ShouldFlagMissing. A refactor that handed the api client a fresh
// counter would leave the guard reading zero and silently re-enable the sweep — every other test
// would stay green, this one goes red. The zero-incoming guard cannot mask it: a survivor is
// present, so #477's branch does not fire.
[Test]
public async Task Projection_Failure_From_The_Api_Suppresses_The_Sweep_End_To_End()
{
var movieRepository = Substitute.For<IJellyfinMovieRepository>();
var scannerProxy = Substitute.For<IScannerProxy>();
var library = new JellyfinLibrary { Id = 3, Name = "Movies" };
movieRepository.GetExistingMovies(library)
.Returns(new List<JellyfinItemEtag>
{
new() { ItemId = "movie-1", Etag = "e1", State = MediaItemState.Normal },
new() { ItemId = "movie-2", Etag = "e2", State = MediaItemState.Normal }
});
// Left short-circuits the per-item metadata path; the id is still recorded as incoming
movieRepository.GetOrAdd(library, Arg.Any<JellyfinMovie>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Left<BaseError, MediaItemScanResult<JellyfinMovie>>(BaseError.New("skip metadata in test")));
scannerProxy.UpdateProgress(Arg.Any<decimal>(), Arg.Any<CancellationToken>()).Returns(true);
var scanner = new TestMovieLibraryScanner(scannerProxy)
{
MovieEntries =
[
new JellyfinMovie
{
ItemId = "movie-1",
Etag = "e1",
MovieMetadata = new List<MovieMetadata> { new() { Title = "Keeper" } }
}
],
ProjectionFailures = 1
};
Either<BaseError, Unit> result = await scanner.Scan(
movieRepository,
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
library);
result.IsRight.ShouldBeTrue();
await movieRepository.DidNotReceive().FlagFileNotFound(
Arg.Any<JellyfinLibrary>(), Arg.Any<List<string>>());
}
// positive control for the test above: identical arrangement, zero failures => movie-2 IS swept.
[Test]
public async Task Partial_Deletion_With_No_Projection_Failures_Still_Sweeps()
{
var movieRepository = Substitute.For<IJellyfinMovieRepository>();
var scannerProxy = Substitute.For<IScannerProxy>();
var library = new JellyfinLibrary { Id = 3, Name = "Movies" };
movieRepository.GetExistingMovies(library)
.Returns(new List<JellyfinItemEtag>
{
new() { ItemId = "movie-1", Etag = "e1", State = MediaItemState.Normal },
new() { ItemId = "movie-2", Etag = "e2", State = MediaItemState.Normal }
});
movieRepository.GetOrAdd(library, Arg.Any<JellyfinMovie>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Left<BaseError, MediaItemScanResult<JellyfinMovie>>(BaseError.New("skip metadata in test")));
scannerProxy.UpdateProgress(Arg.Any<decimal>(), Arg.Any<CancellationToken>()).Returns(true);
// an unstubbed substitute returns null, which would NRE on ids.ToArray()
movieRepository.FlagFileNotFound(Arg.Any<JellyfinLibrary>(), Arg.Any<List<string>>())
.Returns(new List<int> { 7 });
scannerProxy.ReindexMediaItems(Arg.Any<int[]>(), Arg.Any<CancellationToken>()).Returns(true);
var scanner = new TestMovieLibraryScanner(scannerProxy)
{
MovieEntries =
[
new JellyfinMovie
{
ItemId = "movie-1",
Etag = "e1",
MovieMetadata = new List<MovieMetadata> { new() { Title = "Keeper" } }
}
],
ProjectionFailures = 0
};
Either<BaseError, Unit> result = await scanner.Scan(
movieRepository,
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
library);
result.IsRight.ShouldBeTrue();
await movieRepository.Received(1).FlagFileNotFound(
library,
Arg.Is<List<string>>(l => l.Count == 1 && l.Contains("movie-2")));
}
}
// 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.
private sealed class TestMovieLibraryScanner : MediaServerMovieLibraryScanner<
JellyfinConnectionParameters, JellyfinLibrary, JellyfinMovie, JellyfinItemEtag>
{
public TestMovieLibraryScanner(IScannerProxy scannerProxy)
: base(
scannerProxy,
Substitute.For<IFileSystem>(),
Substitute.For<ILocalChaptersProvider>(),
Substitute.For<IMetadataRepository>(),
Substitute.For<ILogger>())
{
}
public Task<Either<BaseError, Unit>> Scan(
IJellyfinMovieRepository movieRepository,
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library) =>
ScanLibrary(
movieRepository,
connectionParameters,
library,
_ => string.Empty,
false,
CancellationToken.None);
// #484: incoming movies plus how many projection failures the enumeration reports into the
// counter the SCANNER created — this is what pins same-instance wiring end to end.
public JellyfinMovie[] MovieEntries { get; init; } = [];
public int ProjectionFailures { get; init; }
protected override IAsyncEnumerable<Tuple<JellyfinMovie, int>> GetMovieLibraryItems(
JellyfinConnectionParameters connectionParameters,
JellyfinLibrary library,
MediaServerProjectionFailureCounter projectionFailures) =>
Movies(MovieEntries, ProjectionFailures, projectionFailures);
protected override string MediaServerItemId(JellyfinMovie movie) => movie.ItemId;
protected override string MediaServerEtag(JellyfinMovie movie) => movie.Etag;
protected override Task<Option<MovieMetadata>> GetFullMetadata(
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
MediaItemScanResult<JellyfinMovie> result, JellyfinMovie incoming, bool deepScan) =>
throw new NotSupportedException();
protected override Task<Option<Tuple<MovieMetadata, MediaVersion>>> GetFullMetadataAndStatistics(
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
MediaItemScanResult<JellyfinMovie> result, JellyfinMovie incoming) =>
throw new NotSupportedException();
protected override Task<Either<BaseError, MediaItemScanResult<JellyfinMovie>>> UpdateMetadata(
MediaItemScanResult<JellyfinMovie> result, MovieMetadata fullMetadata,
CancellationToken cancellationToken) =>
throw new NotSupportedException();
// mirrors production: failures are recorded DURING enumeration into the caller's counter
private static async IAsyncEnumerable<Tuple<JellyfinMovie, int>> Movies(
JellyfinMovie[] movies,
int projectionFailureCount,
MediaServerProjectionFailureCounter projectionFailures)
{
await Task.CompletedTask;
for (var i = 0; i < projectionFailureCount; i++)
{
projectionFailures.RecordFailure();
}
foreach (JellyfinMovie movie in movies)
{
yield return new Tuple<JellyfinMovie, int>(movie, movies.Length + projectionFailureCount);
}
}
}
}