Files
ersatztv/ErsatzTV.Scanner.Tests/Core/Metadata/MediaServerTelevisionLibraryScannerTests.cs
T
timothyandClaude Opus 4.8 61bd374494
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 7s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 24s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 7s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 5m52s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 5m36s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 8s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 9s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 19m14s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(476): cascade FileNotFound from removed shows/seasons to descendants
The media-server television scanner reconciles removed items at three levels
via existing.Except(incoming), but the season and episode sweeps live inside
the per-parent loops (ScanSeasons inside the show loop, ScanEpisodes inside the
season loop). Those loops only iterate parents present in the incoming list, so
a show (or season) that is gone from the media server is never visited and its
descendants are never swept — they keep their last state. On Jellyfin/Emby that
is RemoteOnly, which PlayoutBuilder does NOT skip even with PlayoutSkipMissingItems
on, so every orphaned episode keeps getting scheduled as a guaranteed tune-in
failure (the #473 reproduction; 717 stale prod rows across 10 removed shows).

Fix: cascade the flag by parent MediaItem.Id. Two provider-agnostic repo helpers
(Season.ShowId / Episode.SeasonId are on the base tables) flag descendants and the
scanner drives them after each parent sweep — show → seasons → episodes, and
season → episodes for the show-present case. Shared abstract base fixes
Jellyfin/Plex/Emby at once; Movie/OtherVideo are flat and have no such gap.

Tests: a Scanner.Tests case asserts the scanner cascades through the (substituted)
repository (non-vacuous — fails if the cascade calls are removed), and Integration
tests exercise the real cascade SQL against the schema, proving it flags only the
targeted subtree and no-ops on empty input.

fixes #476

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

174 lines
8.6 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.
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" };
// one show exists in the DB but is NOT in the incoming (empty) list => it was removed upstream
televisionRepository.GetExistingShows(library, Arg.Any<CancellationToken>())
.Returns(new List<JellyfinItemEtag>
{
new() { ItemId = "show-6366", State = MediaItemState.FileNotFound }
});
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,
EmptyShows());
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>());
}
private static async IAsyncEnumerable<Tuple<JellyfinShow, int>> EmptyShows()
{
await Task.CompletedTask;
yield break;
}
}
// 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();
}
}