Adds per-library consumption settings to the domain, storage, and API

This commit is contained in:
BinaryAlley committed 2026-10-24 10:20:00 +03:00
1 parent eaca69bf2a
commit b2d274f609
47 files changed
+4371 -11

No files matched your search

@@ -31,7 +31,9 @@ public class AddMediaLibraryRequestMappingTests
isLocked: false,
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false
shouldSkipUnchangedDirectoriesDuringScan: false,
shouldTrackConsumptionProgress: true,
completionThresholdPercentage: 90
);
// Act
@@ -47,6 +49,8 @@ public class AddMediaLibraryRequestMappingTests
Assert.Equal(request.IsLocked, result.IsLocked);
Assert.Equal(request.CanDownloadMetadataFromWeb, result.CanDownloadMetadataFromWeb);
Assert.Equal(request.ShouldSaveMetadataInMediaDirectories, result.ShouldSaveMetadataInMediaDirectories);
Assert.Equal(request.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
Assert.Equal(request.CompletionThresholdPercentage, result.CompletionThresholdPercentage);
}
[Theory]
@@ -45,6 +45,8 @@ public class LibraryEntityMappingTests
Assert.Equal(entity.LibraryType, result.LibraryType);
Assert.Equal(entity.ContentLocations.Select(l => l.Path), result.ContentLocations);
Assert.Equal(entity.CoverImage, result.CoverImage);
Assert.Equal(entity.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
Assert.Equal(entity.CompletionThresholdPercentage, result.CompletionThresholdPercentage);
Assert.Equal(entity.CreatedOnUtc, result.CreatedOnUtc);
Assert.Equal(entity.UpdatedOnUtc, result.UpdatedOnUtc);
}
@@ -198,6 +200,8 @@ public class LibraryEntityMappingTests
Assert.Equal(entity.Title, result.Value.Title);
Assert.Equal(entity.LibraryType, result.Value.LibraryType);
Assert.Equal("D:/cover.jpg", result.Value.CoverImage.Value);
Assert.Equal(entity.ShouldTrackConsumptionProgress, result.Value.ConsumptionSettings.ShouldTrackConsumptionProgress);
Assert.Equal(entity.CompletionThresholdPercentage, result.Value.ConsumptionSettings.CompletionThresholdPercentage.HasValue ? result.Value.ConsumptionSettings.CompletionThresholdPercentage.Value : null);
Assert.Equal(2, result.Value.PathTemplate.Parts.Count);
Assert.Equal(LibraryPathPartKind.Literal, result.Value.PathTemplate.Parts[0].Kind);
Assert.Equal("Various Artists", result.Value.PathTemplate.Parts[0].Representation);
@@ -59,6 +59,8 @@ public class LibraryMappingTests
Assert.False(result.IsLocked);
Assert.True(result.CanDownloadMetadataFromWeb);
Assert.False(result.ShouldSaveMetadataInMediaDirectories);
Assert.Equal(library.ConsumptionSettings.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
Assert.Equal(library.ConsumptionSettings.CompletionThresholdPercentage.HasValue ? library.ConsumptionSettings.CompletionThresholdPercentage.Value : null, result.CompletionThresholdPercentage);
Assert.Equal(library.ScanIds.Select(scanId => scanId.Value), result.LibraryScans.Select(libraryScan => libraryScan.Id));
}
@@ -31,7 +31,9 @@ public class UpdateLibraryRequestMappingTests
isLocked: false,
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false);
shouldSkipUnchangedDirectoriesDuringScan: false,
shouldTrackConsumptionProgress: true,
completionThresholdPercentage: 90);
// Act
UpdateLibraryCommand result = request.ToCommand();
@@ -47,6 +49,8 @@ public class UpdateLibraryRequestMappingTests
Assert.Equal(request.CanDownloadMetadataFromWeb, result.CanDownloadMetadataFromWeb);
Assert.Equal(request.ShouldSaveMetadataInMediaDirectories, result.ShouldSaveMetadataInMediaDirectories);
Assert.Equal(request.ShouldSkipUnchangedDirectoriesDuringScan, result.ShouldSkipUnchangedDirectoriesDuringScan);
Assert.Equal(request.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
Assert.Equal(request.CompletionThresholdPercentage, result.CompletionThresholdPercentage);
}
[Fact]
@@ -113,6 +113,49 @@ public class AddLibraryCommandHandlerTests
_mockDomainEventsQueue.Received(1).Enqueue(Arg.Any<LibrarySavedDomainEvent>());
}
[Fact]
public async Task HandleAsync_WhenThresholdIsSuppliedWhileEffectiveTrackingIsOff_ShouldReturnThresholdMustBeNullError()
{
// Arrange
AddLibraryCommand command = _addLibraryCommandFixture.Create(
libraryType: nameof(LibraryType.Music),
shouldTrackConsumptionProgress: null,
completionThresholdPercentage: 50);
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff, result.FirstError);
await _mockLibraryRepository.DidNotReceive().InsertAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenProgressTrackingIsEnabledWithoutThreshold_ShouldUseLibraryTypeDefaultThreshold()
{
// Arrange
AddLibraryCommand command = _addLibraryCommandFixture.Create(
libraryType: nameof(LibraryType.Book),
shouldTrackConsumptionProgress: true,
completionThresholdPercentage: null);
LibraryEntity persistedLibrary = _libraryEntityFixture.Create(userId: _userId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(persistedLibrary));
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
await _mockLibraryRepository.Received(1).InsertAsync(
Arg.Is<LibraryEntity>(library =>
library.ShouldTrackConsumptionProgress &&
library.CompletionThresholdPercentage == 100),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenProviderConfigurationSeedingFails_ShouldReturnErrorWithoutSaving()
{
@@ -118,6 +118,58 @@ public class UpdateLibraryCommandHandlerTests
_mockDomainEventsQueue.Received(1).Enqueue(Arg.Any<LibrarySavedDomainEvent>());
}
[Fact]
public async Task HandleAsync_WhenThresholdIsSuppliedWhileStoredTrackingIsOff_ShouldReturnThresholdMustBeNullError()
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create(
shouldTrackConsumptionProgress: null,
completionThresholdPercentage: 50);
LibraryEntity existingLibrary = _libraryEntityFixture.Create(
id: command.Id,
userId: command.OwnerId,
shouldTrackConsumptionProgress: false);
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.CompletionThresholdPercentageMustBeNullWhenProgressTrackingIsOff, result.FirstError);
await _mockLibraryRepository.DidNotReceive().UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenProgressTrackingIsEnabledWithoutThreshold_ShouldUseLibraryTypeDefaultThreshold()
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create(
libraryType: nameof(LibraryType.Book),
shouldTrackConsumptionProgress: true,
completionThresholdPercentage: null);
LibraryEntity existingLibrary = _libraryEntityFixture.Create(
id: command.Id,
userId: command.OwnerId,
libraryType: LibraryType.Music,
shouldTrackConsumptionProgress: false);
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
Result<LibraryResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
await _mockLibraryRepository.Received(1).UpdateAsync(
Arg.Is<LibraryEntity>(library =>
library.ShouldTrackConsumptionProgress &&
library.CompletionThresholdPercentage == 100),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenLibraryTypeIsUnchanged_ShouldNotReconcileProviderConfigurations()
{
@@ -60,6 +60,8 @@ public class LibraryConfigurationTests
IProperty libraryTypeProperty = entityType.GetProperty(nameof(LibraryEntity.LibraryType));
IProperty isEnabledProperty = entityType.GetProperty(nameof(LibraryEntity.IsEnabled));
IProperty downloadMetadataProperty = entityType.GetProperty(nameof(LibraryEntity.CanDownloadMetadataFromWeb));
IProperty shouldTrackConsumptionProgressProperty = entityType.GetProperty(nameof(LibraryEntity.ShouldTrackConsumptionProgress));
IProperty completionThresholdPercentageProperty = entityType.GetProperty(nameof(LibraryEntity.CompletionThresholdPercentage));
// Assert
Assert.False(titleProperty.IsNullable);
@@ -68,6 +70,8 @@ public class LibraryConfigurationTests
Assert.Equal("TEXT", libraryTypeProperty.GetColumnType());
Assert.Equal(true, isEnabledProperty.GetDefaultValue());
Assert.Equal(true, downloadMetadataProperty.GetDefaultValue());
Assert.Equal(false, shouldTrackConsumptionProgressProperty.GetDefaultValue());
Assert.True(completionThresholdPercentageProperty.IsNullable);
}
[Fact]
@@ -86,6 +90,21 @@ public class LibraryConfigurationTests
Assert.Equal(11, fingerprintProperty.GetColumnOrder());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureConsumptionSettingsColumnOrders()
{
// Arrange
IEntityType entityType = DesignTimeModel.FindEntityType(typeof(LibraryEntity))!;
// Act
IProperty shouldTrackProperty = entityType.GetProperty(nameof(LibraryEntity.ShouldTrackConsumptionProgress));
IProperty completionThresholdProperty = entityType.GetProperty(nameof(LibraryEntity.CompletionThresholdPercentage));
// Assert
Assert.Equal(16, shouldTrackProperty.GetColumnOrder());
Assert.Equal(17, completionThresholdProperty.GetColumnOrder());
}
[Fact]
public void Configure_WhenApplied_ShouldConfigureOwnedContentLocations()
{
@@ -28,6 +28,7 @@ public class LibraryTests
private readonly UserIdFixture _userIdFixture = new();
private readonly ScanIdFixture _scanIdFixture = new();
private readonly LibraryIdFixture _libraryIdFixture = new();
private readonly LibraryConsumptionSettingsFixture _libraryConsumptionSettingsFixture = new();
[Fact]
public void Create_WhenCalledWithValidData_ShouldCreateLibraryWithAllPropertiesSet()
@@ -39,6 +40,9 @@ public class LibraryTests
List<string> contentLocations = ["C:/Media/Books", "D:/Books"];
string coverImageSourcePath = "C:/Cover/cover.jpg";
List<ScanId> scanIds = [_scanIdFixture.Create(), _scanIdFixture.Create()];
LibraryConsumptionSettings consumptionSettings = _libraryConsumptionSettingsFixture.Create(
shouldTrackConsumptionProgress: true,
completionThresholdPercentage: 75);
// Act
Result<Library> result = Library.Create(
@@ -52,6 +56,7 @@ public class LibraryTests
canDownloadMetadataFromWeb: false,
shouldSaveMetadataInMediaDirectories: true,
shouldSkipUnchangedDirectoriesDuringScan: true,
consumptionSettings,
LibraryPathTemplate.Empty(),
scanIds);
@@ -68,6 +73,7 @@ public class LibraryTests
Assert.False(result.Value.CanDownloadMetadataFromWeb);
Assert.True(result.Value.ShouldSaveMetadataInMediaDirectories);
Assert.True(result.Value.ShouldSkipUnchangedDirectoriesDuringScan);
Assert.Equal(consumptionSettings, result.Value.ConsumptionSettings);
Assert.Equal(scanIds, result.Value.ScanIds);
}
@@ -91,6 +97,7 @@ public class LibraryTests
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false,
_libraryConsumptionSettingsFixture.Create(),
LibraryPathTemplate.Empty(),
scanIds);
@@ -118,6 +125,7 @@ public class LibraryTests
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false,
_libraryConsumptionSettingsFixture.Create(),
LibraryPathTemplate.Empty(),
[]);
@@ -154,6 +162,7 @@ public class LibraryTests
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false,
_libraryConsumptionSettingsFixture.Create(),
LibraryPathTemplate.Empty(),
[]);
@@ -182,6 +191,7 @@ public class LibraryTests
canDownloadMetadataFromWeb: true,
shouldSaveMetadataInMediaDirectories: false,
shouldSkipUnchangedDirectoriesDuringScan: false,
_libraryConsumptionSettingsFixture.Create(),
LibraryPathTemplate.Empty(),
[]);
@@ -105,7 +105,9 @@ public class AddLibraryEndpointTests
command.IsLocked == request.IsLocked &&
command.CanDownloadMetadataFromWeb == request.CanDownloadMetadataFromWeb &&
command.ShouldSaveMetadataInMediaDirectories == request.ShouldSaveMetadataInMediaDirectories &&
command.ShouldSkipUnchangedDirectoriesDuringScan == request.ShouldSkipUnchangedDirectoriesDuringScan),
command.ShouldSkipUnchangedDirectoriesDuringScan == request.ShouldSkipUnchangedDirectoriesDuringScan &&
command.ShouldTrackConsumptionProgress == request.ShouldTrackConsumptionProgress &&
command.CompletionThresholdPercentage == request.CompletionThresholdPercentage),
Arg.Is(cancellationToken));
}
@@ -107,7 +107,9 @@ public class UpdateLibraryEndpointTests
command.IsLocked == request.IsLocked &&
command.CanDownloadMetadataFromWeb == request.CanDownloadMetadataFromWeb &&
command.ShouldSaveMetadataInMediaDirectories == request.ShouldSaveMetadataInMediaDirectories &&
command.ShouldSkipUnchangedDirectoriesDuringScan == request.ShouldSkipUnchangedDirectoriesDuringScan),
command.ShouldSkipUnchangedDirectoriesDuringScan == request.ShouldSkipUnchangedDirectoriesDuringScan &&
command.ShouldTrackConsumptionProgress == request.ShouldTrackConsumptionProgress &&
command.CompletionThresholdPercentage == request.CompletionThresholdPercentage),
Arg.Is(cancellationToken));
}