Adds independent metadata and artwork enrichment

Splits the single combined enrichment phase of the media library scan into
independent, modular jobs, so that the metadata and the artwork of a book
are enriched separately and only when their respective triggers fire.

- Adds the MediaContributorRoleCategory enum (Other first, then Author,
  Translator, Illustrator, Publisher and Narrator) and makes the role
  category canonical, so that roles describing the same kind of
  contribution are never treated as distinct.
- Realizes the media contributor bounded context: a MediaContributor is a
  person, unique by display name, without a role. The role moves to the
  per-work participation (BookContributors), so that a single contributor
  can play multiple roles. Contributors are created and linked by the
  metadata enrichment job, and exposed to subsequent jobs.
- Makes the Book aggregate pure: the enrichment tracking state (metadata
  status, providers, timestamps and covers) no longer lives on the domain
  aggregate, but stays in the Books table, managed directly by the jobs.
- Models artwork generically as a BookArtwork child table (type, ordinal,
  content hash, status and provider), so that each piece of artwork is
  tracked independently and a change of one does not require the others to
  be re-fetched.
- Splits the scan DAG into ResultsSave, ProviderConfigurationInvalidation,
  MetadataEnrichment and ArtworkEnrichment jobs. The invalidation job
  fingerprints the provider configuration of a library and resets the
  enrichment state of the affected channel when it changed. The artwork job
  resolves the missing artwork, hashing it to detect identical covers and
  skip re-copying. Content-changed books are reset by the results save job.
- Deletes the book and its stored artwork when the file disappears from the
  library.
- Adds the migration creating the new tables, backfilling the existing
  covers into the artwork table, and adding the configuration fingerprints.
- Localizes the new scan job stages, and fixes the invalidation job wiping
  the library content locations by persisting the fingerprints through the
  change tracker instead of the repository update action.
This commit is contained in:
BinaryAlley committed 2026-08-27 14:41:49 +03:00
1 parent 4ee2976806
commit 565fe68239
98 files changed
+5845 -630

No files matched your search

