#region ========================================================================= USING =====================================================================================
using Nemera.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Nemera.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Nemera.Plugins.Calibre.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Nemera.Plugins.Calibre.IntegrationTests.Core;
///
/// Contains integration tests for the class.
///
[ExcludeFromCodeCoverage]
public class CalibreBookMetadataProviderTests
{
private readonly BookMetadataLookupDtoFixture _bookMetadataLookupDtoFixture = new();
private readonly CalibreBookMetadataProvider _sut = new();
private const string OPF_DOCUMENT = """Test Book TitleTest Author""";
[Fact]
public async Task GetMetadataAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnTheMappedMetadata()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
// Assert
Assert.NotNull(result);
Assert.Equal("Test Book Title", result!.Metadata!.Title);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetMetadataAsync_WhenOpfFileDoesNotExist_ShouldReturnNull()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
BookMetadataDto? result = await _sut.GetMetadataAsync(lookup, CancellationToken.None);
// Assert
Assert.Null(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetSearchResultsAsync_WhenOpfFileExistsNextToTheBook_ShouldReturnASingleMetadataCandidate()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
File.WriteAllText(Path.Combine(bookDirectory, "metadata.opf"), OPF_DOCUMENT);
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
// Assert
BookMetadataDto metadata = Assert.IsType(Assert.Single(result));
Assert.Equal("Test Book Title", metadata.Metadata!.Title);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
[Fact]
public async Task GetSearchResultsAsync_WhenOpfFileDoesNotExist_ShouldReturnAnEmptyList()
{
// Arrange
string bookDirectory = CreateTempDirectory();
try
{
string bookPath = Path.Combine(bookDirectory, "book.epub");
BookMetadataLookupDto lookup = _bookMetadataLookupDtoFixture.Create(path: bookPath);
// Act
IReadOnlyList result = await _sut.GetSearchResultsAsync(lookup, CancellationToken.None);
// Assert
Assert.Empty(result);
}
finally
{
Directory.Delete(bookDirectory, true);
}
}
///
/// Creates a unique temporary directory for the book.
///
/// The file system path of the created temporary directory.
private static string CreateTempDirectory()
{
string directoryPath = Path.Combine(Path.GetTempPath(), $"nemera-calibre-metadata-{Guid.NewGuid():N}");
Directory.CreateDirectory(directoryPath);
return directoryPath;
}
}