Adds LocalLibrariesController (L1-L7: list/get/create/update/delete/move-path/ path-exists) wrapping the existing local-library MediatR commands, mapping to the shared S0 response DTOs. Per design #202 §A.1/§C5/§C6: - 404 for L4/L5/L6 comes from a controller pre-check (GetLocalLibraryById is None), not the handler -- .Apply/.ToEitherAsync both .Join() a NotFoundError into a plain 422, so relying on the handler would be dead code. This is check-then-act; a delete racing the pre-check falls through to the handler's 422, documented in the controller. - L4/L5 409 via IEntityLocker.IsLibraryLocked(id); L6 resolves the source library from the path id (new ILibraryRepository.GetLibraryIdForPath) before its own lock check. - MoveLocalLibraryPathHandler gains same-MediaKind and different-library validation (finding 3) -- Blazor only filtered these client-side in the move dialog, so an API/MCP client could bypass them. - CreateLocalLibraryHandler/UpdateLocalLibraryHandler gain a shared NewPathsMustExist validation (LocalLibraryHandlerBase) that Directory.Exists- checks only new paths (Id < 1); existing rows stay exempt so an unmounted share doesn't block a rename. L7 (path-exists) is a controller-local IFileSystem check with no command. Tests: controller route/mediator-arg tests incl. 404-pre-check vs fall-through-422 and 409-lock cases; handler tests for the move-path cross-kind/same-library 422s, new-path 422 (missing/mixed), and a lossless round-trip proving local paths are identified by normalized path string, not id. Full solution test suite (Scanner/Core/Architecture/Tests/Infrastructure) green, 0 regressions. Deviations: none from the S1 slice description. Did not touch MediaSourceRepository.cs or any Plex/Jellyfin/Emby file (S2/S3 scope). Did not run the OpenAPI regen scripts (separate gate after S1-S3 merge per design §E).
139 lines
4.9 KiB
C#
139 lines
4.9 KiB
C#
using ErsatzTV.Application.Libraries;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Metadata;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Tests.Support;
|
|
using LanguageExt;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
using Unit = LanguageExt.Unit;
|
|
|
|
namespace ErsatzTV.Tests.Application.Libraries;
|
|
|
|
[TestFixture]
|
|
public class MoveLocalLibraryPathHandlerTests
|
|
{
|
|
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_Target_Library_Is_Same_As_Source()
|
|
{
|
|
(int libraryId, int pathId) = await SeedLibraryWithPath("Movies", LibraryMediaKind.Movies);
|
|
|
|
MoveLocalLibraryPathHandler handler = CreateHandler();
|
|
|
|
Either<BaseError, Unit> result =
|
|
await handler.Handle(new MoveLocalLibraryPath(pathId, libraryId), CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
result.IfLeft(error => error.Value.ShouldContain("different"));
|
|
|
|
await using TvContext dbContext = _db.CreateContext();
|
|
(await dbContext.LibraryPaths.FindAsync(pathId))!.LibraryId.ShouldBe(libraryId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Handle_Should_Return_422_When_Target_Library_Has_Different_MediaKind()
|
|
{
|
|
(int sourceLibraryId, int pathId) = await SeedLibraryWithPath("Movies", LibraryMediaKind.Movies);
|
|
int targetLibraryId = await SeedLibrary("TV Shows", LibraryMediaKind.Shows);
|
|
|
|
MoveLocalLibraryPathHandler handler = CreateHandler();
|
|
|
|
Either<BaseError, Unit> result =
|
|
await handler.Handle(new MoveLocalLibraryPath(pathId, targetLibraryId), CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
result.IfLeft(error => error.Value.ShouldContain("media kind"));
|
|
|
|
await using TvContext dbContext = _db.CreateContext();
|
|
(await dbContext.LibraryPaths.FindAsync(pathId))!.LibraryId.ShouldBe(sourceLibraryId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Handle_Should_Move_Path_When_Target_Library_Is_Different_And_Same_MediaKind()
|
|
{
|
|
(_, int pathId) = await SeedLibraryWithPath("Movies", LibraryMediaKind.Movies);
|
|
int targetLibraryId = await SeedLibrary("More Movies", LibraryMediaKind.Movies);
|
|
|
|
MoveLocalLibraryPathHandler handler = CreateHandler();
|
|
|
|
Either<BaseError, Unit> result =
|
|
await handler.Handle(new MoveLocalLibraryPath(pathId, targetLibraryId), CancellationToken.None);
|
|
|
|
result.IsRight.ShouldBeTrue();
|
|
|
|
await using TvContext dbContext = _db.CreateContext();
|
|
(await dbContext.LibraryPaths.FindAsync(pathId))!.LibraryId.ShouldBe(targetLibraryId);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Handle_Should_Return_422_When_Path_Does_Not_Exist()
|
|
{
|
|
int targetLibraryId = await SeedLibrary("Movies", LibraryMediaKind.Movies);
|
|
|
|
MoveLocalLibraryPathHandler handler = CreateHandler();
|
|
|
|
Either<BaseError, Unit> result =
|
|
await handler.Handle(new MoveLocalLibraryPath(9999, targetLibraryId), CancellationToken.None);
|
|
|
|
result.IsLeft.ShouldBeTrue();
|
|
}
|
|
|
|
private MoveLocalLibraryPathHandler CreateHandler() =>
|
|
new(
|
|
Substitute.For<ISearchIndex>(),
|
|
Substitute.For<ISearchRepository>(),
|
|
Substitute.For<IFallbackMetadataProvider>(),
|
|
Substitute.For<ILanguageCodeService>(),
|
|
_db.Factory,
|
|
NullLogger<MoveLocalLibraryPathHandler>.Instance);
|
|
|
|
private async Task<int> SeedLibrary(string name, LibraryMediaKind mediaKind)
|
|
{
|
|
await using TvContext context = _db.CreateContext();
|
|
var source = new LocalMediaSource
|
|
{
|
|
Libraries =
|
|
[
|
|
new LocalLibrary { Name = name, MediaKind = mediaKind, Paths = [] }
|
|
]
|
|
};
|
|
await context.LocalMediaSources.AddAsync(source);
|
|
await context.SaveChangesAsync();
|
|
return source.Libraries[0].Id;
|
|
}
|
|
|
|
private async Task<(int LibraryId, int PathId)> SeedLibraryWithPath(string name, LibraryMediaKind mediaKind)
|
|
{
|
|
await using TvContext context = _db.CreateContext();
|
|
var source = new LocalMediaSource
|
|
{
|
|
Libraries =
|
|
[
|
|
new LocalLibrary
|
|
{
|
|
Name = name,
|
|
MediaKind = mediaKind,
|
|
Paths = [new LibraryPath { Path = "/media/one" }]
|
|
}
|
|
]
|
|
};
|
|
await context.LocalMediaSources.AddAsync(source);
|
|
await context.SaveChangesAsync();
|
|
return (source.Libraries[0].Id, source.Libraries[0].Paths[0].Id);
|
|
}
|
|
}
|