U6: IndexHtmlInjector (markers OFFERINGS-JSON-LD / OFFERINGS-HTML), 60s cache, invalidatie bij wijziging, ETag
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
||||
|
||||
public class OfferingsIndexMarkersTests
|
||||
{
|
||||
private static OfferingDto Dto(string title, string price = "€ 300", string note = "eenmalig", string layout = "card") => new(
|
||||
Guid.NewGuid().ToString(), "slug", title, "Beschrijving", price, note, ["a"], "Start", false, 0, null, null, layout);
|
||||
|
||||
[Theory]
|
||||
[InlineData("€ 300", "300")]
|
||||
[InlineData("€300", "300")]
|
||||
[InlineData("€ 1.500,50", "1500.50")]
|
||||
[InlineData("€ 1.500", "1500")]
|
||||
[InlineData("€ 49,-", "49")]
|
||||
[InlineData("vanaf € 79,95 p/m", "79.95")]
|
||||
[InlineData("Op aanvraag", null)]
|
||||
[InlineData("", null)]
|
||||
[InlineData("$ 300", null)]
|
||||
public void ParsePrice_HandlesDutchNotation_AndNeverGuesses(string label, string? expected)
|
||||
=> OfferingsIndexMarkers.ParsePrice(label).Should().Be(expected);
|
||||
|
||||
[Fact]
|
||||
public void JsonLd_IsAnItemListOfOffers_WithPositions_AndTheBannerIncluded()
|
||||
{
|
||||
var json = OfferingsIndexMarkers.JsonLd([Dto("Start"), Dto("Banner", layout: "banner")], "https://slpsoftware.nl");
|
||||
|
||||
json.Should().StartWith("<script type=\"application/ld+json\">").And.EndWith("</script>");
|
||||
var doc = JsonDocument.Parse(json["<script type=\"application/ld+json\">".Length..^"</script>".Length]).RootElement;
|
||||
doc.GetProperty("@context").GetString().Should().Be("https://schema.org");
|
||||
doc.GetProperty("@type").GetString().Should().Be("ItemList");
|
||||
var items = doc.GetProperty("itemListElement").EnumerateArray().ToList();
|
||||
items.Should().HaveCount(2);
|
||||
items[1].GetProperty("position").GetInt32().Should().Be(2);
|
||||
var offer = items[0].GetProperty("item");
|
||||
offer.GetProperty("@type").GetString().Should().Be("Offer");
|
||||
offer.GetProperty("name").GetString().Should().Be("Start");
|
||||
offer.GetProperty("url").GetString().Should().Be("https://slpsoftware.nl/#pakketten");
|
||||
offer.GetProperty("price").GetString().Should().Be("300");
|
||||
offer.GetProperty("priceCurrency").GetString().Should().Be("EUR");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonLd_OmitsPriceAndCurrency_WhenTheLabelIsNotParseable()
|
||||
{
|
||||
var json = OfferingsIndexMarkers.JsonLd([Dto("Maatwerk", price: "Op aanvraag")], "https://x.nl");
|
||||
|
||||
json.Should().NotContain("\"price\"").And.NotContain("priceCurrency");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonLd_CannotCloseTheScriptElement()
|
||||
{
|
||||
var json = OfferingsIndexMarkers.JsonLd([Dto("</script><b>x</b> & y")], "https://x.nl");
|
||||
|
||||
json.Substring("<script type=\"application/ld+json\">".Length).Replace("</script>", string.Empty, StringComparison.Ordinal)
|
||||
.Should().NotContain("<");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Html_IsASemanticList_WithEverythingEncoded()
|
||||
{
|
||||
var html = OfferingsIndexMarkers.Html([Dto("A <b>& B</b>", price: "€ 300", note: "eenmalig")]);
|
||||
|
||||
html.Should().Be(
|
||||
"<section id=\"packages-static\" aria-label=\"Pakketten\"><ul><li><h3>A <b>& B</b></h3>" +
|
||||
"<p>Beschrijving</p><p>€ 300 eenmalig</p></li></ul></section>");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithoutOfferings_BothMarkersAreEmpty()
|
||||
{
|
||||
var service = Substitute.For<IOfferingsService>();
|
||||
service.GetOfferingsAsync().Returns([]);
|
||||
|
||||
var markers = await new OfferingsIndexMarkers(service).RenderAsync("https://x.nl", CancellationToken.None);
|
||||
|
||||
markers[OfferingsIndexMarkers.JsonLdMarker].Should().BeEmpty();
|
||||
markers[OfferingsIndexMarkers.HtmlMarker].Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
@@ -267,6 +267,23 @@ public class OfferingsServiceTests
|
||||
offering.DeletedAt.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EveryChange_InvalidatesTheWebsitePageCache()
|
||||
{
|
||||
var cache = Substitute.For<SlpModularCms.Core.Hosting.Website.IIndexHtmlCache>();
|
||||
var sut = new OfferingsService(_repo, Substitute.For<IOfferingCategoryRepository>(), _logger, cache);
|
||||
var offering = ExistingOffering();
|
||||
_repo.GetByIdAsync(offering.Id).Returns(offering);
|
||||
_repo.GetAllAsync().Returns([offering]);
|
||||
|
||||
await sut.CreateAsync(CreateRequest(), Guid.NewGuid());
|
||||
await sut.UpdateAsync(offering.Id, UpdateRequestWithLayout(null), Guid.NewGuid());
|
||||
await sut.ReorderAsync([offering.Id], Guid.NewGuid());
|
||||
await sut.DeleteAsync(offering.Id, Guid.NewGuid());
|
||||
|
||||
cache.Received(4).Invalidate();
|
||||
}
|
||||
|
||||
// --- Layout (card/banner), at most one banner ---
|
||||
|
||||
private static CreateOfferingRequest CreateRequestWithLayout(string? layout) => new(
|
||||
|
||||
Reference in New Issue
Block a user