#region ========================================================================= USING ===================================================================================== using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading; using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary.Reading; using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; using Lumina.Plugins.Contracts.Core.Reading; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Plugins.Contracts.UnitTests.Core.Reading; /// /// Contains unit tests for the interface. /// [ExcludeFromCodeCoverage] public class IBookReaderGenericTests { private readonly ReadingDocumentDtoFixture _readingDocumentDtoFixture = new(); [Fact] public async Task OpenAsync_WhenCalled_ShouldForwardArgumentsToTheReaderImplementation() { // Arrange string path = "/books/test.epub"; string workingDirectory = "/tmp/reading-cache"; bool shouldRenderPdfAsImages = true; CancellationToken cancellationToken = CancellationToken.None; ReadingDocumentDto expectedDocument = _readingDocumentDtoFixture.Create(); TestBookReader reader = new(expectedDocument, [1, 2, 3]); // Act ReadingDocumentDto result = await reader.OpenAsync(path, workingDirectory, shouldRenderPdfAsImages, cancellationToken); // Assert Assert.Equal(expectedDocument, result); Assert.Equal(path, reader.ReceivedPath); Assert.Equal(workingDirectory, reader.ReceivedWorkingDirectory); Assert.Equal(shouldRenderPdfAsImages, reader.ReceivedShouldRenderPdfAsImages); Assert.Equal(cancellationToken, reader.ReceivedCancellationToken); } [Fact] public async Task GetResourceAsync_WhenCalled_ShouldForwardArgumentsToTheReaderImplementation() { // Arrange string path = "/books/test.epub"; string workingDirectory = "/tmp/reading-cache"; string resourceKey = "resources/cover.png"; CancellationToken cancellationToken = CancellationToken.None; byte[] expectedResource = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; TestBookReader reader = new(_readingDocumentDtoFixture.Create(), expectedResource); // Act byte[] result = await reader.GetResourceAsync(path, workingDirectory, resourceKey, cancellationToken); // Assert Assert.Equal(expectedResource, result); Assert.Equal(path, reader.ReceivedResourcePath); Assert.Equal(workingDirectory, reader.ReceivedResourceWorkingDirectory); Assert.Equal(resourceKey, reader.ReceivedResourceKey); Assert.Equal(cancellationToken, reader.ReceivedResourceCancellationToken); } /// /// Test double for the book reader interface that records the forwarded arguments. /// [ExcludeFromCodeCoverage] private sealed class TestBookReader : IBookReader { private readonly ReadingDocumentDto _document; private readonly byte[] _resource; /// /// Gets the path that was forwarded to the OpenAsync implementation. /// public string? ReceivedPath { get; private set; } /// /// Gets the working directory that was forwarded to the OpenAsync implementation. /// public string? ReceivedWorkingDirectory { get; private set; } /// /// Gets the rendering preference that was forwarded to the OpenAsync implementation. /// public bool ReceivedShouldRenderPdfAsImages { get; private set; } /// /// Gets the cancellation token that was forwarded to the OpenAsync implementation. /// public CancellationToken ReceivedCancellationToken { get; private set; } /// /// Gets the path that was forwarded to the GetResourceAsync implementation. /// public string? ReceivedResourcePath { get; private set; } /// /// Gets the working directory that was forwarded to the GetResourceAsync implementation. /// public string? ReceivedResourceWorkingDirectory { get; private set; } /// /// Gets the resource key that was forwarded to the GetResourceAsync implementation. /// public string? ReceivedResourceKey { get; private set; } /// /// Gets the cancellation token that was forwarded to the GetResourceAsync implementation. /// public CancellationToken ReceivedResourceCancellationToken { get; private set; } /// /// Initializes a new instance of the class. /// /// The document returned by the OpenAsync implementation. /// The resource data returned by the GetResourceAsync implementation. public TestBookReader(ReadingDocumentDto document, byte[] resource) { _document = document; _resource = resource; } /// public IReadOnlyList SupportedExtensions => [".epub"]; /// public IReadOnlyList SupportedLibraryTypes => [LibraryType.EBook]; /// public Task OpenAsync(string path, string workingDirectory, bool shouldRenderPdfAsImages, CancellationToken cancellationToken) { ReceivedPath = path; ReceivedWorkingDirectory = workingDirectory; ReceivedShouldRenderPdfAsImages = shouldRenderPdfAsImages; ReceivedCancellationToken = cancellationToken; return Task.FromResult(_document); } /// public Task GetResourceAsync(string path, string workingDirectory, string resourceKey, CancellationToken cancellationToken) { ReceivedResourcePath = path; ReceivedResourceWorkingDirectory = workingDirectory; ReceivedResourceKey = resourceKey; ReceivedResourceCancellationToken = cancellationToken; return Task.FromResult(_resource); } } }