H4: hero in de eerste HTML (regio, preload), gedeeld datablok met contributors (Offerings byte-identiek), ASF golden files, volledige pijplijntest
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# Golden files of the website build (ASF): hero
|
||||
|
||||
`host-injection/hero/` is a copy of `web/contract/host-injection/hero` of the Andrea Slop Fotografie website repository,
|
||||
ASF commit `13f78c4` (branch `feature/hero-module`). It is the contract for the plain HTML the CMS host writes between the
|
||||
CMS markers `CMS:HERO-HTML` (the hero) and `CMS:HERO-PRELOAD` (the preload links in the head); see
|
||||
`host-injection/hero/README.md` for the rules. `HeroGoldenTests` runs `HeroPictureRenderer` 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. `seed.json` is the seed data of the ASF test CMS (used by `scripts/Seed-Hero.ps1`).
|
||||
@@ -0,0 +1,33 @@
|
||||
# Hero contract (ASF site and CMS host)
|
||||
|
||||
Golden files for the hero regions that the CMS host fills, model B (decided 2026-11-16): the host cuts a `wide` (3:2) and a `tall` (4:5) variant per photo around the focus point, and the site uses `<picture>`. Same idea as the packages (`../README.md`): for every `NAME.input.json` the host's template must give exactly `NAME.expected.html` (between `<!--CMS:HERO-HTML-->` and `<!--/CMS:HERO-HTML-->`) and `NAME.expected-preload.html` (between `<!--CMS:HERO-PRELOAD-->` and `<!--/CMS:HERO-PRELOAD-->` in the head). The file ends with one newline that is not part of the HTML. The ASF repo tests its own implementation (`src/lib/hero.ts`, `src/lib/heroHtml.ts`) against the same files; to change the contract, change the code and run `UPDATE_GOLDEN=1 pnpm test src/lib/heroHtml.test.ts`, then send the new files to the CMS session. The ASF commit is in `git log -1 -- web/contract/host-injection/hero`.
|
||||
|
||||
## Input
|
||||
|
||||
The answer of `GET /api/v1/hero` (an object). A 204 (no hero) is `null`: the output is empty and the baked hero stays.
|
||||
|
||||
## Rules
|
||||
|
||||
1. Usable hero: an object with a non-empty `title` (trimmed) and at least one usable slide; otherwise both outputs are empty strings (fail open: the baked page stays).
|
||||
2. A slide is kept when: `id` is non-empty; `image.src` is a path of our host (starts with one `/`, not `//`, no backslash); `image.width` and `image.height` are whole numbers above 0; and it has an `alt` (non-empty after trimming) or `decorative` is `true`. Other slides are skipped. Order is the given order. Only the FIRST kept slide is written; the client takes the rest from the data block.
|
||||
3. A decorative slide writes `alt=""`.
|
||||
4. Variants: kept when `ratio`, `type` and `url` are non-empty, `width` is a whole number above 0 and `url` is a path of our host. Only `image/webp` variants are used here (a preload cannot say "AVIF if supported", so a second format would be fetched twice). `wide` variants make the `img` `srcset`, `tall` variants the `source` `srcset`; sorted by width ascending, written `URL WIDTHw`, joined by `, `.
|
||||
5. `focus` (`x` and `y` both between 0 and 1) is written as `style="object-position:X% Y%"` on the `img` (percentages without trailing zeros, at most two decimals); without a valid focus there is no `style`.
|
||||
6. Buttons: at most 2 (the first two valid ones). A button is valid with a non-empty `label` and a `target` that is an anchor (`#name`), a path (one leading `/`, not `//`, no backslash) or an `https://` address that parses. `lead` and `display` are not written in the HTML.
|
||||
7. Escape every text and attribute (`&` `&`, `<` `<`, `>` `>`, `"` `"`, `'` `'`). A new line in the title becomes `<br>` (after escaping).
|
||||
|
||||
## Output, HERO-HTML
|
||||
|
||||
`<picture class="cms-hero-picture">` + (when a `tall` webp variant exists: `<source media="(orientation: portrait)" srcset="TALL" sizes="100vw" type="image/webp">`) + `<img class="cms-hero-image" src="SRC"` + (when a `wide` webp variant exists: ` srcset="WIDE" sizes="100vw"`) + ` width="W" height="H" alt="ALT"` + (focus: ` style="object-position:X% Y%"`) + ` fetchpriority="high">` + `</picture>` + `<h1 class="cms-hero-title">` + (tagline: `<span class="cms-hero-tagline">TAGLINE</span> `) + `<span class="cms-hero-name">TITLE</span></h1>` + (when there are buttons: `<p class="cms-hero-buttons">` + `<a class="cms-hero-button" href="TARGET">LABEL</a>`... + `</p>`).
|
||||
|
||||
`src` is `image.src`; `W` and `H` are `image.width` and `image.height`. Selection is by orientation, not width, because the hero is as high as the screen.
|
||||
|
||||
## Output, HERO-PRELOAD
|
||||
|
||||
With a `tall` variant: `<link rel="preload" as="image" imagesrcset="TALL" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high">` followed by the landscape link with `media="(orientation: landscape)"`. The landscape link has `imagesrcset="WIDE" imagesizes="100vw"`, or `href="SRC"` when there is no wide set. Without a tall variant there is one link without `media`. The `srcset`, `sizes` and `media` values are byte for byte the same as in the picture; the golden files check this.
|
||||
|
||||
## Files
|
||||
|
||||
`hero-standard` (two slides, wide and tall, AVIF ignored), `hero-minimal` (no variants, no tagline, no button), `hero-two-lines` (line break, anchor, https and a third button that is cut, decorative slide), `hero-hostile` (escaping, unsafe buttons, unusable slides skipped), `hero-no-alt` and `hero-none` (empty). `seed.json` holds the seed for the ASF test CMS with a first focus estimate per photo.
|
||||
|
||||
URL scheme suggestion for the variants (the CMS decides): `/media/hero/<hash>/wide-1600.webp`, `/media/hero/<hash>/tall-960.avif`.
|
||||
+1
@@ -0,0 +1 @@
|
||||
<link rel="preload" as="image" imagesrcset="/media/hero/a"b/w800.webp 800w" imagesizes="100vw" fetchpriority="high">
|
||||
+1
@@ -0,0 +1 @@
|
||||
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/a"b/w1600.webp" srcset="/media/hero/a"b/w800.webp 800w" sizes="100vw" width="1600" height="1067" alt="Een "foto" <script>" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline"><i>x</i></span> <span class="cms-hero-name"><b>"Andrea" & 'Slop'</b></span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/a?b=1&c="2""><Goed></a></p>
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"display": "slideshow",
|
||||
"title": "<b>\"Andrea\" & 'Slop'</b>",
|
||||
"tagline": "<i>x</i>",
|
||||
"buttons": [
|
||||
{ "label": "javascript", "target": "javascript:alert(1)" },
|
||||
{ "label": "Protocolvrij", "target": "//evil.example/x" },
|
||||
{ "label": "<Goed>", "target": "/a?b=1&c=\"2\"" }
|
||||
],
|
||||
"slides": [
|
||||
{
|
||||
"id": "skip1",
|
||||
"alt": "",
|
||||
"decorative": false,
|
||||
"image": { "width": 10, "height": 10, "src": "/media/x.webp" }
|
||||
},
|
||||
{
|
||||
"id": "skip2",
|
||||
"alt": "extern",
|
||||
"decorative": false,
|
||||
"image": {
|
||||
"width": 10,
|
||||
"height": 10,
|
||||
"src": "https://evil.example/x.webp"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "skip3",
|
||||
"alt": "geen afmeting",
|
||||
"decorative": false,
|
||||
"image": { "width": 0, "height": 10, "src": "/media/x.webp" }
|
||||
},
|
||||
{
|
||||
"id": "a1",
|
||||
"alt": "Een \"foto\" <script>",
|
||||
"decorative": false,
|
||||
"image": {
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"src": "/media/hero/a\"b/w1600.webp",
|
||||
"variants": [
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 800,
|
||||
"type": "image/webp",
|
||||
"url": "/media/hero/a\"b/w800.webp"
|
||||
},
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 400,
|
||||
"type": "image/webp",
|
||||
"url": "https://evil.example/w400.webp"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<link rel="preload" as="image" href="/media/hero/abc123/w2560.webp" fetchpriority="high">
|
||||
+1
@@ -0,0 +1 @@
|
||||
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w2560.webp" width="2560" height="1702" alt="Lifestyle fotografie" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-name">Andrea Slop</span></h1>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"display": "single",
|
||||
"title": "Andrea Slop",
|
||||
"buttons": [],
|
||||
"slides": [
|
||||
{
|
||||
"id": "a1",
|
||||
"alt": "Lifestyle fotografie",
|
||||
"decorative": false,
|
||||
"image": {
|
||||
"width": 2560,
|
||||
"height": 1702,
|
||||
"src": "/media/hero/abc123/w2560.webp"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"display": "slideshow",
|
||||
"title": "Titel",
|
||||
"buttons": [],
|
||||
"slides": [
|
||||
{
|
||||
"id": "x",
|
||||
"alt": "",
|
||||
"decorative": false,
|
||||
"image": { "width": 1, "height": 1, "src": "/m.webp" }
|
||||
}
|
||||
]
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
null
|
||||
+1
@@ -0,0 +1 @@
|
||||
<link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" imagesizes="100vw" media="(orientation: portrait)" fetchpriority="high"><link rel="preload" as="image" imagesrcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" imagesizes="100vw" media="(orientation: landscape)" fetchpriority="high">
|
||||
+1
@@ -0,0 +1 @@
|
||||
<picture class="cms-hero-picture"><source media="(orientation: portrait)" srcset="/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w" sizes="100vw" type="image/webp"><img class="cms-hero-image" src="/media/hero/3f9a1c/wide-1600.webp" srcset="/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" sizes="100vw" width="1600" height="1067" alt="Zwangerschapsfoto in de duinen" style="object-position:50% 55%" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Lifestyle fotograaf · Huizen</span> <span class="cms-hero-name">Andrea Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="/over-mij">Lees meer</a></p>
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"display": "slideshow",
|
||||
"title": "Andrea Slop",
|
||||
"tagline": "Lifestyle fotograaf · Huizen",
|
||||
"lead": null,
|
||||
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
|
||||
"slides": [
|
||||
{
|
||||
"id": "11111111-1111-1111-1111-111111111111",
|
||||
"alt": "Zwangerschapsfoto in de duinen",
|
||||
"decorative": false,
|
||||
"image": {
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"focus": { "x": 0.5, "y": 0.55 },
|
||||
"src": "/media/hero/3f9a1c/wide-1600.webp",
|
||||
"variants": [
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 1600,
|
||||
"type": "image/webp",
|
||||
"url": "/media/hero/3f9a1c/wide-1600.webp"
|
||||
},
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 960,
|
||||
"type": "image/webp",
|
||||
"url": "/media/hero/3f9a1c/wide-960.webp"
|
||||
},
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 1600,
|
||||
"type": "image/avif",
|
||||
"url": "/media/hero/3f9a1c/wide-1600.avif"
|
||||
},
|
||||
{
|
||||
"ratio": "wide",
|
||||
"width": 960,
|
||||
"type": "image/avif",
|
||||
"url": "/media/hero/3f9a1c/wide-960.avif"
|
||||
},
|
||||
{
|
||||
"ratio": "tall",
|
||||
"width": 960,
|
||||
"type": "image/webp",
|
||||
"url": "/media/hero/3f9a1c/tall-960.webp"
|
||||
},
|
||||
{
|
||||
"ratio": "tall",
|
||||
"width": 640,
|
||||
"type": "image/webp",
|
||||
"url": "/media/hero/3f9a1c/tall-640.webp"
|
||||
},
|
||||
{
|
||||
"ratio": "tall",
|
||||
"width": 960,
|
||||
"type": "image/avif",
|
||||
"url": "/media/hero/3f9a1c/tall-960.avif"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "22222222-2222-2222-2222-222222222222",
|
||||
"alt": "Knusse newbornfoto met de familie",
|
||||
"decorative": false,
|
||||
"image": {
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"focus": { "x": 0.45, "y": 0.5 },
|
||||
"src": "/media/hero/7b2e90/wide-1600.webp",
|
||||
"variants": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
<link rel="preload" as="image" href="/media/hero/abc123/w1600.webp" fetchpriority="high">
|
||||
+1
@@ -0,0 +1 @@
|
||||
<picture class="cms-hero-picture"><img class="cms-hero-image" src="/media/hero/abc123/w1600.webp" width="1600" height="1067" alt="" fetchpriority="high"></picture><h1 class="cms-hero-title"><span class="cms-hero-tagline">Fotografie</span> <span class="cms-hero-name">Andrea<br>Slop</span></h1><p class="cms-hero-buttons"><a class="cms-hero-button" href="#contact">Boek een shoot</a><a class="cms-hero-button" href="https://andreaslop.nl/portfolio">Portfolio</a></p>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"display": "slideshow",
|
||||
"title": "Andrea\nSlop",
|
||||
"tagline": "Fotografie",
|
||||
"buttons": [
|
||||
{ "label": "Boek een shoot", "target": "#contact" },
|
||||
{ "label": "Portfolio", "target": "https://andreaslop.nl/portfolio" },
|
||||
{ "label": "Derde knop", "target": "/derde" }
|
||||
],
|
||||
"slides": [
|
||||
{
|
||||
"id": "a1",
|
||||
"alt": "",
|
||||
"decorative": true,
|
||||
"image": {
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"src": "/media/hero/abc123/w1600.webp",
|
||||
"variants": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"note": "Seed for the ASF hero on the test CMS. Focus points are a first estimate from looking at the photos (x and y between 0 and 1, from the top left); Bryan adjusts them in the admin. Sources are the existing exports in web/src/assets (not camera originals; never upscale).",
|
||||
"display": "slideshow",
|
||||
"title": "Andrea Slop",
|
||||
"tagline": "Lifestyle fotograaf · Huizen",
|
||||
"buttons": [{ "label": "Lees meer", "target": "/over-mij" }],
|
||||
"slides": [
|
||||
{
|
||||
"file": "hero-8.jpg",
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"alt": "Zwangerschapsfoto in de duinen",
|
||||
"focus": { "x": 0.5, "y": 0.55 },
|
||||
"subject": "belly and hands"
|
||||
},
|
||||
{
|
||||
"file": "hero-7.jpg",
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"alt": "Knusse newbornfoto met de familie",
|
||||
"focus": { "x": 0.45, "y": 0.5 },
|
||||
"subject": "the kiss on the baby"
|
||||
},
|
||||
{
|
||||
"file": "hero-1.jpg",
|
||||
"width": 2560,
|
||||
"height": 1702,
|
||||
"alt": "Lifestyle fotografie",
|
||||
"focus": { "x": 0.55, "y": 0.6 },
|
||||
"subject": "tattoo and heather"
|
||||
},
|
||||
{
|
||||
"file": "hero-2.jpg",
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"alt": "Zwangerschapsshoot in de buitenlucht",
|
||||
"focus": { "x": 0.45, "y": 0.45 },
|
||||
"subject": "the couple"
|
||||
},
|
||||
{
|
||||
"file": "hero-3.jpg",
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"alt": "Newbornshoot van een pasgeboren baby",
|
||||
"focus": { "x": 0.45, "y": 0.4 },
|
||||
"subject": "the baby's face"
|
||||
},
|
||||
{
|
||||
"file": "hero-4.jpg",
|
||||
"width": 2048,
|
||||
"height": 1362,
|
||||
"alt": "Spontaan portret buiten",
|
||||
"focus": { "x": 0.45, "y": 0.35 },
|
||||
"subject": "the face"
|
||||
},
|
||||
{
|
||||
"file": "hero-5.jpg",
|
||||
"width": 1600,
|
||||
"height": 1067,
|
||||
"alt": "Natuurlijke lifestylefoto",
|
||||
"focus": { "x": 0.5, "y": 0.55 },
|
||||
"subject": "the hands with the ring"
|
||||
},
|
||||
{
|
||||
"file": "hero-6.jpg",
|
||||
"width": 2048,
|
||||
"height": 1362,
|
||||
"alt": "Portret in de natuur",
|
||||
"focus": { "x": 0.5, "y": 0.35 },
|
||||
"subject": "the face"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using FsCheck;
|
||||
using FsCheck.Fluent;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Hosting.Website;
|
||||
using SlpModularCms.Core.Media;
|
||||
using SlpModularCms.Modules.Hero.Models;
|
||||
using SlpModularCms.Modules.Hero.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The HTML written between the hero markers must be exactly what the website expects: the golden files of the website
|
||||
/// repository (Golden/SOURCE.md names the commit) are run through <see cref="HeroPictureRenderer"/>.
|
||||
/// </summary>
|
||||
public class HeroGoldenTests
|
||||
{
|
||||
private static string Dir => Path.Combine(AppContext.BaseDirectory, "Golden", "host-injection", "hero");
|
||||
|
||||
private static readonly JsonSerializerOptions Web = new(JsonSerializerDefaults.Web);
|
||||
|
||||
public static IEnumerable<object[]> Cases()
|
||||
=> Directory.GetFiles(Dir, "*.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 file)
|
||||
{
|
||||
var text = File.ReadAllText(Path.Combine(Dir, file));
|
||||
return text.EndsWith('\n') ? text[..^1] : text;
|
||||
}
|
||||
|
||||
private static PublicHeroDto? Input(string name)
|
||||
=> JsonSerializer.Deserialize<PublicHeroDto?>(File.ReadAllText(Path.Combine(Dir, name + ".input.json")), Web);
|
||||
|
||||
[Fact]
|
||||
public void TheGoldenFiles_AreThere() => Cases().Count().Should().BeGreaterThanOrEqualTo(6);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Cases))]
|
||||
public void TheRegion_MatchesTheGoldenFile(string name)
|
||||
=> HeroPictureRenderer.Render(Input(name)).Region.Should().Be(Expected(name + ".expected.html"));
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Cases))]
|
||||
public void ThePreload_MatchesTheGoldenFile(string name)
|
||||
=> HeroPictureRenderer.Render(Input(name)).Preload.Should().Be(Expected(name + ".expected-preload.html"));
|
||||
|
||||
[Fact]
|
||||
public void ThePreloadAndThePicture_ShareTheirSrcsetSizesAndMedia()
|
||||
{
|
||||
var injection = HeroPictureRenderer.Render(Input("hero-standard"));
|
||||
|
||||
foreach (var set in new[] { "/media/hero/3f9a1c/tall-640.webp 640w, /media/hero/3f9a1c/tall-960.webp 960w", "/media/hero/3f9a1c/wide-960.webp 960w, /media/hero/3f9a1c/wide-1600.webp 1600w" })
|
||||
{
|
||||
injection.Region.Should().Contain($"srcset=\"{set}\"");
|
||||
injection.Preload.Should().Contain($"imagesrcset=\"{set}\"");
|
||||
}
|
||||
|
||||
injection.Region.Should().Contain("media=\"(orientation: portrait)\"");
|
||||
injection.Preload.Should().Contain("media=\"(orientation: portrait)\"").And.Contain("media=\"(orientation: landscape)\"");
|
||||
}
|
||||
}
|
||||
|
||||
public class HeroPictureRendererTests
|
||||
{
|
||||
private static PublicHeroDto Hero(PublicImageDto? image = null, string title = "Titel", string? tagline = null, params HeroButtonDto[] buttons)
|
||||
=> new("single", title, tagline, null, buttons,
|
||||
[new PublicSlideDto("s1", "Een foto", false, image ?? Image())]);
|
||||
|
||||
private static PublicImageDto Image(FocusDto? focus = null, params HeroVariantDto[] variants)
|
||||
=> new(1600, 1067, focus ?? new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-1600.webp", variants);
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.5, 0.55, "50% 55%")]
|
||||
[InlineData(0.455, 0.0, "45.5% 0%")]
|
||||
[InlineData(1.0, 1.0, "100% 100%")]
|
||||
[InlineData(0.123456, 0.987654, "12.35% 98.77%")]
|
||||
public void TheFocus_BecomesAnObjectPosition(double x, double y, string expected)
|
||||
=> HeroPictureRenderer.Render(Hero(Image(new FocusDto(x, y, 1)))).Region.Should().Contain($" style=\"object-position:{expected}\"");
|
||||
|
||||
[Theory]
|
||||
[InlineData(-0.1, 0.5)]
|
||||
[InlineData(0.5, 1.1)]
|
||||
[InlineData(double.NaN, 0.5)]
|
||||
public void AnInvalidFocus_GivesNoStyle(double x, double y)
|
||||
=> HeroPictureRenderer.Render(Hero(Image(new FocusDto(x, y, 1)))).Region.Should().NotContain("style=");
|
||||
|
||||
[Fact]
|
||||
public void WithoutATall_ThereIsOnePreload_WithoutMedia()
|
||||
{
|
||||
var wide = new HeroVariantDto("wide", 960, 640, "image/webp", "/media/hero/0123456789abcdef/wide-960.webp");
|
||||
|
||||
var injection = HeroPictureRenderer.Render(Hero(Image(null, wide)));
|
||||
|
||||
injection.Preload.Should().Be("<link rel=\"preload\" as=\"image\" imagesrcset=\"/media/hero/0123456789abcdef/wide-960.webp 960w\" imagesizes=\"100vw\" fetchpriority=\"high\">");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AvifVariants_NeverAppearInThePictureOrThePreload()
|
||||
{
|
||||
var variants = new[]
|
||||
{
|
||||
new HeroVariantDto("wide", 960, 640, "image/webp", "/media/hero/0123456789abcdef/wide-960.webp"),
|
||||
new HeroVariantDto("wide", 960, 640, "image/avif", "/media/hero/0123456789abcdef/wide-960.avif"),
|
||||
};
|
||||
|
||||
var injection = HeroPictureRenderer.Render(Hero(Image(null, variants)));
|
||||
|
||||
(injection.Region + injection.Preload).Should().NotContain("avif");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ATitleWithoutSlides_OrWithoutATitle_RendersNothing()
|
||||
{
|
||||
HeroPictureRenderer.Render(new PublicHeroDto("single", "Titel", null, null, [], [])).IsEmpty.Should().BeTrue();
|
||||
HeroPictureRenderer.Render(Hero(title: " ")).IsEmpty.Should().BeTrue();
|
||||
HeroPictureRenderer.Render(null).IsEmpty.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThePictureIsTheFirstUsableSlide_NotJustTheFirst()
|
||||
{
|
||||
var hero = new PublicHeroDto("slideshow", "Titel", null, null, [], [
|
||||
new PublicSlideDto("a", "", false, Image()),
|
||||
new PublicSlideDto("b", "Tweede", false, Image()),
|
||||
]);
|
||||
|
||||
HeroPictureRenderer.Render(hero).Region.Should().Contain("alt=\"Tweede\"");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoTextOfTheCmsCanBreakOutOfTheHtml()
|
||||
{
|
||||
var hero = Hero(Image(), "<script>alert(1)</script>\n\"x\" & 'y'", "<img src=x onerror=alert(1)>", new HeroButtonDto("<b>", "/ok?a=1&b=\"2\""));
|
||||
|
||||
var html = HeroPictureRenderer.Render(hero).Region;
|
||||
|
||||
html.Should().NotContain("<script").And.NotContain("<img src=x").And.NotContain("<b>");
|
||||
html.Should().Contain("<script>alert(1)</script><br>"x" & 'y'");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ARendererNeverThrows_OnArbitraryText()
|
||||
=> Prop.ForAll(
|
||||
Gen.Elements("a", "<", ">", "&", "\"", "'", "\n", " ", "é", "\\", "/", "#").ArrayOf().Select(p => string.Concat(p)).ToArbitrary(),
|
||||
text =>
|
||||
{
|
||||
var hero = new PublicHeroDto("single", "T" + text, text, text, [new HeroButtonDto(text, "/" + text)],
|
||||
[new PublicSlideDto("id", "a" + text, false, new PublicImageDto(10, 10, new FocusDto(0.5, 0.5, 1), "/media/x/" + text.Replace("\\", string.Empty), []))]);
|
||||
var html = HeroPictureRenderer.Render(hero).Region;
|
||||
html.Should().NotContain("<script");
|
||||
return true;
|
||||
}).Check(Config.QuickThrowOnFailure.WithMaxTest(1000));
|
||||
}
|
||||
|
||||
public class HeroHtmlTransformTests
|
||||
{
|
||||
private static readonly HtmlTransformContext Context = new("https://test.andreaslop.nl");
|
||||
|
||||
private const string Page =
|
||||
"<head><!--CMS:HERO-PRELOAD-->baked preload<!--/CMS:HERO-PRELOAD--></head>" +
|
||||
"<body><!--CMS:HERO-HTML-->baked hero<!--/CMS:HERO-HTML--><p>rest</p></body>";
|
||||
|
||||
private static PublicHeroDto Hero() => new("single", "Andrea Slop", null, null, [],
|
||||
[new PublicSlideDto("s1", "Een foto", false, new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), "/media/hero/0123456789abcdef/wide-1600.webp",
|
||||
[new HeroVariantDto("wide", 1600, 1067, "image/webp", "/media/hero/0123456789abcdef/wide-1600.webp")]))]);
|
||||
|
||||
private static HeroHtmlTransform Sut(PublicHeroDto? hero)
|
||||
{
|
||||
var model = Substitute.For<IHeroPageModel>();
|
||||
model.GetAsync(default).ReturnsForAnyArgs(hero);
|
||||
return new HeroHtmlTransform(model);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("<p>nothing</p>", false)]
|
||||
[InlineData("<!--CMS:HERO-HTML--><!--/CMS:HERO-HTML-->", true)]
|
||||
[InlineData("<!--CMS:HERO-PRELOAD--><!--/CMS:HERO-PRELOAD-->", true)]
|
||||
[InlineData("<!--CMS:OFFERINGS-HTML--><!--/CMS:OFFERINGS-HTML-->", false)]
|
||||
public void Wants_OnlyPagesWithHeroMarkers(string html, bool expected) => Sut(null).Wants(html).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public async Task TheRegions_AreFilled_AndTheMarkersStay()
|
||||
{
|
||||
var html = await Sut(Hero()).ApplyAsync(Page, Context, default);
|
||||
|
||||
html.Should().Contain("<!--CMS:HERO-HTML--><picture class=\"cms-hero-picture\">").And.Contain("</h1><!--/CMS:HERO-HTML-->");
|
||||
html.Should().Contain("<!--CMS:HERO-PRELOAD--><link rel=\"preload\" as=\"image\"").And.Contain("><!--/CMS:HERO-PRELOAD-->");
|
||||
html.Should().NotContain("baked").And.Contain("<p>rest</p>");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithoutAHero_ThePageStaysAsItWasBuilt()
|
||||
{
|
||||
(await Sut(null).ApplyAsync(Page, Context, default)).Should().Be(Page);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AHeroWithoutAUsableSlide_LeavesTheBakedHero()
|
||||
{
|
||||
var textOnly = new PublicHeroDto("single", "Titel", null, null, [], []);
|
||||
|
||||
(await Sut(textOnly).ApplyAsync(Page, Context, default)).Should().Be(Page);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task APageWithOnlyOneMarker_OnlyGetsThatPart()
|
||||
{
|
||||
var html = await Sut(Hero()).ApplyAsync("<body><!--CMS:HERO-HTML-->x<!--/CMS:HERO-HTML--></body>", Context, default);
|
||||
|
||||
html.Should().Contain("cms-hero-picture").And.NotContain("preload");
|
||||
}
|
||||
}
|
||||
|
||||
public class HeroPageModelTests
|
||||
{
|
||||
private static PublicHeroDto Hero(string src = "/media/hero/0123456789abcdef/wide-1600.webp") => new("single", "Titel", null, null, [],
|
||||
[new PublicSlideDto("s1", "Foto", false, new PublicImageDto(1600, 1067, new FocusDto(0.5, 0.5, 1), src,
|
||||
[new HeroVariantDto("wide", 1600, 1067, "image/webp", src), new HeroVariantDto("tall", 640, 800, "image/webp", "/media/hero/0123456789abcdef/tall-640.webp")]))]);
|
||||
|
||||
private static (HeroPageModel Model, IMediaStore Store) Make(PublicHeroDto? hero)
|
||||
{
|
||||
var publicHero = Substitute.For<IPublicHeroService>();
|
||||
publicHero.GetActiveAsync(default).ReturnsForAnyArgs(hero);
|
||||
var store = Substitute.For<IMediaStore>();
|
||||
store.ExistsAsync(default!, default).ReturnsForAnyArgs(true);
|
||||
return (new HeroPageModel(publicHero, store, NullLogger<HeroPageModel>.Instance), store);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenTheFilesOfTheFirstSlideExist_TheHeroIsReturned()
|
||||
{
|
||||
var (model, _) = Make(Hero());
|
||||
|
||||
(await model.GetAsync(default)).Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WhenAFileOfTheFirstSlideIsMissing_NothingIsReturned()
|
||||
{
|
||||
var (model, store) = Make(Hero());
|
||||
store.ExistsAsync("hero/0123456789abcdef/tall-640.webp", default).Returns(false);
|
||||
|
||||
(await model.GetAsync(default)).Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithoutAHero_OrWithoutSlides_ThereIsNothingToCheck()
|
||||
{
|
||||
(await Make(null).Model.GetAsync(default)).Should().BeNull();
|
||||
var textOnly = new PublicHeroDto("single", "Titel", null, null, [], []);
|
||||
var (model, store) = Make(textOnly);
|
||||
|
||||
(await model.GetAsync(default)).Should().Be(textOnly);
|
||||
await store.DidNotReceiveWithAnyArgs().ExistsAsync(default!, default);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheDataContributor_WritesTheHero_OrNull()
|
||||
{
|
||||
var withHero = Make(Hero()).Model;
|
||||
var without = Make(null).Model;
|
||||
|
||||
(await new HeroDataContributor(withHero).GetAsync(new HtmlTransformContext("https://x"), default)).Should().BeOfType<PublicHeroDto>();
|
||||
(await new HeroDataContributor(without).GetAsync(new HtmlTransformContext("https://x"), default)).Should().BeNull();
|
||||
new HeroDataContributor(withHero).Key.Should().Be("hero");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheDataBlock_CarriesTheOfferingsFirst_ThenTheHero()
|
||||
{
|
||||
var offerings = Substitute.For<ICmsDataBlockContributor>();
|
||||
offerings.Key.Returns("offerings");
|
||||
offerings.Order.Returns(100);
|
||||
offerings.GetAsync(default!, default).ReturnsForAnyArgs(new[] { 1 });
|
||||
var block = new CmsDataBlockTransform([new HeroDataContributor(Make(Hero()).Model), offerings]);
|
||||
|
||||
var html = await block.ApplyAsync("<!--CMS:DATA--><script type=\"application/json\" id=\"cms-data\">{}</script>", new HtmlTransformContext("https://x"), default);
|
||||
|
||||
var json = html["<script type=\"application/json\" id=\"cms-data\">".Length..^"</script>".Length];
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
doc.RootElement.EnumerateObject().Select(p => p.Name).Should().Equal("siteUrl", "offerings", "hero");
|
||||
doc.RootElement.GetProperty("hero").GetProperty("slides")[0].GetProperty("image").GetProperty("variants").GetArrayLength().Should().Be(2);
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,24 @@ public class HeroImageServiceTests
|
||||
rig.Db.HeroImages.Count().Should().Be(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheSamePhotoAgain_RepairsLostFiles()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var photo = HeroTestRig.Photo();
|
||||
var first = await rig.Upload(photo);
|
||||
System.IO.Directory.Delete(rig.Directory, recursive: true);
|
||||
|
||||
var again = await rig.Upload(photo);
|
||||
|
||||
again.Id.Should().Be(first.Id);
|
||||
rig.Db.HeroImages.Count().Should().Be(1);
|
||||
foreach (var variant in again.Variants)
|
||||
{
|
||||
(await rig.Store.ExistsAsync(variant.Url["/media/".Length..], CancellationToken.None)).Should().BeTrue(variant.Url);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ADefaultFocus_IsTheCentre_ForEveryRatio()
|
||||
{
|
||||
|
||||
@@ -27,6 +27,10 @@
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Golden\**" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Hero\SlpModularCms.Modules.Hero.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user