Fixes library cover image handling on save

Validates the cover image only when a new one is chosen, so an unchanged
stored cover does not block saving unrelated changes, and keeps the cover
already copied to the internal media directory instead of re-copying the
stored relative path, which was treated as an absolute file system path.
This commit is contained in:
BinaryAlley committed 2026-08-26 19:15:49 +03:00
1 parent 00dcf3d471
commit ccefb65c83
4 files changed
+101 -12

No files matched your search

@@ -168,6 +168,11 @@ public class UpdateLibraryCommandHandlerTests
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
command = command with { CoverImage = "C:/Users/user/cover.jpg" };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>())
.Returns(Result.From(ImageType.None));
@@ -177,7 +182,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.CoverFileMustBeAnImage, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -186,6 +191,11 @@ public class UpdateLibraryCommandHandlerTests
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
command = command with { CoverImage = "C:/Users/user/cover.jpg" };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to determine image type"));
@@ -194,7 +204,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -204,6 +214,10 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
command = command with { CoverImage = " " };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -211,10 +225,31 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.FileSystemManagement.InvalidPath, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenCoverImageIsUnchanged_ShouldSkipImageTypeCheck()
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = command.CoverImage;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
await _mockEnvironmentContext.FileTypeService.DidNotReceive()
.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(2).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenLibraryDoesNotExist_ShouldReturnNotFoundError()
{
@@ -156,6 +156,30 @@ public class LibrarySavedDomainEventHandlerTests
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenCoverImageIsAlreadyInMediaDirectory_ShouldKeepItWithoutRecopying()
{
// Arrange
Guid libraryId = Guid.NewGuid();
Library library = _libraryFixture.Create(id: libraryId, coverImage: "/Media/Libraries/guid/cover.png");
LibrarySavedDomainEvent domainEvent = new(Guid.NewGuid(), library, DateTime.UtcNow);
// mirror the real CombinePath behavior, which treats the combined name as a directory segment and appends a trailing
// separator; the internal cover path resolution must still yield the file path without that trailing separator
_mockPathService.CombinePath(Arg.Any<string>(), Arg.Any<string>())
.Returns(callInfo => Result.From(string.Concat(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)) + '/'));
string internalImagePath = $"{AppContext.BaseDirectory.TrimEnd('/')}/Media/Libraries/guid/cover.png";
_mockFileProviderService.FileExists(Arg.Any<FileSystemPathId>())
.Returns(callInfo => Result.From(callInfo.Arg<FileSystemPathId>().Path == internalImagePath));
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
Assert.Equal("/Media/Libraries/guid/cover.png", library.CoverImage);
_mockFileProviderService.DidNotReceive().CopyFile(Arg.Any<FileSystemPathId>(), Arg.Any<FileSystemPathId>(), Arg.Any<bool>());
await _mockLibraryRepository.Received(1).UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenCoverSourceFileDoesNotExist_ShouldThrowEventualConsistencyException()
{