fix(477): guard media-server library sweeps against successful-but-empty fetches
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>
This commit is contained in:
2026-07-19 23:28:27 +02:00
co-authored by Claude Opus 4.8
parent de11eb01b3
commit aa32fd78dd
9 changed files with 460 additions and 12 deletions
@@ -17,6 +17,9 @@ 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]
@@ -30,13 +33,24 @@ public class MediaServerTelevisionLibraryScannerTests
var library = new JellyfinLibrary { Id = 42, Name = "TV Shows" };
// one show exists in the DB but is NOT in the incoming (empty) list => it was removed upstream
// 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>())
@@ -57,7 +71,11 @@ public class MediaServerTelevisionLibraryScannerTests
televisionRepository,
new JellyfinConnectionParameters("http://jellyfin.example", "abc", 7),
library,
EmptyShows());
Shows(new JellyfinShow
{
ItemId = "show-keep",
ShowMetadata = new List<ShowMetadata> { new() { Title = "Keeper" } }
}));
result.IsRight.ShouldBeTrue();
@@ -78,11 +96,58 @@ public class MediaServerTelevisionLibraryScannerTests
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