Review verdict was MERGEABLE; these are the findings worth fixing before merge. M1 (the one gating finding): LibraryMediaKind.Mixed was accepted by POST /api/v1/libraries/local. That created a library with no scanner, which then failed every scan forever and logged at ERROR on each scheduler tick. The invariant was asserted in prose twice and enforced nowhere -- and hiding it from the SPA dropdown is not enforcement, since machine clients are a stated audience for /api/v1. Now validated in CreateLocalLibraryHandler. M3: an empty or whitespace CollectionType did not match the `null` pattern and fell through to None, so on Jellyfin versions that serialize it that way the whole feature silently no-opped -- no library, no log line. Normalized via IsNullOrWhiteSpace, covered by a TestCase for "" and " ". L4: the cancellation test asserted IsLeft but not that ScanCanceled reaches the caller's info-log branch. That is the fragile bit -- folding it into an aggregate BaseError would silently demote a user cancellation to an ERROR log (#410). Now pinned in both directions. Nit: reverted the show-guard message to match the byte-identical Plex/Emby guards; the original wording is still accurate since the guard only fires when the kind is neither Shows nor Mixed. Also documented three accepted limitations in decisions.md (progress bar resets per kind, one failing kind forces the others to rescan, and the fact that the music-video scanner does no reconciliation at all -- which is why it cannot cross-delete, but also means removals never propagate). Negative controls: reverting the M1 validation fails its test; reverting the projection/dispatch fails theirs. Refs #489
125 lines
4.2 KiB
C#
125 lines
4.2 KiB
C#
using System.IO.Abstractions;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Application;
|
|
using ErsatzTV.Application.Libraries;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Locking;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Tests.Support;
|
|
using LanguageExt;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
using Testably.Abstractions.Testing;
|
|
using ThreadingChannel = System.Threading.Channels.Channel;
|
|
|
|
namespace ErsatzTV.Tests.Application.Libraries;
|
|
|
|
[TestFixture]
|
|
public class CreateLocalLibraryHandlerTests
|
|
{
|
|
private InMemoryTvContext _db = null!;
|
|
|
|
[SetUp]
|
|
public async Task SetUp() => _db = await InMemoryTvContext.CreateAsync();
|
|
|
|
[TearDown]
|
|
public async Task TearDown() => await _db.DisposeAsync();
|
|
|
|
[Test]
|
|
public async Task Handle_Should_Return_422_When_New_Path_Does_Not_Exist()
|
|
{
|
|
await SeedLocalMediaSource();
|
|
|
|
CreateLocalLibraryHandler handler = CreateHandler(new MockFileSystem());
|
|
|
|
Either<BaseError, LocalLibraryViewModel> result = await handler.Handle(
|
|
new CreateLocalLibrary("Movies", LibraryMediaKind.Movies, ["/media/movies"]),
|
|
CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
result.IfLeft(error => error.Value.ShouldContain("/media/movies"));
|
|
}
|
|
|
|
// LibraryMediaKind.Mixed exists only for remote (Jellyfin) libraries, where the media server
|
|
// classifies each item. No local folder scanner handles it, so a local Mixed library would fail
|
|
// every scan forever and log at ERROR on every scheduler tick. The API takes a raw
|
|
// LibraryMediaKind, so hiding it from the SPA dropdown is not enforcement (#489 review M1).
|
|
[Test]
|
|
public async Task Handle_Should_Reject_The_Mixed_Media_Kind_For_Local_Libraries()
|
|
{
|
|
await SeedLocalMediaSource();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
fileSystem.Directory.CreateDirectory("/media/music");
|
|
|
|
CreateLocalLibraryHandler handler = CreateHandler(fileSystem);
|
|
|
|
Either<BaseError, LocalLibraryViewModel> result = await handler.Handle(
|
|
new CreateLocalLibrary("Music", LibraryMediaKind.Mixed, ["/media/music"]),
|
|
CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
result.IfLeft(error => error.Value.ShouldContain("Mixed"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task Handle_Should_List_Only_The_Missing_Paths_When_Mixed()
|
|
{
|
|
await SeedLocalMediaSource();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
fileSystem.Directory.CreateDirectory("/media/movies");
|
|
|
|
CreateLocalLibraryHandler handler = CreateHandler(fileSystem);
|
|
|
|
Either<BaseError, LocalLibraryViewModel> result = await handler.Handle(
|
|
new CreateLocalLibrary("Movies", LibraryMediaKind.Movies, ["/media/movies", "/media/missing"]),
|
|
CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
result.IfLeft(error =>
|
|
{
|
|
error.Value.ShouldContain("/media/missing");
|
|
error.Value.ShouldNotContain("/media/movies");
|
|
});
|
|
}
|
|
|
|
[Test]
|
|
public async Task Handle_Should_Create_When_All_New_Paths_Exist()
|
|
{
|
|
await SeedLocalMediaSource();
|
|
|
|
var fileSystem = new MockFileSystem();
|
|
fileSystem.Directory.CreateDirectory("/media/movies");
|
|
|
|
CreateLocalLibraryHandler handler = CreateHandler(fileSystem);
|
|
|
|
Either<BaseError, LocalLibraryViewModel> result = await handler.Handle(
|
|
new CreateLocalLibrary("Movies", LibraryMediaKind.Movies, ["/media/movies"]),
|
|
CancellationToken.None);
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
result.IfRight(vm =>
|
|
{
|
|
vm.Name.ShouldBe("Movies");
|
|
vm.MediaKind.ShouldBe(LibraryMediaKind.Movies);
|
|
});
|
|
}
|
|
|
|
private CreateLocalLibraryHandler CreateHandler(IFileSystem fileSystem) =>
|
|
new(
|
|
ThreadingChannel.CreateUnbounded<IScannerBackgroundServiceRequest>().Writer,
|
|
Substitute.For<IEntityLocker>(),
|
|
fileSystem,
|
|
_db.Factory);
|
|
|
|
private async Task SeedLocalMediaSource()
|
|
{
|
|
await using TvContext context = _db.CreateContext();
|
|
await context.LocalMediaSources.AddAsync(new LocalMediaSource());
|
|
await context.SaveChangesAsync();
|
|
}
|
|
}
|