@@ -1,8 +1,11 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.MediaContributorBoundedContext.MediaContributorAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaContributorBoundedContext.MediaContributorAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -15,46 +18,73 @@ namespace Lumina.Domain.UnitTests.Core.BoundedContexts.WrittenContentLibraryBoun
public class BookTests
{
private readonly BookFixture _bookFixture = new();
private readonly MediaContributorIdFixture _mediaContributorIdFixture = new();
[Fact]
public void Create_WhenCalledWithValidData_ShouldCreateBookWithPendingMetadataStatus()
public void Create_WhenCalledWithValidData_ShouldCreateBook()
{
// Act
Book book = _bookFixture.Create();
// Assert
Assert.NotNull(book);
Assert.Equal(MetadataStatus.Pending, book.MetadataStatus);
Assert.False(book.LastMetadataUpdateUtc.HasValue);
Assert.False(book.MetadataProvider.HasValue);
Assert.NotNull(book.Metadata);
Assert.NotNull(book.Path);
Assert.NotNull(book.LibraryId);
}
[Fact]
public void MarkMetadataAsEnriched_WhenCalled_ShouldUpdateStatusAndProvider()
public void UpdateContributors_WhenCalled_ShouldReplaceTheContributors()
{
// Arrange
Book book = _bookFixture.Create();
DateTime lastUpdateUtc = DateTime.UtcNow;
MediaContributorId firstContributorId = _mediaContributorIdFixture.Create();
MediaContributorId secondContributorId = _mediaContributorIdFixture.Create();
// Act
book.MarkMetadataAsEnriched("Goodreads", lastUpdateUtc);
book.UpdateContributors([firstContributorId, secondContributorId]);
// Assert
Assert.Equal(MetadataStatus.Enriched, book.MetadataStatus);
Assert.Equal("Goodreads", book.MetadataProvider.Value);
Assert.Equal(lastUpdateUtc, book.LastMetadataUpdateUtc.Value);
Assert.Equal(2, book.Contributors.Count);
Assert.Contains(book.Contributors, contributorId => contributorId == firstContributorId);
Assert.Contains(book.Contributors, contributorId => contributorId == secondContributorId);
}
[Fact]
public void MarkMetadataAsFailed_WhenCalled_ShouldUpdateStatus()
public void UpdateContributors_WhenCalledAgain_ShouldReplaceTheContributors()
{
// Arrange
Book book = _bookFixture.Create();
book.UpdateContributors([_mediaContributorIdFixture.Create()]);
MediaContributorId replacementContributorId = _mediaContributorIdFixture.Create();
// Act
book.MarkMetadataAsFailed();
book.UpdateContributors([replacementContributorId]);
// Assert
Assert.Equal(MetadataStatus.Failed, book.MetadataStatus);
// the previous contributors are replaced, not appended
MediaContributorId contributorId = Assert.Single(book.Contributors);
Assert.Equal(replacementContributorId, contributorId);
}
[Fact]
public void Create_WhenCalled_ShouldNotExposeEnrichmentTracking()
{
// Arrange
Book book = _bookFixture.Create();
Type bookType = book.GetType();
// Act
bool hasMetadataStatus = bookType.GetProperty("MetadataStatus") is not null;
bool hasLastMetadataUpdateUtc = bookType.GetProperty("LastMetadataUpdateUtc") is not null;
bool hasMetadataProvider = bookType.GetProperty("MetadataProvider") is not null;
bool hasCoverImagePath = bookType.GetProperty("CoverImagePath") is not null;
// Assert
// the enrichment state is a persistence concern, tracked on the repository entity by the enrichment jobs
Assert.False(hasMetadataStatus);
Assert.False(hasLastMetadataUpdateUtc);
Assert.False(hasMetadataProvider);
Assert.False(hasCoverImagePath);
}
}
@@ -40,7 +40,7 @@ public class BookLibraryTypeScannerTests
}
[Fact]
public void CreateScanJobsForLibrary_WhenCalled_ShouldCreateJobChainWithMetadataEnrichment()
public void CreateScanJobsForLibrary_WhenCalled_ShouldCreateJobChainWithEnrichmentJobs()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
@@ -48,7 +48,9 @@ public class BookLibraryTypeScannerTests
TestScanJob diffJob = CreateJob<IMediaLibraryScanDiffJob>();
TestScanJob hashJob = CreateJob<IMediaLibraryScanHashJob>();
TestScanJob saveJob = CreateJob<IMediaLibraryScanResultsSaveJob>();
TestScanJob invalidationJob = CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>();
TestScanJob enrichmentJob = CreateJob<IMediaLibraryScanMetadataEnrichmentJob>();
TestScanJob artworkEnrichmentJob = CreateJob<IMediaLibraryScanArtworkEnrichmentJob>();
BookLibraryTypeScanner sut = new(_mockJobFactory);
// Act
@@ -63,9 +65,14 @@ public class BookLibraryTypeScannerTests
Assert.Contains(diffJob, hashJob.Parents);
Assert.Contains(saveJob, hashJob.Children);
Assert.Contains(hashJob, saveJob.Parents);
Assert.Contains(enrichmentJob, saveJob.Children);
Assert.Contains(saveJob, enrichmentJob.Parents);
Assert.Contains(invalidationJob, saveJob.Children);
Assert.Contains(saveJob, invalidationJob.Parents);
Assert.Contains(enrichmentJob, invalidationJob.Children);
Assert.Contains(invalidationJob, enrichmentJob.Parents);
Assert.Contains(artworkEnrichmentJob, enrichmentJob.Children);
Assert.Contains(enrichmentJob, artworkEnrichmentJob.Parents);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanMetadataEnrichmentJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanArtworkEnrichmentJob>(libraryId);
}
[Fact]
@@ -77,7 +84,9 @@ public class BookLibraryTypeScannerTests
CreateJob<IMediaLibraryScanDiffJob>();
CreateJob<IMediaLibraryScanHashJob>();
CreateJob<IMediaLibraryScanResultsSaveJob>();
CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>();
CreateJob<IMediaLibraryScanMetadataEnrichmentJob>();
CreateJob<IMediaLibraryScanArtworkEnrichmentJob>();
BookLibraryTypeScanner sut = new(_mockJobFactory);
// Act
@@ -88,7 +97,9 @@ public class BookLibraryTypeScannerTests
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanDiffJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanHashJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanResultsSaveJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanMetadataEnrichmentJob>(libraryId);
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanArtworkEnrichmentJob>(libraryId);
}
private TestScanJob CreateJob<TJob>() where TJob : class, IMediaLibraryScanJob
@@ -111,7 +122,9 @@ public class BookLibraryTypeScannerTests
IMediaLibraryScanDiffJob,
IMediaLibraryScanHashJob,
IMediaLibraryScanResultsSaveJob,
IMediaLibraryScanMetadataEnrichmentJob
IMediaLibraryScanProviderConfigurationInvalidationJob,
IMediaLibraryScanMetadataEnrichmentJob,
IMediaLibraryScanArtworkEnrichmentJob
{
/// <summary>
/// Executes the payload of the media library scan job.