From d3525c1316cf932f390d911505d90dc96cb6a24a Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Mon, 16 Nov 2026 16:54:37 +0100 Subject: [PATCH] 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 --- .../assumptions-and-open-questions.md | 2 + .../site-contract-agreed-with-asf.md | 6 + .../HeroAsfSiteE2ETests.cs | 140 ++++++++++++++++++ .../HeroInjectionTests.cs | 8 +- .../HeroTextOnlyTests.cs | 67 +++++++++ .../Services/HeroInjection.cs | 4 +- .../Services/HeroPictureRenderer.cs | 60 ++++++-- 7 files changed, 271 insertions(+), 16 deletions(-) create mode 100644 src/SlpModularCms.Api.Tests/HeroAsfSiteE2ETests.cs create mode 100644 src/SlpModularCms.Modules.Hero.Tests/HeroTextOnlyTests.cs diff --git a/aidlc-docs/features/hero-module/assumptions-and-open-questions.md b/aidlc-docs/features/hero-module/assumptions-and-open-questions.md index a8a461c..e42dbfb 100644 --- a/aidlc-docs/features/hero-module/assumptions-and-open-questions.md +++ b/aidlc-docs/features/hero-module/assumptions-and-open-questions.md @@ -26,3 +26,5 @@ Kept while Bryan was away (2026-11-16): no questions to him, a likely assumption | 20 | **ASF production** hosting (IIS, MijnHostingPartner): can the application pool write a data directory, and is it backed up? | Unknown; Bryan tests with ASF. The storage sits behind `IMediaStore` so a database store can follow. | Build the database store (2 to 3 days). | `IMediaStore`, `FileSystemMediaStore` | | 21 | **SLS title** on the page. | The CMS delivers one title with line breaks; the SLP site derives the screen-reader title from the lines and adds the brand prefix itself. | Another split (a separate `heading` field) would be a model change. | `HeroSaveRequest.Title`, `HeroPictureRenderer` (`
`), SLS site | | 22 | **Production seed**. | Not done; only the test hosts, with Bryan's password, after his go. | n/a | `scripts/Seed-Hero.ps1`, `docs/release-checklist-productie.md` section 4b | +| 23 | What does the host write for a hero **without any slide** (SLP Software)? | A text-only region in the first HTML (so a CMS change is visible without JavaScript, LCP is the text): `h1.cms-hero-title` with an optional `span.cms-hero-tagline` and `span.cms-hero-title-lines` (title lines separated by `
`, no brand prefix), then `p.cms-hero-lead` (lead, line breaks as `
`) and `p.cms-hero-buttons` with `a.cms-hero-button`. Nothing to preload, and the preload region is left as built. A hero WITH slides but none usable stays empty (ASF unchanged). If the ASF CMS hero ever has no slides at all, ASF would get this text region instead of its baked hero. | A flag per site, or text-only only when the markers say so (for instance `CMS:HERO-TEXT`). | `HeroPictureRenderer.Render` and `TextOnlyRegion`, `HeroHtmlTransform.ApplyAsync`, `HeroTextOnlyTests` | +| 24 | Does the data block carry the hero when it has no slides? | Yes (intended): the site reads the hero from the data block, the baked block is `{}`. | n/a | `HeroPageModel`, `HeroDataContributor` | diff --git a/aidlc-docs/features/hero-module/inception/requirements/site-contract-agreed-with-asf.md b/aidlc-docs/features/hero-module/inception/requirements/site-contract-agreed-with-asf.md index 7dd9c3d..3545b7e 100644 --- a/aidlc-docs/features/hero-module/inception/requirements/site-contract-agreed-with-asf.md +++ b/aidlc-docs/features/hero-module/inception/requirements/site-contract-agreed-with-asf.md @@ -72,3 +72,9 @@ Folder `web/contract/host-injection/hero/` (README with all rules, `seed.json`, 7. Seed (first focus estimates by looking at the photos, (x,y) from the top left): hero-8 (0.50,0.55), hero-7 (0.45,0.50), hero-1 (0.55,0.60), hero-2 (0.45,0.45), hero-3 (0.45,0.40), hero-4 (0.45,0.35), hero-5 (0.50,0.55), hero-6 (0.50,0.35). Bryan adjusts them in the admin. Not yet built on the ASF side (after this design): hydration and slider from the data block, marker injection in the build, e2e against a stand-in host, the baked fallback from the CMS-driven format. + +## Text-only hero (SLP Software), added 2026-11-16 + +A hero **without any slide** is written as text (SLP Software has no images; the site reads the same hero from the data block): + +`

` + (tagline: `TAGLINE `) + `LINE
LINE

` + (lead: `

LEAD

`, line breaks as `
`) + (buttons: `

LABEL...

