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
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>
239 lines
12 KiB
C#
239 lines
12 KiB
C#
using System.IO.Abstractions;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
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;
|
|
|
|
// #476: a show/season gone from the media server is absent from the incoming list, so the per-parent
|
|
// loop never visits it and the descendant sweeps never run for it. These tests assert the scanner
|
|
// cascades the FileNotFound flag to descendants via the repository, using a substituted repository.
|
|
// #477: an empty incoming list is treated as a suspect (mid-restore / emptied) fetch and the sweep is
|
|
// skipped instead of nuking the whole library — so the #476 cascade is now exercised with a survivor
|
|
// present (a genuine partial deletion), and the empty case asserts nothing is flagged.
|
|
public class MediaServerTelevisionLibraryScannerTests
|
|
{
|
|
[TestFixture]
|
|
public class CleanupFileNotFoundItems
|
|
{
|
|
[Test]
|
|
public async Task Removed_Show_Cascades_FileNotFound_To_Seasons_And_Episodes()
|
|
{
|
|
var televisionRepository = Substitute.For<IJellyfinTelevisionRepository>();
|
|
var scannerProxy = Substitute.For<IScannerProxy>();
|
|
|
|
var library = new JellyfinLibrary { Id = 42, Name = "TV Shows" };
|
|
|
|
// two shows exist; the survivor is still in the (non-empty) incoming list, so this is a
|
|
// genuine partial deletion — "show-6366" is gone upstream and must be flagged + cascaded.
|
|
// (#477: an EMPTY incoming would instead skip the sweep — see the guard test below.)
|
|
televisionRepository.GetExistingShows(library, Arg.Any<CancellationToken>())
|
|
.Returns(new List<JellyfinItemEtag>
|
|
{
|
|
new() { ItemId = "show-keep", State = MediaItemState.Normal },
|
|
new() { ItemId = "show-6366", State = MediaItemState.FileNotFound }
|
|
});
|
|
|
|
// the survivor short-circuits to Left so the per-item metadata path (unsupported in this
|
|
// harness) is never entered; it is still recorded as incoming, so it is not swept.
|
|
televisionRepository.GetOrAdd(library, Arg.Any<JellyfinShow>(), Arg.Any<CancellationToken>())
|
|
.Returns(Left<BaseError, MediaItemScanResult<JellyfinShow>>(BaseError.New("skip metadata in test")));
|
|
|
|
// the per-item loop reports progress; an unstubbed substitute returns false => ScanCanceled
|
|
scannerProxy.UpdateProgress(Arg.Any<decimal>(), Arg.Any<CancellationToken>()).Returns(true);
|
|
|
|
televisionRepository
|
|
.FlagFileNotFoundShows(library, Arg.Is<List<string>>(l => l.Contains("show-6366")),
|
|
Arg.Any<CancellationToken>())
|
|
.Returns(new List<int> { 100 });
|
|
televisionRepository
|
|
.FlagFileNotFoundSeasonsForShows(Arg.Is<List<int>>(l => l.SequenceEqual(new[] { 100 })),
|
|
Arg.Any<CancellationToken>())
|
|
.Returns(new List<int> { 200, 201 });
|
|
televisionRepository
|
|
.FlagFileNotFoundEpisodesForSeasons(Arg.Is<List<int>>(l => l.SequenceEqual(new[] { 200, 201 })),
|
|
Arg.Any<CancellationToken>())
|
|
.Returns(new List<int> { 300, 301, 302 });
|
|
scannerProxy.ReindexMediaItems(Arg.Any<int[]>(), Arg.Any<CancellationToken>()).Returns(true);
|
|
|
|
var scanner = new TestTelevisionLibraryScanner(scannerProxy);
|
|
|
|
Either<BaseError, Unit> result = await scanner.Scan(
|
|
televisionRepository,
|
|
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
|
|
library,
|
|
Shows(new JellyfinShow
|
|
{
|
|
ItemId = "show-keep",
|
|
ShowMetadata = new List<ShowMetadata> { new() { Title = "Keeper" } }
|
|
}));
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
|
|
await televisionRepository.Received(1).FlagFileNotFoundShows(
|
|
library,
|
|
Arg.Is<List<string>>(l => l.Count == 1 && l.Contains("show-6366")),
|
|
Arg.Any<CancellationToken>());
|
|
await televisionRepository.Received(1).FlagFileNotFoundSeasonsForShows(
|
|
Arg.Is<List<int>>(l => l.SequenceEqual(new[] { 100 })),
|
|
Arg.Any<CancellationToken>());
|
|
await televisionRepository.Received(1).FlagFileNotFoundEpisodesForSeasons(
|
|
Arg.Is<List<int>>(l => l.SequenceEqual(new[] { 200, 201 })),
|
|
Arg.Any<CancellationToken>());
|
|
|
|
// every affected item (show + seasons + episodes) is reindexed so search reflects the new state
|
|
await scannerProxy.Received(1).ReindexMediaItems(
|
|
Arg.Is<int[]>(a => new[] { 100, 200, 201, 300, 301, 302 }.All(id => a.Contains(id))),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Empty_Incoming_With_Existing_Shows_Does_Not_Flag()
|
|
{
|
|
var televisionRepository = Substitute.For<IJellyfinTelevisionRepository>();
|
|
var scannerProxy = Substitute.For<IScannerProxy>();
|
|
|
|
var library = new JellyfinLibrary { Id = 42, Name = "TV Shows" };
|
|
|
|
// shows exist locally, but a successful fetch returned ZERO items (server mid-restore or the
|
|
// library was emptied upstream). #477: flagging here would nuke the entire library, so the
|
|
// sweep must be skipped and nothing flagged or reindexed.
|
|
televisionRepository.GetExistingShows(library, Arg.Any<CancellationToken>())
|
|
.Returns(new List<JellyfinItemEtag>
|
|
{
|
|
new() { ItemId = "show-1", State = MediaItemState.Normal },
|
|
new() { ItemId = "show-2", State = MediaItemState.Normal }
|
|
});
|
|
|
|
var scanner = new TestTelevisionLibraryScanner(scannerProxy);
|
|
|
|
Either<BaseError, Unit> result = await scanner.Scan(
|
|
televisionRepository,
|
|
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
|
|
library,
|
|
EmptyShows());
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
|
|
await televisionRepository.DidNotReceive().FlagFileNotFoundShows(
|
|
Arg.Any<JellyfinLibrary>(), Arg.Any<List<string>>(), Arg.Any<CancellationToken>());
|
|
await televisionRepository.DidNotReceive().FlagFileNotFoundSeasonsForShows(
|
|
Arg.Any<List<int>>(), Arg.Any<CancellationToken>());
|
|
await televisionRepository.DidNotReceive().FlagFileNotFoundEpisodesForSeasons(
|
|
Arg.Any<List<int>>(), Arg.Any<CancellationToken>());
|
|
await scannerProxy.DidNotReceive().ReindexMediaItems(
|
|
Arg.Any<int[]>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
private static async IAsyncEnumerable<Tuple<JellyfinShow, int>> EmptyShows()
|
|
{
|
|
await Task.CompletedTask;
|
|
yield break;
|
|
}
|
|
|
|
private static async IAsyncEnumerable<Tuple<JellyfinShow, int>> Shows(params JellyfinShow[] shows)
|
|
{
|
|
await Task.CompletedTask;
|
|
foreach (JellyfinShow show in shows)
|
|
{
|
|
yield return new Tuple<JellyfinShow, int>(show, shows.Length);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Minimal concrete subclass that exposes the abstract scanner's cleanup path. The incoming show list
|
|
// is supplied directly (empty = "all shows removed"), so none of the per-item metadata members below
|
|
// are ever invoked — they exist only to satisfy the abstract contract.
|
|
private sealed class TestTelevisionLibraryScanner : MediaServerTelevisionLibraryScanner<
|
|
JellyfinConnectionParameters, JellyfinLibrary, JellyfinShow, JellyfinSeason, JellyfinEpisode,
|
|
JellyfinItemEtag>
|
|
{
|
|
public TestTelevisionLibraryScanner(IScannerProxy scannerProxy)
|
|
: base(
|
|
scannerProxy,
|
|
Substitute.For<IFileSystem>(),
|
|
Substitute.For<ILocalChaptersProvider>(),
|
|
Substitute.For<IMetadataRepository>(),
|
|
Substitute.For<ILogger>())
|
|
{
|
|
}
|
|
|
|
public Task<Either<BaseError, Unit>> Scan(
|
|
IMediaServerTelevisionRepository<JellyfinLibrary, JellyfinShow, JellyfinSeason, JellyfinEpisode,
|
|
JellyfinItemEtag> televisionRepository,
|
|
JellyfinConnectionParameters connectionParameters,
|
|
JellyfinLibrary library,
|
|
IAsyncEnumerable<Tuple<JellyfinShow, int>> showEntries) =>
|
|
ScanLibrary(
|
|
televisionRepository,
|
|
connectionParameters,
|
|
library,
|
|
_ => string.Empty,
|
|
showEntries,
|
|
false,
|
|
CancellationToken.None);
|
|
|
|
protected override IAsyncEnumerable<Tuple<JellyfinShow, int>> GetShowLibraryItems(
|
|
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override string MediaServerItemId(JellyfinShow show) => show.ItemId;
|
|
protected override string MediaServerItemId(JellyfinSeason season) => season.ItemId;
|
|
protected override string MediaServerItemId(JellyfinEpisode episode) => episode.ItemId;
|
|
protected override string MediaServerEtag(JellyfinShow show) => show.Etag;
|
|
protected override string MediaServerEtag(JellyfinSeason season) => season.Etag;
|
|
protected override string MediaServerEtag(JellyfinEpisode episode) => episode.Etag;
|
|
|
|
protected override IAsyncEnumerable<Tuple<JellyfinSeason, int>> GetSeasonLibraryItems(
|
|
JellyfinLibrary library, JellyfinConnectionParameters connectionParameters, JellyfinShow show) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override IAsyncEnumerable<Tuple<JellyfinEpisode, int>> GetEpisodeLibraryItems(
|
|
JellyfinLibrary library, JellyfinConnectionParameters connectionParameters, JellyfinShow show,
|
|
JellyfinSeason season, bool isNewSeason) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Option<ShowMetadata>> GetFullMetadata(
|
|
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
|
|
MediaItemScanResult<JellyfinShow> result, JellyfinShow incoming, bool deepScan) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Option<SeasonMetadata>> GetFullMetadata(
|
|
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
|
|
MediaItemScanResult<JellyfinSeason> result, JellyfinSeason incoming, bool deepScan) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Option<EpisodeMetadata>> GetFullMetadata(
|
|
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
|
|
MediaItemScanResult<JellyfinEpisode> result, JellyfinEpisode incoming, bool deepScan) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Option<Tuple<EpisodeMetadata, MediaVersion>>> GetFullMetadataAndStatistics(
|
|
JellyfinConnectionParameters connectionParameters, JellyfinLibrary library,
|
|
MediaItemScanResult<JellyfinEpisode> result, JellyfinEpisode incoming) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Either<BaseError, MediaItemScanResult<JellyfinShow>>> UpdateMetadata(
|
|
MediaItemScanResult<JellyfinShow> result, ShowMetadata fullMetadata) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Either<BaseError, MediaItemScanResult<JellyfinSeason>>> UpdateMetadata(
|
|
MediaItemScanResult<JellyfinSeason> result, SeasonMetadata fullMetadata) =>
|
|
throw new NotSupportedException();
|
|
|
|
protected override Task<Either<BaseError, MediaItemScanResult<JellyfinEpisode>>> UpdateMetadata(
|
|
MediaItemScanResult<JellyfinEpisode> result, EpisodeMetadata fullMetadata,
|
|
CancellationToken cancellationToken) =>
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|