Hero: een gewijzigd focuspunt geldt pas bij het opslaan van de hero (Bryan); het focus-venster houdt de wijziging lokaal

Het eindpunt PUT /hero/images/{id}/focus is vervallen; de focus gaat mee in de opslag (imageFocus) en de varianten worden dan in nieuwe bestanden opnieuw gesneden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 20:05:17 +01:00
co-authored by Claude Sonnet 5.5
parent 01703ccddf
commit 0b56fa6294
20 changed files with 318 additions and 197 deletions
@@ -0,0 +1,103 @@
using System.ComponentModel.DataAnnotations;
using FluentAssertions;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
namespace SlpModularCms.Modules.Hero.Tests;
/// <summary>A changed focus is part of saving the hero: nothing live changes before that save.</summary>
public class HeroFocusSaveTests
{
private static HeroSaveRequest Save(Guid imageId, IReadOnlyList<HeroImageFocusRequest>? focus)
=> new("Andrea Slop", null, null, "single", [], true, null, null, [new HeroSlideRequest(null, imageId, "foto")], focus);
[Fact]
public async Task TheLiveHero_KeepsItsAddresses_UntilTheHeroIsSavedWithTheNewFocus()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var before = (await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src;
// Nothing but a save changes what is live: the focus travels with the save request.
(await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src.Should().Be(before);
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.5, 0.5, 1), null, new FocusDto(1.0, 0.5, 1))]), HeroTestRig.Admin, default);
var after = (await rig.Public.GetActiveAsync(default))!.Slides[0].Image;
var beforeSet = before.Split('/')[3];
after.Variants.Should().OnlyContain(v => !v.Url.Contains(beforeSet), "a changed focus gives new addresses");
after.FocusByRatio.Should().ContainKey("tall");
}
[Fact]
public async Task TheOldFiles_StayOnDisk_AfterTheSave_UntilTheCleanUp()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var oldVariant = image.Variants.First();
await rig.Service.SaveAsync(Save(id, [new HeroImageFocusRequest(id, new FocusDto(0.1, 0.1, 1.5))]), HeroTestRig.Admin, default);
(await rig.Store.ExistsAsync(oldVariant.Url["/media/".Length..], default)).Should().BeTrue("a site that still holds the old data keeps working for the retention time");
rig.Db.HeroRetiredFiles.Should().ContainSingle(f => f.Kind == "set");
}
[Fact]
public async Task APreparedFocusThatIsNeverSaved_LeavesOnlyUnreferencedFiles_ThatTheCleanUpRemoves()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var id = Guid.Parse(image.Id);
await rig.Service.SaveAsync(Save(id, null), HeroTestRig.Admin, default);
var live = rig.Db.HeroImages.Single().SetHash;
// The save stops after the new files were written (here: the hero refers to a slide of another hero).
var failing = new HeroSaveRequest("Andrea Slop", null, null, "single", [], true, null, null, [new HeroSlideRequest(Guid.NewGuid(), id, "foto")],
[new HeroImageFocusRequest(id, new FocusDto(0.9, 0.9, 2))]);
var act = async () => await rig.Service.SaveAsync(failing, HeroTestRig.Admin, default);
await act.Should().ThrowAsync<HeroSaveRejectedException>();
rig.Db.HeroImages.Single().SetHash.Should().Be(live, "the failed save did not change the stored image");
(await rig.Public.GetActiveAsync(default))!.Slides[0].Image.Src.Should().Contain(live);
}
[Fact]
public async Task AFocusForAnUnknownImage_IsRejected()
{
using var rig = new HeroTestRig();
var act = async () => await rig.Service.SaveAsync(Save(Guid.NewGuid(), null) with { Slides = [], ImageFocus = [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(0.5, 0.5, 1))] }, HeroTestRig.Admin, default);
(await act.Should().ThrowAsync<HeroSaveRejectedException>()).Which.Field.Should().Be("ImageFocus[0].ImageId");
}
[Theory]
[InlineData(-0.1, 0.5, 1.0)]
[InlineData(0.5, 1.1, 1.0)]
[InlineData(0.5, 0.5, 0.5)]
[InlineData(0.5, 0.5, 4.5)]
public void AnInvalidFocusInTheRequest_IsRefusedByTheValidation(double x, double y, double zoom)
{
var request = Save(Guid.NewGuid(), [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(x, y, zoom))]);
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, true);
results.SelectMany(r => r.MemberNames).Should().Contain("ImageFocus[0]");
}
[Fact]
public void AValidFocusEdit_PassesTheValidation()
{
var request = Save(Guid.NewGuid(), [new HeroImageFocusRequest(Guid.NewGuid(), new FocusDto(0.2, 0.9, 2), new FocusDto(0.1, 0.1, 1), null)]);
var results = new List<ValidationResult>();
Validator.TryValidateObject(request, new ValidationContext(request), results, true);
results.Should().BeEmpty();
}
}