`, at most two). The title lines carry no brand prefix: the site adds "SLP Software:" to its own screen-reader title. There is nothing to preload, so `CMS:HERO-PRELOAD` is left as the build made it. Everything is HTML-encoded. A hero with slides of which none is usable is still empty (the ASF golden files are unchanged). diff --git a/src/SlpModularCms.Api.Tests/HeroAsfSiteE2ETests.cs b/src/SlpModularCms.Api.Tests/HeroAsfSiteE2ETests.cs new file mode 100644 index 0000000..ccd235f --- /dev/null +++ b/src/SlpModularCms.Api.Tests/HeroAsfSiteE2ETests.cs @@ -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; + +/// +/// Runs the real master host with ASF settings over the REAL build of the ASF website (its dist/client), with the +/// hero of ASF's seed made through the module's services. Optional: it only runs when ASF_WEB_DIR points at the +/// web folder of the ASF website repository (with a build in dist/client); otherwise it does nothing. +/// With HERO_E2E_OUT set it also writes the served page and the API answer there, so they can be compared +/// with the website's own renderer. +/// +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 { ["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 +{ + private readonly AsfSiteFactory _factory; + + public HeroAsfSiteE2ETests(AsfSiteFactory factory) => _factory = factory; + + private async Task InScope(Func> 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(); + 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(); + 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().UploadAsync(new MemoryStream(photo), photo.Length, caller, default)); + var focus = slide.GetProperty("focus"); + await InScope(sp => sp.GetRequiredService().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().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("").And.Contain(""); + page.Should().NotContain("").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(); + db.Heroes.RemoveRange(await db.Heroes.ToListAsync()); + await db.SaveChangesAsync(); + return 0; + }); + } + } +} diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroInjectionTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroInjectionTests.cs index df0699f..bf154a2 100644 --- a/src/SlpModularCms.Modules.Hero.Tests/HeroInjectionTests.cs +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroInjectionTests.cs @@ -112,9 +112,9 @@ public class HeroPictureRendererTests } [Fact] - public void ATitleWithoutSlides_OrWithoutATitle_RendersNothing() + public void ASlideThatIsUnusable_OrNoTitle_RendersNothing() { - HeroPictureRenderer.Render(new PublicHeroDto("single", "Titel", null, null, [], [])).IsEmpty.Should().BeTrue(); + HeroPictureRenderer.Render(new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))])).IsEmpty.Should().BeTrue(); HeroPictureRenderer.Render(Hero(title: " ")).IsEmpty.Should().BeTrue(); HeroPictureRenderer.Render(null).IsEmpty.Should().BeTrue(); } @@ -200,9 +200,9 @@ public class HeroHtmlTransformTests [Fact] public async Task AHeroWithoutAUsableSlide_LeavesTheBakedHero() { - var textOnly = new PublicHeroDto("single", "Titel", null, null, [], []); + var unusable = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]); - (await Sut(textOnly).ApplyAsync(Page, Context, default)).Should().Be(Page); + (await Sut(unusable).ApplyAsync(Page, Context, default)).Should().Be(Page); } [Fact] diff --git a/src/SlpModularCms.Modules.Hero.Tests/HeroTextOnlyTests.cs b/src/SlpModularCms.Modules.Hero.Tests/HeroTextOnlyTests.cs new file mode 100644 index 0000000..ecd8751 --- /dev/null +++ b/src/SlpModularCms.Modules.Hero.Tests/HeroTextOnlyTests.cs @@ -0,0 +1,67 @@ +using FluentAssertions; +using NSubstitute; +using SlpModularCms.Core.Hosting.Website; +using SlpModularCms.Modules.Hero.Models; +using SlpModularCms.Modules.Hero.Services; + +namespace SlpModularCms.Modules.Hero.Tests; + +/// A hero without slides (SLP Software): the host writes the title, the lead and the buttons into the first HTML. +public class HeroTextOnlyTests +{ + private static PublicHeroDto TextOnly(string title = "Websites,\napps\n& meer.", string? tagline = null, string? lead = "Een lead.", params HeroButtonDto[] buttons) + => new("single", title, tagline, lead, buttons, []); + + [Fact] + public void ADisplayedHeroWithoutSlides_IsATextOnlyRegion_WithNothingToPreload() + { + var hero = TextOnly(buttons: [new HeroButtonDto("Bekijk pakketten", "#pakketten"), new HeroButtonDto("Plan een gesprek", "#contact")]); + + var injection = HeroPictureRenderer.Render(hero); + + injection.Region.Should().Be( + "

Websites,
apps
& meer.

" + + "

Een lead.

" + + "

Bekijk pakkettenPlan een gesprek

"); + injection.Preload.Should().BeEmpty(); + injection.IsEmpty.Should().BeFalse(); + } + + [Fact] + public void WithoutALead_OrButtons_ThosePartsAreLeftOut_AndATaglineComesFirst() + { + var injection = HeroPictureRenderer.Render(TextOnly("Titel", "Label", null)); + + injection.Region.Should().Be("

Label Titel

"); + } + + [Fact] + public void TheLead_KeepsItsLineBreaks_AndEverythingIsEncoded() + { + var injection = HeroPictureRenderer.Render(TextOnly("x", null, "a & b\nc \"d\"", new HeroButtonDto("", "/p?a=1&b=2"))); + + injection.Region.Should().Contain("<b>x</b>").And.Contain("a & b
c "d"").And.Contain("href=\"/p?a=1&b=2\"><i>"); + injection.Region.Should().NotContain("").And.NotContain(""); + } + + [Fact] + public void AHeroWithSlidesThatAreAllUnusable_StaysEmpty_AsForTheAsfSite() + { + var hero = new PublicHeroDto("single", "Titel", null, null, [], [new PublicSlideDto("x", "", false, new PublicImageDto(1, 1, new FocusDto(0.5, 0.5, 1), "/m.webp", []))]); + + HeroPictureRenderer.Render(hero).IsEmpty.Should().BeTrue(); + } + + [Fact] + public async Task TheTransform_FillsTheTextRegion_AndLeavesThePreloadRegionAlone() + { + var model = Substitute.For(); + model.GetAsync(default).ReturnsForAnyArgs(TextOnly()); + const string page = "baked"; + + var html = await new HeroHtmlTransform(model).ApplyAsync(page, new HtmlTransformContext("https://x"), default); + + html.Should().Contain("

").And.NotContain("baked"); + html.Should().Contain(""); + } +} diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroInjection.cs b/src/SlpModularCms.Modules.Hero/Services/HeroInjection.cs index c3fa1c1..4b61714 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroInjection.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroInjection.cs @@ -64,7 +64,9 @@ public sealed class HeroHtmlTransform(IHeroPageModel model) : IHtmlPageTransform } html = HtmlMarkerEditor.ReplaceRegion(html, HtmlRegion, rendered.Region); - return HtmlMarkerEditor.ReplaceRegion(html, PreloadRegion, rendered.Preload); + + // A text-only hero has nothing to preload: the page keeps whatever its build put there. + return rendered.Preload.Length == 0 ? html : HtmlMarkerEditor.ReplaceRegion(html, PreloadRegion, rendered.Preload); } } diff --git a/src/SlpModularCms.Modules.Hero/Services/HeroPictureRenderer.cs b/src/SlpModularCms.Modules.Hero/Services/HeroPictureRenderer.cs index 62cc056..22f33cf 100644 --- a/src/SlpModularCms.Modules.Hero/Services/HeroPictureRenderer.cs +++ b/src/SlpModularCms.Modules.Hero/Services/HeroPictureRenderer.cs @@ -36,7 +36,14 @@ public static class HeroPictureRenderer return HeroInjection.None; } - var slide = (hero.Slides ?? []).FirstOrDefault(IsUsable); + // A hero without any slide is a text-only hero (SLP Software): the region holds the title lines, the lead and + // the buttons, and there is nothing to preload. A hero WITH slides of which none is usable stays empty (ASF). + if ((hero.Slides ?? []).Count == 0) + { + return new HeroInjection(TextOnlyRegion(hero), string.Empty); + } + + var slide = hero.Slides.FirstOrDefault(IsUsable); if (slide is null) { return HeroInjection.None; @@ -81,23 +88,54 @@ public static class HeroPictureRenderer html.Append("").Append(Encode(hero.Tagline)).Append(" "); } - html.Append("").Append(string.Join("
", hero.Title.Replace("\r\n", "\n").Split('\n').Select(Encode))).Append("

"); + html.Append("").Append(Lines(hero.Title)).Append(""); + AppendButtons(html, hero); + return html.ToString(); + } - var buttons = (hero.Buttons ?? []).Where(IsUsable).Take(2).ToList(); - if (buttons.Count > 0) + /// + /// The text-only hero: h1.cms-hero-title with the tagline (when there is one) and the title lines in + /// span.cms-hero-title-lines separated by <br> (no brand prefix: the site adds it to its own + /// screen-reader title), then p.cms-hero-lead and the buttons row. + /// + private static string TextOnlyRegion(PublicHeroDto hero) + { + var html = new StringBuilder(); + html.Append("

"); + if (!string.IsNullOrWhiteSpace(hero.Tagline)) { - html.Append("

"); - foreach (var button in buttons) - { - html.Append("").Append(Encode(button.Label)).Append(""); - } - - html.Append("

"); + html.Append("").Append(Encode(hero.Tagline)).Append(" "); } + html.Append("").Append(Lines(hero.Title)).Append("

"); + if (!string.IsNullOrWhiteSpace(hero.Lead)) + { + html.Append("

").Append(Lines(hero.Lead.Trim())).Append("

"); + } + + AppendButtons(html, hero); return html.ToString(); } + private static string Lines(string text) => string.Join("
", text.Replace("\r\n", "\n").Split('\n').Select(Encode)); + + private static void AppendButtons(StringBuilder html, PublicHeroDto hero) + { + var buttons = (hero.Buttons ?? []).Where(IsUsable).Take(2).ToList(); + if (buttons.Count == 0) + { + return; + } + + html.Append("

"); + foreach (var button in buttons) + { + html.Append("").Append(Encode(button.Label)).Append(""); + } + + html.Append("

"); + } + private static string Preload(string src, string wide, string tall) { const string head = "