using FluentAssertions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using SlpModularCms.Core.Media.Library; using SlpModularCms.Modules.Media.Data; using SlpModularCms.Modules.Media.Services; namespace SlpModularCms.Modules.Media.Tests; /// Phase 2, step A: the write-through mirrors one module into the library tables, only when it is switched on, and never throws. public class MediaLibrarySyncTests : IDisposable { private static readonly DateTimeOffset Now = new(2026, 11, 17, 12, 0, 0, TimeSpan.Zero); private readonly MediaDbContext _db = new(new DbContextOptionsBuilder().UseInMemoryDatabase("media-sync-" + Guid.NewGuid()).Options); public void Dispose() => _db.Dispose(); private sealed class Source(string module, Func> assets, Func> uses, bool fails = false) : IMediaMigrationSource { public string Module => module; public Task> ListAssetsAsync(CancellationToken cancellationToken) => fails ? throw new InvalidOperationException("source failed") : Task.FromResult>(assets()); public Task> ListUsesAsync(CancellationToken cancellationToken) => Task.FromResult>(uses()); } private static MediaAssetSnapshot Asset(string module) => new(Guid.NewGuid(), module, Guid.NewGuid().ToString("N") + Guid.NewGuid().ToString("N"), "originals/x.jpg", "jpg", 3000, 2000, 2_000_000, 0.5, 0.5, 1, "0123456789abcdef", [new MediaCropSnapshot("wide", 0.5, 0.5, 1)], Now.AddDays(-9), Guid.NewGuid(), "zomer.jpg", null, null); private MediaLibrarySync Sync(bool on, params IMediaMigrationSource[] sources) => new(new MediaLibraryCopier(_db, sources, NullLogger.Instance), Options.Create(new MediaLibraryOptions { DualWrite = on }), NullLogger.Instance); [Fact] public async Task Does_nothing_while_the_switch_is_off() { var a = Asset("hero"); await Sync(false, new Source("hero", () => [a], () => [])).SyncAsync("hero", CancellationToken.None); (await _db.MediaAssets.CountAsync()).Should().Be(0); } [Fact] public async Task Mirrors_only_the_given_module_and_follows_the_changes_of_the_source() { var hero = Asset("hero"); var other = Asset("gallery"); var heroAssets = new List { hero }; var sync = Sync(true, new Source("hero", () => heroAssets.ToList(), () => []), new Source("gallery", () => [other], () => [])); await sync.SyncAsync("hero", CancellationToken.None); (await _db.MediaAssets.Select(a => a.Module).ToListAsync()).Should().Equal("hero"); var second = Asset("hero"); heroAssets.Add(second); heroAssets.Remove(hero); await sync.SyncAsync("hero", CancellationToken.None); (await _db.MediaAssets.Select(a => a.Id).ToListAsync()).Should().Equal(second.Id); } [Fact] public async Task Stores_the_crops_of_a_use_when_the_source_gives_them() { var hero = Asset("hero"); var use = new MediaUseSnapshot(Guid.NewGuid(), hero.Id, "hero", "slide", 1, true, "fedcba9876543210", [new MediaCropSnapshot("wide", 0.1, 0.9, 1.4)]); await Sync(true, new Source("hero", () => [hero], () => [use])).SyncAsync("hero", CancellationToken.None); var row = await _db.MediaUses.SingleAsync(); row.SetHash.Should().Be("fedcba9876543210"); row.Crops.Should().Contain("0.9").And.Contain("1.4"); } [Fact] public async Task Never_throws_when_the_source_fails() { var act = () => Sync(true, new Source("hero", () => [], () => [], fails: true)).SyncAsync("hero", CancellationToken.None); await act.Should().NotThrowAsync(); } }