ASF-injectie 3/5: CmsPageHtml (neutrale lijst + banner-HTML, prijsregels) en OfferingsHtmlTransform; golden files van ASF (16027f5) slagen

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 16:08:31 +01:00
co-authored by Claude Sonnet 5.5
parent a13661517d
commit ef4fb9dda5
29 changed files with 650 additions and 0 deletions
@@ -0,0 +1,7 @@
# Golden files of the website build (ASF)
`host-injection/` is a copy of `web/contract/host-injection` of the Andrea Slop Fotografie website repository,
ASF commit `16027f5` (branch `feature/asf-packages-cms`). It is the contract for the plain HTML the CMS host writes
between the CMS markers (see `host-injection/README.md` for the rules). `GoldenHtmlTests` runs `CmsPageHtml` against
every input/expected pair. When the website changes the contract it sends new files and the commit; replace the
folder and note the new commit here.
@@ -0,0 +1,28 @@
# Host injection contract (ASF site and CMS host)
The ASF build leaves markers in the prerendered pages; the CMS host fills them from live data. This folder holds the golden files for the two list regions. For every `NAME.input.json` there is a `NAME.expected.html`: the host's template must give exactly that text (the file ends with one newline that is not part of the HTML) for that input. The ASF repo tests its own implementation (`src/lib/cmsHtml.ts`) against the same files; to change the contract, change the code and run `UPDATE_GOLDEN=1 pnpm test src/lib/cmsHtml.test.ts`, then send the new files to the CMS session. The files come from ASF commit: see `git log -1 -- web/contract/host-injection` in the ASF repo.
## Inputs
- `offerings-*.input.json`: the answer of `GET /api/v1/offerings` (an array).
- `banner-*.input.json`: `{ "position": "top" | "bottom", "answer": <answer of GET /api/v1/offerings/banner?position=...> | null }`; `null` is a 204.
## Rules for the packages region (`<!--CMS:OFFERINGS-HTML-->`)
1. Keep a package only when all hold: `slug` is a string matching `^[a-z0-9-]+$`; `title` is a non-empty string after trimming; `features` is a non-empty array of non-empty strings (each trimmed); `price` is readable as whole euros above zero (below); the slug was not used by an earlier kept package (the first one wins). Trim all texts. Any other package is skipped.
2. Order by `displayOrder` (a number; missing counts as 0), ties keep the given order.
3. Price text to whole euros: trim, then the regex `^\D*?(\d{1,3}(?:\.\d{3})+|\d+)(?:,(?:-|0{1,2}))?\D*$`; group 1 without dots is the amount; it must be a whole number above zero. So "€ 175", "€ 1.250", "175,-", "€ 175,00" are readable; "op aanvraag", "€ 12,50", "€ 0", "175 of 185" are not.
4. Output: `<ul class="cms-offerings">` + one `<li>` per package + `</ul>`; nothing at all (empty string) when no package is kept.
5. Per package: `<li class="cms-offering" data-slug="SLUG"[ data-featured="true"]>` + `<h3>NAME</h3>` + `<p class="cms-price">€&nbsp;AMOUNT</p>` + `<ul class="cms-features">` + `<li>FEATURE</li>`... + `</ul>` + `<a href="/?pakket=SLUG#contact">Boek NAME</a>` + `</li>`. `data-featured="true"` only when `featured` is the boolean `true`. AMOUNT uses a dot as thousands separator (1.250). The literal text `&nbsp;` is written, not a character. `description`, `priceNote` and `ctaLabel` are not used.
6. Escape every text (name, features, slug, amount) as: `&` `&amp;`, `<` `&lt;`, `>` `&gt;`, `"` `&quot;`, `'` `&#39;`.
## Rules for the banner regions (`<!--CMS:BANNER-TOP-HTML-->`, `<!--CMS:BANNER-BOTTOM-HTML-->`)
1. `null` answer: empty string. Bottom: the title is required, otherwise empty string. Top: title or text is required, otherwise empty string.
2. Output `<aside class="cms-banner">` + the parts that are present, joined by one space, + `</aside>`. Parts: `<strong>TITLE</strong>`, `<span>TEXT</span>`, `<a href="URL">LABEL</a>` (trim all texts; empty means absent).
3. The link is written only when label and URL are both present and the URL is safe: it starts with `https://` and parses as a URL, or it starts with one `/` (not `//`, no backslash). Otherwise the link part is left out.
4. Escape as above (also in the `href`).
## Where the region sits
`<!--CMS:OFFERINGS-HTML-->…<!--/CMS:OFFERINGS-HTML-->` (and the two banner pairs) are written by the build; the host replaces what is between the markers and leaves the markers. See `aidlc-docs/features/asf-cms-host-injection/plan.md` for the other markers (data block, JSON-LD price, inline price).
@@ -0,0 +1 @@
<aside class="cms-banner"><strong>Kerstactie</strong> <span>Alleen in december</span> <a href="/contact">Boek de actie</a></aside>
@@ -0,0 +1,10 @@
{
"position": "bottom",
"answer": {
"title": "Kerstactie",
"text": "Alleen in december",
"linkLabel": "Boek de actie",
"linkUrl": "/contact",
"position": "bottom"
}
}
@@ -0,0 +1 @@
<aside class="cms-banner"><strong>Past er niets bij?</strong></aside>
@@ -0,0 +1,10 @@
{
"position": "bottom",
"answer": {
"title": "Past er niets bij?",
"text": null,
"linkLabel": null,
"linkUrl": null,
"position": "bottom"
}
}
@@ -0,0 +1 @@
<aside class="cms-banner"><strong>&lt;img src=x onerror=alert(1)&gt;</strong> <span>&quot;quote&quot; &amp; &#39;single&#39;</span> <a href="https://voorbeeld.nl/?a=1&amp;b=2">Meer</a></aside>
@@ -0,0 +1,10 @@
{
"position": "bottom",
"answer": {
"title": "<img src=x onerror=alert(1)>",
"text": "\"quote\" & 'single'",
"linkLabel": "Meer",
"linkUrl": "https://voorbeeld.nl/?a=1&b=2",
"position": "bottom"
}
}
@@ -0,0 +1,4 @@
{
"position": "bottom",
"answer": null
}
@@ -0,0 +1 @@
<aside class="cms-banner"><span>Nog twee plekken in december</span></aside>
@@ -0,0 +1,10 @@
{
"position": "top",
"answer": {
"title": "",
"text": "Nog twee plekken in december",
"linkLabel": null,
"linkUrl": null,
"position": "top"
}
}
@@ -0,0 +1 @@
<aside class="cms-banner"><strong>Nieuw</strong> <span>x</span></aside>
@@ -0,0 +1,10 @@
{
"position": "top",
"answer": {
"title": "Nieuw",
"text": "x",
"linkLabel": "Hier",
"linkUrl": "http://voorbeeld.nl",
"position": "top"
}
}
@@ -0,0 +1 @@
<ul class="cms-offerings"><li class="cms-offering" data-slug="newborn" data-featured="true"><h3>Newborn</h3><p class="cms-price">€&nbsp;185</p><ul class="cms-features"><li>1-2 uur fotografie</li></ul><a href="/?pakket=newborn#contact">Boek Newborn</a></li><li class="cms-offering" data-slug="gezin"><h3>Gezin</h3><p class="cms-price">€&nbsp;1.250</p><ul class="cms-features"><li>Hele dag</li></ul><a href="/?pakket=gezin#contact">Boek Gezin</a></li></ul>
@@ -0,0 +1,18 @@
[
{
"slug": "newborn",
"title": "Newborn",
"price": "€ 185",
"features": ["1-2 uur fotografie"],
"displayOrder": 0,
"featured": true
},
{
"slug": "gezin",
"title": "Gezin",
"price": "€ 1.250,-",
"features": ["Hele dag"],
"displayOrder": 1,
"featured": false
}
]
@@ -0,0 +1 @@
<ul class="cms-offerings"><li class="cms-offering" data-slug="xss"><h3>&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt; &amp; &quot;co&quot;</h3><p class="cms-price">€&nbsp;100</p><ul class="cms-features"><li>&lt;b&gt;vet&lt;/b&gt;</li><li>o&#39;brien &amp; zoon</li></ul><a href="/?pakket=xss#contact">Boek &lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt; &amp; &quot;co&quot;</a></li></ul>
@@ -0,0 +1,9 @@
[
{
"slug": "xss",
"title": "<script>alert(\"x\")</script> & \"co\"",
"price": "€ 100",
"features": ["<b>vet</b>", "o'brien & zoon"],
"displayOrder": 0
}
]
@@ -0,0 +1 @@
<ul class="cms-offerings"><li class="cms-offering" data-slug="goed"><h3>Goed</h3><p class="cms-price">€&nbsp;100</p><ul class="cms-features"><li>a</li></ul><a href="/?pakket=goed#contact">Boek Goed</a></li></ul>
@@ -0,0 +1,58 @@
[
{
"slug": "goed",
"title": "Goed",
"price": "€ 100",
"features": ["a"],
"displayOrder": 0
},
{
"slug": "Slecht Slug",
"title": "Fout",
"price": "€ 100",
"features": ["a"],
"displayOrder": 1
},
{
"slug": "goed",
"title": "Dubbel",
"price": "€ 100",
"features": ["a"],
"displayOrder": 2
},
{
"slug": "aanvraag",
"title": "Op aanvraag",
"price": "op aanvraag",
"features": ["a"],
"displayOrder": 3
},
{
"slug": "leeg",
"title": "Zonder kenmerken",
"price": "€ 100",
"features": [],
"displayOrder": 4
},
{
"slug": "zonder-titel",
"title": "",
"price": "€ 100",
"features": ["a"],
"displayOrder": 5
},
{
"slug": "nul",
"title": "Nul",
"price": "€ 0",
"features": ["a"],
"displayOrder": 6
},
{
"slug": "komma",
"title": "Komma",
"price": "€ 12,50",
"features": ["a"],
"displayOrder": 7
}
]
@@ -0,0 +1 @@
<ul class="cms-offerings"><li class="cms-offering" data-slug="zwangerschap"><h3>Zwangerschap</h3><p class="cms-price">€&nbsp;175</p><ul class="cms-features"><li>1 uur fotografie</li><li>Persoonlijke online fotogalerij</li></ul><a href="/?pakket=zwangerschap#contact">Boek Zwangerschap</a></li><li class="cms-offering" data-slug="newborn"><h3>Newborn</h3><p class="cms-price">€&nbsp;185</p><ul class="cms-features"><li>1-2 uur fotografie</li></ul><a href="/?pakket=newborn#contact">Boek Newborn</a></li><li class="cms-offering" data-slug="gezin"><h3>Gezin</h3><p class="cms-price">€&nbsp;175</p><ul class="cms-features"><li>1 uur fotografie</li><li>Minstens 30 zorgvuldig nabewerkte foto&#39;s</li></ul><a href="/?pakket=gezin#contact">Boek Gezin</a></li></ul>
@@ -0,0 +1,29 @@
[
{
"slug": "gezin",
"title": "Gezin",
"price": "€ 175",
"features": [
"1 uur fotografie",
"Minstens 30 zorgvuldig nabewerkte foto's"
],
"displayOrder": 2,
"description": "Een shoot met het hele gezin!",
"priceNote": "eenmalig, incl. btw"
},
{
"slug": "zwangerschap",
"title": "Zwangerschap",
"price": "€ 175",
"features": ["1 uur fotografie", "Persoonlijke online fotogalerij"],
"displayOrder": 0
},
{
"slug": "newborn",
"title": "Newborn",
"price": "€ 185",
"features": ["1-2 uur fotografie"],
"displayOrder": 1,
"ctaLabel": "Boek nu"
}
]
@@ -0,0 +1,99 @@
using System.Text.Json;
using FluentAssertions;
using SlpModularCms.Modules.Offerings.Models;
using SlpModularCms.Modules.Offerings.Services;
namespace SlpModularCms.Modules.Offerings.Tests.Services;
/// <summary>
/// The plain HTML written between the CMS markers must be exactly what the website build expects: the golden files of
/// the website repository (Golden/SOURCE.md names the commit) are run through <see cref="CmsPageHtml"/>.
/// </summary>
public class GoldenHtmlTests
{
private static string Dir => Path.Combine(AppContext.BaseDirectory, "Golden", "host-injection");
public static IEnumerable<object[]> Offerings() => Names("offerings-");
public static IEnumerable<object[]> Banners() => Names("banner-");
private static IEnumerable<object[]> Names(string prefix) =>
Directory.GetFiles(Dir, prefix + "*.input.json").Select(f => new object[] { Path.GetFileName(f)[..^".input.json".Length] }).OrderBy(o => (string)o[0]);
// The expected files end with one newline that is not part of the HTML.
private static string Expected(string name)
{
var text = File.ReadAllText(Path.Combine(Dir, name + ".expected.html"));
return text.EndsWith('\n') ? text[..^1] : text;
}
private static JsonElement Input(string name) => JsonDocument.Parse(File.ReadAllText(Path.Combine(Dir, name + ".input.json"))).RootElement;
private static string? Text(JsonElement e, string property) =>
e.TryGetProperty(property, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() : null;
[Fact]
public void TheGoldenFiles_AreThere()
{
Offerings().Count().Should().BeGreaterThanOrEqualTo(5);
Banners().Count().Should().BeGreaterThanOrEqualTo(6);
}
[Theory]
[MemberData(nameof(Offerings))]
public void OfferingsList_MatchesTheGoldenFile(string name)
{
var offerings = Input(name).EnumerateArray().Select((o, i) => new OfferingDto(
Id: i.ToString(),
Slug: Text(o, "slug")!,
Title: Text(o, "title")!,
Description: Text(o, "description") ?? string.Empty,
Price: Text(o, "price")!,
PriceNote: Text(o, "priceNote") ?? string.Empty,
Features: o.GetProperty("features").EnumerateArray().Select(f => f.GetString()!).ToList(),
CtaLabel: Text(o, "ctaLabel") ?? string.Empty,
Featured: o.TryGetProperty("featured", out var f) && f.ValueKind == JsonValueKind.True,
DisplayOrder: o.TryGetProperty("displayOrder", out var d) ? d.GetInt32() : 0)).ToList();
CmsPageHtml.OfferingsList(offerings).Should().Be(Expected(name));
}
[Theory]
[MemberData(nameof(Banners))]
public void Banner_MatchesTheGoldenFile(string name)
{
var input = Input(name);
var position = input.GetProperty("position").GetString()!;
var answer = input.GetProperty("answer");
PublicOfferingBannerDto? banner = answer.ValueKind == JsonValueKind.Null
? null
: new PublicOfferingBannerDto(Text(answer, "title")!, Text(answer, "text")!, Text(answer, "linkLabel"), Text(answer, "linkUrl"), position);
CmsPageHtml.Banner(banner, position).Should().Be(Expected(name));
}
[Theory]
[InlineData("€ 175", 175)]
[InlineData("€ 1.250", 1250)]
[InlineData("175,-", 175)]
[InlineData("€ 175,00", 175)]
[InlineData("€ 1.250,-", 1250)]
[InlineData("op aanvraag", null)]
[InlineData("€ 12,50", null)]
[InlineData("€ 0", null)]
[InlineData("175 of 185", null)]
[InlineData("", null)]
[InlineData(null, null)]
public void WholeEuros_ReadsOnlyWholeEuroPrices(string? text, int? expected) =>
CmsPageHtml.WholeEuros(text).Should().Be(expected);
[Theory]
[InlineData(185, "185")]
[InlineData(1250, "1.250")]
[InlineData(1000000, "1.000.000")]
public void FormatAmount_UsesADotAsThousandsSeparator(int euros, string expected) =>
CmsPageHtml.FormatAmount(euros).Should().Be(expected);
[Fact]
public void Encode_EscapesTheFiveCharacters_AndLeavesAccentsAlone()
=> CmsPageHtml.Encode("é & < > \" '").Should().Be("é &amp; &lt; &gt; &quot; &#39;");
}
@@ -26,6 +26,10 @@
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<None Include="Golden\**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlpModularCms.Modules.Offerings\SlpModularCms.Modules.Offerings.csproj" />
</ItemGroup>