Hero: tekst-only regio voor een hero zonder slides (SLP), e2e-test op de echte ASF-build, bijgewerkte aannames en contract
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SlpModularCms.Modules.Hero.Data;
|
||||
using SlpModularCms.Modules.Hero.Models;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Api.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the real master host with ASF settings over the REAL build of the ASF website (its <c>dist/client</c>), with the
|
||||
/// hero of ASF's seed made through the module's services. Optional: it only runs when <c>ASF_WEB_DIR</c> points at the
|
||||
/// <c>web</c> folder of the ASF website repository (with a build in <c>dist/client</c>); otherwise it does nothing.
|
||||
/// With <c>HERO_E2E_OUT</c> set it also writes the served page and the API answer there, so they can be compared
|
||||
/// with the website's own renderer.
|
||||
/// </summary>
|
||||
public sealed class AsfSiteFactory : AsfVariantFactory, IDisposable
|
||||
{
|
||||
public string WebRoot { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-site-" + Guid.NewGuid().ToString("N"));
|
||||
public string MediaDirectory { get; } = Path.Combine(Path.GetTempPath(), "slp-asf-media-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
var web = Environment.GetEnvironmentVariable("ASF_WEB_DIR");
|
||||
if (!string.IsNullOrEmpty(web))
|
||||
{
|
||||
CopyDirectory(Path.Combine(web, "dist", "client"), Path.Combine(WebRoot, "web"));
|
||||
}
|
||||
|
||||
builder.UseWebRoot(WebRoot);
|
||||
builder.ConfigureAppConfiguration((_, config) =>
|
||||
config.AddInMemoryCollection(new Dictionary<string, string?> { ["Media:Directory"] = MediaDirectory, ["Website:PublicBaseUrl"] = "https://test.andreaslop.nl" }));
|
||||
}
|
||||
|
||||
private static void CopyDirectory(string from, string to)
|
||||
{
|
||||
Directory.CreateDirectory(to);
|
||||
foreach (var file in Directory.GetFiles(from)) File.Copy(file, Path.Combine(to, Path.GetFileName(file)), overwrite: true);
|
||||
foreach (var dir in Directory.GetDirectories(from)) CopyDirectory(dir, Path.Combine(to, Path.GetFileName(dir)));
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
if (disposing)
|
||||
{
|
||||
foreach (var directory in new[] { WebRoot, MediaDirectory })
|
||||
{
|
||||
if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Collection("HeroDatabase")]
|
||||
public class HeroAsfSiteE2ETests : IClassFixture<AsfSiteFactory>
|
||||
{
|
||||
private readonly AsfSiteFactory _factory;
|
||||
|
||||
public HeroAsfSiteE2ETests(AsfSiteFactory factory) => _factory = factory;
|
||||
|
||||
private async Task<T> InScope<T>(Func<IServiceProvider, Task<T>> action)
|
||||
{
|
||||
await using var scope = _factory.Services.CreateAsyncScope();
|
||||
return await action(scope.ServiceProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheRealAsfBuild_GetsTheSeededHero_InItsFirstHtml()
|
||||
{
|
||||
var web = Environment.GetEnvironmentVariable("ASF_WEB_DIR");
|
||||
if (string.IsNullOrEmpty(web) || !File.Exists(Path.Combine(web, "dist", "client", "index.html")))
|
||||
{
|
||||
return; // optional check: needs the ASF website repository and its build
|
||||
}
|
||||
|
||||
var client = _factory.CreateClient();
|
||||
var caller = Guid.NewGuid();
|
||||
await InScope(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<HeroDbContext>();
|
||||
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
var seed = JsonDocument.Parse(File.ReadAllText(Path.Combine(web, "contract", "host-injection", "hero", "seed.json"))).RootElement;
|
||||
var slides = new List<HeroSlideRequest>();
|
||||
foreach (var slide in seed.GetProperty("slides").EnumerateArray())
|
||||
{
|
||||
var photo = File.ReadAllBytes(Path.Combine(web, "src", "assets", slide.GetProperty("file").GetString()!));
|
||||
var image = await InScope(sp => sp.GetRequiredService<IHeroImageService>().UploadAsync(new MemoryStream(photo), photo.Length, caller, default));
|
||||
var focus = slide.GetProperty("focus");
|
||||
await InScope(sp => sp.GetRequiredService<IHeroImageService>().SetFocusAsync(
|
||||
Guid.Parse(image.Id), new HeroFocusRequest(new FocusDto(focus.GetProperty("x").GetDouble(), focus.GetProperty("y").GetDouble(), 1)), caller, default));
|
||||
slides.Add(new HeroSlideRequest(null, Guid.Parse(image.Id), slide.GetProperty("alt").GetString()));
|
||||
}
|
||||
|
||||
await InScope(sp => sp.GetRequiredService<IHeroService>().SaveAsync(
|
||||
new HeroSaveRequest(seed.GetProperty("title").GetString(), seed.GetProperty("tagline").GetString(), null, seed.GetProperty("display").GetString(),
|
||||
seed.GetProperty("buttons").EnumerateArray().Select(b => new HeroButtonRequest(b.GetProperty("label").GetString(), b.GetProperty("target").GetString())).ToList(),
|
||||
true, null, null, slides), caller, default));
|
||||
|
||||
var response = await client.GetAsync("/");
|
||||
var page = await response.Content.ReadAsStringAsync();
|
||||
var api = await client.GetStringAsync("/api/v1/hero");
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
page.Should().Contain("<picture class=\"cms-hero-picture\">").And.Contain("<link rel=\"preload\" as=\"image\"");
|
||||
page.Should().Contain("<!--CMS:HERO-HTML--><picture").And.Contain("</p><!--/CMS:HERO-HTML-->");
|
||||
page.Should().NotContain("<!--CMS:DATA-->").And.Contain("\"hero\":{");
|
||||
JsonDocument.Parse(api).RootElement.GetProperty("slides").GetArrayLength().Should().Be(8);
|
||||
|
||||
var outDir = Environment.GetEnvironmentVariable("HERO_E2E_OUT");
|
||||
if (!string.IsNullOrEmpty(outDir))
|
||||
{
|
||||
Directory.CreateDirectory(outDir);
|
||||
File.WriteAllText(Path.Combine(outDir, "page.html"), page);
|
||||
File.WriteAllText(Path.Combine(outDir, "api.json"), api);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
await InScope(async sp =>
|
||||
{
|
||||
var db = sp.GetRequiredService<HeroDbContext>();
|
||||
db.Heroes.RemoveRange(await db.Heroes.ToListAsync());
|
||||
await db.SaveChangesAsync();
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user