#region ========================================================================= USING =====================================================================================
using Lumina.Plugins.Calibre.Common.Models.DTO.Opf;
using Lumina.Plugins.Calibre.Core.Opf;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
#endregion
namespace Lumina.Plugins.Calibre.UnitTests.Core.Opf;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class OpfReaderTests
{
[Fact]
public void Read_WhenOpfFileContainsAllSupportedFields_ShouldParseThemAll()
{
// Arrange
string opfFilePath = CreateOpfFile("""Test Book TitleTest AuthorSecond Creatorcalibre (3.48.0) [https://calibre-ebook.com]Test Translator<p>A test description with <b>markup</b>.</p>Test Publisher2015-06-15en978-0-306-40615-7123456B00TEST1test-lccnocm12345678OL100Mgoogle-idScience fictionSpace opera""");
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Equal("Test Book Title", result.Title);
Assert.Equal("
A test description with markup.
", result.Description);
Assert.Equal("Test Publisher", result.Publisher);
Assert.Equal(2015, result.PublishDate!.Value.Year);
Assert.Equal(6, result.PublishDate.Value.Month);
Assert.Equal(15, result.PublishDate.Value.Day);
Assert.Equal("en", result.LanguageCode);
Assert.Equal("The Series", result.Series);
Assert.Equal(2.5, result.SeriesIndex);
Assert.Equal(8, result.Rating);
Assert.Equal("cover.jpg", result.CoverHref);
Assert.Equal(2, result.Creators.Count);
Assert.Equal("Test Author", result.Creators[0].Name);
Assert.Equal("aut", result.Creators[0].Role);
Assert.Equal(2, result.Contributors.Count);
Assert.Equal("calibre (3.48.0) [https://calibre-ebook.com]", result.Contributors[0].Name);
Assert.Equal("bkp", result.Contributors[0].Role);
Assert.Equal(7, result.Identifiers.Count);
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ISBN" && identifier.Value == "978-0-306-40615-7");
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "goodreads" && identifier.Value == "123456");
Assert.Contains(result.Identifiers, identifier => identifier.Scheme == "ASIN" && identifier.Value == "B00TEST1");
Assert.Equal(2, result.Subjects.Count);
Assert.Equal("Science fiction", result.Subjects[0]);
Assert.Equal("Space opera", result.Subjects[1]);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenOpfFileDoesNotExist_ShouldReturnEmptyDocument()
{
// Act
OpfDocumentDto result = OpfReader.Read(Path.Combine(Path.GetTempPath(), $"missing-{Guid.NewGuid():N}.opf"));
// Assert
Assert.Null(result.Title);
Assert.Empty(result.Identifiers);
Assert.Empty(result.Creators);
Assert.Empty(result.Subjects);
}
[Fact]
public void Read_WhenMetadataContainsWhitespaceOnlyValues_ShouldSkipThem()
{
// Arrange
string opfFilePath = CreateOpfFile(""" """);
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
Assert.Empty(result.Creators);
Assert.Empty(result.Subjects);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenMetadataElementIsMissing_ShouldReturnEmptyDocument()
{
// Arrange
string opfFilePath = CreateOpfFile("""""");
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
Assert.Empty(result.Identifiers);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenOpfFileDeclaresADoctypeWithEntities_ShouldNotExpandThem()
{
// Arrange
// the DTD is prohibited, so the document must not be parsed and the entity must not be expanded into the title
string opfFilePath = CreateOpfFile("""]>&xxe;""");
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenOpfFileContainsBillionLaughsStyleEntityExpansion_ShouldNotExpandThem()
{
// Arrange
string opfFilePath = CreateOpfFile("""]>&lol3;""");
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenOpfFileIsOversized_ShouldReturnEmptyDocument()
{
// Arrange
string opfFilePath = Path.Combine(Path.GetTempPath(), $"oversized-{Guid.NewGuid():N}.opf");
string oversizedContent = $"""{new string('a', (5 * 1024 * 1024) + 100)}""";
File.WriteAllText(opfFilePath, oversizedContent);
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
Assert.Empty(result.Identifiers);
}
finally
{
File.Delete(opfFilePath);
}
}
[Fact]
public void Read_WhenOpfFileIsMalformedXml_ShouldReturnEmptyDocument()
{
// Arrange
string opfFilePath = CreateOpfFile("""Unclosed""");
try
{
// Act
OpfDocumentDto result = OpfReader.Read(opfFilePath);
// Assert
Assert.Null(result.Title);
Assert.Empty(result.Identifiers);
}
finally
{
File.Delete(opfFilePath);
}
}
///
/// Creates a temporary OPF file with the given content and returns its path.
///
/// The content of the OPF file to create.
/// The file system path of the created OPF file.
private static string CreateOpfFile(string content)
{
string opfFilePath = Path.Combine(Path.GetTempPath(), $"opf-{Guid.NewGuid():N}.opf");
File.WriteAllText(opfFilePath, content);
return opfFilePath;
}
}