using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using SlpModularCms.Modules.Hero.Data;
using SlpModularCms.Modules.Hero.Data.Entities;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
using Xunit;
namespace SlpModularCms.Modules.Hero.Tests;
/// Phase 3b: the hero's photos live in the library's table (rows of module "hero"), and HeroImages stays a complete copy.
public class HeroPhotosInLibraryTests
{
private static HeroSaveRequest Request(params HeroSlideRequest[] slides) => new("Titel", null, null, "slideshow", [], slides);
[Fact]
public void The_model_maps_the_photos_to_the_library_table_only_when_the_switch_is_on()
{
using var off = new HeroTestRig();
using var on = new HeroTestRig(photosInLibrary: true);
off.Db.Model.FindEntityType(typeof(HeroImage))!.GetTableName().Should().Be("HeroImages");
off.Db.Model.FindEntityType(typeof(LegacyHeroImage)).Should().BeNull();
var photo = on.Db.Model.FindEntityType(typeof(HeroImage))!;
photo.GetTableName().Should().Be("MediaAssets");
photo.FindProperty(nameof(HeroImage.FocusX))!.GetColumnName().Should().Be("DefaultFocusX");
photo.FindProperty(nameof(HeroImage.Zoom))!.GetColumnName().Should().Be("DefaultZoom");
photo.FindProperty(HeroDbContext.ModuleColumn).Should().NotBeNull();
on.Db.Model.FindEntityType(typeof(LegacyHeroImage))!.GetTableName().Should().Be("HeroImages");
}
[Fact]
public async Task Everything_the_hero_does_with_a_photo_works_and_HeroImages_stays_a_complete_copy()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
var saved = await rig.Service.SaveAsync(Request(new HeroSlideRequest(null, id, "een"), new HeroSlideRequest(null, id, "twee")), HeroTestRig.Admin, CancellationToken.None);
await rig.Images.SetFocusAsync(id, new HeroFocusRequest(new FocusDto(0.3, 0.4, 1.4), new FocusDto(0.2, 0.2, 1.1), null), HeroTestRig.Admin, CancellationToken.None);
await rig.Images.SetSlideFocusAsync(Guid.Parse(saved.Slides[1].Id), new HeroFocusRequest(new FocusDto(0.9, 0.9, 1.2)), HeroTestRig.Admin, CancellationToken.None);
await rig.RenderPending();
var other = await rig.Upload(HeroTestRig.Photo(seed: 5));
await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(other.Id), HeroTestRig.Admin, CancellationToken.None);
var library = await rig.Db.HeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync();
var legacy = await rig.Db.LegacyHeroImages.AsNoTracking().OrderBy(i => i.CreatedAt).ToListAsync();
library.Should().HaveCount(2);
legacy.Should().HaveCount(2);
library.Select(i => i.Id).Should().Equal(legacy.Select(i => i.Id));
foreach (var (l, c) in library.Zip(legacy))
{
(l.OriginalSha256, l.SetHash, l.FocusX, l.WideFocusX, l.DeletedAt, l.OriginalFileName).Should().Be((c.OriginalSha256, c.SetHash, c.FocusX, c.WideFocusX, c.DeletedAt, c.OriginalFileName));
l.Variants.Should().Equal(c.Variants);
}
library.Single(i => i.Id == id).Variants.Should().NotBeEmpty();
var publicHero = (await rig.Public.GetActiveAsync(CancellationToken.None))!;
publicHero.Slides.Should().HaveCount(2);
publicHero.Slides[0].Image.Focus.Should().Be(new FocusDto(0.3, 0.4, 1.4));
}
[Fact]
public async Task The_clean_up_removes_a_trashed_photo_from_both_tables()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
await new HeroMediaUsageProvider(rig.Repository, rig.Images).TrashAsync(Guid.Parse(image.Id), HeroTestRig.Admin, CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(60);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
(await rig.Db.HeroImages.CountAsync()).Should().Be(0);
(await rig.Db.LegacyHeroImages.CountAsync()).Should().Be(0);
}
[Fact]
public async Task The_start_up_check_passes_for_a_complete_copy_and_refuses_when_the_library_misses_a_photo_or_its_variants()
{
using var rig = new HeroTestRig(photosInLibrary: true);
var image = await rig.Upload(HeroTestRig.Photo());
await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
// A photo that exists in HeroImages only (the copy was not run).
rig.Db.LegacyHeroImages.Add(new LegacyHeroImage { Id = Guid.NewGuid(), OriginalSha256 = new string('a', 64), OriginalKey = "originals/a.jpg", SourceFormat = "jpg", SetHash = "0123456789abcdef", CreatedAt = DateTimeOffset.UtcNow });
await rig.Db.SaveChangesAsync();
var missing = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
(await missing.Should().ThrowAsync()).Which.Message.Should().Contain("is not in the library").And.Contain("copy");
// And a photo whose variants differ (a copy made before the variants were stored).
rig.Db.LegacyHeroImages.RemoveRange(rig.Db.LegacyHeroImages.Where(i => i.OriginalSha256 == new string('a', 64)));
var photo = await rig.Db.HeroImages.SingleAsync(i => i.Id == Guid.Parse(image.Id));
photo.Variants = [];
await rig.Db.SaveChangesAsync();
var differs = () => HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
(await differs.Should().ThrowAsync()).Which.Message.Should().Contain("differs from the library");
}
[Fact]
public async Task With_the_switch_off_nothing_is_mirrored_and_the_check_does_nothing()
{
using var rig = new HeroTestRig();
await rig.Upload(HeroTestRig.Photo());
await HeroPhotoStorageGuard.VerifyAsync(rig.Db, CancellationToken.None);
await rig.LegacyMirror.SyncAsync(CancellationToken.None);
(await rig.Db.HeroImages.CountAsync()).Should().Be(1);
}
}