Files
slp-modular-cms/src/SlpModularCms.Modules.Hero.Tests/HeroMediaUsageProviderTests.cs
T
SluijsensandClaude Sonnet 5.5 7263a9a39a
Continuous Integration / config-sls (pull_request) Successful in 8s
Continuous Integration / changes (pull_request) Successful in 17s
Continuous Integration / config-asf (pull_request) Successful in 9s
Continuous Integration / frontend-prepare (pull_request) Successful in 30s
Continuous Integration / compile-sites (pull_request) Successful in 1m40s
Continuous Integration / frontend-build (pull_request) Successful in 1m9s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m48s
Continuous Integration / frontend-lint (pull_request) Successful in 41s
Continuous Integration / frontend-test (pull_request) Successful in 2m26s
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 10m10s
Mediabibliotheek: prullenbak, delete met tweede bevestiging, laatste gebruik, bestandsnaam (backend)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 15:08:05 +01:00

195 lines
9.2 KiB
C#

using FluentAssertions;
using SlpModularCms.Core.Media.Library;
using SlpModularCms.Modules.Hero.Models;
using SlpModularCms.Modules.Hero.Services;
using Xunit;
namespace SlpModularCms.Modules.Hero.Tests;
public class HeroMediaUsageProviderTests
{
private static HeroMediaUsageProvider Provider(HeroTestRig rig) => new(rig.Repository, rig.Images);
private static Task<HeroSaveRequest> NoSlides() => Task.FromResult(Save([]));
private static HeroSaveRequest Save(params HeroSlideRequest[] slides) => new("Titel", null, null, "single", [], slides);
[Fact]
public async Task Lists_the_images_newest_first_with_a_thumbnail_and_reports_one_use_per_slide_with_its_position()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
rig.Time.Now = rig.Time.Now.AddMinutes(5);
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
rig.Time.Now = rig.Time.Now.AddMinutes(5);
var unused = await rig.Upload(HeroTestRig.Photo(seed: 3));
await rig.Service.SaveAsync(
Save(new HeroSlideRequest(null, Guid.Parse(b.Id), "tweede"), new HeroSlideRequest(null, Guid.Parse(a.Id), "eerste", IsActive: false)),
HeroTestRig.Admin, CancellationToken.None);
var provider = Provider(rig);
var photos = await provider.ListPhotosAsync(CancellationToken.None);
var uses = await provider.ListUsesAsync(photos.Select(p => p.Id).ToList(), CancellationToken.None);
provider.Module.Should().Be("hero");
photos.Select(p => p.Id).Should().Equal(Guid.Parse(unused.Id), Guid.Parse(b.Id), Guid.Parse(a.Id));
photos.Should().OnlyContain(p => p.ThumbnailUrl != null && p.ThumbnailUrl.StartsWith("/media/hero/"));
uses.Should().HaveCount(2);
uses.Single(u => u.PhotoId == Guid.Parse(b.Id)).Should().Be(new MediaUse("hero", Guid.Parse(b.Id), "slide", 1, true));
uses.Single(u => u.PhotoId == Guid.Parse(a.Id)).Should().Be(new MediaUse("hero", Guid.Parse(a.Id), "slide", 2, false));
uses.Should().NotContain(u => u.PhotoId == Guid.Parse(unused.Id));
}
[Fact]
public async Task Reading_changes_nothing()
{
using var rig = new HeroTestRig();
var image = await rig.Upload(HeroTestRig.Photo());
var provider = Provider(rig);
await provider.ListPhotosAsync(CancellationToken.None);
await provider.ListUsesAsync([Guid.Parse(image.Id)], CancellationToken.None);
await provider.ListTrashAsync(CancellationToken.None);
(await rig.Repository.CountImagesAsync(CancellationToken.None)).Should().Be(1);
rig.Db.ChangeTracker.HasChanges().Should().BeFalse();
}
[Fact]
public async Task An_upload_remembers_the_cleaned_original_file_name()
{
using var rig = new HeroTestRig();
var bytes = HeroTestRig.Photo();
await rig.Images.UploadAsync(new MemoryStream(bytes), bytes.Length, HeroTestRig.Admin, CancellationToken.None, "C:\\fotos\\zomer\u0007 2026.jpg");
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().OriginalFileName.Should().Be("zomer 2026.jpg");
}
[Theory]
[InlineData(null, null)]
[InlineData(" ", null)]
[InlineData("../../etc/passwd", "passwd")]
[InlineData("a/b\\c.jpg", "c.jpg")]
public void File_names_are_cleaned_for_display_only(string? input, string? expected) => HeroFileNames.Clean(input).Should().Be(expected);
[Fact]
public void A_very_long_file_name_is_cut() => HeroFileNames.Clean(new string('a', 500))!.Length.Should().Be(HeroFileNames.MaxLength);
[Fact]
public async Task Trashing_a_photo_in_use_removes_its_slides_hides_it_and_remembers_the_last_use()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo(seed: 1));
var b = await rig.Upload(HeroTestRig.Photo(seed: 2));
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "een"), new HeroSlideRequest(null, Guid.Parse(b.Id), "twee")), HeroTestRig.Admin, CancellationToken.None);
var provider = Provider(rig);
rig.Time.Now = rig.Time.Now.AddDays(3);
var removed = await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
removed.Should().Be(1);
(await provider.ListPhotosAsync(CancellationToken.None)).Select(p => p.Id).Should().Equal(Guid.Parse(b.Id));
var trashed = (await provider.ListTrashAsync(CancellationToken.None)).Single();
trashed.Id.Should().Be(Guid.Parse(a.Id));
trashed.DeletedAt.Should().Be(rig.Time.Now);
trashed.UnusedSince.Should().Be(rig.Time.Now);
(await rig.Service.GetAsync(CancellationToken.None)).Slides.Should().ContainSingle().Which.Image.Id.Should().Be(b.Id);
}
[Fact]
public async Task A_trashed_photo_cannot_be_put_in_a_slide_and_is_not_cut_in_the_background()
{
using var rig = new HeroTestRig();
var a = await rig.UploadWithoutRendering(HeroTestRig.Photo());
var provider = Provider(rig);
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
var save = () => rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
await save.Should().ThrowAsync<HeroSaveRejectedException>();
(await rig.RenderPending()).Should().Be(0);
}
[Fact]
public async Task Trashing_an_unknown_or_already_trashed_photo_gives_null()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo());
var provider = Provider(rig);
(await provider.TrashAsync(Guid.NewGuid(), HeroTestRig.Admin, CancellationToken.None)).Should().BeNull();
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
(await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeNull();
}
[Fact]
public async Task Restoring_brings_the_photo_back_without_its_slides()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo());
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
var provider = Provider(rig);
await provider.TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
(await provider.RestoreAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeTrue();
(await provider.RestoreAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None)).Should().BeFalse();
(await provider.ListPhotosAsync(CancellationToken.None)).Should().ContainSingle();
(await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty();
(await provider.ListUsesAsync([Guid.Parse(a.Id)], CancellationToken.None)).Should().BeEmpty();
}
[Fact]
public async Task Uploading_the_same_photo_again_brings_it_back_from_the_trash()
{
using var rig = new HeroTestRig();
var bytes = HeroTestRig.Photo();
var first = await rig.Upload(bytes);
var provider = Provider(rig);
await provider.TrashAsync(Guid.Parse(first.Id), HeroTestRig.Admin, CancellationToken.None);
var again = await rig.Upload(bytes);
again.Id.Should().Be(first.Id);
(await provider.ListTrashAsync(CancellationToken.None)).Should().BeEmpty();
(await provider.ListPhotosAsync(CancellationToken.None)).Should().ContainSingle();
}
[Fact]
public async Task Saving_the_hero_without_a_slide_remembers_when_its_photo_was_last_used()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo());
await rig.Service.SaveAsync(Save(new HeroSlideRequest(null, Guid.Parse(a.Id), "foto")), HeroTestRig.Admin, CancellationToken.None);
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().UnusedSince.Should().BeNull();
rig.Time.Now = rig.Time.Now.AddDays(2);
await rig.Service.SaveAsync(Save(), HeroTestRig.Admin, CancellationToken.None);
(await Provider(rig).ListPhotosAsync(CancellationToken.None)).Single().UnusedSince.Should().Be(rig.Time.Now);
}
[Fact]
public async Task A_trashed_photo_is_emptied_from_the_trash_after_the_trash_period_and_its_files_go()
{
using var rig = new HeroTestRig();
var a = await rig.Upload(HeroTestRig.Photo());
var row = rig.Db.HeroImages.Single();
var (set, original) = (row.SetHash, row.OriginalKey);
await Provider(rig).TrashAsync(Guid.Parse(a.Id), HeroTestRig.Admin, CancellationToken.None);
rig.Time.Now = rig.Time.Now.AddDays(6);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().HaveCount(1);
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeTrue();
rig.Time.Now = rig.Time.Now.AddDays(2);
await rig.Cleanup.RunOnceAsync(CancellationToken.None);
rig.Db.HeroImages.Should().BeEmpty();
(await rig.Store.ListSetsAsync("hero", CancellationToken.None)).Should().NotContain(s => s.SetHash == set);
(await rig.Store.ExistsAsync(original, CancellationToken.None)).Should().BeFalse();
}
}