H2: Hero-module (tabellen en migratie, beheer-API, publieke GET /hero, afbeeldingsupload met focus, opschoonservice, teksten) en OpenAPI-snapshot
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
using ImageMagick;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Core.Media;
|
||||
using SlpModularCms.Core.Media.Magick;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Repositories;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
public sealed class TestTime(DateTimeOffset now) : TimeProvider
|
||||
{
|
||||
public DateTimeOffset Now { get; set; } = now;
|
||||
|
||||
public override DateTimeOffset GetUtcNow() => Now;
|
||||
}
|
||||
|
||||
/// <summary>A hero module over an in-memory database, a temporary media directory and the real image library.</summary>
|
||||
public sealed class HeroTestRig : IDisposable
|
||||
{
|
||||
public static readonly Guid Admin = Guid.Parse("11111111-2222-3333-4444-555555555555");
|
||||
|
||||
public TestTime Time { get; } = new(new DateTimeOffset(2026, 11, 16, 12, 0, 0, TimeSpan.Zero));
|
||||
public string Directory { get; } = Path.Combine(Path.GetTempPath(), "slp-hero-tests-" + Guid.NewGuid().ToString("N"));
|
||||
public MediaOptions Media { get; }
|
||||
public HeroDbContext Db { get; }
|
||||
public IHeroRepository Repository { get; }
|
||||
public FileSystemMediaStore Store { get; }
|
||||
public MagickImageProcessor Processor { get; }
|
||||
public MediaStatus Status { get; } = new();
|
||||
public IIndexHtmlCache IndexCache { get; } = Substitute.For<IIndexHtmlCache>();
|
||||
public HeroImageService Images { get; }
|
||||
public HeroService Service { get; }
|
||||
public PublicHeroService Public { get; }
|
||||
public HeroMediaCleanup Cleanup { get; }
|
||||
public HeroOptions HeroOptions { get; } = new();
|
||||
|
||||
public HeroTestRig(int maxPublishedSlides = 50)
|
||||
{
|
||||
HeroOptions.MaxPublishedSlides = maxPublishedSlides;
|
||||
Media = new MediaOptions { Directory = Directory, BusyWaitSeconds = 0 };
|
||||
var mediaOptions = Options.Create(Media);
|
||||
Db = new HeroDbContext(new DbContextOptionsBuilder<HeroDbContext>().UseInMemoryDatabase("hero-" + Guid.NewGuid()).Options);
|
||||
Repository = new HeroRepository(Db);
|
||||
Store = new FileSystemMediaStore(mediaOptions);
|
||||
Processor = new MagickImageProcessor(mediaOptions, NullLogger<MagickImageProcessor>.Instance);
|
||||
Status.SetProcessor(true, Processor.SupportedInput, Processor.SupportedOutput);
|
||||
Status.SetDirectory(true);
|
||||
var gate = new MediaProcessingGate(mediaOptions);
|
||||
var guard = new MediaUploadGuard(Processor, gate, Status, mediaOptions);
|
||||
Images = new HeroImageService(guard, Processor, Store, gate, Repository, mediaOptions, NullLogger<HeroImageService>.Instance, Time, IndexCache);
|
||||
Service = new HeroService(Repository, Options.Create(HeroOptions), NullLogger<HeroService>.Instance, Time, IndexCache);
|
||||
Public = new PublicHeroService(Repository, Options.Create(HeroOptions), Time);
|
||||
Cleanup = new HeroMediaCleanup(Repository, Store, mediaOptions, NullLogger<HeroMediaCleanup>.Instance, Time);
|
||||
}
|
||||
|
||||
/// <summary>A photo-like fixture: the left half red, the right half blue, with a camera tag.</summary>
|
||||
public static byte[] Photo(int width = 1600, int height = 1067, MagickFormat format = MagickFormat.Jpeg, byte seed = 0)
|
||||
{
|
||||
using var image = new MagickImage(MagickColors.Red, (uint)width, (uint)height);
|
||||
using var blue = new MagickImage(MagickColors.Blue, (uint)(width / 2), (uint)height);
|
||||
image.Composite(blue, width - width / 2, 0, CompositeOperator.Over);
|
||||
if (seed > 0)
|
||||
{
|
||||
using var dot = new MagickImage(new MagickColor((byte)(seed * 20), 0, 0), 10, 10);
|
||||
image.Composite(dot, 3, 3, CompositeOperator.Over);
|
||||
}
|
||||
|
||||
var exif = new ExifProfile();
|
||||
exif.SetValue(ExifTag.Software, "camera firmware");
|
||||
image.SetProfile(exif);
|
||||
using var stream = new MemoryStream();
|
||||
image.Write(stream, format);
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
public Task<SlpModularCms.Modules.Hero.Models.HeroImageDto> Upload(byte[] bytes)
|
||||
=> Images.UploadAsync(new MemoryStream(bytes), bytes.Length, Admin, CancellationToken.None);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Db.Dispose();
|
||||
if (System.IO.Directory.Exists(Directory))
|
||||
{
|
||||
System.IO.Directory.Delete(Directory, recursive: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user