Media: gebruiksdefinities (mozaiek groot/breed/klein, masonry) als pure code met tests
Continuous Integration / config-sls (pull_request) Successful in 5s
Continuous Integration / changes (pull_request) Successful in 12s
Continuous Integration / frontend-prepare (pull_request) Skipped
Continuous Integration / frontend-build (pull_request) Skipped
Continuous Integration / frontend-test (pull_request) Skipped
Continuous Integration / frontend-lint (pull_request) Skipped
Continuous Integration / config-asf (pull_request) Successful in 13s
Continuous Integration / compile-sites (pull_request) Successful in 1m29s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m19s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / backend-build (pull_request) Canceled after 4m36s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 14:18:51 +01:00
co-authored by Claude Sonnet 5.5
parent a71db84632
commit 1e89d8f3d7
3 changed files with 145 additions and 0 deletions
@@ -0,0 +1,72 @@
using FluentAssertions;
using SlpModularCms.Core.Media;
using SlpModularCms.Core.Media.Library;
using Xunit;
namespace SlpModularCms.Core.Tests.Media;
public class MediaUseDefinitionTests
{
private const string Sha = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
[Fact]
public void Every_ratio_name_and_width_fits_the_media_address_scheme()
{
foreach (var use in GalleryUses.All)
{
var plans = use.PlansFor(4000, 3000);
var specs = VariantPlanner.Plan(4000, 3000, plans, new HashSet<ImageFormat> { ImageFormat.WebP });
specs.Should().NotBeEmpty(use.Key);
foreach (var spec in specs)
{
var name = MediaPaths.FileName(spec.Ratio.Name, spec.Width, spec.Format);
MediaPaths.TryParse($"{use.Module}/{use.SetHash(Sha)}/{name}", out _).Should().BeTrue($"{use.Key}: {name}");
}
}
}
[Fact]
public void The_keys_are_unique() => GalleryUses.All.Select(u => u.Key).Should().OnlyHaveUniqueItems();
[Fact]
public void A_mosaic_use_has_a_desktop_and_a_phone_shape_and_a_masonry_use_has_none_of_its_own()
{
GalleryUses.MosaicLarge.Ratios.Select(r => r.Name).Should().Equal("tilelarge", "tilelargem");
GalleryUses.Masonry.Natural.Should().BeTrue();
GalleryUses.Masonry.PlansFor(3000, 2000).Should().ContainSingle().Which.Ratio.Should().Be(new Ratio("natural", 3000, 2000));
}
[Fact]
public void A_natural_use_keeps_the_photos_own_shape_without_a_crop()
{
var spec = VariantPlanner.Plan(3000, 2000, GalleryUses.Masonry.PlansFor(3000, 2000), new HashSet<ImageFormat> { ImageFormat.WebP })
.Single(s => s.Width == 800);
(spec.Crop.Width, spec.Crop.Height).Should().Be((3000, 2000));
spec.Height.Should().Be(533);
}
[Fact]
public void The_crop_of_a_use_changes_its_set_hash_but_not_the_set_hash_of_another_use()
{
var focus = new Dictionary<string, FocusSetting> { ["tilelarge"] = new(0.2, 0.3, 1.5) };
GalleryUses.MosaicLarge.SetHash(Sha, focus).Should().NotBe(GalleryUses.MosaicLarge.SetHash(Sha));
GalleryUses.MosaicWide.SetHash(Sha, focus).Should().Be(GalleryUses.MosaicWide.SetHash(Sha));
GalleryUses.MosaicLarge.SetHash(Sha, focus).Should().Be(GalleryUses.MosaicLarge.SetHash(Sha, focus));
}
[Fact]
public void A_natural_use_does_not_depend_on_a_crop()
=> GalleryUses.Masonry.SetHash(Sha, new Dictionary<string, FocusSetting> { ["natural"] = new(0.1, 0.1, 2) }).Should().Be(GalleryUses.Masonry.SetHash(Sha));
[Fact]
public void The_set_hash_formula_is_the_one_the_hero_uses_today()
{
// Same inputs through the shared function the hero calls: the library does not invent a second scheme.
var hero = new MediaUseDefinition("hero", "hero", [new("wide", 3, 2, [640]), new("tall", 4, 5, [480])]);
var focus = new Dictionary<string, FocusSetting> { ["wide"] = new(0.3, 0.4, 1.2) };
hero.SetHash(Sha, focus).Should().Be(MediaPaths.SetHash(Sha, [("wide", new FocusSetting(0.3, 0.4, 1.2)), ("tall", FocusSetting.Centre)]));
}
}
@@ -0,0 +1,72 @@
namespace SlpModularCms.Core.Media.Library;
/// <summary>One ratio of a use: its name (part of the file name), its shape and the widths cut for it.</summary>
public sealed record UseRatio(string Name, int Numerator, int Denominator, IReadOnlyList<int> Widths);
/// <summary>
/// What a USE of a photo needs (a place on a page, not the photo itself): the ratios with their widths, or the natural
/// shape of the photo without a crop. A photo can have several uses; each use has its own crop (focus per ratio) and so
/// its own set. Pure data, no behaviour that moves anything.
/// </summary>
/// <param name="Key">For example <c>mosaic-large</c>; unique.</param>
/// <param name="Module">The module whose pages show it (also its media area).</param>
/// <param name="Ratios">Empty for a natural-ratio use.</param>
/// <param name="Natural">The photo is shown in its own shape: no crop, one ratio taken from the photo itself.</param>
public sealed record MediaUseDefinition(string Key, string Module, IReadOnlyList<UseRatio> Ratios, bool Natural = false)
{
public const string NaturalRatioName = "natural";
/// <summary>The ratio plans for a photo, given the focus per ratio name (centre when none is given).</summary>
public IReadOnlyList<RatioPlan> PlansFor(int sourceWidth, int sourceHeight, IReadOnlyDictionary<string, FocusSetting>? focus = null)
{
if (Natural)
{
var widths = Ratios.Count > 0 ? Ratios[0].Widths : [];
return [new RatioPlan(new Ratio(NaturalRatioName, sourceWidth, sourceHeight), FocusSetting.Centre, widths)];
}
return Ratios
.Select(r => new RatioPlan(new Ratio(r.Name, r.Numerator, r.Denominator), focus is not null && focus.TryGetValue(r.Name, out var f) ? f : FocusSetting.Centre, r.Widths))
.ToList();
}
/// <summary>
/// The set hash of this use for a photo: the existing formula over the photo's hash and the crops of this use, so a
/// photo gets the same address for the same use and crops, and another use never shares a set by accident.
/// A natural-ratio use has no crop, so its set depends on the photo alone (and the use key).
/// </summary>
public string SetHash(string originalSha256, IReadOnlyDictionary<string, FocusSetting>? focus = null)
{
var crops = Natural
? [(NaturalRatioName, FocusSetting.Centre)]
: Ratios.Select(r => (r.Name, focus is not null && focus.TryGetValue(r.Name, out var f) ? f : FocusSetting.Centre)).ToList();
return MediaPaths.SetHash(originalSha256, crops);
}
}
/// <summary>
/// The uses of the gallery that the Andrea Slop Fotografie site needs (answer of the ASF session, 2026-11-17): the
/// mosaic tiles (a shape fixed per page by the number and order of the photos, a desktop and a phone shape each) and the
/// masonry portfolio (natural shape). Ratio names are letters only, 2 to 10 long, so they fit the address allow-list.
/// </summary>
public static class GalleryUses
{
public const string Module = "gallery";
private static readonly int[] Large = [480, 640, 960, 1280];
private static readonly int[] Small = [312, 480, 640];
/// <summary>Photo 1 of 2 to 4: about 1:1 on desktop (max about 656 wide), 4:3 on a phone.</summary>
public static readonly MediaUseDefinition MosaicLarge = new("mosaic-large", Module, [new("tilelarge", 1, 1, Large), new("tilelargem", 4, 3, Large)]);
/// <summary>Photo 2 of 3 or 4: about 2.2:1 on desktop, 3:2 on a phone.</summary>
public static readonly MediaUseDefinition MosaicWide = new("mosaic-wide", Module, [new("tilewide", 11, 5, Large), new("tilewidem", 3, 2, Large)]);
/// <summary>Photos 3 and 4 of 4: about 1.04:1 on desktop (max about 312 wide), 1:1 on a phone.</summary>
public static readonly MediaUseDefinition MosaicSmall = new("mosaic-small", Module, [new("tilesmall", 26, 25, Small), new("tilesmallm", 1, 1, Small)]);
/// <summary>The portfolio: the photo's own shape, no crop; thumbnail widths about 480 and 800.</summary>
public static readonly MediaUseDefinition Masonry = new("masonry", Module, [new(MediaUseDefinition.NaturalRatioName, 1, 1, [480, 800])], Natural: true);
public static IReadOnlyList<MediaUseDefinition> All { get; } = [MosaicLarge, MosaicWide, MosaicSmall, Masonry];
}