Files
asteria-qt/tests/formats/AuthorAttributionTest.cpp
Agel bde4e310b5 feat(formats): implement author attribution service and propagate spec tests
Book metadata extraction previously handled author parsing naively by taking only the first raw dc:creator string, ignoring MARC relator roles, multi-creator ordering, deduplication, and structured author fallbacks.

Define the formal author attribution model in specs/book-author-parsing.allium and propagate the specification into a comprehensive test suite (AuthorAttributionTest) covering all 23 obligations across value equality, lifecycle state transitions, contract invariants, and surface exposure.

Implement AuthorAttribution, AuthorAttributionService, and BookMetadataSurface to fulfill:
- Dublin Core precedence over structured author elements
- MARC role code resolution (aut -> Author, edt -> Editor, trl -> Translator, ill -> Illustrator, custom capitalized)
- Grouping creators by resolved role in order of appearance with deduplication
- Fallback formatting for structured author fields (first, middle, last, nickname)
- Complete status lifecycle enforcement (unparsed to parsed)

Register AuthorAttributionTest in CMakeLists.txt and update AGENTS.md documentation.
2026-10-29 15:21:06 +01:00

496 lines
20 KiB
C++

#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"