Adds tests for the book reading feature

This commit is contained in:
BinaryAlley committed 2026-09-02 04:45:51 -07:00
1 parent f7ccdf3d1c
commit 3a3c0c047c
192 files changed
+14104 -342

No files matched your search

@@ -7,7 +7,7 @@ using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Artwork;
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
@@ -9,7 +9,7 @@ using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.File
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Artwork;
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
using Lumina.Infrastructure.Fixtures.Common.Setup;
using Microsoft.Extensions.Options;
using NSubstitute;
@@ -23,7 +23,7 @@ using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BooksLibrary.Artwork;
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Artwork;
/// <summary>
/// Contains unit tests for the <see cref="BookArtworkService"/> class.
@@ -0,0 +1,102 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
/// <summary>
/// Contains unit tests for the <see cref="BookReaderEnablementCache"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BookReaderEnablementCacheTests
{
private readonly BookReaderEnablementCache _sut = new();
[Fact]
public void Get_WhenNothingIsCached_ShouldReturnNull()
{
// Act
bool? result = _sut.Get(Guid.NewGuid(), Guid.NewGuid());
// Assert
Assert.Null(result);
}
[Theory]
[InlineData(true)] // reader enabled
[InlineData(false)] // reader disabled
public void Set_WhenCalled_ThenGetShouldReturnTheCachedValue(bool isEnabled)
{
// Arrange
Guid libraryId = Guid.NewGuid();
Guid pluginId = Guid.NewGuid();
// Act
_sut.Set(libraryId, pluginId, isEnabled);
// Assert
Assert.Equal(isEnabled, _sut.Get(libraryId, pluginId));
}
[Fact]
public void Invalidate_WhenCalled_ShouldRemoveOnlyTheGivenLibraryAndPluginPair()
{
// Arrange
Guid libraryId = Guid.NewGuid();
Guid firstPluginId = Guid.NewGuid();
Guid secondPluginId = Guid.NewGuid();
_sut.Set(libraryId, firstPluginId, isEnabled: true);
_sut.Set(libraryId, secondPluginId, isEnabled: false);
// Act
_sut.Invalidate(libraryId, firstPluginId);
// Assert
Assert.Null(_sut.Get(libraryId, firstPluginId));
Assert.False(_sut.Get(libraryId, secondPluginId));
}
[Fact]
public void InvalidateLibrary_WhenCalled_ShouldRemoveEveryEntryOfTheLibraryAndKeepTheOthers()
{
// Arrange
Guid libraryId = Guid.NewGuid();
Guid otherLibraryId = Guid.NewGuid();
Guid pluginId = Guid.NewGuid();
Guid otherPluginId = Guid.NewGuid();
_sut.Set(libraryId, pluginId, isEnabled: true);
_sut.Set(libraryId, otherPluginId, isEnabled: false);
_sut.Set(otherLibraryId, pluginId, isEnabled: true);
// Act
_sut.InvalidateLibrary(libraryId);
// Assert
Assert.Null(_sut.Get(libraryId, pluginId));
Assert.Null(_sut.Get(libraryId, otherPluginId));
Assert.True(_sut.Get(otherLibraryId, pluginId));
}
[Fact]
public void InvalidatePlugin_WhenCalled_ShouldRemoveEveryEntryOfThePluginAndKeepTheOthers()
{
// Arrange
Guid libraryId = Guid.NewGuid();
Guid otherLibraryId = Guid.NewGuid();
Guid pluginId = Guid.NewGuid();
Guid otherPluginId = Guid.NewGuid();
_sut.Set(libraryId, pluginId, isEnabled: true);
_sut.Set(otherLibraryId, pluginId, isEnabled: false);
_sut.Set(libraryId, otherPluginId, isEnabled: true);
// Act
_sut.InvalidatePlugin(pluginId);
// Assert
Assert.Null(_sut.Get(libraryId, pluginId));
Assert.Null(_sut.Get(otherLibraryId, pluginId));
Assert.True(_sut.Get(libraryId, otherPluginId));
}
}
@@ -0,0 +1,146 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Plugins;
using Lumina.Application.Common.Infrastructure.Reading;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Lumina.Plugins.Contracts.Core.Plugins;
using Lumina.Plugins.Contracts.Core.Reading;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
/// <summary>
/// Contains unit tests for the <see cref="BookReaderRegistry"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BookReaderRegistryTests
{
private readonly IPluginManager _mockPluginManager;
private readonly IServiceProvider _serviceProvider;
private readonly BookReaderRegistry _sut;
private readonly Guid _pluginId = Guid.NewGuid();
/// <summary>
/// Initializes a new instance of the <see cref="BookReaderRegistryTests"/> class.
/// </summary>
public BookReaderRegistryTests()
{
_mockPluginManager = Substitute.For<IPluginManager>();
_serviceProvider = new ServiceCollection().BuildServiceProvider();
_sut = new BookReaderRegistry(_mockPluginManager, _serviceProvider);
}
[Fact]
public void GetSupportedExtensionsByPluginId_WhenReadersAreRegistered_ShouldReturnExtensionsKeyedByPluginId()
{
// Arrange
IPlugin plugin = CreatePlugin(_pluginId, "Test Plugin");
IBookReader reader = CreateReader(".epub", ".EPUB");
_mockPluginManager.GetPlugins().Returns([plugin]);
ServiceProvider serviceProvider = BuildServiceProviderWithReaders(plugin.Id, reader);
// Act
IReadOnlyDictionary<Guid, IReadOnlyList<string>> result = new BookReaderRegistry(_mockPluginManager, serviceProvider).GetSupportedExtensionsByPluginId();
// Assert
IReadOnlyList<string> extensions = Assert.Single(result.Values);
Assert.Equal([".epub"], extensions);
}
[Fact]
public void GetSupportedExtensionsByPluginId_WhenReaderHasNoUsableExtensions_ShouldSkipThePlugin()
{
// Arrange
IPlugin plugin = CreatePlugin(_pluginId, "Test Plugin");
IBookReader reader = CreateReader(string.Empty);
_mockPluginManager.GetPlugins().Returns([plugin]);
ServiceProvider serviceProvider = BuildServiceProviderWithReaders(plugin.Id, reader);
// Act
IReadOnlyDictionary<Guid, IReadOnlyList<string>> result = new BookReaderRegistry(_mockPluginManager, serviceProvider).GetSupportedExtensionsByPluginId();
// Assert
Assert.Empty(result);
}
[Fact]
public void GetSupportedExtensionsByPluginId_WhenNoReadersAreRegistered_ShouldReturnEmptyDictionary()
{
// Arrange
_mockPluginManager.GetPlugins().Returns([]);
// Act
IReadOnlyDictionary<Guid, IReadOnlyList<string>> result = _sut.GetSupportedExtensionsByPluginId();
// Assert
Assert.Empty(result);
}
[Fact]
public void GetSupportedExtensionsByPluginId_WhenMultipleReadersOfOnePlugin_ShouldMergeAndDistinctExtensions()
{
// Arrange
IPlugin plugin = CreatePlugin(_pluginId, "Test Plugin");
IBookReader firstReader = CreateReader(".pdf");
IBookReader secondReader = CreateReader(".pdf", ".epub");
_mockPluginManager.GetPlugins().Returns([plugin]);
ServiceCollection services = new();
services.AddKeyedSingleton(plugin.Id, firstReader);
services.AddKeyedSingleton(plugin.Id, secondReader);
ServiceProvider serviceProvider = services.BuildServiceProvider();
// Act
IReadOnlyDictionary<Guid, IReadOnlyList<string>> result = new BookReaderRegistry(_mockPluginManager, serviceProvider).GetSupportedExtensionsByPluginId();
// Assert
IReadOnlyList<string> extensions = Assert.Single(result.Values);
Assert.Equal([".epub", ".pdf"], extensions);
}
/// <summary>
/// Creates a mocked plugin with the provided identity.
/// </summary>
/// <param name="id">The Id of the plugin.</param>
/// <param name="name">The name of the plugin.</param>
/// <returns>The created plugin mock.</returns>
private static IPlugin CreatePlugin(Guid id, string name)
{
IPlugin plugin = Substitute.For<IPlugin>();
plugin.Id.Returns(id);
plugin.Name.Returns(name);
return plugin;
}
/// <summary>
/// Creates a mocked book reader with the provided extensions.
/// </summary>
/// <param name="extensions">The supported extensions of the reader.</param>
/// <returns>The created reader mock.</returns>
private static IBookReader CreateReader(params string[] extensions)
{
IBookReader reader = Substitute.For<IBookReader>();
reader.SupportedExtensions.Returns(extensions);
reader.SupportedLibraryTypes.Returns([LibraryType.EBook]);
return reader;
}
/// <summary>
/// Builds a service provider in which the provided readers are registered as keyed services of the plugin.
/// </summary>
/// <param name="pluginId">The Id of the plugin providing the readers.</param>
/// <param name="readers">The readers to register.</param>
/// <returns>The built service provider.</returns>
private static ServiceProvider BuildServiceProviderWithReaders(Guid pluginId, params IBookReader[] readers)
{
ServiceCollection services = new();
foreach (IBookReader reader in readers)
services.AddKeyedSingleton<IBookReader>(pluginId, reader);
return services.BuildServiceProvider();
}
}
@@ -0,0 +1,816 @@
#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.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Lumina.Contracts.Responses.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Reading;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using Lumina.Plugins.Contracts.Core.Plugins;
using Lumina.Plugins.Contracts.Core.Reading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
/// <summary>
/// Contains unit tests for the <see cref="BookReadingService"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class BookReadingServiceTests : IDisposable
{
private readonly IPluginManager _mockPluginManager;
private readonly IPlugin _mockPlugin;
private readonly IBookReader _mockReader;
private readonly IUnitOfWork _mockUnitOfWork;
private readonly ILibraryBookReaderConfigurationRepository _mockConfigurationRepository;
private readonly IServiceProvider _serviceProvider;
private readonly BookReadingService _sut;
private readonly TestBookReaderEnablementCache _enablementCache = new();
private readonly Guid _pluginId = Guid.NewGuid();
private readonly Guid _bookId = Guid.NewGuid();
private readonly Guid _libraryId = Guid.NewGuid();
private readonly string _bookPath;
private readonly ReadingDocumentDtoFixture _readingDocumentDtoFixture = new();
private readonly ReadingSpineItemDtoFixture _readingSpineItemDtoFixture = new();
private readonly ReadingResourceInfoDtoFixture _readingResourceInfoDtoFixture = new();
private readonly LibraryBookReaderConfigurationEntityFixture _configurationEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="BookReadingServiceTests"/> class.
/// </summary>
public BookReadingServiceTests()
{
_mockReader = Substitute.For<IBookReader>();
_mockReader.SupportedExtensions.Returns([".epub"]);
_mockReader.SupportedLibraryTypes.Returns([LibraryType.EBook]);
_mockPlugin = Substitute.For<IPlugin>();
_mockPlugin.Id.Returns(_pluginId);
_mockPlugin.Name.Returns("Test Reader");
_mockPluginManager = Substitute.For<IPluginManager>();
_mockPluginManager.GetPlugins().Returns([_mockPlugin]);
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockConfigurationRepository = Substitute.For<ILibraryBookReaderConfigurationRepository>();
_mockUnitOfWork.LibraryBookReaderConfigurationRepository.Returns(_mockConfigurationRepository);
LibraryBookReaderConfigurationEntity enabledConfiguration = _configurationEntityFixture.Create(libraryId: _libraryId, pluginId: _pluginId, isEnabled: true);
_mockConfigurationRepository.GetByLibraryAndPluginIdAsync(_libraryId, _pluginId, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryBookReaderConfigurationEntity?>(enabledConfiguration));
ServiceCollection services = new();
services.AddKeyedSingleton(_pluginId, _mockReader);
services.AddSingleton(_mockUnitOfWork);
_serviceProvider = services.BuildServiceProvider();
IServiceScopeFactory serviceScopeFactory = _serviceProvider.GetRequiredService<IServiceScopeFactory>();
_bookPath = Path.Combine(Path.GetTempPath(), $"lumina-test-book-{Guid.NewGuid():N}.epub");
File.WriteAllText(_bookPath, "test epub content");
_sut = new BookReadingService(_serviceProvider, _mockPluginManager, _enablementCache, serviceScopeFactory, Substitute.For<ILogger<BookReadingService>>());
}
[Fact]
public async Task GetManifestAsync_WhenBookIsNotCached_ShouldExtractAndReturnTheManifest()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(document.Title, result.Value.Title);
Assert.Equal(document.Author, result.Value.Author);
await _mockReader.Received(1).OpenAsync(_bookPath, Arg.Any<string>(), Arg.Any<bool>(), cancellationToken);
}
[Fact]
public async Task GetManifestAsync_WhenFileIsUnchanged_ShouldReuseTheCachedDocument()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
Result<ReadingManifestResponse> secondResult = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
// Assert
Assert.False(secondResult.IsFailure);
await _mockReader.Received(1).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenFileChanged_ShouldReExtractTheBook()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
File.WriteAllText(_bookPath, "modified epub content, longer than before");
Result<ReadingManifestResponse> secondResult = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
// Assert
Assert.False(secondResult.IsFailure);
await _mockReader.Received(2).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenRenderingPreferenceDiffers_ShouldExtractTheBookTwice()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
Result<ReadingManifestResponse> secondResult = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: true, cancellationToken);
// Assert
Assert.False(secondResult.IsFailure);
await _mockReader.Received(2).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
await _mockReader.Received(1).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), false, Arg.Any<CancellationToken>());
await _mockReader.Received(1).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), true, Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenBothRenderingPreferencesAreExtracted_ShouldKeepTheirWorkingDirectoriesSeparate()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: true, cancellationToken);
// The text extraction must survive the image extraction of the same book, and still be served from the cache.
Result<ReadingManifestResponse> thirdResult = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, cancellationToken);
// Assert
Assert.False(thirdResult.IsFailure);
string textWorkingDirectory = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "text");
string imageWorkingDirectory = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "images");
Assert.True(File.Exists(Path.Combine(textWorkingDirectory, "sections", "0.html")));
Assert.True(File.Exists(Path.Combine(imageWorkingDirectory, "sections", "0.html")));
await _mockReader.Received(2).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenConcurrentRequests_ShouldShareASingleExtraction()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
TaskCompletionSource<bool> extractionStarted = new();
TaskCompletionSource<bool> allowExtractionToFinish = new();
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(callInfo => Task.Run(async () =>
{
extractionStarted.SetResult(true);
await allowExtractionToFinish.Task;
return document;
}));
// Act
Task<Result<ReadingManifestResponse>> firstTask = _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
await extractionStarted.Task;
Task<Result<ReadingManifestResponse>> secondTask = _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
allowExtractionToFinish.SetResult(true);
Result<ReadingManifestResponse> firstResult = await firstTask;
Result<ReadingManifestResponse> secondResult = await secondTask;
// Assert
Assert.False(firstResult.IsFailure);
Assert.False(secondResult.IsFailure);
await _mockReader.Received(1).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenNoReaderSupportsTheFormat_ShouldReturnNoReaderAvailableError()
{
// Arrange
_mockPluginManager.GetPlugins().Returns([]);
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.NoReaderAvailable, result.FirstError);
await _mockReader.DidNotReceive().OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenReaderIsDisabledForTheLibrary_ShouldReturnReaderDisabledError()
{
// Arrange
_mockConfigurationRepository.GetByLibraryAndPluginIdAsync(_libraryId, _pluginId, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryBookReaderConfigurationEntity?>(null));
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.ReaderDisabled, result.FirstError);
await _mockReader.DidNotReceive().OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetSectionAsync_WhenSectionExists_ShouldReturnSanitizedContent()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "section-1", shouldRenderPdfAsImages: false, shouldPreserveStyles: false, cancellationToken);
// Assert
Assert.False(result.IsFailure);
Assert.Equal("section-1", result.Value.LocationRef);
Assert.Contains("Chapter 1", result.Value.ContentHtml, StringComparison.Ordinal);
}
[Fact]
public async Task GetSectionAsync_WhenSectionDoesNotExist_ShouldReturnSectionNotFoundError()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "missing-section", shouldRenderPdfAsImages: false, shouldPreserveStyles: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.SectionNotFound, result.FirstError);
}
[Fact]
public async Task GetSectionAsync_WhenSectionFileDoesNotExist_ShouldReturnSectionNotFoundError()
{
// Arrange
ReadingDocumentDto document = _readingDocumentDtoFixture.Create(
title: "Test Book",
tableOfContents: [],
spine:
[
_readingSpineItemDtoFixture.Create(locationRef: "section-1", title: "Chapter 1", relativeSectionFilePath: "sections/missing.html")
],
resources: new Dictionary<string, ReadingResourceInfoDto>()
);
StubOpenAsyncToExtract(document);
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "section-1", shouldRenderPdfAsImages: false, shouldPreserveStyles: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.SectionNotFound, result.FirstError);
}
[Fact]
public async Task GetSectionAsync_WhenSectionPathEscapesTheWorkingDirectory_ShouldReturnSectionNotFoundError()
{
// Arrange
ReadingDocumentDto document = _readingDocumentDtoFixture.Create(
title: "Test Book",
tableOfContents: [],
spine:
[
_readingSpineItemDtoFixture.Create(locationRef: "section-1", title: "Chapter 1", relativeSectionFilePath: "../../escape.html")
],
resources: new Dictionary<string, ReadingResourceInfoDto>()
);
StubOpenAsyncToExtract(document);
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "section-1", shouldRenderPdfAsImages: false, shouldPreserveStyles: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.SectionNotFound, result.FirstError);
}
[Fact]
public async Task GetResourceAsync_WhenResourceDoesNotExistInTheDocument_ShouldReturnResourceNotFoundError()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "missing-key", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.ResourceNotFound, result.FirstError);
await _mockReader.DidNotReceive().GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenResourcePathEscapesTheWorkingDirectory_ShouldReturnResourceNotFoundError()
{
// Arrange
ReadingDocumentDto document = _readingDocumentDtoFixture.Create(
title: "Test Book",
tableOfContents: [],
spine: [],
resources: new Dictionary<string, ReadingResourceInfoDto>
{
["escaping"] = _readingResourceInfoDtoFixture.Create(relativeFilePath: "../../escape.png", mimeType: "image/png")
}
);
StubOpenAsyncToExtract(document);
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "escaping", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.ResourceNotFound, result.FirstError);
await _mockReader.DidNotReceive().GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenResourceFileAlreadyExists_ShouldServeItFromDiskWithoutCallingTheReader()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
string workingDirectory = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "text");
string resourceFilePath = Path.Combine(workingDirectory, "resources", "cover.png");
Directory.CreateDirectory(Path.GetDirectoryName(resourceFilePath)!);
File.WriteAllBytes(resourceFilePath, [1, 2, 3]);
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(new byte[] { 1, 2, 3 }, result.Value.Data);
Assert.Equal("image/png", result.Value.MimeType);
await _mockReader.DidNotReceive().GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenResourceFileDoesNotExist_ShouldProduceItAndCacheItToDisk()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
byte[] producedResource = [4, 5, 6];
_mockReader.GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), "cover", Arg.Any<CancellationToken>()).Returns(producedResource);
// Act
Result<ReadingResourceDataDto> firstResult = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
Result<ReadingResourceDataDto> secondResult = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.False(firstResult.IsFailure);
Assert.Equal(new byte[] { 4, 5, 6 }, firstResult.Value.Data);
Assert.Equal("image/png", firstResult.Value.MimeType);
Assert.False(secondResult.IsFailure);
Assert.Equal(new byte[] { 4, 5, 6 }, secondResult.Value.Data);
Assert.Equal("image/png", secondResult.Value.MimeType);
await _mockReader.Received(1).GetResourceAsync(_bookPath, Arg.Any<string>(), "cover", Arg.Any<CancellationToken>());
string cachedFilePath = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "text", "resources", "cover.png");
Assert.True(File.Exists(cachedFilePath));
Assert.Equal(new byte[] { 4, 5, 6 }, File.ReadAllBytes(cachedFilePath));
}
[Fact]
public async Task GetResourceAsync_WhenReaderIsDisabledForTheLibrary_ShouldReturnReaderDisabledError()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
_mockConfigurationRepository.GetByLibraryAndPluginIdAsync(_libraryId, _pluginId, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryBookReaderConfigurationEntity?>(null));
// The resource already exists on disk, from an earlier extraction, but the enablement is read on every request, so it must not be served.
string workingDirectory = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "text");
string resourceFilePath = Path.Combine(workingDirectory, "resources", "cover.png");
Directory.CreateDirectory(Path.GetDirectoryName(resourceFilePath)!);
File.WriteAllBytes(resourceFilePath, [1, 2, 3]);
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.ReaderDisabled, result.FirstError);
await _mockReader.DidNotReceive().GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenEnablementCacheSaysDisabledAndResourceFileAlreadyExists_ShouldNotServeTheResource()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
// The reader was disabled after the book was extracted, so the enablement cache already knows it; the request must be cut
// off before the already extracted resource on disk is considered, and without consulting the database again.
_enablementCache.Set(_libraryId, _pluginId, isEnabled: false);
string workingDirectory = Path.Combine(ReadingCachePaths.GetRootDirectory(), _bookId.ToString("N"), "text");
string resourceFilePath = Path.Combine(workingDirectory, "resources", "cover.png");
Directory.CreateDirectory(Path.GetDirectoryName(resourceFilePath)!);
File.WriteAllBytes(resourceFilePath, [1, 2, 3]);
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.ReaderDisabled, result.FirstError);
await _mockReader.DidNotReceive().GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockConfigurationRepository.DidNotReceive().GetByLibraryAndPluginIdAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenReaderThrowsWhileProducingTheResource_ShouldReturnResourceProductionFailedError()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
_mockReader.GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), "cover", Arg.Any<CancellationToken>())
.Returns(Task.FromException<byte[]>(new InvalidOperationException("The page renderer failed")));
// Act
Result<ReadingResourceDataDto> result = await _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal("Reading.ResourceProductionFailed", result.FirstError.Code);
await _mockReader.Received(1).GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), "cover", Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetResourceAsync_WhenReaderProductionIsCancelled_ShouldPropagateTheCancellation()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
_mockReader.GetResourceAsync(Arg.Any<string>(), Arg.Any<string>(), "cover", Arg.Any<CancellationToken>())
.Returns(Task.FromException<byte[]>(new OperationCanceledException()));
// Act
Task<Result<ReadingResourceDataDto>> operationTask = _sut.GetResourceAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "cover", shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
[Fact]
public async Task GetSectionAsync_WhenStylesArePreserved_ShouldKeepTheStyleAttributeInTheContent()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtractStyledContent(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "section-1", shouldRenderPdfAsImages: false, shouldPreserveStyles: true, cancellationToken);
// Assert
Assert.False(result.IsFailure);
Assert.Contains("style", result.Value.ContentHtml, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task GetSectionAsync_WhenStylesAreNotPreserved_ShouldStripTheStyleAttributeFromTheContent()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtractStyledContent(document);
CancellationToken cancellationToken = CancellationToken.None;
// Act
Result<ReadingSectionDto> result = await _sut.GetSectionAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, "section-1", shouldRenderPdfAsImages: false, shouldPreserveStyles: false, cancellationToken);
// Assert
Assert.False(result.IsFailure);
Assert.DoesNotContain("style", result.Value.ContentHtml, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("color:red", result.Value.ContentHtml, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Chapter 1", result.Value.ContentHtml, StringComparison.Ordinal);
}
[Fact]
public async Task GetAvailabilityAsync_WhenReaderIsAvailableAndEnabled_ShouldReportAvailable()
{
// Arrange
CancellationToken cancellationToken = CancellationToken.None;
// Act
Result<ReadingAvailabilityResponse> result = await _sut.GetAvailabilityAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, cancellationToken);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.IsAvailable);
Assert.Null(result.Value.ErrorCode);
await _mockReader.DidNotReceive().OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetAvailabilityAsync_WhenNoReaderSupportsTheFormat_ShouldReportUnavailableWithNoReaderAvailable()
{
// Arrange
_mockPluginManager.GetPlugins().Returns([]);
// Act
Result<ReadingAvailabilityResponse> result = await _sut.GetAvailabilityAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.IsAvailable);
Assert.Equal(nameof(Errors.Reading.NoReaderAvailable), result.Value.ErrorCode);
}
[Fact]
public async Task GetAvailabilityAsync_WhenReaderIsDisabled_ShouldReportUnavailableWithReaderDisabled()
{
// Arrange
_mockConfigurationRepository.GetByLibraryAndPluginIdAsync(_libraryId, _pluginId, Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryBookReaderConfigurationEntity?>(null));
// Act
Result<ReadingAvailabilityResponse> result = await _sut.GetAvailabilityAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.IsAvailable);
Assert.Equal(nameof(Errors.Reading.ReaderDisabled), result.Value.ErrorCode);
}
[Fact]
public async Task GetManifestAsync_WhenReaderOpenThrowsFileNotFoundException_ShouldReturnBookFileNotFoundError()
{
// Arrange
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<ReadingDocumentDto>(new FileNotFoundException("book missing")));
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.BookFileNotFound, result.FirstError);
}
[Fact]
public async Task GetManifestAsync_WhenReaderOpenThrowsDirectoryNotFoundException_ShouldReturnBookFileNotFoundError()
{
// Arrange
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<ReadingDocumentDto>(new DirectoryNotFoundException("directory missing")));
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.BookFileNotFound, result.FirstError);
}
[Fact]
public async Task GetManifestAsync_WhenReaderOpenThrowsOtherException_ShouldReturnExtractionFailedError()
{
// Arrange
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<ReadingDocumentDto>(new InvalidDataException("broken book")));
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal("Reading.ExtractionFailed", result.FirstError.Code);
}
[Fact]
public async Task GetManifestAsync_WhenReaderOpenIsCancelled_ShouldPropagateTheCancellation()
{
// Arrange
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<ReadingDocumentDto>(new OperationCanceledException()));
// Act
Task<Result<ReadingManifestResponse>> operationTask = _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => operationTask);
}
[Fact]
public async Task GetManifestAsync_WhenBookFileIsDeletedAfterExtraction_ShouldReExtractAndFail()
{
// Arrange
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException<ReadingDocumentDto>(new FileNotFoundException("book missing")));
File.Delete(_bookPath);
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.BookFileNotFound, result.FirstError);
await _mockReader.Received(2).OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenConfigurationsReadFails_ShouldReturnError()
{
// Arrange
_mockConfigurationRepository.GetByLibraryAndPluginIdAsync(_libraryId, _pluginId, Arg.Any<CancellationToken>())
.Returns(Error.Failure("Reading.Error", "Failed to read the configuration"));
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, _bookPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal("Reading.Error", result.FirstError.Code);
await _mockReader.DidNotReceive().OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task GetManifestAsync_WhenReaderMatchesExtensionCaseInsensitively_ShouldResolveIt()
{
// Arrange
string uppercaseExtensionPath = Path.Combine(Path.GetTempPath(), $"lumina-test-book-{Guid.NewGuid():N}.EPUB");
File.WriteAllText(uppercaseExtensionPath, "test epub content");
try
{
ReadingDocumentDto document = CreateExtractableDocument();
StubOpenAsyncToExtract(document);
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, uppercaseExtensionPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
}
finally
{
File.Delete(uppercaseExtensionPath);
}
}
[Fact]
public async Task GetManifestAsync_WhenReaderSupportsTheLibraryTypeButNotTheExtension_ShouldReturnNoReaderAvailableError()
{
// Arrange
string unsupportedExtensionPath = Path.Combine(Path.GetTempPath(), $"lumina-test-book-{Guid.NewGuid():N}.txt");
File.WriteAllText(unsupportedExtensionPath, "test content");
try
{
// Act
Result<ReadingManifestResponse> result = await _sut.GetManifestAsync(_bookId, _libraryId, unsupportedExtensionPath, LibraryType.EBook, shouldRenderPdfAsImages: false, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Reading.NoReaderAvailable, result.FirstError);
await _mockReader.DidNotReceive().OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
}
finally
{
File.Delete(unsupportedExtensionPath);
}
}
/// <summary>
/// Creates a reading document whose spine and resources are extractable into the working directory.
/// </summary>
/// <returns>The created reading document.</returns>
private ReadingDocumentDto CreateExtractableDocument()
{
return _readingDocumentDtoFixture.Create(
title: "Test Book",
author: "Test Author",
tableOfContents: [],
spine:
[
_readingSpineItemDtoFixture.Create(locationRef: "section-1", title: "Chapter 1", relativeSectionFilePath: "sections/0.html")
],
resources: new Dictionary<string, ReadingResourceInfoDto>
{
["cover"] = _readingResourceInfoDtoFixture.Create(relativeFilePath: "resources/cover.png", mimeType: "image/png")
},
hasTextContent: true
);
}
/// <summary>
/// Stubs the reader's OpenAsync to create the extractable files of the document into the working directory.
/// </summary>
/// <param name="document">The document returned by the extraction.</param>
private void StubOpenAsyncToExtract(ReadingDocumentDto document)
{
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(callInfo =>
{
string workingDirectory = callInfo.ArgAt<string>(1);
Directory.CreateDirectory(Path.Combine(workingDirectory, "sections"));
File.WriteAllText(Path.Combine(workingDirectory, "sections", "0.html"), "<section><h1>Chapter 1</h1><p>Some text.</p></section>");
return Task.FromResult(document);
});
}
/// <summary>
/// Stubs the reader's OpenAsync to create a section whose content carries a style attribute, so that the sanitization
/// preference of the service is observable.
/// </summary>
/// <param name="document">The document returned by the extraction.</param>
private void StubOpenAsyncToExtractStyledContent(ReadingDocumentDto document)
{
_mockReader.OpenAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(callInfo =>
{
string workingDirectory = callInfo.ArgAt<string>(1);
Directory.CreateDirectory(Path.Combine(workingDirectory, "sections"));
File.WriteAllText(Path.Combine(workingDirectory, "sections", "0.html"), "<section><h1>Chapter 1</h1><p style=\"color:red\">Some text.</p></section>");
return Task.FromResult(document);
});
}
/// <inheritdoc/>
public void Dispose()
{
if (Directory.Exists(ReadingCachePaths.GetRootDirectory()))
Directory.Delete(ReadingCachePaths.GetRootDirectory(), recursive: true);
if (File.Exists(_bookPath))
File.Delete(_bookPath);
(_serviceProvider as IDisposable)?.Dispose();
}
/// <summary>
/// In-memory test double for the <see cref="IBookReaderEnablementCache"/> used by the service under test.
/// </summary>
[ExcludeFromCodeCoverage]
private sealed class TestBookReaderEnablementCache : IBookReaderEnablementCache
{
private readonly Dictionary<(Guid LibraryId, Guid PluginId), bool> _enablements = [];
/// <inheritdoc/>
public bool? Get(Guid libraryId, Guid pluginId)
{
return _enablements.TryGetValue((libraryId, pluginId), out bool isEnabled) ? isEnabled : null;
}
/// <inheritdoc/>
public void Set(Guid libraryId, Guid pluginId, bool isEnabled)
{
_enablements[(libraryId, pluginId)] = isEnabled;
}
/// <inheritdoc/>
public void Invalidate(Guid libraryId, Guid pluginId)
{
_enablements.Remove((libraryId, pluginId));
}
/// <inheritdoc/>
public void InvalidateLibrary(Guid libraryId)
{
foreach (Guid cachedPluginId in _enablements.Keys.Where(cachedPair => cachedPair.LibraryId == libraryId).Select(cachedPair => cachedPair.PluginId).ToList())
_enablements.Remove((libraryId, cachedPluginId));
}
/// <inheritdoc/>
public void InvalidatePlugin(Guid pluginId)
{
foreach (Guid cachedLibraryId in _enablements.Keys.Where(cachedPair => cachedPair.PluginId == pluginId).Select(cachedPair => cachedPair.LibraryId).ToList())
_enablements.Remove((cachedLibraryId, pluginId));
}
}
}
@@ -0,0 +1,32 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
/// <summary>
/// Contains unit tests for the <see cref="ReadingCachePaths"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ReadingCachePathsTests
{
[Fact]
public void GetRootDirectory_WhenCalled_ShouldReturnBaseDirectoryWithReadingCacheDirectoryName()
{
// Act
string result = ReadingCachePaths.GetRootDirectory();
// Assert
Assert.Equal(Path.Combine(AppContext.BaseDirectory, ReadingCachePaths.DIRECTORY_NAME), result);
}
[Fact]
public void DirectoryName_WhenAccessed_ShouldReturnReadingCache()
{
// Assert
Assert.Equal("reading-cache", ReadingCachePaths.DIRECTORY_NAME);
}
}
@@ -0,0 +1,159 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Infrastructure.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading;
/// <summary>
/// Contains unit tests for the <see cref="ReadingContentSanitizer"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ReadingContentSanitizerTests
{
[Fact]
public void Sanitize_WhenHtmlContainsScript_ShouldStripTheScript()
{
// Arrange
string html = "<section><p>Hello</p><script>alert('xss')</script></section>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.DoesNotContain("script", result, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("alert", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenHtmlContainsInlineEventHandler_ShouldStripTheHandler()
{
// Arrange
string html = "<p onclick=\"alert('xss')\">Hello</p>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.DoesNotContain("onclick", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenHtmlContainsJavascriptUrl_ShouldStripTheReference()
{
// Arrange
string html = "<a href=\"javascript:alert('xss')\">Link</a>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.DoesNotContain("javascript", result, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void Sanitize_WhenHtmlContainsIframe_ShouldStripTheIframe()
{
// Arrange
string html = "<iframe src=\"https://evil.example\"></iframe><p>Hello</p>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.DoesNotContain("iframe", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenHtmlContainsDataLuminaResourceMarker_ShouldKeepTheMarker()
{
// Arrange
string html = $"<img {ReadingContentSanitizer.RESOURCE_ATTRIBUTE}=\"page:1\" alt=\"Page\" />";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.Contains(ReadingContentSanitizer.RESOURCE_ATTRIBUTE, result, StringComparison.Ordinal);
Assert.Contains("page:1", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenHtmlContainsPlainContent_ShouldKeepTheContent()
{
// Arrange
string html = "<section><h1>Chapter 1</h1><p>Some text.</p></section>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.Contains("Chapter 1", result, StringComparison.Ordinal);
Assert.Contains("Some text.", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenShouldPreserveStylesIsTrue_ShouldKeepTheStyleAttribute()
{
// Arrange
string html = "<p style=\"color:red\">Hello</p>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: true);
// Assert
Assert.Contains("style", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
}
[Fact]
public void Sanitize_WhenShouldPreserveStylesIsFalse_ShouldStripTheStyleAttribute()
{
// Arrange
string html = "<p style=\"color:red\">Hello</p>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles: false);
// Assert
Assert.DoesNotContain("style", result, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("color:red", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
}
[Theory]
[InlineData(true)] // preserve styles
[InlineData(false)] // strip styles
public void Sanitize_WhenHtmlContainsActiveContent_ShouldStripItRegardlessOfTheStylePreference(bool shouldPreserveStyles)
{
// Arrange
string html = "<section><p style=\"color:red\">Hello</p><script>alert('xss')</script><p onclick=\"alert('xss')\">World</p></section>";
// Act
string result = ReadingContentSanitizer.Sanitize(html, shouldPreserveStyles);
// Assert
Assert.DoesNotContain("script", result, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("alert", result, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("onclick", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Hello", result, StringComparison.Ordinal);
Assert.Contains("World", result, StringComparison.Ordinal);
}
[Theory]
[InlineData(null)] // null input
[InlineData("")] // empty input
[InlineData(" ")] // whitespace input
public void Sanitize_WhenInputIsBlank_ShouldReturnEmptyString(string? html)
{
// Act
string result = ReadingContentSanitizer.Sanitize(html!, shouldPreserveStyles: false);
// Assert
Assert.Equal(string.Empty, result);
}
}