mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 04:11:10 +00:00
Adds tests for the book reading feature
This commit is contained in:
1 parent
f7ccdf3d1c
commit
3a3c0c047c
192 files changed
+14104
-342
No files matched your search
+526
-13
@@ -1,14 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Application.Common.Infrastructure.Reading;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Infrastructure.Core.Plugins;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using Lumina.Plugins.Contracts.Core.Plugins;
|
||||
using Lumina.Plugins.Contracts.Core.Reading;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
@@ -30,9 +32,12 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly ILibraryMetadataProviderConfigurationRepository _mockMetadataConfigurationRepository;
|
||||
private readonly IArtworkProviderConfigurationRepository _mockArtworkConfigurationRepository;
|
||||
private readonly ILibraryBookReaderConfigurationRepository _mockBookReaderConfigurationRepository;
|
||||
private readonly IPluginManager _mockPluginManager;
|
||||
private readonly IBookReaderEnablementCache _mockEnablementCache;
|
||||
private readonly LibraryMetadataProviderConfigurationEntityFixture _metadataConfigurationFixture = new();
|
||||
private readonly LibraryArtworkProviderConfigurationEntityFixture _artworkConfigurationFixture = new();
|
||||
private readonly LibraryBookReaderConfigurationEntityFixture _bookReaderConfigurationFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaLibraryProviderConfigurationStoreTests"/> class.
|
||||
@@ -42,10 +47,13 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockMetadataConfigurationRepository = Substitute.For<ILibraryMetadataProviderConfigurationRepository>();
|
||||
_mockArtworkConfigurationRepository = Substitute.For<IArtworkProviderConfigurationRepository>();
|
||||
_mockBookReaderConfigurationRepository = Substitute.For<ILibraryBookReaderConfigurationRepository>();
|
||||
_mockUnitOfWork.LibraryMetadataProviderConfigurationRepository.Returns(_mockMetadataConfigurationRepository);
|
||||
_mockUnitOfWork.ArtworkProviderConfigurationRepository.Returns(_mockArtworkConfigurationRepository);
|
||||
_mockUnitOfWork.LibraryBookReaderConfigurationRepository.Returns(_mockBookReaderConfigurationRepository);
|
||||
_mockPluginManager = Substitute.For<IPluginManager>();
|
||||
_mockPluginManager.GetPlugins().Returns([]);
|
||||
_mockEnablementCache = Substitute.For<IBookReaderEnablementCache>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -77,8 +85,8 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
IPlugin betaPlugin = CreatePlugin(betaPluginId, "Beta");
|
||||
_mockPluginManager.GetPlugins().Returns([alphaPlugin, betaPlugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut(
|
||||
(alphaPluginId, CreateMetadataProvider(LibraryType.Book), null),
|
||||
(betaPluginId, CreateMetadataProvider(LibraryType.Book), CreateArtworkProvider(LibraryType.Book)));
|
||||
(alphaPluginId, CreateMetadataProvider(LibraryType.Book), null, null),
|
||||
(betaPluginId, CreateMetadataProvider(LibraryType.Book), CreateArtworkProvider(LibraryType.Book), null));
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
@@ -112,7 +120,7 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Book), null));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Book), null, null));
|
||||
LibraryMetadataProviderConfigurationEntity existingConfiguration = _metadataConfigurationFixture.Create(libraryId, pluginId, 4);
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([existingConfiguration]));
|
||||
@@ -133,7 +141,7 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Movie), null));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Movie), null, null));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureProviderConfigurationsAsync(libraryId, LibraryType.Book, CancellationToken.None);
|
||||
@@ -152,7 +160,7 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Book), null));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, CreateMetadataProvider(LibraryType.Book), null, null));
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to read configurations"));
|
||||
|
||||
@@ -174,13 +182,15 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
IPlugin betaPlugin = CreatePlugin(betaPluginId, "Beta");
|
||||
_mockPluginManager.GetPlugins().Returns([alphaPlugin, betaPlugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut(
|
||||
(alphaPluginId, CreateMetadataProvider(LibraryType.Book), null),
|
||||
(betaPluginId, CreateMetadataProvider(LibraryType.EBook), null));
|
||||
(alphaPluginId, CreateMetadataProvider(LibraryType.Book), null, null),
|
||||
(betaPluginId, CreateMetadataProvider(LibraryType.EBook), null, null));
|
||||
LibraryMetadataProviderConfigurationEntity existingConfiguration = _metadataConfigurationFixture.Create(libraryId, alphaPluginId, 1);
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([existingConfiguration]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([]));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockMetadataConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, Arg.Any<IEnumerable<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockMetadataConfigurationRepository.UpsertAsync(Arg.Any<LibraryMetadataProviderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
@@ -198,6 +208,7 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockMetadataConfigurationRepository.DidNotReceive().UpsertAsync(
|
||||
Arg.Is<LibraryMetadataProviderConfigurationEntity>(configuration => configuration.PluginId == alphaPluginId), Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -209,6 +220,8 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
.Returns(Result.Deleted);
|
||||
_mockArtworkConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockBookReaderConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
@@ -218,6 +231,8 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockMetadataConfigurationRepository.Received(1).DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>());
|
||||
await _mockArtworkConfigurationRepository.Received(1).DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.Received(1).DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.Received(1).InvalidateLibrary(libraryId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -229,6 +244,8 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
.Returns(Result.Deleted);
|
||||
_mockArtworkConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockBookReaderConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
@@ -238,24 +255,505 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockMetadataConfigurationRepository.Received(1).DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>());
|
||||
await _mockArtworkConfigurationRepository.Received(1).DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.Received(1).DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.Received(1).InvalidatePlugin(pluginId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveProviderConfigurationsForLibraryAsync_WhenDeletingMetadataFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
_mockMetadataConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveProviderConfigurationsForLibraryAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
await _mockArtworkConfigurationRepository.DidNotReceive().DeleteByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().DeleteByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.DidNotReceive().InvalidateLibrary(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveProviderConfigurationsForLibraryAsync_WhenDeletingArtworkFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
_mockMetadataConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockArtworkConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveProviderConfigurationsForLibraryAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().DeleteByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.DidNotReceive().InvalidateLibrary(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveProviderConfigurationsAsync_WhenDeletingMetadataFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_mockMetadataConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveProviderConfigurationsAsync(pluginId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
await _mockArtworkConfigurationRepository.DidNotReceive().DeleteByPluginIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().DeleteByPluginIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.DidNotReceive().InvalidatePlugin(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveProviderConfigurationsAsync_WhenDeletingArtworkFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_mockMetadataConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockArtworkConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveProviderConfigurationsAsync(pluginId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().DeleteByPluginIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.DidNotReceive().InvalidatePlugin(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenBookReaderSupportsTheLibraryType_ShouldAddDisabledConfiguration()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockBookReaderConfigurationRepository.UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.Received(1).UpsertAsync(
|
||||
Arg.Is<LibraryBookReaderConfigurationEntity>(configuration => configuration.PluginId == pluginId && !configuration.IsEnabled),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenConfigurationAlreadyExists_ShouldNotAddAnotherOne()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
LibraryBookReaderConfigurationEntity existingConfiguration = _bookReaderConfigurationFixture.Create(libraryId: libraryId, pluginId: pluginId);
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([existingConfiguration]));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenNoBookReaderSupportsTheLibraryType_ShouldNotAddConfiguration()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.Movie, ".epub")));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().GetByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenReaderDeclaresNoExtension_ShouldNotAddConfiguration()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, supportedExtension: null)));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenReadingConfigurationsFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to read configurations"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureBookReaderConfigurationsAsync_WhenUpsertFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockBookReaderConfigurationRepository.UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to persist the configuration"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileBookReaderConfigurationsAsync_WhenCalled_ShouldRemoveStaleAndAddTheSupportedBookReaders()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid alphaPluginId = Guid.NewGuid();
|
||||
Guid betaPluginId = Guid.NewGuid();
|
||||
IPlugin alphaPlugin = CreatePlugin(alphaPluginId, "Alpha Reader");
|
||||
IPlugin betaPlugin = CreatePlugin(betaPluginId, "Beta Reader");
|
||||
_mockPluginManager.GetPlugins().Returns([alphaPlugin, betaPlugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut(
|
||||
(alphaPluginId, null, null, CreateBookReader(LibraryType.Book, ".pdf")),
|
||||
(betaPluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
LibraryBookReaderConfigurationEntity existingConfiguration = _bookReaderConfigurationFixture.Create(libraryId: libraryId, pluginId: alphaPluginId);
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([existingConfiguration]));
|
||||
_mockBookReaderConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, Arg.Any<IEnumerable<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockBookReaderConfigurationRepository.UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.Received(1).DeleteByLibraryIdAndPluginIdsAsync(
|
||||
libraryId, Arg.Is<IEnumerable<Guid>>(pluginIds => pluginIds.SequenceEqual(new[] { alphaPluginId })), Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.Received(1).UpsertAsync(
|
||||
Arg.Is<LibraryBookReaderConfigurationEntity>(configuration => configuration.PluginId == betaPluginId && !configuration.IsEnabled),
|
||||
Arg.Any<CancellationToken>());
|
||||
await _mockBookReaderConfigurationRepository.DidNotReceive().UpsertAsync(
|
||||
Arg.Is<LibraryBookReaderConfigurationEntity>(configuration => configuration.PluginId == alphaPluginId), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileBookReaderConfigurationsAsync_WhenReadingConfigurationsFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to read configurations"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileBookReaderConfigurationsAsync_WhenDeletingStaleConfigurationsFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
Guid stalePluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
LibraryBookReaderConfigurationEntity staleConfiguration = _bookReaderConfigurationFixture.Create(libraryId: libraryId, pluginId: stalePluginId);
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([staleConfiguration]));
|
||||
_mockBookReaderConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, Arg.Any<IEnumerable<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileBookReaderConfigurationsAsync_WhenUpsertFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Reader Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, null, CreateBookReader(LibraryType.EBook, ".epub")));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockBookReaderConfigurationRepository.UpsertAsync(Arg.Any<LibraryBookReaderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to persist the configuration"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileBookReaderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveBookReaderConfigurationsForLibraryAsync_WhenCalled_ShouldDeleteTheConfigurationsOfTheLibrary()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
_mockBookReaderConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveBookReaderConfigurationsForLibraryAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.Received(1).DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.Received(1).InvalidateLibrary(libraryId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveBookReaderConfigurationsForLibraryAsync_WhenDeletionFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
_mockBookReaderConfigurationRepository.DeleteByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveBookReaderConfigurationsForLibraryAsync(libraryId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
_mockEnablementCache.DidNotReceive().InvalidateLibrary(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveBookReaderConfigurationsAsync_WhenCalled_ShouldDeleteTheConfigurationsOfThePlugin()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_mockBookReaderConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveBookReaderConfigurationsAsync(pluginId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockBookReaderConfigurationRepository.Received(1).DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>());
|
||||
_mockEnablementCache.Received(1).InvalidatePlugin(pluginId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RemoveBookReaderConfigurationsAsync_WhenDeletionFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_mockBookReaderConfigurationRepository.DeleteByPluginIdAsync(pluginId, Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await sut.RemoveBookReaderConfigurationsAsync(pluginId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
_mockEnablementCache.DidNotReceive().InvalidatePlugin(Arg.Any<Guid>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileProviderConfigurationsAsync_WhenArtworkConfigurationsAreStale_ShouldDeleteAndAddThem()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid alphaPluginId = Guid.NewGuid();
|
||||
Guid betaPluginId = Guid.NewGuid();
|
||||
IPlugin alphaPlugin = CreatePlugin(alphaPluginId, "Alpha");
|
||||
IPlugin betaPlugin = CreatePlugin(betaPluginId, "Beta");
|
||||
_mockPluginManager.GetPlugins().Returns([alphaPlugin, betaPlugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut(
|
||||
(alphaPluginId, null, CreateArtworkProvider(LibraryType.Book), null),
|
||||
(betaPluginId, null, CreateArtworkProvider(LibraryType.EBook), null));
|
||||
LibraryArtworkProviderConfigurationEntity existingArtworkConfiguration = _artworkConfigurationFixture.Create(libraryId, alphaPluginId, 1);
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([existingArtworkConfiguration]));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, Arg.Any<IEnumerable<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Deleted);
|
||||
_mockArtworkConfigurationRepository.UpsertAsync(Arg.Any<LibraryArtworkProviderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileProviderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockArtworkConfigurationRepository.Received(1).DeleteByLibraryIdAndPluginIdsAsync(
|
||||
libraryId, Arg.Is<IEnumerable<Guid>>(pluginIds => pluginIds.SequenceEqual(new[] { alphaPluginId })), Arg.Any<CancellationToken>());
|
||||
await _mockArtworkConfigurationRepository.Received(1).UpsertAsync(
|
||||
Arg.Is<LibraryArtworkProviderConfigurationEntity>(configuration => configuration.PluginId == betaPluginId && configuration.Rank == 2 && !configuration.IsEnabled),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReconcileProviderConfigurationsAsync_WhenDeletingStaleArtworkConfigurationsFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid alphaPluginId = Guid.NewGuid();
|
||||
IPlugin alphaPlugin = CreatePlugin(alphaPluginId, "Alpha");
|
||||
_mockPluginManager.GetPlugins().Returns([alphaPlugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((alphaPluginId, null, CreateArtworkProvider(LibraryType.Movie), null));
|
||||
LibraryArtworkProviderConfigurationEntity staleArtworkConfiguration = _artworkConfigurationFixture.Create(libraryId, alphaPluginId, 1);
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([staleArtworkConfiguration]));
|
||||
_mockArtworkConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, Arg.Any<IEnumerable<Guid>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to delete the configurations"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.ReconcileProviderConfigurationsAsync(libraryId, LibraryType.EBook, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureProviderConfigurationsAsync_WhenArtworkConfigurationAlreadyExists_ShouldNotAddAnotherOne()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, CreateArtworkProvider(LibraryType.Book), null));
|
||||
LibraryArtworkProviderConfigurationEntity existingConfiguration = _artworkConfigurationFixture.Create(libraryId, pluginId, 3);
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([existingConfiguration]));
|
||||
_mockBookReaderConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryBookReaderConfigurationEntity>>([]));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureProviderConfigurationsAsync(libraryId, LibraryType.Book, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockArtworkConfigurationRepository.DidNotReceive().UpsertAsync(Arg.Any<LibraryArtworkProviderConfigurationEntity>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsureProviderConfigurationsAsync_WhenArtworkUpsertFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin plugin = CreatePlugin(pluginId, "Plugin");
|
||||
_mockPluginManager.GetPlugins().Returns([plugin]);
|
||||
MediaLibraryProviderConfigurationStore sut = CreateSut((pluginId, null, CreateArtworkProvider(LibraryType.Book), null));
|
||||
_mockMetadataConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.GetByLibraryIdAsync(libraryId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([]));
|
||||
_mockArtworkConfigurationRepository.UpsertAsync(Arg.Any<LibraryArtworkProviderConfigurationEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure(description: "Failed to persist the configuration"));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await sut.EnsureProviderConfigurationsAsync(libraryId, LibraryType.Book, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates the store under test wired to the mocked dependencies, optionally registering the providers of the loaded plugins.
|
||||
/// Creates the store under test wired to the mocked dependencies, optionally registering the providers and book readers of the loaded plugins.
|
||||
/// </summary>
|
||||
/// <param name="pluginProviders">The providers registered by the loaded plugins, keyed by their plugin, when any.</param>
|
||||
/// <param name="pluginServices">The providers and book readers registered by the loaded plugins, keyed by their plugin, when any.</param>
|
||||
/// <returns>The created store.</returns>
|
||||
private MediaLibraryProviderConfigurationStore CreateSut(params (Guid pluginId, IMetadataProvider? metadataProvider, IArtworkProvider? artworkProvider)[] pluginProviders)
|
||||
private MediaLibraryProviderConfigurationStore CreateSut(params (Guid pluginId, IMetadataProvider? metadataProvider, IArtworkProvider? artworkProvider, IBookReader? bookReader)[] pluginServices)
|
||||
{
|
||||
ServiceCollection services = new();
|
||||
foreach ((Guid pluginId, IMetadataProvider? metadataProvider, IArtworkProvider? artworkProvider) in pluginProviders)
|
||||
foreach ((Guid pluginId, IMetadataProvider? metadataProvider, IArtworkProvider? artworkProvider, IBookReader? bookReader) in pluginServices)
|
||||
{
|
||||
if (metadataProvider is not null)
|
||||
services.AddKeyedSingleton<IMetadataProvider>(pluginId, (_, _) => metadataProvider);
|
||||
if (artworkProvider is not null)
|
||||
services.AddKeyedSingleton<IArtworkProvider>(pluginId, (_, _) => artworkProvider);
|
||||
if (bookReader is not null)
|
||||
services.AddKeyedSingleton<IBookReader>(pluginId, (_, _) => bookReader);
|
||||
}
|
||||
return new MediaLibraryProviderConfigurationStore(_mockUnitOfWork, _mockPluginManager, services.BuildServiceProvider());
|
||||
return new MediaLibraryProviderConfigurationStore(_mockUnitOfWork, _mockPluginManager, services.BuildServiceProvider(), _mockEnablementCache);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -295,4 +793,19 @@ public class MediaLibraryProviderConfigurationStoreTests
|
||||
provider.SupportedLibraryTypes.Returns([supportedLibraryType]);
|
||||
return provider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a mocked book reader supporting the provided library type and file extension.
|
||||
/// </summary>
|
||||
/// <param name="supportedLibraryType">The library type supported by the reader.</param>
|
||||
/// <param name="supportedExtension">The file extension supported by the reader, or <see langword="null"/> for none.</param>
|
||||
/// <returns>The created book reader mock.</returns>
|
||||
private static IBookReader CreateBookReader(LibraryType supportedLibraryType, string? supportedExtension = ".epub")
|
||||
{
|
||||
IBookReader reader = Substitute.For<IBookReader>();
|
||||
reader.SupportedLibraryTypes.Returns([supportedLibraryType]);
|
||||
reader.SupportedExtensions.Returns(supportedExtension is null ? [] : [supportedExtension]);
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -71,6 +71,8 @@ public class PluginDetectionSyncJobTests
|
||||
.Returns(Result.Deleted);
|
||||
_mockProviderConfigurationStore.EnsureProviderConfigurationsAsync(Arg.Any<Guid>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
_mockProviderConfigurationStore.EnsureBookReaderConfigurationsAsync(Arg.Any<Guid>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Success);
|
||||
_mockServiceProvider.GetService(typeof(IUnitOfWork)).Returns(_mockUnitOfWork);
|
||||
_mockServiceProvider.GetService(typeof(IMediaLibraryProviderConfigurationStore)).Returns(_mockProviderConfigurationStore);
|
||||
|
||||
@@ -222,6 +224,7 @@ public class PluginDetectionSyncJobTests
|
||||
|
||||
// Assert
|
||||
await _mockProviderConfigurationStore.Received(1).EnsureProviderConfigurationsAsync(bookLibrary.Id, LibraryType.Book, Arg.Any<CancellationToken>());
|
||||
await _mockProviderConfigurationStore.Received(1).EnsureBookReaderConfigurationsAsync(bookLibrary.Id, LibraryType.Book, Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,24 @@ public class PluginLoaderTests
|
||||
Assert.Contains("invalid.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsNativeLibrary_ShouldSkipItSilently()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// A native library (like pdfium.dll) is a valid PE image starting with the DOS signature, but it is not a managed assembly.
|
||||
temporaryPluginDirectory.CreateFile("native.dll", "MZ\u0001\u0000\u0000\u0000garbage native library content");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Empty(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsAssemblyWithoutPluginDescriptors_ShouldReturnNoPluginsAndNoErrors()
|
||||
{
|
||||
@@ -85,6 +103,104 @@ public class PluginLoaderTests
|
||||
Assert.Contains(services, descriptor => descriptor.ImplementationInstance as string == "plugin-services-registered");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsValidPluginAssembly_ShouldRetainTheLoadContextOfThePlugin()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Single(result.Plugins);
|
||||
// A collectible load context is only kept alive by a direct reference to the context object itself, so the context of every loaded plugin is retained
|
||||
// for the lifetime of the host, keeping the plugin assemblies from being unloaded by the garbage collector.
|
||||
Assert.Single(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsManagedDependencyAssemblyAlongsideAPlugin_ShouldSkipTheDependencyAndLoadOnlyThePlugin()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
temporaryPluginDirectory.CopyDll(GetTestPluginAssemblyPath());
|
||||
// A plugin ships its managed dependencies into the plugins directory (like PdfPig.dll and SkiaSharp.dll next to the PDF plugin). A managed dependency is a valid
|
||||
// assembly that does not reference the plugin contracts, so it is not a plugin and must be skipped without reflecting over its types, which would otherwise surface
|
||||
// spurious load errors for a file that is not a plugin.
|
||||
temporaryPluginDirectory.CopyDll(GetDependencyAssemblyPath());
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
IPlugin loadedPlugin = Assert.Single(result.Plugins);
|
||||
Assert.Equal(Guid.Parse("7B5A3E5D-9B7F-4A1E-8D9C-2F3A4B5C6D7E"), loadedPlugin.Id);
|
||||
Assert.Equal("Test Plugin", loadedPlugin.Name);
|
||||
Assert.Empty(result.Errors);
|
||||
Assert.Single(result.LoadContexts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsFileShorterThanDosSignature_ShouldRecordInvalidAssemblyError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// A single-character file is not long enough to carry the two-byte DOS signature "MZ", so it is not a PE image.
|
||||
temporaryPluginDirectory.CreateFile("tiny.dll", "M");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("tiny", error.AssemblyName);
|
||||
Assert.Contains("tiny.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsFileWithWrongDosSignature_ShouldRecordInvalidAssemblyError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
// The file starts with the wrong DOS signature, so it is not a PE image at all.
|
||||
temporaryPluginDirectory.CreateFile("notpe.dll", "XY\u0000\u0000\u0000some content that is not a PE image");
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("notpe", error.AssemblyName);
|
||||
Assert.Contains("notpe.dll", error.ErrorMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPlugins_WhenDirectoryContainsLockedNativeLibrary_ShouldNotThrowAndRecordError()
|
||||
{
|
||||
// Arrange
|
||||
using TemporaryPluginDirectory temporaryPluginDirectory = new();
|
||||
string nativePath = temporaryPluginDirectory.CreateFileReturningPath("locked.dll", "MZ\u0000\u0000\u0000locked native library content");
|
||||
using FileStream lockStream = new(nativePath, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
PluginLoadResultDto result = PluginLoader.LoadPlugins(temporaryPluginDirectory.Path, services);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result.Plugins);
|
||||
// The file cannot be read to verify its PE signature, so it is reported as not loadable rather than crashing the loader.
|
||||
PluginLoadErrorDto error = Assert.Single(result.Errors);
|
||||
Assert.Equal("locked", error.AssemblyName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of the <c>Lumina.TestPlugin</c> assembly from the test output directory.
|
||||
/// The assembly is referenced by the test project but no type from it is used, so it is not loaded
|
||||
@@ -96,6 +212,18 @@ public class PluginLoaderTests
|
||||
return Path.Combine(AppContext.BaseDirectory, "Lumina.TestPlugin.dll");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path of a managed dependency assembly from the test output directory, standing in for the managed dependencies
|
||||
/// a plugin ships next to itself (like PdfPig.dll and SkiaSharp.dll next to the PDF plugin). The <c>Microsoft.AspNetCore.SignalR.Core</c>
|
||||
/// assembly is used because it does not reference the plugin contracts assembly, and reflecting over its types inside a plugin load
|
||||
/// context throws, which reproduces the spurious load errors the loader used to record for dependency assemblies.
|
||||
/// </summary>
|
||||
/// <returns>The path of the managed dependency assembly.</returns>
|
||||
private static string GetDependencyAssemblyPath()
|
||||
{
|
||||
return Path.Combine(AppContext.BaseDirectory, "Microsoft.AspNetCore.SignalR.Core.dll");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test helper managing a temporary plugins directory, deleted when the test finishes.
|
||||
/// </summary>
|
||||
@@ -116,6 +244,13 @@ public class PluginLoaderTests
|
||||
File.WriteAllText(System.IO.Path.Combine(Path, fileName), content);
|
||||
}
|
||||
|
||||
public string CreateFileReturningPath(string fileName, string content)
|
||||
{
|
||||
string filePath = System.IO.Path.Combine(Path, fileName);
|
||||
File.WriteAllText(filePath, content);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void CopyDll(string sourceDllPath)
|
||||
{
|
||||
File.Copy(sourceDllPath, System.IO.Path.Combine(Path, System.IO.Path.GetFileName(sourceDllPath)));
|
||||
|
||||
@@ -5,7 +5,6 @@ using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Plugins;
|
||||
@@ -22,7 +21,7 @@ public class PluginManagerTests
|
||||
// Arrange
|
||||
IPlugin firstPlugin = CreatePlugin(Guid.NewGuid(), "First Plugin");
|
||||
IPlugin secondPlugin = CreatePlugin(Guid.NewGuid(), "Second Plugin");
|
||||
PluginManager sut = new([firstPlugin, secondPlugin]);
|
||||
PluginManager sut = new([firstPlugin, secondPlugin], []);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<IPlugin> result = sut.GetPlugins();
|
||||
@@ -37,7 +36,7 @@ public class PluginManagerTests
|
||||
public void GetPlugins_WhenNoPluginsWereLoaded_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
PluginManager sut = new([]);
|
||||
PluginManager sut = new([], []);
|
||||
|
||||
// Act
|
||||
IReadOnlyList<IPlugin> result = sut.GetPlugins();
|
||||
@@ -52,7 +51,7 @@ public class PluginManagerTests
|
||||
// Arrange
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
IPlugin expectedPlugin = CreatePlugin(pluginId, "Expected Plugin");
|
||||
PluginManager sut = new([expectedPlugin, CreatePlugin(Guid.NewGuid(), "Other Plugin")]);
|
||||
PluginManager sut = new([expectedPlugin, CreatePlugin(Guid.NewGuid(), "Other Plugin")], []);
|
||||
|
||||
// Act
|
||||
IPlugin? result = sut.GetPlugin(pluginId);
|
||||
@@ -65,7 +64,7 @@ public class PluginManagerTests
|
||||
public void GetPlugin_WhenPluginWithIdWasNotLoaded_ShouldReturnNull()
|
||||
{
|
||||
// Arrange
|
||||
PluginManager sut = new([CreatePlugin(Guid.NewGuid(), "Loaded Plugin")]);
|
||||
PluginManager sut = new([CreatePlugin(Guid.NewGuid(), "Loaded Plugin")], []);
|
||||
|
||||
// Act
|
||||
IPlugin? result = sut.GetPlugin(Guid.NewGuid());
|
||||
|
||||
Reference in new issue
Block a user