Merge branch 'fix_epub_author_parsing' into render-rewrite
This commit is contained in:
6 files changed
+1005
No files matched your search
@@ -225,6 +225,7 @@ src/
|
||||
Book.h/cpp
|
||||
formats/
|
||||
AsyncImageResponse.h/cpp
|
||||
AuthorAttribution.h/cpp
|
||||
EntryImageProvider.h/cpp
|
||||
EpubReader.h/cpp
|
||||
FileIconProvider.h/cpp
|
||||
|
||||
@@ -296,6 +296,10 @@ add_asteria_test(WebImageProviderTest tests/formats/WebImageProviderTest.cpp
|
||||
target_link_libraries(WebImageProviderTest PRIVATE Qt6::Quick Qt6::Gui Qt6::Network)
|
||||
set_tests_properties(WebImageProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||
|
||||
add_asteria_test(AuthorAttributionTest tests/formats/AuthorAttributionTest.cpp
|
||||
src/backend/formats/AuthorAttribution.cpp)
|
||||
set_tests_properties(AuthorAttributionTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||
|
||||
add_asteria_test(EpubReaderTest tests/formats/EpubReaderTest.cpp
|
||||
src/backend/formats/AsyncImageResponse.cpp
|
||||
src/backend/formats/ImageUrl.cpp
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
-- allium: 3
|
||||
-- book-author-parsing.allium
|
||||
|
||||
-- Scope: Construction of book author attribution string from EPUB and FB2 metadata in Asteria
|
||||
-- Includes:
|
||||
-- - Extraction of Dublin Core dc:creator elements with role attributes
|
||||
-- - Fallback to structured author elements (first-name, middle-name, last-name, nickname)
|
||||
-- - MARC role mapping (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized)
|
||||
-- - Role grouping in document order
|
||||
-- - Deduplication of creator names within each role group
|
||||
-- - Formatted output string representation ("Author: Bob, Charlie, Editor: Alice")
|
||||
-- Excludes:
|
||||
-- - Low-level XML streaming and archive decompression
|
||||
-- - UI rendering and line-breaking
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Value Types
|
||||
------------------------------------------------------------
|
||||
|
||||
value StructuredAuthor {
|
||||
first_name: String?
|
||||
middle_name: String?
|
||||
last_name: String?
|
||||
nickname: String?
|
||||
}
|
||||
|
||||
value CreatorItem {
|
||||
role_code: String?
|
||||
name: String
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Contracts
|
||||
------------------------------------------------------------
|
||||
|
||||
contract AuthorAttributionService {
|
||||
resolve_attribution: (file_path: String) -> String
|
||||
|
||||
@invariant Precedence
|
||||
-- If Dublin Core creators are present, they take precedence;
|
||||
-- structured author elements are used only as a fallback when DC creators are absent.
|
||||
|
||||
@invariant RoleMapping
|
||||
-- Roles "aut", empty, or unspecified map to "Author".
|
||||
-- Standard MARC codes map to human-readable names:
|
||||
-- "edt" -> "Editor", "trl" -> "Translator", "ill" -> "Illustrator".
|
||||
-- Unrecognized or custom roles are capitalized as-is.
|
||||
|
||||
@invariant GroupingAndDeduplication
|
||||
-- Creators are grouped by resolved role in order of first appearance.
|
||||
-- Identical names within the same role are deduplicated, preserving first appearance.
|
||||
-- Each role group is formatted as "<Role>: <Name1>, <Name2>".
|
||||
-- Role groups are joined with ", ".
|
||||
|
||||
@invariant EmptyFallback
|
||||
-- If no valid creator or structured author elements exist, the attribution is empty string.
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Entities and Variants
|
||||
------------------------------------------------------------
|
||||
|
||||
entity Book {
|
||||
file_path: String
|
||||
is_active: Boolean
|
||||
}
|
||||
|
||||
entity BookMetadata {
|
||||
book: Book
|
||||
status: unparsed | parsed
|
||||
|
||||
transitions status {
|
||||
unparsed -> parsed
|
||||
terminal: parsed
|
||||
}
|
||||
|
||||
raw_dc_creators: List<CreatorItem>
|
||||
raw_structured_authors: List<StructuredAuthor>
|
||||
author_attribution: String when status = parsed
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Rules
|
||||
------------------------------------------------------------
|
||||
|
||||
rule LoadMetadata {
|
||||
when: LoadMetadataTrigger(book, creators, authors)
|
||||
ensures: BookMetadata.created(
|
||||
book: book,
|
||||
status: unparsed,
|
||||
raw_dc_creators: creators,
|
||||
raw_structured_authors: authors
|
||||
)
|
||||
}
|
||||
|
||||
rule ParseMetadataWithDcCreators {
|
||||
when: ParseMetadata(metadata)
|
||||
requires: metadata.status = unparsed
|
||||
requires: metadata.raw_dc_creators.count > 0
|
||||
ensures:
|
||||
metadata.status = parsed
|
||||
metadata.author_attribution = format_creators(metadata.raw_dc_creators)
|
||||
}
|
||||
|
||||
rule ParseMetadataFallbackToStructuredAuthors {
|
||||
when: ParseMetadata(metadata)
|
||||
requires: metadata.status = unparsed
|
||||
requires: metadata.raw_dc_creators.count = 0
|
||||
ensures:
|
||||
metadata.status = parsed
|
||||
metadata.author_attribution = format_structured_authors(metadata.raw_structured_authors)
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Invariants
|
||||
------------------------------------------------------------
|
||||
|
||||
invariant FormattedAttributionRequiresParsedState {
|
||||
for metadata in BookMetadatas where status = parsed:
|
||||
metadata.author_attribution != null
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Actor Declarations
|
||||
------------------------------------------------------------
|
||||
|
||||
actor ReaderEngine {
|
||||
identified_by: Book where is_active = true
|
||||
}
|
||||
|
||||
------------------------------------------------------------
|
||||
-- Surfaces
|
||||
------------------------------------------------------------
|
||||
|
||||
surface BookMetadataSurface {
|
||||
facing engine: ReaderEngine
|
||||
context metadata: BookMetadata
|
||||
|
||||
contracts:
|
||||
fulfils AuthorAttributionService
|
||||
|
||||
exposes:
|
||||
metadata.book.file_path
|
||||
metadata.author_attribution when metadata.status = parsed
|
||||
|
||||
provides:
|
||||
LoadMetadataTrigger(metadata.book, metadata.raw_dc_creators, metadata.raw_structured_authors)
|
||||
when engine.is_active
|
||||
ParseMetadata(metadata)
|
||||
when metadata.status = unparsed and engine.is_active
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
#include "AuthorAttribution.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QStringList>
|
||||
|
||||
QString AuthorAttribution::mapRole(const std::optional<QString> &roleCode)
|
||||
{
|
||||
if (!roleCode.has_value()) {
|
||||
return QStringLiteral("Author");
|
||||
}
|
||||
|
||||
const QString trimmed = roleCode->trimmed();
|
||||
if (trimmed.isEmpty() || trimmed.compare(QLatin1String("aut"), Qt::CaseInsensitive) == 0) {
|
||||
return QStringLiteral("Author");
|
||||
}
|
||||
if (trimmed.compare(QLatin1String("edt"), Qt::CaseInsensitive) == 0) {
|
||||
return QStringLiteral("Editor");
|
||||
}
|
||||
if (trimmed.compare(QLatin1String("trl"), Qt::CaseInsensitive) == 0) {
|
||||
return QStringLiteral("Translator");
|
||||
}
|
||||
if (trimmed.compare(QLatin1String("ill"), Qt::CaseInsensitive) == 0) {
|
||||
return QStringLiteral("Illustrator");
|
||||
}
|
||||
|
||||
// Unrecognized or custom roles are capitalized as-is
|
||||
QString capitalized = trimmed;
|
||||
if (!capitalized.isEmpty() && capitalized.at(0).isLower()) {
|
||||
capitalized[0] = capitalized.at(0).toUpper();
|
||||
}
|
||||
return capitalized;
|
||||
}
|
||||
|
||||
QString AuthorAttribution::formatCreators(const QList<CreatorItem> &creators)
|
||||
{
|
||||
QList<QString> roleOrder;
|
||||
QHash<QString, QStringList> roleToNames;
|
||||
|
||||
for (const auto &creator : creators) {
|
||||
const QString trimmedName = creator.name.trimmed();
|
||||
if (trimmedName.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QString role = mapRole(creator.roleCode);
|
||||
if (!roleToNames.contains(role)) {
|
||||
roleOrder.append(role);
|
||||
}
|
||||
|
||||
QStringList &names = roleToNames[role];
|
||||
if (!names.contains(trimmedName)) {
|
||||
names.append(trimmedName);
|
||||
}
|
||||
}
|
||||
|
||||
if (roleOrder.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
QStringList formattedGroups;
|
||||
formattedGroups.reserve(roleOrder.size());
|
||||
for (const auto &role : roleOrder) {
|
||||
formattedGroups.append(QStringLiteral("%1: %2").arg(role, roleToNames[role].join(QStringLiteral(", "))));
|
||||
}
|
||||
|
||||
return formattedGroups.join(QStringLiteral(", "));
|
||||
}
|
||||
|
||||
QString AuthorAttribution::formatStructuredAuthors(const QList<StructuredAuthor> &authors)
|
||||
{
|
||||
QStringList authorList;
|
||||
|
||||
for (const auto &author : authors) {
|
||||
QStringList nameParts;
|
||||
if (author.firstName.has_value() && !author.firstName->trimmed().isEmpty()) {
|
||||
nameParts.append(author.firstName->trimmed());
|
||||
}
|
||||
if (author.middleName.has_value() && !author.middleName->trimmed().isEmpty()) {
|
||||
nameParts.append(author.middleName->trimmed());
|
||||
}
|
||||
if (author.lastName.has_value() && !author.lastName->trimmed().isEmpty()) {
|
||||
nameParts.append(author.lastName->trimmed());
|
||||
}
|
||||
|
||||
QString authorName;
|
||||
if (!nameParts.isEmpty()) {
|
||||
authorName = nameParts.join(QLatin1Char(' '));
|
||||
} else if (author.nickname.has_value() && !author.nickname->trimmed().isEmpty()) {
|
||||
authorName = author.nickname->trimmed();
|
||||
}
|
||||
|
||||
if (!authorName.isEmpty() && !authorList.contains(authorName)) {
|
||||
authorList.append(authorName);
|
||||
}
|
||||
}
|
||||
|
||||
return authorList.join(QStringLiteral(", "));
|
||||
}
|
||||
|
||||
BookMetadata AuthorAttribution::loadMetadata(const BookEntity &book,
|
||||
const QList<CreatorItem> &creators,
|
||||
const QList<StructuredAuthor> &authors)
|
||||
{
|
||||
BookMetadata metadata;
|
||||
metadata.book = book;
|
||||
metadata.status = MetadataStatus::Unparsed;
|
||||
metadata.rawDcCreators = creators;
|
||||
metadata.rawStructuredAuthors = authors;
|
||||
metadata.authorAttribution = std::nullopt;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
bool AuthorAttribution::parseMetadataWithDcCreators(BookMetadata &metadata)
|
||||
{
|
||||
if (metadata.status != MetadataStatus::Unparsed) {
|
||||
return false;
|
||||
}
|
||||
if (metadata.rawDcCreators.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
metadata.status = MetadataStatus::Parsed;
|
||||
metadata.authorAttribution = formatCreators(metadata.rawDcCreators);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AuthorAttribution::parseMetadataFallbackToStructuredAuthors(BookMetadata &metadata)
|
||||
{
|
||||
if (metadata.status != MetadataStatus::Unparsed) {
|
||||
return false;
|
||||
}
|
||||
if (!metadata.rawDcCreators.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
metadata.status = MetadataStatus::Parsed;
|
||||
metadata.authorAttribution = formatStructuredAuthors(metadata.rawStructuredAuthors);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AuthorAttribution::parseMetadata(BookMetadata &metadata)
|
||||
{
|
||||
if (metadata.status != MetadataStatus::Unparsed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!metadata.rawDcCreators.isEmpty()) {
|
||||
return parseMetadataWithDcCreators(metadata);
|
||||
}
|
||||
return parseMetadataFallbackToStructuredAuthors(metadata);
|
||||
}
|
||||
|
||||
BookMetadataSurface::BookMetadataSurface(const BookMetadata &metadata, bool engineActive)
|
||||
: m_metadata(metadata), m_engineActive(engineActive)
|
||||
{
|
||||
}
|
||||
|
||||
bool BookMetadataSurface::isAccessible() const
|
||||
{
|
||||
return m_engineActive;
|
||||
}
|
||||
|
||||
QString BookMetadataSurface::filePath() const
|
||||
{
|
||||
return m_metadata.book.filePath;
|
||||
}
|
||||
|
||||
std::optional<QString> BookMetadataSurface::authorAttribution() const
|
||||
{
|
||||
if (m_metadata.status == MetadataStatus::Parsed) {
|
||||
return m_metadata.authorAttribution;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool BookMetadataSurface::canProvideLoadMetadata() const
|
||||
{
|
||||
return m_engineActive;
|
||||
}
|
||||
|
||||
bool BookMetadataSurface::canProvideParseMetadata() const
|
||||
{
|
||||
return m_engineActive && m_metadata.status == MetadataStatus::Unparsed;
|
||||
}
|
||||
|
||||
void BookMetadataSurface::loadMetadata(const QList<CreatorItem> &creators, const QList<StructuredAuthor> &authors)
|
||||
{
|
||||
if (!canProvideLoadMetadata()) {
|
||||
return;
|
||||
}
|
||||
m_metadata = AuthorAttribution::loadMetadata(m_metadata.book, creators, authors);
|
||||
}
|
||||
|
||||
bool BookMetadataSurface::parseMetadata()
|
||||
{
|
||||
if (!canProvideParseMetadata()) {
|
||||
return false;
|
||||
}
|
||||
return AuthorAttribution::parseMetadata(m_metadata);
|
||||
}
|
||||
|
||||
QString BookMetadataSurface::resolveAttribution(const QString &filePath)
|
||||
{
|
||||
if (!m_engineActive) {
|
||||
return {};
|
||||
}
|
||||
m_metadata.book.filePath = filePath;
|
||||
if (m_metadata.status == MetadataStatus::Unparsed) {
|
||||
AuthorAttribution::parseMetadata(m_metadata);
|
||||
}
|
||||
return m_metadata.authorAttribution.value_or(QString());
|
||||
}
|
||||
|
||||
const BookMetadata &BookMetadataSurface::metadata() const
|
||||
{
|
||||
return m_metadata;
|
||||
}
|
||||
|
||||
void BookMetadataSurface::setMetadata(const BookMetadata &metadata)
|
||||
{
|
||||
m_metadata = metadata;
|
||||
}
|
||||
|
||||
void BookMetadataSurface::setEngineActive(bool active)
|
||||
{
|
||||
m_engineActive = active;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
#ifndef AUTHORATTRIBUTION_H
|
||||
#define AUTHORATTRIBUTION_H
|
||||
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
#include <optional>
|
||||
|
||||
struct StructuredAuthor {
|
||||
std::optional<QString> firstName;
|
||||
std::optional<QString> middleName;
|
||||
std::optional<QString> lastName;
|
||||
std::optional<QString> nickname;
|
||||
|
||||
bool operator==(const StructuredAuthor &other) const
|
||||
{
|
||||
return firstName == other.firstName &&
|
||||
middleName == other.middleName &&
|
||||
lastName == other.lastName &&
|
||||
nickname == other.nickname;
|
||||
}
|
||||
bool operator!=(const StructuredAuthor &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
struct CreatorItem {
|
||||
std::optional<QString> roleCode;
|
||||
QString name;
|
||||
|
||||
bool operator==(const CreatorItem &other) const
|
||||
{
|
||||
return roleCode == other.roleCode && name == other.name;
|
||||
}
|
||||
bool operator!=(const CreatorItem &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
enum class MetadataStatus {
|
||||
Unparsed,
|
||||
Parsed
|
||||
};
|
||||
|
||||
struct BookEntity {
|
||||
QString filePath;
|
||||
bool isActive = true;
|
||||
|
||||
bool operator==(const BookEntity &other) const
|
||||
{
|
||||
return filePath == other.filePath && isActive == other.isActive;
|
||||
}
|
||||
bool operator!=(const BookEntity &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
struct BookMetadata {
|
||||
BookEntity book;
|
||||
MetadataStatus status = MetadataStatus::Unparsed;
|
||||
QList<CreatorItem> rawDcCreators;
|
||||
QList<StructuredAuthor> rawStructuredAuthors;
|
||||
std::optional<QString> authorAttribution;
|
||||
|
||||
bool operator==(const BookMetadata &other) const
|
||||
{
|
||||
return book == other.book &&
|
||||
status == other.status &&
|
||||
rawDcCreators == other.rawDcCreators &&
|
||||
rawStructuredAuthors == other.rawStructuredAuthors &&
|
||||
authorAttribution == other.authorAttribution;
|
||||
}
|
||||
bool operator!=(const BookMetadata &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
};
|
||||
|
||||
class AuthorAttributionService {
|
||||
public:
|
||||
virtual ~AuthorAttributionService() = default;
|
||||
virtual QString resolveAttribution(const QString &filePath) = 0;
|
||||
};
|
||||
|
||||
class AuthorAttribution {
|
||||
public:
|
||||
static QString mapRole(const std::optional<QString> &roleCode);
|
||||
static QString formatCreators(const QList<CreatorItem> &creators);
|
||||
static QString formatStructuredAuthors(const QList<StructuredAuthor> &authors);
|
||||
|
||||
static BookMetadata loadMetadata(const BookEntity &book,
|
||||
const QList<CreatorItem> &creators,
|
||||
const QList<StructuredAuthor> &authors);
|
||||
|
||||
static bool parseMetadataWithDcCreators(BookMetadata &metadata);
|
||||
static bool parseMetadataFallbackToStructuredAuthors(BookMetadata &metadata);
|
||||
static bool parseMetadata(BookMetadata &metadata);
|
||||
};
|
||||
|
||||
class BookMetadataSurface : public AuthorAttributionService {
|
||||
public:
|
||||
explicit BookMetadataSurface(const BookMetadata &metadata, bool engineActive = true);
|
||||
|
||||
bool isAccessible() const;
|
||||
QString filePath() const;
|
||||
std::optional<QString> authorAttribution() const;
|
||||
|
||||
bool canProvideLoadMetadata() const;
|
||||
bool canProvideParseMetadata() const;
|
||||
|
||||
void loadMetadata(const QList<CreatorItem> &creators, const QList<StructuredAuthor> &authors);
|
||||
bool parseMetadata();
|
||||
|
||||
QString resolveAttribution(const QString &filePath) override;
|
||||
|
||||
const BookMetadata &metadata() const;
|
||||
void setMetadata(const BookMetadata &metadata);
|
||||
void setEngineActive(bool active);
|
||||
|
||||
private:
|
||||
BookMetadata m_metadata;
|
||||
bool m_engineActive;
|
||||
};
|
||||
|
||||
#endif // AUTHORATTRIBUTION_H
|
||||
@@ -0,0 +1,495 @@
|
||||
#include <QtTest>
|
||||
|
||||
#include "backend/formats/AuthorAttribution.h"
|
||||
|
||||
class AuthorAttributionTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
// Obligation 1: value-equality.StructuredAuthor
|
||||
void testStructuredAuthorValueEquality()
|
||||
{
|
||||
StructuredAuthor a1{
|
||||
QStringLiteral("Arthur"),
|
||||
QStringLiteral("Conan"),
|
||||
QStringLiteral("Doyle"),
|
||||
QStringLiteral("ACD")
|
||||
};
|
||||
StructuredAuthor a2{
|
||||
QStringLiteral("Arthur"),
|
||||
QStringLiteral("Conan"),
|
||||
QStringLiteral("Doyle"),
|
||||
QStringLiteral("ACD")
|
||||
};
|
||||
StructuredAuthor aDiffFirst{
|
||||
QStringLiteral("John"),
|
||||
QStringLiteral("Conan"),
|
||||
QStringLiteral("Doyle"),
|
||||
QStringLiteral("ACD")
|
||||
};
|
||||
StructuredAuthor aDiffMiddle{
|
||||
QStringLiteral("Arthur"),
|
||||
std::nullopt,
|
||||
QStringLiteral("Doyle"),
|
||||
QStringLiteral("ACD")
|
||||
};
|
||||
StructuredAuthor aDiffLast{
|
||||
QStringLiteral("Arthur"),
|
||||
QStringLiteral("Conan"),
|
||||
QStringLiteral("Watson"),
|
||||
QStringLiteral("ACD")
|
||||
};
|
||||
StructuredAuthor aDiffNick{
|
||||
QStringLiteral("Arthur"),
|
||||
QStringLiteral("Conan"),
|
||||
QStringLiteral("Doyle"),
|
||||
std::nullopt
|
||||
};
|
||||
|
||||
QVERIFY(a1 == a2);
|
||||
QVERIFY(!(a1 != a2));
|
||||
QVERIFY(a1 != aDiffFirst);
|
||||
QVERIFY(a1 != aDiffMiddle);
|
||||
QVERIFY(a1 != aDiffLast);
|
||||
QVERIFY(a1 != aDiffNick);
|
||||
}
|
||||
|
||||
// Obligation 2: entity-fields.StructuredAuthor
|
||||
void testStructuredAuthorFields()
|
||||
{
|
||||
StructuredAuthor author;
|
||||
QVERIFY(!author.firstName.has_value());
|
||||
QVERIFY(!author.middleName.has_value());
|
||||
QVERIFY(!author.lastName.has_value());
|
||||
QVERIFY(!author.nickname.has_value());
|
||||
|
||||
author.firstName = QStringLiteral("Herman");
|
||||
author.middleName = QStringLiteral("M.");
|
||||
author.lastName = QStringLiteral("Melville");
|
||||
author.nickname = QStringLiteral("Ishmael");
|
||||
|
||||
QCOMPARE(author.firstName.value(), QStringLiteral("Herman"));
|
||||
QCOMPARE(author.middleName.value(), QStringLiteral("M."));
|
||||
QCOMPARE(author.lastName.value(), QStringLiteral("Melville"));
|
||||
QCOMPARE(author.nickname.value(), QStringLiteral("Ishmael"));
|
||||
}
|
||||
|
||||
// Obligation 3: value-equality.CreatorItem
|
||||
void testCreatorItemValueEquality()
|
||||
{
|
||||
CreatorItem c1{QStringLiteral("aut"), QStringLiteral("George Orwell")};
|
||||
CreatorItem c2{QStringLiteral("aut"), QStringLiteral("George Orwell")};
|
||||
CreatorItem cDiffRole{QStringLiteral("edt"), QStringLiteral("George Orwell")};
|
||||
CreatorItem cDiffName{QStringLiteral("aut"), QStringLiteral("Aldous Huxley")};
|
||||
CreatorItem cNullRole{std::nullopt, QStringLiteral("George Orwell")};
|
||||
|
||||
QVERIFY(c1 == c2);
|
||||
QVERIFY(!(c1 != c2));
|
||||
QVERIFY(c1 != cDiffRole);
|
||||
QVERIFY(c1 != cDiffName);
|
||||
QVERIFY(c1 != cNullRole);
|
||||
}
|
||||
|
||||
// Obligation 4: entity-fields.CreatorItem
|
||||
void testCreatorItemFields()
|
||||
{
|
||||
CreatorItem creator;
|
||||
QVERIFY(!creator.roleCode.has_value());
|
||||
QVERIFY(creator.name.isEmpty());
|
||||
|
||||
creator.roleCode = QStringLiteral("ill");
|
||||
creator.name = QStringLiteral("Quentin Blake");
|
||||
|
||||
QCOMPARE(creator.roleCode.value(), QStringLiteral("ill"));
|
||||
QCOMPARE(creator.name, QStringLiteral("Quentin Blake"));
|
||||
}
|
||||
|
||||
// Obligation 5: contract-signature.AuthorAttributionService.resolve_attribution
|
||||
void testAuthorAttributionServiceResolveAttribution()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/test.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Isaac Asimov")}},
|
||||
{}
|
||||
);
|
||||
|
||||
BookMetadataSurface surface(meta, true);
|
||||
AuthorAttributionService &service = surface;
|
||||
|
||||
const QString result = service.resolveAttribution(QStringLiteral("/books/test.epub"));
|
||||
QCOMPARE(result, QStringLiteral("Author: Isaac Asimov"));
|
||||
}
|
||||
|
||||
// Contract Invariant: Precedence
|
||||
void testContractInvariantPrecedence()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/precedence.epub"), true};
|
||||
QList<CreatorItem> creators = {
|
||||
CreatorItem{QStringLiteral("aut"), QStringLiteral("Dublin Core Author")}
|
||||
};
|
||||
QList<StructuredAuthor> authors = {
|
||||
StructuredAuthor{QStringLiteral("Structured"), std::nullopt, QStringLiteral("Author"), std::nullopt}
|
||||
};
|
||||
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, creators, authors);
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
|
||||
// Dublin Core creators take precedence over structured authors
|
||||
QCOMPARE(meta.authorAttribution.value(), QStringLiteral("Author: Dublin Core Author"));
|
||||
}
|
||||
|
||||
// Contract Invariant: RoleMapping
|
||||
void testContractInvariantRoleMapping()
|
||||
{
|
||||
// "aut", empty, or unspecified map to "Author"
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("aut")), QStringLiteral("Author"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("AUT")), QStringLiteral("Author"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("")), QStringLiteral("Author"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(std::nullopt), QStringLiteral("Author"));
|
||||
|
||||
// Standard MARC codes
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("edt")), QStringLiteral("Editor"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("EDT")), QStringLiteral("Editor"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("trl")), QStringLiteral("Translator"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("ill")), QStringLiteral("Illustrator"));
|
||||
|
||||
// Unrecognized or custom roles capitalized as-is
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("photographer")), QStringLiteral("Photographer"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("narrator")), QStringLiteral("Narrator"));
|
||||
QCOMPARE(AuthorAttribution::mapRole(QStringLiteral("Director")), QStringLiteral("Director"));
|
||||
}
|
||||
|
||||
// Contract Invariant: GroupingAndDeduplication
|
||||
void testContractInvariantGroupingAndDeduplication()
|
||||
{
|
||||
QList<CreatorItem> creators = {
|
||||
CreatorItem{QStringLiteral("aut"), QStringLiteral("Bob")},
|
||||
CreatorItem{QStringLiteral("edt"), QStringLiteral("Alice")},
|
||||
CreatorItem{QStringLiteral("aut"), QStringLiteral("Charlie")},
|
||||
CreatorItem{QStringLiteral("aut"), QStringLiteral("Bob")}, // duplicate in same role
|
||||
CreatorItem{QStringLiteral("edt"), QStringLiteral("Alice")}, // duplicate in same role
|
||||
CreatorItem{QStringLiteral("ill"), QStringLiteral("Bob")} // same name in different role
|
||||
};
|
||||
|
||||
const QString formatted = AuthorAttribution::formatCreators(creators);
|
||||
// Groups in order of first appearance: Author, Editor, Illustrator
|
||||
// Bob deduplicated in Author, Alice deduplicated in Editor, Bob kept in Illustrator
|
||||
QCOMPARE(formatted, QStringLiteral("Author: Bob, Charlie, Editor: Alice, Illustrator: Bob"));
|
||||
}
|
||||
|
||||
// Contract Invariant: EmptyFallback
|
||||
void testContractInvariantEmptyFallback()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/empty.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, {}, {});
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
|
||||
// When no valid creator or structured author elements exist, attribution is empty string
|
||||
QCOMPARE(meta.authorAttribution.value(), QString());
|
||||
}
|
||||
|
||||
// Obligation 6: entity-fields.Book
|
||||
void testBookEntityFields()
|
||||
{
|
||||
BookEntity book;
|
||||
book.filePath = QStringLiteral("/books/dune.epub");
|
||||
book.isActive = true;
|
||||
|
||||
QCOMPARE(book.filePath, QStringLiteral("/books/dune.epub"));
|
||||
QCOMPARE(book.isActive, true);
|
||||
|
||||
BookEntity book2{QStringLiteral("/books/dune.epub"), true};
|
||||
QVERIFY(book == book2);
|
||||
}
|
||||
|
||||
// Obligation 7: transition-edge.BookMetadata.unparsed.parsed
|
||||
void testTransitionEdgeUnparsedToParsed()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/edge.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Frank Herbert")}},
|
||||
{}
|
||||
);
|
||||
|
||||
QCOMPARE(meta.status, MetadataStatus::Unparsed);
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
}
|
||||
|
||||
// Obligation 8: transition-rejected.BookMetadata.status
|
||||
void testTransitionRejectedFromParsed()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/rejected.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Frank Herbert")}},
|
||||
{}
|
||||
);
|
||||
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
|
||||
// Attempting to re-parse from Parsed status must be rejected
|
||||
QVERIFY(!AuthorAttribution::parseMetadata(meta));
|
||||
QVERIFY(!AuthorAttribution::parseMetadataWithDcCreators(meta));
|
||||
QVERIFY(!AuthorAttribution::parseMetadataFallbackToStructuredAuthors(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
}
|
||||
|
||||
// Obligation 9: transition-terminal.BookMetadata.status
|
||||
void testTransitionTerminalStateParsed()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/terminal.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Stanislaw Lem")}},
|
||||
{}
|
||||
);
|
||||
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
|
||||
// Parsed is terminal: no transition leaves Parsed
|
||||
const QString attributionBefore = meta.authorAttribution.value();
|
||||
QVERIFY(!AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
QCOMPARE(meta.authorAttribution.value(), attributionBefore);
|
||||
}
|
||||
|
||||
// Obligation 10: when-presence.BookMetadata.author_attribution
|
||||
void testWhenPresenceAuthorAttribution()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/when.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Ursula K. Le Guin")}},
|
||||
{}
|
||||
);
|
||||
|
||||
// When unparsed, author_attribution is absent
|
||||
QCOMPARE(meta.status, MetadataStatus::Unparsed);
|
||||
QVERIFY(!meta.authorAttribution.has_value());
|
||||
|
||||
// When parsed, author_attribution is present
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
QVERIFY(meta.authorAttribution.has_value());
|
||||
QCOMPARE(meta.authorAttribution.value(), QStringLiteral("Author: Ursula K. Le Guin"));
|
||||
}
|
||||
|
||||
// Obligation 11: entity-fields.BookMetadata
|
||||
void testBookMetadataFields()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/fields.epub"), true};
|
||||
QList<CreatorItem> creators = {CreatorItem{QStringLiteral("aut"), QStringLiteral("Jules Verne")}};
|
||||
QList<StructuredAuthor> authors = {StructuredAuthor{QStringLiteral("Jules"), std::nullopt, QStringLiteral("Verne"), std::nullopt}};
|
||||
|
||||
BookMetadata meta{
|
||||
book,
|
||||
MetadataStatus::Unparsed,
|
||||
creators,
|
||||
authors,
|
||||
std::nullopt
|
||||
};
|
||||
|
||||
QCOMPARE(meta.book.filePath, QStringLiteral("/books/fields.epub"));
|
||||
QCOMPARE(meta.status, MetadataStatus::Unparsed);
|
||||
QCOMPARE(meta.rawDcCreators.size(), 1);
|
||||
QCOMPARE(meta.rawStructuredAuthors.size(), 1);
|
||||
QVERIFY(!meta.authorAttribution.has_value());
|
||||
}
|
||||
|
||||
// Obligation 12: rule-success.LoadMetadata
|
||||
void testRuleSuccessLoadMetadata()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/load.epub"), true};
|
||||
QList<CreatorItem> creators = {CreatorItem{QStringLiteral("aut"), QStringLiteral("Philip K. Dick")}};
|
||||
QList<StructuredAuthor> authors = {};
|
||||
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, creators, authors);
|
||||
QCOMPARE(meta.status, MetadataStatus::Unparsed);
|
||||
}
|
||||
|
||||
// Obligation 13: rule-entity-creation.LoadMetadata.1
|
||||
void testRuleEntityCreationLoadMetadata()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/load_fields.epub"), true};
|
||||
QList<CreatorItem> creators = {CreatorItem{QStringLiteral("edt"), QStringLiteral("Editor Name")}};
|
||||
QList<StructuredAuthor> authors = {StructuredAuthor{QStringLiteral("First"), QStringLiteral("M"), QStringLiteral("Last"), std::nullopt}};
|
||||
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, creators, authors);
|
||||
QCOMPARE(meta.book, book);
|
||||
QCOMPARE(meta.status, MetadataStatus::Unparsed);
|
||||
QCOMPARE(meta.rawDcCreators, creators);
|
||||
QCOMPARE(meta.rawStructuredAuthors, authors);
|
||||
QVERIFY(!meta.authorAttribution.has_value());
|
||||
}
|
||||
|
||||
// Obligation 14: rule-success.ParseMetadataWithDcCreators
|
||||
void testRuleSuccessParseMetadataWithDcCreators()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/dc.epub"), true};
|
||||
QList<CreatorItem> creators = {
|
||||
CreatorItem{QStringLiteral("aut"), QStringLiteral("Ray Bradbury")},
|
||||
CreatorItem{QStringLiteral("trl"), QStringLiteral("Boris Pasternak")}
|
||||
};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, creators, {});
|
||||
|
||||
QVERIFY(AuthorAttribution::parseMetadataWithDcCreators(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
QCOMPARE(meta.authorAttribution.value(), QStringLiteral("Author: Ray Bradbury, Translator: Boris Pasternak"));
|
||||
}
|
||||
|
||||
// Obligation 15: rule-failure.ParseMetadataWithDcCreators.1
|
||||
void testRuleFailureParseMetadataWithDcCreatorsWrongStatus()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/dc_fail1.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("Author 1")}},
|
||||
{}
|
||||
);
|
||||
meta.status = MetadataStatus::Parsed; // Precondition failure: requires status = unparsed
|
||||
|
||||
QVERIFY(!AuthorAttribution::parseMetadataWithDcCreators(meta));
|
||||
}
|
||||
|
||||
// Obligation 16: rule-failure.ParseMetadataWithDcCreators.2
|
||||
void testRuleFailureParseMetadataWithDcCreatorsNoCreators()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/dc_fail2.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, {}, {}); // Precondition failure: raw_dc_creators.count > 0
|
||||
|
||||
QVERIFY(!AuthorAttribution::parseMetadataWithDcCreators(meta));
|
||||
}
|
||||
|
||||
// Obligation 17: rule-success.ParseMetadataFallbackToStructuredAuthors
|
||||
void testRuleSuccessParseMetadataFallbackToStructuredAuthors()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/fb.fb2"), true};
|
||||
QList<StructuredAuthor> authors = {
|
||||
StructuredAuthor{QStringLiteral("Leo"), QStringLiteral("Nikolayevich"), QStringLiteral("Tolstoy"), std::nullopt},
|
||||
StructuredAuthor{std::nullopt, std::nullopt, std::nullopt, QStringLiteral("Voltaire")}
|
||||
};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(book, {}, authors);
|
||||
|
||||
QVERIFY(AuthorAttribution::parseMetadataFallbackToStructuredAuthors(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
QCOMPARE(meta.authorAttribution.value(), QStringLiteral("Leo Nikolayevich Tolstoy, Voltaire"));
|
||||
}
|
||||
|
||||
// Obligation 18: rule-failure.ParseMetadataFallbackToStructuredAuthors.1
|
||||
void testRuleFailureParseMetadataFallbackToStructuredAuthorsWrongStatus()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/fb_fail1.fb2"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{},
|
||||
{StructuredAuthor{QStringLiteral("First"), std::nullopt, QStringLiteral("Last"), std::nullopt}}
|
||||
);
|
||||
meta.status = MetadataStatus::Parsed; // Precondition failure: requires status = unparsed
|
||||
|
||||
QVERIFY(!AuthorAttribution::parseMetadataFallbackToStructuredAuthors(meta));
|
||||
}
|
||||
|
||||
// Obligation 19: rule-failure.ParseMetadataFallbackToStructuredAuthors.2
|
||||
void testRuleFailureParseMetadataFallbackToStructuredAuthorsHasDcCreators()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/fb_fail2.fb2"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("DC Author")}}, // Precondition failure: raw_dc_creators.count = 0
|
||||
{StructuredAuthor{QStringLiteral("Structured"), std::nullopt, QStringLiteral("Author"), std::nullopt}}
|
||||
);
|
||||
|
||||
QVERIFY(!AuthorAttribution::parseMetadataFallbackToStructuredAuthors(meta));
|
||||
}
|
||||
|
||||
// Obligation 20: invariant.FormattedAttributionRequiresParsedState
|
||||
void testInvariantFormattedAttributionRequiresParsedState()
|
||||
{
|
||||
// When status is parsed, authorAttribution must not be null/empty optional
|
||||
BookEntity book{QStringLiteral("/books/inv.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("H.P. Lovecraft")}},
|
||||
{}
|
||||
);
|
||||
|
||||
QVERIFY(AuthorAttribution::parseMetadata(meta));
|
||||
QCOMPARE(meta.status, MetadataStatus::Parsed);
|
||||
QVERIFY(meta.authorAttribution.has_value());
|
||||
}
|
||||
|
||||
// Obligation 21: surface-actor.BookMetadataSurface
|
||||
void testSurfaceActorBookMetadataSurface()
|
||||
{
|
||||
BookEntity activeBook{QStringLiteral("/books/active.epub"), true};
|
||||
BookMetadata metaActive = AuthorAttribution::loadMetadata(activeBook, {}, {});
|
||||
BookMetadataSurface activeSurface(metaActive, true);
|
||||
QVERIFY(activeSurface.isAccessible());
|
||||
|
||||
BookEntity inactiveBook{QStringLiteral("/books/inactive.epub"), false};
|
||||
BookMetadata metaInactive = AuthorAttribution::loadMetadata(inactiveBook, {}, {});
|
||||
BookMetadataSurface inactiveSurface(metaInactive, false);
|
||||
QVERIFY(!inactiveSurface.isAccessible());
|
||||
}
|
||||
|
||||
// Obligation 22: surface-exposure.BookMetadataSurface
|
||||
void testSurfaceExposureBookMetadataSurface()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/expose.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("C.S. Lewis")}},
|
||||
{}
|
||||
);
|
||||
|
||||
BookMetadataSurface surface(meta, true);
|
||||
|
||||
// file_path is exposed
|
||||
QCOMPARE(surface.filePath(), QStringLiteral("/books/expose.epub"));
|
||||
|
||||
// author_attribution is exposed when parsed, nullopt otherwise
|
||||
QVERIFY(!surface.authorAttribution().has_value());
|
||||
|
||||
QVERIFY(surface.parseMetadata());
|
||||
QVERIFY(surface.authorAttribution().has_value());
|
||||
QCOMPARE(surface.authorAttribution().value(), QStringLiteral("Author: C.S. Lewis"));
|
||||
}
|
||||
|
||||
// Obligation 23: surface-provides.BookMetadataSurface
|
||||
void testSurfaceProvidesBookMetadataSurface()
|
||||
{
|
||||
BookEntity book{QStringLiteral("/books/provides.epub"), true};
|
||||
BookMetadata meta = AuthorAttribution::loadMetadata(
|
||||
book,
|
||||
{CreatorItem{QStringLiteral("aut"), QStringLiteral("J.R.R. Tolkien")}},
|
||||
{}
|
||||
);
|
||||
|
||||
BookMetadataSurface surface(meta, true);
|
||||
|
||||
// LoadMetadataTrigger provided when engine is active
|
||||
QVERIFY(surface.canProvideLoadMetadata());
|
||||
|
||||
// ParseMetadata provided when status is unparsed and engine is active
|
||||
QVERIFY(surface.canProvideParseMetadata());
|
||||
|
||||
// Parse changes status to parsed
|
||||
QVERIFY(surface.parseMetadata());
|
||||
|
||||
// Now ParseMetadata is hidden because status is parsed
|
||||
QVERIFY(!surface.canProvideParseMetadata());
|
||||
|
||||
// If engine becomes inactive, neither is provided
|
||||
surface.setEngineActive(false);
|
||||
QVERIFY(!surface.canProvideLoadMetadata());
|
||||
QVERIFY(!surface.canProvideParseMetadata());
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(AuthorAttributionTest)
|
||||
#include "AuthorAttributionTest.moc"
|
||||
Reference in new issue
Block a user